ModCommandManager.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package me.km.permissions;
  2. import com.google.common.collect.Maps;
  3. import com.mojang.brigadier.builder.ArgumentBuilder;
  4. import com.mojang.brigadier.builder.RequiredArgumentBuilder;
  5. import com.mojang.brigadier.tree.CommandNode;
  6. import com.mojang.brigadier.tree.RootCommandNode;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Map;
  11. import me.hammerle.snuviscript.code.ISnuviScheduler;
  12. import me.km.events.CommandEvent;
  13. import me.km.snuviscript.ScriptEvents;
  14. import me.km.snuviscript.Scripts;
  15. import net.minecraft.command.CommandSource;
  16. import net.minecraft.command.Commands;
  17. import net.minecraft.command.ICommandSource;
  18. import net.minecraft.command.ISuggestionProvider;
  19. import net.minecraft.command.arguments.SuggestionProviders;
  20. import net.minecraft.entity.Entity;
  21. import net.minecraft.entity.player.PlayerEntity;
  22. import net.minecraft.entity.player.ServerPlayerEntity;
  23. import net.minecraft.network.play.server.SCommandListPacket;
  24. public class ModCommandManager extends Commands
  25. {
  26. private final HashSet<String> otherCommands = new HashSet<>();
  27. private final HashMap<String, Command> commands = new HashMap<>();
  28. private final PermissionManager perms;
  29. private final ScriptEvents events;
  30. private final Scripts scripts;
  31. public ModCommandManager(boolean isDedicatedServer, PermissionManager perms, ScriptEvents events, Scripts scripts, ISnuviScheduler scheduler)
  32. {
  33. super(isDedicatedServer);
  34. getDispatcher().getRoot().getChildren().forEach(c ->
  35. {
  36. otherCommands.add(c.getName());
  37. });
  38. scheduler.scheduleTask(() ->
  39. {
  40. getDispatcher().getRoot().getChildren().forEach(c ->
  41. {
  42. if(otherCommands.add(c.getName()))
  43. {
  44. perms.addOtherGroupPermission(c.getName());
  45. }
  46. });
  47. });
  48. this.perms = perms;
  49. this.events = events;
  50. this.scripts = scripts;
  51. }
  52. public void registerCommand(Command command)
  53. {
  54. commands.put(command.getName(), command);
  55. for(String alias : command.getAliases())
  56. {
  57. commands.put(alias, command);
  58. }
  59. }
  60. private String getCommandName(String rawCommand)
  61. {
  62. if(rawCommand.isEmpty())
  63. {
  64. return "";
  65. }
  66. int index = rawCommand.indexOf(' ');
  67. return rawCommand.substring(rawCommand.charAt(0) == '/' ? 1 : 0, index == -1 ? rawCommand.length() : index);
  68. }
  69. private static String[] getArguments(String rawCommand)
  70. {
  71. int old = rawCommand.indexOf(' ') + 1;
  72. if(old == 0)
  73. {
  74. return new String[0];
  75. }
  76. int pos = old;
  77. ArrayList<String> list = new ArrayList<>();
  78. while(pos < rawCommand.length())
  79. {
  80. char c = rawCommand.charAt(pos);
  81. switch(c)
  82. {
  83. case ' ':
  84. if(pos - old > 0)
  85. {
  86. list.add(rawCommand.substring(old, pos));
  87. }
  88. old = pos + 1;
  89. break;
  90. case '"':
  91. if(pos - old > 0)
  92. {
  93. list.add(rawCommand.substring(old, pos));
  94. }
  95. old = pos + 1;
  96. pos = old;
  97. while(pos < rawCommand.length() && rawCommand.charAt(pos) != '"')
  98. {
  99. pos++;
  100. }
  101. list.add(rawCommand.substring(old, pos));
  102. old = pos + 1;
  103. break;
  104. }
  105. pos++;
  106. }
  107. if(pos - old > 0)
  108. {
  109. list.add(rawCommand.substring(old, pos));
  110. }
  111. return list.toArray(new String[list.size()]);
  112. }
  113. private ICommandSource getSource(CommandSource cs)
  114. {
  115. if(cs.getEntity() != null)
  116. {
  117. return cs.getEntity();
  118. }
  119. return cs.getServer();
  120. }
  121. private String lowerRawCommand(String raw)
  122. {
  123. int index = raw.indexOf(" ");
  124. if(index == -1)
  125. {
  126. return raw.toLowerCase();
  127. }
  128. return raw.substring(0, index).toLowerCase() + raw.substring(index);
  129. }
  130. @Override
  131. public int handleCommand(CommandSource cs, String rawCommand)
  132. {
  133. rawCommand = lowerRawCommand(rawCommand);
  134. String commandName = getCommandName(rawCommand);
  135. Command command = commands.get(commandName);
  136. if(command != null)
  137. {
  138. if(perms.hasPermission(cs, command.getName()))
  139. {
  140. command.execute(getSource(cs), getArguments(rawCommand));
  141. return 1;
  142. }
  143. events.onMissingPermission(getSource(cs), command.getName());
  144. return 0;
  145. }
  146. if(scripts.isRegisteredScriptCommand(commandName))
  147. {
  148. Entity ent = cs.getEntity();
  149. if(ent != null && ent instanceof PlayerEntity)
  150. {
  151. events.onCustomCommand((PlayerEntity) ent, commandName, getArguments(rawCommand));
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. if(otherCommands.contains(commandName))
  157. {
  158. if(perms.hasPermission(cs, commandName))
  159. {
  160. Entity ent = cs.getEntity();
  161. if(ent != null && ent instanceof PlayerEntity)
  162. {
  163. CommandEvent e = new CommandEvent((PlayerEntity) ent, commandName);
  164. events.onCommand(e);
  165. if(e.isCanceled())
  166. {
  167. return 0;
  168. }
  169. }
  170. return super.handleCommand(cs, rawCommand);
  171. }
  172. events.onMissingPermission(getSource(cs), commandName);
  173. return 0;
  174. }
  175. events.onMissingCommand(getSource(cs), commandName);
  176. return 0;
  177. }
  178. @Override
  179. public void send(ServerPlayerEntity player)
  180. {
  181. Map<CommandNode<CommandSource>, CommandNode<ISuggestionProvider>> map = Maps.newHashMap();
  182. RootCommandNode<ISuggestionProvider> rootcommandnode = new RootCommandNode<>();
  183. map.put(getDispatcher().getRoot(), rootcommandnode);
  184. this.commandSourceNodesToSuggestionNodes(getDispatcher().getRoot(), rootcommandnode, player.getCommandSource(), map);
  185. player.connection.sendPacket(new SCommandListPacket(rootcommandnode));
  186. }
  187. private void commandSourceNodesToSuggestionNodes(CommandNode<CommandSource> node, CommandNode<ISuggestionProvider> suggestion, CommandSource source, Map<CommandNode<CommandSource>, CommandNode<ISuggestionProvider>> commandNodeToSuggestionNode)
  188. {
  189. for(CommandNode<CommandSource> commandnode : node.getChildren())
  190. {
  191. if(perms.hasPermission(source, commandnode.getName()))
  192. //if(commandnode.canUse(source))
  193. {
  194. ArgumentBuilder<ISuggestionProvider, ?> argumentbuilder = (ArgumentBuilder) commandnode.createBuilder();
  195. argumentbuilder.requires((p_197060_0_) ->
  196. {
  197. return true;
  198. });
  199. if(argumentbuilder.getCommand() != null)
  200. {
  201. argumentbuilder.executes((p_197053_0_) ->
  202. {
  203. return 0;
  204. });
  205. }
  206. if(argumentbuilder instanceof RequiredArgumentBuilder)
  207. {
  208. RequiredArgumentBuilder<ISuggestionProvider, ?> requiredargumentbuilder = (RequiredArgumentBuilder) argumentbuilder;
  209. if(requiredargumentbuilder.getSuggestionsProvider() != null)
  210. {
  211. requiredargumentbuilder.suggests(SuggestionProviders.ensureKnown(requiredargumentbuilder.getSuggestionsProvider()));
  212. }
  213. }
  214. if(argumentbuilder.getRedirect() != null)
  215. {
  216. argumentbuilder.redirect(commandNodeToSuggestionNode.get(argumentbuilder.getRedirect()));
  217. }
  218. CommandNode<ISuggestionProvider> commandnode1 = argumentbuilder.build();
  219. commandNodeToSuggestionNode.put(commandnode, commandnode1);
  220. suggestion.addChild(commandnode1);
  221. if(!commandnode.getChildren().isEmpty())
  222. {
  223. this.commandSourceNodesToSuggestionNodes(commandnode, commandnode1, source, commandNodeToSuggestionNode);
  224. }
  225. }
  226. }
  227. }
  228. }