ModServerCommandManager.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.TextComponentString;
  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 ICommandManager manager;
  20. public ModServerCommandManager(MinecraftServer serverIn)
  21. {
  22. super(serverIn);
  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. if(manager == null)
  48. {
  49. throw new NullPointerException();
  50. }
  51. this.manager = manager;
  52. }
  53. @Override
  54. public void notifyListener(ICommandSender cs, ICommand command, int flags, String key, Object... args)
  55. {
  56. ITextComponent text = new TextComponentTranslation("chat.type.admin", new Object[] {cs.getName(), new TextComponentTranslation(key, args)});
  57. text.getStyle().setColor(TextFormatting.GRAY);
  58. text.getStyle().setItalic(true);
  59. cs.sendMessage(text);
  60. }
  61. private String[] dropFirstString(String[] input)
  62. {
  63. String[] astring = new String[input.length - 1];
  64. System.arraycopy(input, 1, astring, 0, input.length - 1);
  65. return astring;
  66. }
  67. @Override
  68. public int executeCommand(ICommandSender sender, String rawCommand)
  69. {
  70. rawCommand = rawCommand.trim();
  71. if(rawCommand.startsWith("/"))
  72. {
  73. rawCommand = rawCommand.substring(1);
  74. }
  75. String[] args = rawCommand.split(" ");
  76. String command = args[0];
  77. args = dropFirstString(args);
  78. ICommand icommand = this.getCommands().get(command);
  79. if(icommand == null)
  80. {
  81. if(manager.executeCustomCommand(sender, command, args))
  82. {
  83. return 1;
  84. }
  85. manager.printMissingCommand(sender, command);
  86. return 0;
  87. }
  88. if(manager.hasPermission(sender, icommand.getName()))
  89. {
  90. CommandEvent e = new CommandEvent(icommand, sender, args);
  91. if(net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(e))
  92. {
  93. if(e.getException() != null)
  94. {
  95. com.google.common.base.Throwables.propagateIfPossible(e.getException());
  96. }
  97. return 1;
  98. }
  99. if(e.getParameters() != null)
  100. {
  101. args = e.getParameters();
  102. }
  103. this.tryExecute(sender, args, icommand, rawCommand);
  104. }
  105. else
  106. {
  107. manager.printMissingPermission(sender, icommand);
  108. }
  109. return 1;
  110. }
  111. @Override
  112. public List<String> getTabCompletions(ICommandSender sender, String input, @Nullable BlockPos pos)
  113. {
  114. String[] astring = input.split(" ", -1);
  115. String command = astring[0];
  116. if(astring.length == 1)
  117. {
  118. return this.getCommands().entrySet().stream()
  119. .filter(e -> (CommandBase.doesStringStartWith(command, e.getKey()) &&
  120. manager.hasPermission(sender, e.getKey())))
  121. .map(e -> e.getKey())
  122. .collect(Collectors.toList());
  123. }
  124. else if(astring.length > 1)
  125. {
  126. ICommand icommand = this.getCommands().get(command);
  127. if(icommand != null && manager.hasPermission(sender, icommand.getName()))
  128. {
  129. return icommand.getTabCompletions(this.getServer(), sender, dropFirstString(astring), pos);
  130. }
  131. }
  132. return Collections.<String>emptyList();
  133. }
  134. @Override
  135. public List<ICommand> getPossibleCommands(ICommandSender sender)
  136. {
  137. return this.getCommands().entrySet().stream()
  138. .filter(e -> manager.hasPermission(sender, e.getKey()))
  139. .map(e -> e.getValue())
  140. .collect(Collectors.toList());
  141. }
  142. }