Explorar el Código

fixed custom command permissions for client auto complete

Kajetan Johannes Hammerle hace 4 años
padre
commit
9bb8b64c42

+ 58 - 29
src/main/java/me/km/permissions/ModCommandManager.java

@@ -49,11 +49,11 @@ public class ModCommandManager extends Commands {
         this.perms = perms;
         this.events = events;
         this.scripts = scripts;
-        
+
         // forge command which fails ...
-        ignoredCommands.add("config"); 
+        ignoredCommands.add("config");
     }
-    
+
     public void clearCustomNodes() {
         customNodes.clear();
     }
@@ -65,10 +65,10 @@ public class ModCommandManager extends Commands {
     public void clearIgnoredCommands() {
         ignoredCommands.clear();
         // forge command which fails ...
-        ignoredCommands.add("config"); 
+        ignoredCommands.add("config");
     }
 
-    public void addCustomNodes(CommandNode node) {
+    public void addCustomNode(CommandNode node) {
         customNodes.add(node);
     }
 
@@ -187,15 +187,14 @@ public class ModCommandManager extends Commands {
     @Override
     public void send(ServerPlayerEntity player) {
         Map<CommandNode<CommandSource>, CommandNode<ISuggestionProvider>> map = Maps.newHashMap();
-        RootCommandNode<ISuggestionProvider> rootcommandnode = new RootCommandNode<>();
-        map.put(getDispatcher().getRoot(), rootcommandnode);
-        this.commandSourceNodesToSuggestionNodes(true, getDispatcher().getRoot(), rootcommandnode, player.getCommandSource(), map);
-
+        RootCommandNode<ISuggestionProvider> rootNode = new RootCommandNode<>();
+        map.put(getDispatcher().getRoot(), rootNode);
+        CommandSource cs = player.getCommandSource();
+        commandSourceNodesToSuggestionNodes(true, getDispatcher().getRoot(), rootNode, cs, map);
         for(CommandNode node : customNodes) {
-            rootcommandnode.addChild(node);
+            commandSourceNodesToSuggestionNodes(node, rootNode, cs, map);
         }
-
-        player.connection.sendPacket(new SCommandListPacket(rootcommandnode));
+        player.connection.sendPacket(new SCommandListPacket(rootNode));
     }
 
     private void commandSourceNodesToSuggestionNodes(boolean first,
@@ -203,35 +202,65 @@ public class ModCommandManager extends Commands {
             CommandNode<ISuggestionProvider> suggestion,
             CommandSource source,
             Map<CommandNode<CommandSource>, CommandNode<ISuggestionProvider>> map) {
-        for(CommandNode<CommandSource> commandnode : node.getChildren()) {
-            if(first && ignoredCommands.contains(commandnode.getName())) {
+        for(CommandNode<CommandSource> childNode : node.getChildren()) {
+            if(first && ignoredCommands.contains(childNode.getName())) {
                 continue;
             }
-            if((first && perms.has(source, commandnode.getName()) || (!first && commandnode.canUse(source)))) {
-                ArgumentBuilder<ISuggestionProvider, ?> argumentbuilder = (ArgumentBuilder) commandnode.createBuilder();
-                argumentbuilder.requires(a -> true);
-                if(argumentbuilder.getCommand() != null) {
-                    argumentbuilder.executes(a -> 0);
+            if((first && perms.has(source, childNode.getName()) || (!first && childNode.canUse(source)))) {
+                ArgumentBuilder<ISuggestionProvider, ?> arg = (ArgumentBuilder) childNode.createBuilder();
+                arg.requires(a -> true);
+                if(arg.getCommand() != null) {
+                    arg.executes(a -> 0);
                 }
 
-                if(argumentbuilder instanceof RequiredArgumentBuilder) {
-                    RequiredArgumentBuilder<ISuggestionProvider, ?> requiredargumentbuilder = (RequiredArgumentBuilder) argumentbuilder;
-                    if(requiredargumentbuilder.getSuggestionsProvider() != null) {
-                        requiredargumentbuilder.suggests(SuggestionProviders.ensureKnown(requiredargumentbuilder.getSuggestionsProvider()));
+                if(arg instanceof RequiredArgumentBuilder) {
+                    RequiredArgumentBuilder<ISuggestionProvider, ?> required = (RequiredArgumentBuilder) arg;
+                    if(required.getSuggestionsProvider() != null) {
+                        required.suggests(SuggestionProviders.ensureKnown(required.getSuggestionsProvider()));
                     }
                 }
 
-                if(argumentbuilder.getRedirect() != null) {
-                    argumentbuilder.redirect(map.get(argumentbuilder.getRedirect()));
+                if(arg.getRedirect() != null) {
+                    arg.redirect(map.get(arg.getRedirect()));
                 }
 
-                CommandNode<ISuggestionProvider> commandNode = argumentbuilder.build();
-                map.put(commandnode, commandNode);
+                CommandNode<ISuggestionProvider> commandNode = arg.build();
+                map.put(childNode, commandNode);
                 suggestion.addChild(commandNode);
-                if(!commandnode.getChildren().isEmpty()) {
-                    this.commandSourceNodesToSuggestionNodes(false, commandnode, commandNode, source, map);
+                if(!childNode.getChildren().isEmpty()) {
+                    this.commandSourceNodesToSuggestionNodes(false, childNode, commandNode, source, map);
                 }
             }
         }
     }
+
+    private void commandSourceNodesToSuggestionNodes(
+            CommandNode<CommandSource> node,
+            CommandNode<ISuggestionProvider> parentNode,
+            CommandSource cs,
+            Map<CommandNode<CommandSource>, CommandNode<ISuggestionProvider>> map) {
+        if(!node.canUse(cs)) {
+            return;
+        }
+        ArgumentBuilder<ISuggestionProvider, ?> arg = (ArgumentBuilder) node.createBuilder();
+        arg.requires(a -> true);
+        if(arg.getCommand() != null) {
+            arg.executes(a -> 0);
+        }
+        if(arg instanceof RequiredArgumentBuilder) {
+            RequiredArgumentBuilder<ISuggestionProvider, ?> required = (RequiredArgumentBuilder) arg;
+            if(required.getSuggestionsProvider() != null) {
+                required.suggests(SuggestionProviders.ensureKnown(required.getSuggestionsProvider()));
+            }
+        }
+        if(arg.getRedirect() != null) {
+            arg.redirect(map.get(arg.getRedirect()));
+        }
+        CommandNode<ISuggestionProvider> commandNode = arg.build();
+        map.put(node, commandNode);
+        parentNode.addChild(commandNode);
+        for(CommandNode<CommandSource> childNode : node.getChildren()) {
+            commandSourceNodesToSuggestionNodes(childNode, commandNode, cs, map);
+        }
+    }
 }

+ 4 - 6
src/main/java/me/km/permissions/Permissions.java

@@ -40,17 +40,15 @@ public class Permissions {
     }
 
     public boolean has(Entity ent, String perm) {
-        return Permissions.this.has(ent.getUniqueID(), perm);
+        return has(ent.getUniqueID(), perm);
     }
 
     public boolean has(UUID uuid, String perm) {
         if(debug) {
             return true;
         }
-        if(perm.equals("script")) {
-            if(uuid.equals(MARVINIUS) || uuid.equals(KAJETANJOHANNES)) {
-                return true;
-            }
+        if(perm.equals("script") && (uuid.equals(MARVINIUS) || uuid.equals(KAJETANJOHANNES))) {
+            return true;
         }
         ArrayList<Integer> groups = playerGroups.get(uuid);
         if(groups == null) {
@@ -66,7 +64,7 @@ public class Permissions {
     public boolean has(CommandSource cs, String perm) {
         Entity ent = cs.getEntity();
         if(ent != null) {
-            return Permissions.this.has(ent.getUniqueID(), perm);
+            return has(ent.getUniqueID(), perm);
         }
         return true;
     }

+ 19 - 10
src/main/java/me/km/snuviscript/commands/CommandCommands.java

@@ -17,19 +17,22 @@ public class CommandCommands {
         sm.registerConsumer("command.addignored", (sc, in) -> commands.addIgnoredCommands(in[0].getString(sc)));
         sm.registerConsumer("command.clearignored", (sc, in) -> commands.clearIgnoredCommands());
         sm.registerFunction("command.newhelp", (sc, in) -> {
-            return Commands.literal(in[0].getString(sc)).requires(p -> perms.has(p, in[1].getString(sc)));
+            final String perm = in[1].getString(sc);
+            return Commands.literal(in[0].getString(sc)).requires(p -> perms.has(p, perm));
         });
         sm.registerFunction("command.newhelpliteral", (sc, in) -> {
             LiteralArgumentBuilder<CommandSource> arg = Commands.literal(in[0].getString(sc));
             if(in.length >= 2) {
-                arg.requires(p -> perms.has(p, in[1].getString(sc)));
+                final String perm = in[1].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
         sm.registerFunction("command.newhelpbool", (sc, in) -> {
             RequiredArgumentBuilder<CommandSource, Boolean> arg = Commands.argument(in[0].getString(sc), BoolArgumentType.bool());
             if(in.length >= 2) {
-                arg.requires(p -> perms.has(p, in[1].getString(sc)));
+                final String perm = in[1].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -38,7 +41,8 @@ public class CommandCommands {
             double max = in[2].getDouble(sc);
             RequiredArgumentBuilder<CommandSource, Double> arg = Commands.argument(in[0].getString(sc), DoubleArgumentType.doubleArg(min, max));
             if(in.length >= 4) {
-                arg.requires(p -> perms.has(p, in[3].getString(sc)));
+                final String perm = in[3].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -47,7 +51,8 @@ public class CommandCommands {
             float max = in[2].getFloat(sc);
             RequiredArgumentBuilder<CommandSource, Float> arg = Commands.argument(in[0].getString(sc), FloatArgumentType.floatArg(min, max));
             if(in.length >= 4) {
-                arg.requires(p -> perms.has(p, in[3].getString(sc)));
+                final String perm = in[3].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -56,7 +61,8 @@ public class CommandCommands {
             int max = in[2].getInt(sc);
             RequiredArgumentBuilder<CommandSource, Integer> arg = Commands.argument(in[0].getString(sc), IntegerArgumentType.integer(min, max));
             if(in.length >= 4) {
-                arg.requires(p -> perms.has(p, in[3].getString(sc)));
+                final String perm = in[3].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -65,7 +71,8 @@ public class CommandCommands {
             long max = in[2].getLong(sc);
             RequiredArgumentBuilder<CommandSource, Long> arg = Commands.argument(in[0].getString(sc), LongArgumentType.longArg(min, max));
             if(in.length >= 4) {
-                arg.requires(p -> perms.has(p, in[3].getString(sc)));
+                final String perm = in[3].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -77,7 +84,8 @@ public class CommandCommands {
                 arg = Commands.argument(in[0].getString(sc), StringArgumentType.string());
             }
             if(in.length >= 3) {
-                arg.requires(p -> perms.has(p, in[2].getString(sc)));
+                final String perm = in[2].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -104,7 +112,8 @@ public class CommandCommands {
                     throw new IllegalArgumentException(String.format("'%s' is not a valid special help", name));
             }
             if(in.length >= 3) {
-                arg.requires(p -> perms.has(p, in[2].getString(sc)));
+                final String perm = in[2].getString(sc);
+                arg.requires(p -> perms.has(p, perm));
             }
             return arg;
         });
@@ -118,7 +127,7 @@ public class CommandCommands {
             ((ArgumentBuilder) in[0].get(sc)).then(((ArgumentBuilder) in[1].get(sc)).build());
         });
         sm.registerConsumer("command.addhelp", (sc, in) -> {
-            commands.addCustomNodes(((LiteralArgumentBuilder<CommandSource>) in[0].get(sc)).build());
+            commands.addCustomNode(((LiteralArgumentBuilder<CommandSource>) in[0].get(sc)).build());
         });
         sm.registerConsumer("command.clearhelp", (sc, in) -> commands.clearCustomNodes());
         sm.registerConsumer("command.add", (sc, in) -> scripts.registerScriptCommand(in[0].getString(sc)));