ModServerCommandManager.java 5.0 KB

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