ModServerCommandManager.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package me.kcm.command;
  2. import java.util.Collections;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import javax.annotation.Nullable;
  7. import net.minecraft.command.CommandBase;
  8. import net.minecraft.command.ICommand;
  9. import net.minecraft.command.ICommandSender;
  10. import net.minecraft.command.ServerCommandManager;
  11. import net.minecraft.server.MinecraftServer;
  12. import net.minecraft.util.math.BlockPos;
  13. import net.minecraft.util.text.ITextComponent;
  14. import net.minecraft.util.text.TextComponentTranslation;
  15. import net.minecraft.util.text.TextFormatting;
  16. import net.minecraftforge.event.CommandEvent;
  17. public class ModServerCommandManager extends ServerCommandManager
  18. {
  19. private LinkedList<String> events = new LinkedList<>();
  20. private ICommandManager manager;
  21. public ModServerCommandManager(MinecraftServer serverIn)
  22. {
  23. super(serverIn);
  24. // the manager must not be null
  25. this.manager = new ICommandManager()
  26. {
  27. @Override
  28. public boolean hasPermission(ICommandSender cs, String perm)
  29. {
  30. return true;
  31. }
  32. @Override
  33. public boolean executeCustomCommand(ICommandSender cs, String command, String[] args)
  34. {
  35. return false;
  36. }
  37. @Override
  38. public void printMissingCommand(ICommandSender cs, String command)
  39. {
  40. }
  41. @Override
  42. public void printMissingPermission(ICommandSender cs, ICommand command)
  43. {
  44. }
  45. @Override
  46. public void onRegisterCommand(ICommand command, String domain)
  47. {
  48. }
  49. };
  50. }
  51. public void setPermissionManager(ICommandManager manager)
  52. {
  53. // the manager must not be null
  54. if(manager == null)
  55. {
  56. throw new NullPointerException();
  57. }
  58. this.manager = manager;
  59. }
  60. public void registerRegistryEvent(String s)
  61. {
  62. events.add(s);
  63. }
  64. @Override
  65. public void notifyListener(ICommandSender cs, ICommand command, int flags, String key, Object... args)
  66. {
  67. ITextComponent text = new TextComponentTranslation("chat.type.admin", new Object[] {cs.getName(), new TextComponentTranslation(key, args)});
  68. text.getStyle().setColor(TextFormatting.GRAY);
  69. text.getStyle().setItalic(true);
  70. cs.sendMessage(text);
  71. }
  72. private String[] dropFirstString(String[] input)
  73. {
  74. String[] astring = new String[input.length - 1];
  75. System.arraycopy(input, 1, astring, 0, input.length - 1);
  76. return astring;
  77. }
  78. @Override
  79. public int executeCommand(ICommandSender sender, String rawCommand)
  80. {
  81. rawCommand = rawCommand.trim();
  82. if(rawCommand.startsWith("/"))
  83. {
  84. rawCommand = rawCommand.substring(1);
  85. }
  86. String[] args = rawCommand.split(" ");
  87. String command = args[0].toLowerCase();
  88. args = dropFirstString(args);
  89. ICommand icommand = this.getCommands().get(command);
  90. if(icommand == null)
  91. {
  92. if(manager.executeCustomCommand(sender, command, args))
  93. {
  94. return 1;
  95. }
  96. manager.printMissingCommand(sender, command);
  97. return 0;
  98. }
  99. if(manager.hasPermission(sender, icommand.getName()))
  100. {
  101. CommandEvent e = new CommandEvent(icommand, sender, args);
  102. if(net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(e))
  103. {
  104. if(e.getException() != null)
  105. {
  106. com.google.common.base.Throwables.propagateIfPossible(e.getException());
  107. }
  108. return 1;
  109. }
  110. if(e.getParameters() != null)
  111. {
  112. args = e.getParameters();
  113. }
  114. this.tryExecute(sender, args, icommand, rawCommand);
  115. }
  116. else
  117. {
  118. manager.printMissingPermission(sender, icommand);
  119. }
  120. return 1;
  121. }
  122. @Override
  123. public List<String> getTabCompletions(ICommandSender sender, String input, @Nullable BlockPos pos)
  124. {
  125. String[] astring = input.split(" ", -1);
  126. String command = astring[0];
  127. if(astring.length == 1)
  128. {
  129. return this.getCommands().entrySet().stream()
  130. .filter(e -> (CommandBase.doesStringStartWith(command, e.getKey()) &&
  131. manager.hasPermission(sender, e.getKey())))
  132. .map(e -> e.getKey())
  133. .collect(Collectors.toList());
  134. }
  135. else if(astring.length > 1)
  136. {
  137. ICommand icommand = this.getCommands().get(command);
  138. if(icommand != null && manager.hasPermission(sender, icommand.getName()))
  139. {
  140. return icommand.getTabCompletions(this.getServer(), sender, dropFirstString(astring), pos);
  141. }
  142. }
  143. return Collections.<String>emptyList();
  144. }
  145. @Override
  146. public List<ICommand> getPossibleCommands(ICommandSender sender)
  147. {
  148. return this.getCommands().entrySet().stream()
  149. .filter(e -> manager.hasPermission(sender, e.getKey()))
  150. .map(e -> e.getValue())
  151. .collect(Collectors.toList());
  152. }
  153. @Override
  154. public ICommand registerCommand(ICommand command)
  155. {
  156. String s = null;
  157. out: for(StackTraceElement e : Thread.currentThread().getStackTrace())
  158. {
  159. String name = e.getClassName();
  160. if(events == null)
  161. {
  162. events = new LinkedList<>();
  163. }
  164. for(String start : events)
  165. {
  166. if(name.startsWith(start))
  167. {
  168. s = name;
  169. break out;
  170. }
  171. }
  172. }
  173. if(s != null)
  174. {
  175. manager.onRegisterCommand(command, s);
  176. }
  177. return super.registerCommand(command);
  178. }
  179. }