123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package me.kcm.command;
- import java.util.Collections;
- import java.util.List;
- import java.util.stream.Collectors;
- import javax.annotation.Nullable;
- import net.minecraft.command.CommandBase;
- import net.minecraft.command.ICommand;
- import net.minecraft.command.ICommandSender;
- import net.minecraft.command.ServerCommandManager;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentTranslation;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraftforge.event.CommandEvent;
- public class ModServerCommandManager extends ServerCommandManager
- {
- private ICommandManager manager;
-
- public ModServerCommandManager(MinecraftServer serverIn)
- {
- super(serverIn);
- // the manager must not be null
- this.manager = new ICommandManager()
- {
- @Override
- public boolean hasPermission(ICommandSender cs, String perm)
- {
- return true;
- }
- @Override
- public boolean executeCustomCommand(ICommandSender cs, String command, String[] args)
- {
- return false;
- }
- @Override
- public void printMissingCommand(ICommandSender cs, String command)
- {
- }
- @Override
- public void printMissingPermission(ICommandSender cs, ICommand command)
- {
- }
- };
- }
-
- public void setPermissionManager(ICommandManager manager)
- {
- // the manager must not be null
- if(manager == null)
- {
- throw new NullPointerException();
- }
- this.manager = manager;
- }
-
- @Override
- public void notifyListener(ICommandSender cs, ICommand command, int flags, String key, Object... args)
- {
- ITextComponent text = new TextComponentTranslation("chat.type.admin", new Object[] {cs.getName(), new TextComponentTranslation(key, args)});
- text.getStyle().setColor(TextFormatting.GRAY);
- text.getStyle().setItalic(true);
- cs.sendMessage(text);
- }
-
- private String[] dropFirstString(String[] input)
- {
- String[] astring = new String[input.length - 1];
- System.arraycopy(input, 1, astring, 0, input.length - 1);
- return astring;
- }
-
- @Override
- public int executeCommand(ICommandSender sender, String rawCommand)
- {
- rawCommand = rawCommand.trim();
- if(rawCommand.startsWith("/"))
- {
- rawCommand = rawCommand.substring(1);
- }
- String[] args = rawCommand.split(" ");
- String command = args[0];
- args = dropFirstString(args);
-
- ICommand icommand = this.getCommands().get(command);
- if(icommand == null)
- {
- if(manager.executeCustomCommand(sender, command, args))
- {
- return 1;
- }
- manager.printMissingCommand(sender, command);
- return 0;
- }
-
- if(manager.hasPermission(sender, icommand.getName()))
- {
- CommandEvent e = new CommandEvent(icommand, sender, args);
- if(net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(e))
- {
- if(e.getException() != null)
- {
- com.google.common.base.Throwables.propagateIfPossible(e.getException());
- }
- return 1;
- }
- if(e.getParameters() != null)
- {
- args = e.getParameters();
- }
- this.tryExecute(sender, args, icommand, rawCommand);
- }
- else
- {
- manager.printMissingPermission(sender, icommand);
- }
- return 1;
- }
- @Override
- public List<String> getTabCompletions(ICommandSender sender, String input, @Nullable BlockPos pos)
- {
- String[] astring = input.split(" ", -1);
- String command = astring[0];
- if(astring.length == 1)
- {
- return this.getCommands().entrySet().stream()
- .filter(e -> (CommandBase.doesStringStartWith(command, e.getKey()) &&
- manager.hasPermission(sender, e.getKey())))
- .map(e -> e.getKey())
- .collect(Collectors.toList());
- }
- else if(astring.length > 1)
- {
- ICommand icommand = this.getCommands().get(command);
- if(icommand != null && manager.hasPermission(sender, icommand.getName()))
- {
- return icommand.getTabCompletions(this.getServer(), sender, dropFirstString(astring), pos);
- }
- }
- return Collections.<String>emptyList();
- }
- @Override
- public List<ICommand> getPossibleCommands(ICommandSender sender)
- {
- return this.getCommands().entrySet().stream()
- .filter(e -> manager.hasPermission(sender, e.getKey()))
- .map(e -> e.getValue())
- .collect(Collectors.toList());
- }
- }
|