From 497c761cc0efefc8232a358459b818ce257c14be Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 4 May 2019 03:06:22 +0200 Subject: [PATCH] Imprv. perm. handling, read only conf data Improved permission handling (can't say fixed because it doesn't work, unless I forgot to test the last version) Added ReadOnlyConfigData --- .../lib/architecture/ConfigData.java | 2 + .../lib/architecture/IHaveConfig.java | 18 +++++++++ .../lib/architecture/ReadOnlyConfigData.java | 15 +++++++ .../java/buttondevteam/lib/chat/Command2.java | 3 ++ .../buttondevteam/lib/chat/Command2MC.java | 39 +++++++++++++++---- 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 ButtonCore/src/main/java/buttondevteam/lib/architecture/ReadOnlyConfigData.java diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java index bc758ef..b14f7c6 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ConfigData.java @@ -109,6 +109,8 @@ public class ConfigData { } public void set(T value) { + if (this instanceof ReadOnlyConfigData) + return; //Safety for Discord channel/role data Object val; if (setter != null && value != null) val = setter.apply(value); diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java index 2b7ce8f..b3d6fbd 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/IHaveConfig.java @@ -83,6 +83,24 @@ public final class IHaveConfig { return (ConfigData) data; } + /** + * This method overload may be used with any class. The given default value will be run through the getter. + * + * @param path The path in config to use + * @param primitiveDef The primitive value to use by default + * @param getter A function that converts a primitive representation to the correct value + * @param setter A function that converts a value to a primitive representation + * @param The type of this variable (can be any class) + * @return The data object that can be used to get or set the value + */ + @SuppressWarnings("unchecked") + public ReadOnlyConfigData getReadOnlyDataPrimDef(String path, Object primitiveDef, Function getter, Function setter) { + ConfigData data = datamap.get(path); + if (data == null) + datamap.put(path, data = new ReadOnlyConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter, saveAction)); + return (ReadOnlyConfigData) data; + } + /** * This method overload should only be used with primitves or String. * diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ReadOnlyConfigData.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ReadOnlyConfigData.java new file mode 100644 index 0000000..5594c21 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ReadOnlyConfigData.java @@ -0,0 +1,15 @@ +package buttondevteam.lib.architecture; + +import org.bukkit.configuration.ConfigurationSection; + +import java.util.function.Function; + +public class ReadOnlyConfigData extends ConfigData { + ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function getter, Function setter, Runnable saveAction) { + super(config, path, def, primitiveDef, getter, setter, saveAction); + } + + ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Runnable saveAction) { + super(config, path, def, primitiveDef, saveAction); + } +} diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java index 8f41b3c..d383e28 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java @@ -48,6 +48,9 @@ public abstract class Command2 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Subcommand { + /** + * Allowed for OPs only by default + */ String MOD_GROUP = "mod"; /** * Help text to show players. A usage message will be also shown below it. diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java index 9dfb3b7..5976b3d 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java @@ -1,6 +1,7 @@ package buttondevteam.lib.chat; import buttondevteam.core.MainPlugin; +import lombok.experimental.var; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; @@ -14,20 +15,39 @@ public class Command2MC extends Command2 { @Override public void registerCommand(ICommand2MC command) { super.registerCommand(command, '/'); - val perm = "thorpe.command." + command.getCommandPath().replace(' ', '.'); + var perm = "thorpe.command." + command.getCommandPath().replace(' ', '.'); + if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset + System.out.println("Adding perm " + perm + " with default: " + + (modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset Bukkit.getPluginManager().addPermission(new Permission(perm, modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test + for (val method : command.getClass().getMethods()) { + if (!method.isAnnotationPresent(Subcommand.class)) continue; + String pg = permGroup(command, method); + if (pg == null) continue; + perm = "thorpe." + pg; + if (Bukkit.getPluginManager().getPermission(perm) == null) //It may occur multiple times + System.out.println("Adding perm " + perm + " with default: " + + PermissionDefault.OP); //Do not allow any commands that belong to a group + if (Bukkit.getPluginManager().getPermission(perm) == null) //It may occur multiple times + Bukkit.getPluginManager().addPermission(new Permission(perm, + //pg.equals(Subcommand.MOD_GROUP) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only + PermissionDefault.OP)); //Do not allow any commands that belong to a group + } } @Override public boolean hasPermission(Command2MCSender sender, ICommand2MC command, Method method) { String pg; - return modOnly(command) - ? MainPlugin.permission.has(sender.getSender(), "tbmc.admin") - : (pg = permGroup(command, method)) != null - ? MainPlugin.permission.has(sender.getSender(), pg) - : MainPlugin.permission.has(sender.getSender(), "thorpe.command." + command.getCommandPath().replace(' ', '.')); + String perm = modOnly(command) + ? "tbmc.admin" + : (pg = permGroup(command, method)) != null //TODO: This way we can't grant specific perms if it has a perm group + ? "thorpe." + pg + : "thorpe.command." + command.getCommandPath().replace(' ', '.'); + //noinspection deprecation + System.out.println("Has permission " + perm + ": " + MainPlugin.permission.playerHas((String) null, sender.getSender().getName(), perm)); + return MainPlugin.permission.playerHas((String) null, sender.getSender().getName(), perm); } /** @@ -47,8 +67,13 @@ public class Command2MC extends Command2 { * @return The permission group for the subcommand or null */ private String permGroup(ICommand2MC command, Method method) { + //System.out.println("Perm group for command " + command.getClass().getSimpleName() + " and method " + method.getName()); val sc = method.getAnnotation(Subcommand.class); - if (sc != null && sc.permGroup().length() > 0) return sc.permGroup(); + if (sc != null && sc.permGroup().length() > 0) { + //System.out.println("Returning sc.permGroup(): " + sc.permGroup()); + return sc.permGroup(); + } + //System.out.println("Returning getAnnForValue(" + command.getClass().getSimpleName() + ", ...): " + getAnnForValue(command.getClass(), CommandClass.class, CommandClass::permGroup, null)); return getAnnForValue(command.getClass(), CommandClass.class, CommandClass::permGroup, null); }