From 7e6eb6250402daeed746ba863c910e04ac380c0c Mon Sep 17 00:00:00 2001 From: SquidDev Date: Thu, 29 Nov 2018 11:57:52 +0000 Subject: [PATCH] Move the /computer command into the main computercraft command I don't think anyone has actually ended up using this, so it's unlikely to break anything (though do tell me if this is the case). On the flip side, this allows us to queue events on multiple computers, and means we can provide a little more documentation. --- .../shared/command/CommandComputer.java | 60 ------------------- .../shared/command/CommandComputerCraft.java | 30 ++++++++++ .../proxy/ComputerCraftProxyCommon.java | 2 - 3 files changed, 30 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/dan200/computercraft/shared/command/CommandComputer.java diff --git a/src/main/java/dan200/computercraft/shared/command/CommandComputer.java b/src/main/java/dan200/computercraft/shared/command/CommandComputer.java deleted file mode 100644 index 22879b2b0..000000000 --- a/src/main/java/dan200/computercraft/shared/command/CommandComputer.java +++ /dev/null @@ -1,60 +0,0 @@ -package dan200.computercraft.shared.command; - -import dan200.computercraft.ComputerCraft; -import dan200.computercraft.shared.computer.core.ComputerFamily; -import dan200.computercraft.shared.computer.core.ServerComputer; -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.server.MinecraftServer; -import org.apache.commons.lang3.ArrayUtils; - -import javax.annotation.Nonnull; - -public class CommandComputer extends CommandBase -{ - @Override - @Nonnull - public String getName() - { - return "computer"; - } - - @Override - @Nonnull - public String getUsage( @Nonnull ICommandSender sender ) - { - return "computer [value2]..."; - } - - @Override - public void execute( @Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] args ) throws CommandException - { - if( args.length < 2 ) - { - throw new CommandException( "Usage: /computer [value2]..." ); - } - try - { - ServerComputer computer = ComputerCraft.serverComputerRegistry.lookup( Integer.valueOf( args[ 0 ] ) ); - if( computer != null && computer.getFamily() == ComputerFamily.Command ) - { - computer.queueEvent( "computer_command", ArrayUtils.remove( args, 0 ) ); - } - else - { - throw new CommandException( "Computer #" + args[ 0 ] + " is not a Command Computer" ); - } - } - catch( NumberFormatException e ) - { - throw new CommandException( "Invalid ID" ); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 0; - } -} diff --git a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java index ddb340909..07ae05904 100644 --- a/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java +++ b/src/main/java/dan200/computercraft/shared/command/CommandComputerCraft.java @@ -9,6 +9,7 @@ import dan200.computercraft.core.tracking.Tracking; import dan200.computercraft.core.tracking.TrackingContext; import dan200.computercraft.core.tracking.TrackingField; import dan200.computercraft.shared.command.framework.*; +import dan200.computercraft.shared.computer.core.ComputerFamily; import dan200.computercraft.shared.computer.core.ServerComputer; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; @@ -379,6 +380,35 @@ public final class CommandComputerCraft extends CommandDelegate } } ); + root.register( new SubCommandBase( + "queue", " [args...]", "Send a computer_command event to a command computer", UserLevel.ANYONE, + "Send a computer_command event to a command computer, passing through the additional arguments. " + + "This is mostly designed for map makers, acting as a more computer-friendly version of /trigger. Any " + + "player can run the command, which would most likely be done through a text component's click event." + ) + { + @Override + public void execute( @Nonnull CommandContext context, @Nonnull List arguments ) throws CommandException + { + if( arguments.size() < 1 ) throw new CommandException( context.getFullUsage() ); + + String selector = arguments.get( 0 ); + Object[] rest = arguments.subList( 1, arguments.size() ).toArray(); + + boolean found = false; + for( ServerComputer computer : ComputerSelector.getComputers( selector ) ) + { + if( computer.getFamily() != ComputerFamily.Command || !computer.isOn() ) continue; + found = true; + computer.queueEvent( "computer_command", rest ); + } + + if( !found ) + { + throw new CommandException( "Could not find any command computers matching " + selector ); + } + } + } ); return root; } diff --git a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java index 5be69e778..be9c917c5 100644 --- a/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java +++ b/src/main/java/dan200/computercraft/shared/proxy/ComputerCraftProxyCommon.java @@ -10,7 +10,6 @@ import dan200.computercraft.ComputerCraft; import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.pocket.IPocketUpgrade; import dan200.computercraft.core.computer.MainThread; -import dan200.computercraft.shared.command.CommandComputer; import dan200.computercraft.shared.command.CommandComputerCraft; import dan200.computercraft.shared.command.ContainerViewComputer; import dan200.computercraft.shared.common.ColourableRecipe; @@ -131,7 +130,6 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy public void initServer( MinecraftServer server ) { CommandHandler handler = (CommandHandler) server.getCommandManager(); - handler.registerCommand( new CommandComputer() ); handler.registerCommand( new CommandComputerCraft() ); }