diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index 9d9910a8d..d5e45ebeb 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -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, 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, 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(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhat.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhat.java index 93bbd72a3..b24ea3e6a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhat.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandhat.java @@ -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; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanditemname.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanditemname.java index 0fec9064d..cae0f9338 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanditemname.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanditemname.java @@ -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; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/perm/IPermissionsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/perm/IPermissionsHandler.java index 1e8b38a4f..8b02383bf 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/perm/IPermissionsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/perm/IPermissionsHandler.java @@ -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); diff --git a/Essentials/src/main/java/com/earth2me/essentials/perm/PermissionsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/perm/PermissionsHandler.java index 9ca7e8754..e1e83870b 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/perm/PermissionsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/perm/PermissionsHandler.java @@ -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(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/perm/impl/ConfigPermissionsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/perm/impl/ConfigPermissionsHandler.java index 1086818f4..730591369 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/perm/impl/ConfigPermissionsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/perm/impl/ConfigPermissionsHandler.java @@ -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"; diff --git a/Essentials/src/main/java/com/earth2me/essentials/perm/impl/SuperpermsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/perm/impl/SuperpermsHandler.java index d91efefde..db1b59e2d 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/perm/impl/SuperpermsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/perm/impl/SuperpermsHandler.java @@ -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; diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/TriState.java b/Essentials/src/main/java/com/earth2me/essentials/utils/TriState.java new file mode 100644 index 000000000..52373fdb8 --- /dev/null +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/TriState.java @@ -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 +}