Add prevent-type permissions for /itemname (#4133)

Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>

Adds `essentials.itemname.prevent-type.<material_name>` permissions. Closes #4044.
This commit is contained in:
Josh Roy 2021-05-28 13:35:33 -04:00 committed by GitHub
parent ec50b28f4b
commit b2d7d1b4cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import com.earth2me.essentials.economy.EconomyLayer;
import com.earth2me.essentials.economy.EconomyLayers;
import com.earth2me.essentials.messaging.IMessageRecipient;
import com.earth2me.essentials.messaging.SimpleMessageRecipient;
import com.earth2me.essentials.utils.TriState;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.FormatUtil;
@ -117,8 +118,15 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
return isPermSetCheck(node);
}
private boolean isAuthorizedCheck(final String node) {
/**
* Checks if the given permission is explicitly defined and returns its value, otherwise
* {@link TriState#UNSET}.
*/
public TriState isAuthorizedExact(final String node) {
return isAuthorizedExactCheck(node);
}
private boolean isAuthorizedCheck(final String node) {
if (base instanceof OfflinePlayer) {
return false;
}
@ -154,6 +162,24 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
}
}
private TriState isAuthorizedExactCheck(final String node) {
if (base instanceof OfflinePlayer) {
return TriState.UNSET;
}
try {
return ess.getPermissionsHandler().isPermissionSetExact(base, node);
} catch (final Exception ex) {
if (ess.getSettings().isDebug()) {
ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex);
} else {
ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage());
}
return TriState.UNSET;
}
}
@Override
public void healCooldown() throws Exception {
final Calendar now = new GregorianCalendar();

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import com.earth2me.essentials.utils.TriState;
import com.earth2me.essentials.utils.VersionUtil;
import com.google.common.collect.Lists;
import org.bukkit.Material;
@ -33,7 +34,9 @@ public class Commandhat extends EssentialsCommand {
return;
}
if (user.isAuthorized("essentials.hat.prevent-type." + hand.getType().name().toLowerCase())) {
final TriState wildcard = user.isAuthorizedExact(PERM_PREFIX + "*");
final TriState material = user.isAuthorizedExact(PERM_PREFIX + hand.getType().name().toLowerCase());
if ((wildcard == TriState.TRUE && material != TriState.FALSE) || ((wildcard != TriState.TRUE) && material == TriState.TRUE)) {
user.sendMessage(tl("hatFail"));
return;
}

View File

@ -1,17 +1,17 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.TriState;
import com.earth2me.essentials.utils.FormatUtil;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Locale;
import static com.earth2me.essentials.I18n.tl;
public class Commanditemname extends EssentialsCommand {
public static final String PERM_PREFIX = "essentials.itemname.prevent-type.";
public Commanditemname() {
super("itemname");
@ -21,7 +21,14 @@ public class Commanditemname extends EssentialsCommand {
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack item = user.getBase().getItemInHand();
if (item.getType() == Material.AIR) {
user.sendMessage(tl("itemnameInvalidItem", item.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ')));
user.sendMessage(tl("itemnameInvalidItem"));
return;
}
final TriState wildcard = user.isAuthorizedExact(PERM_PREFIX + "*");
final TriState material = user.isAuthorizedExact(PERM_PREFIX + item.getType().name().toLowerCase());
if ((wildcard == TriState.TRUE && material != TriState.FALSE) || ((wildcard != TriState.TRUE) && material == TriState.TRUE)) {
user.sendMessage(tl("itemnameInvalidItem"));
return;
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.perm;
import com.earth2me.essentials.utils.TriState;
import org.bukkit.entity.Player;
import java.util.List;
@ -20,6 +21,8 @@ public interface IPermissionsHandler {
// Does not check for * permissions
boolean isPermissionSet(Player base, String node);
TriState isPermissionSetExact(Player base, String node);
String getPrefix(Player base);
String getSuffix(Player base);

View File

@ -7,6 +7,7 @@ import com.earth2me.essentials.perm.impl.GenericVaultHandler;
import com.earth2me.essentials.perm.impl.LuckPermsHandler;
import com.earth2me.essentials.perm.impl.ModernVaultHandler;
import com.earth2me.essentials.perm.impl.SuperpermsHandler;
import com.earth2me.essentials.utils.TriState;
import com.google.common.collect.ImmutableSet;
import org.bukkit.entity.Player;
@ -76,6 +77,11 @@ public class PermissionsHandler implements IPermissionsHandler {
return handler.isPermissionSet(base, node);
}
@Override
public TriState isPermissionSetExact(Player base, String node) {
return handler.isPermissionSetExact(base, node);
}
@Override
public String getPrefix(final Player base) {
final long start = System.nanoTime();

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.utils.TriState;
import net.ess3.api.IEssentials;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -22,6 +23,12 @@ public class ConfigPermissionsHandler extends SuperpermsHandler {
return ess.getSettings().isPlayerCommand(cmds[cmds.length - 1]) || super.hasPermission(base, node);
}
@Override
public TriState isPermissionSetExact(Player base, String node) {
final String[] cmds = node.split("\\.", 2);
return ess.getSettings().isPlayerCommand(cmds[cmds.length - 1]) ? TriState.TRUE : super.isPermissionSetExact(base, node);
}
@Override
public String getBackendName() {
return "Essentials";

View File

@ -1,9 +1,11 @@
package com.earth2me.essentials.perm.impl;
import com.earth2me.essentials.perm.IPermissionsHandler;
import com.earth2me.essentials.utils.TriState;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import java.util.Arrays;
@ -101,6 +103,16 @@ public class SuperpermsHandler implements IPermissionsHandler {
return base.isPermissionSet(node);
}
@Override
public TriState isPermissionSetExact(Player base, String node) {
for (final PermissionAttachmentInfo perm : base.getEffectivePermissions()) {
if (perm.getPermission().equalsIgnoreCase(node)) {
return perm.getValue() ? TriState.TRUE : TriState.FALSE;
}
}
return TriState.UNSET;
}
@Override
public String getPrefix(final Player base) {
return null;

View File

@ -0,0 +1,14 @@
package com.earth2me.essentials.utils;
import org.bukkit.entity.Player;
/**
* A state that can be either true, false or unset.
*
* @see com.earth2me.essentials.perm.IPermissionsHandler#isPermissionSetExact(Player, String)
*/
public enum TriState {
TRUE,
FALSE,
UNSET
}