package me.kcm.command; import java.util.Collections; import java.util.LinkedList; import java.util.List; import javax.annotation.Nullable; 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 LinkedList events = new LinkedList<>(); 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) { } @Override public void onRegisterCommand(ICommand command, String domain) { } }; } public void setPermissionManager(ICommandManager manager) { // the manager must not be null if(manager == null) { throw new NullPointerException(); } this.manager = manager; } public void registerRegistryEvent(String s) { events.add(s); } @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].toLowerCase(); args = dropFirstString(args); if(manager.executeCustomCommand(sender, command, args)) { return 1; } ICommand icommand = this.getCommands().get(command); if(icommand == null) { 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 getTabCompletions(ICommandSender sender, String input, @Nullable BlockPos pos) { String[] astring = input.split(" ", -1); String command = astring[0]; if(astring.length == 1) { return Collections.emptyList(); /*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.emptyList(); } @Override public List getPossibleCommands(ICommandSender sender) { return Collections.emptyList(); /*return this.getCommands().entrySet().stream() .filter(e -> manager.hasPermission(sender, e.getKey())) .map(e -> e.getValue()) .collect(Collectors.toList());*/ } @Override public ICommand registerCommand(ICommand command) { String s = null; out: for(StackTraceElement e : Thread.currentThread().getStackTrace()) { String name = e.getClassName(); if(events == null) { events = new LinkedList<>(); } for(String start : events) { if(name.startsWith(start)) { s = name; break out; } } } if(s != null) { manager.onRegisterCommand(command, s); } return super.registerCommand(command); } }