diff --git a/src/main/java/mekanism/common/CommandMekanism.java b/src/main/java/mekanism/common/CommandMekanism.java deleted file mode 100644 index 05e1d83e8..000000000 --- a/src/main/java/mekanism/common/CommandMekanism.java +++ /dev/null @@ -1,99 +0,0 @@ -package mekanism.common; - -import java.util.Arrays; -import java.util.List; -import javax.annotation.Nonnull; -import mekanism.api.EnumColor; -import mekanism.api.MekanismAPI; -import net.minecraft.command.CommandBase; -import net.minecraft.command.ICommandSender; -import net.minecraft.server.MinecraftServer; -import net.minecraft.util.text.TextComponentString; -import net.minecraftforge.fml.common.FMLCommonHandler; - -public class CommandMekanism extends CommandBase { - - @Override - public boolean checkPermission(MinecraftServer server, ICommandSender sender) { - return server.isSinglePlayer() || super.checkPermission(server, sender); - } - - @Nonnull - @Override - public String getName() { - return "mk"; - } - - @Nonnull - @Override - public String getUsage(@Nonnull ICommandSender sender) { - return "/mk "; - } - - @Nonnull - @Override - public List getAliases() { - return Arrays.asList("mekanism", "mek", "mekanica"); - } - - @Override - public void execute(@Nonnull MinecraftServer server, @Nonnull ICommandSender sender, @Nonnull String[] params) { - if (params.length < 1) { - sender.sendMessage(new TextComponentString( - EnumColor.GREY + " * Version: " + EnumColor.DARK_GREY + Mekanism.versionNumber)); - sender.sendMessage(new TextComponentString(EnumColor.GREY + " * Code, textures, and ideas by aidancbrady")); - } else { - if (params[0].equalsIgnoreCase("help")) { - if (params.length == 1) { - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk" + EnumColor.GREY + " -- displays the main page.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk help" + EnumColor.GREY + " -- displays this guide.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk debug" + EnumColor.GREY + " -- toggles Mekanism's debug mode.")); - sender.sendMessage(new TextComponentString(EnumColor.INDIGO + " /mk teleporter" + EnumColor.GREY - + " -- provides information on teleporters.")); - } else if (params[1].equalsIgnoreCase("teleporter")) { - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk teleporter freq list" + EnumColor.GREY - + " -- displays a list of the public frequencies.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk teleporter freq list [user]" + EnumColor.GREY - + " -- displays a list of a certain user's private frequencies.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk teleporter freq delete [freq]" + EnumColor.GREY - + " -- removes a frequency from the public list.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk teleporter freq delete [user] [freq]" + EnumColor.GREY - + " -- removes a freqency from a certain user's private list.")); - sender.sendMessage(new TextComponentString( - EnumColor.INDIGO + " /mk teleporter freq deleteAll [user]" + EnumColor.GREY - + " -- removes all frequencies owned by a certain user.")); - } - } else if (params[0].equalsIgnoreCase("debug")) { - MekanismAPI.debug = !MekanismAPI.debug; - sender.sendMessage(new TextComponentString( - EnumColor.DARK_BLUE + Mekanism.LOG_TAG + EnumColor.GREY + " Debug mode set to " - + EnumColor.DARK_GREY + MekanismAPI.debug)); - } else if (params[0].equalsIgnoreCase("op")) { - MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance(); - - if (Mekanism.gameProfile != null) { - minecraftserver.getPlayerList().addOp(Mekanism.gameProfile); - notifyCommandListener(sender, this, "commands.op.success", Mekanism.LOG_TAG); - } - } else if (params[0].equalsIgnoreCase("deop")) { - MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance(); - - if (Mekanism.gameProfile != null) { - minecraftserver.getPlayerList().removeOp(Mekanism.gameProfile); - notifyCommandListener(sender, this, "commands.deop.success", Mekanism.LOG_TAG); - } - } else { - sender.sendMessage(new TextComponentString( - EnumColor.DARK_BLUE + Mekanism.LOG_TAG + EnumColor.GREY + " Unknown command. Type '" - + EnumColor.INDIGO + "/mk help" + EnumColor.GREY + "' for help.")); - } - } - } -} diff --git a/src/main/java/mekanism/common/Mekanism.java b/src/main/java/mekanism/common/Mekanism.java index 3834df9f9..b21163f96 100644 --- a/src/main/java/mekanism/common/Mekanism.java +++ b/src/main/java/mekanism/common/Mekanism.java @@ -33,6 +33,7 @@ import mekanism.common.block.states.BlockStateMachine; import mekanism.common.block.states.BlockStateTransmitter.TransmitterType; import mekanism.common.capabilities.Capabilities; import mekanism.common.chunkloading.ChunkManager; +import mekanism.common.command.CommandMek; import mekanism.common.config.MekanismConfig.general; import mekanism.common.config.MekanismConfig.usage; import mekanism.common.content.boiler.SynchronizedBoilerData; @@ -768,7 +769,7 @@ public class Mekanism { @EventHandler public void serverStarting(FMLServerStartingEvent event) { - event.registerServerCommand(new CommandMekanism()); + CommandMek.register(event); } @EventHandler diff --git a/src/main/java/mekanism/common/command/CommandChunk.java b/src/main/java/mekanism/common/command/CommandChunk.java new file mode 100644 index 000000000..084543b92 --- /dev/null +++ b/src/main/java/mekanism/common/command/CommandChunk.java @@ -0,0 +1,75 @@ +package mekanism.common.command; + +import java.util.HashSet; +import java.util.Set; +import mekanism.common.command.CommandMek.Cmd; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.ChunkPos; +import net.minecraft.util.text.TextComponentString; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.world.ChunkEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.server.command.CommandTreeBase; + +public class CommandChunk extends CommandTreeBase { + + private Set chunkWatchers = new HashSet<>(); + + public CommandChunk() { + addSubcommand(new Cmd("watch", "cmd.mek.chunk.watch", this::addWatcher)); + addSubcommand(new Cmd("unwatch", "cmd.mek.chunk.unwatch", this::removeWatcher)); + addSubcommand(new Cmd("clear", "cmd.mek.chunk.clear", this::clearWatchers)); + + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public String getName() { + return "chunk"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "cmd.mek.chunk.usage"; + } + + @SubscribeEvent + public void onChunkLoad(ChunkEvent.Load event) { + handleChunkEvent(event, "Loaded"); + } + + @SubscribeEvent + public void onChunkUnload(ChunkEvent.Unload event) { + handleChunkEvent(event, "Unloaded"); + } + + private void handleChunkEvent(ChunkEvent event, String direction) { + ChunkPos pos = event.getChunk().getPos(); + long key = ChunkPos.asLong(pos.x, pos.z); + if (chunkWatchers.contains(key)) { + String msg = String.format("%s chunk %d, %d", direction, pos.x, pos.z); + event.getWorld().getMinecraftServer().getPlayerList().sendMessage(new TextComponentString(msg)); + } + } + + public void addWatcher(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + ChunkPos cpos = new ChunkPos(sender.getPosition()); + chunkWatchers.add(ChunkPos.asLong(cpos.x, cpos.z)); + CommandBase.notifyCommandListener(sender, this, "cmd.mek.chunk.watch", cpos.x, cpos.z); + } + + public void removeWatcher(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + ChunkPos cpos = new ChunkPos(sender.getPosition()); + chunkWatchers.remove(ChunkPos.asLong(cpos.x, cpos.z)); + CommandBase.notifyCommandListener(sender, this, "cmd.mek.chunk.unwatch", cpos.x, cpos.z); + } + + public void clearWatchers(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + int count = chunkWatchers.size(); + chunkWatchers.clear(); + CommandBase.notifyCommandListener(sender, this, "cmd.mek.chunk.clear", count); + } +} diff --git a/src/main/java/mekanism/common/command/CommandMek.java b/src/main/java/mekanism/common/command/CommandMek.java new file mode 100644 index 000000000..8bd36041e --- /dev/null +++ b/src/main/java/mekanism/common/command/CommandMek.java @@ -0,0 +1,149 @@ +package mekanism.common.command; + + +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; +import java.util.UUID; +import mekanism.api.MekanismAPI; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.CommandGameRule; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.GameRules; +import net.minecraftforge.fml.common.event.FMLServerStartedEvent; +import net.minecraftforge.fml.common.event.FMLServerStartingEvent; +import net.minecraftforge.server.command.CommandTreeBase; + +public class CommandMek extends CommandTreeBase { + + private Map> tpStack = new HashMap<>(); + + public CommandMek() { + addSubcommand(new Cmd("debug", "cmd.mek.debug", this::toggleDebug)); + addSubcommand(new Cmd("testrules", "cmd.mek.testrules", this::setupTestRules)); + addSubcommand(new Cmd("tp", "cmd.mek.tp", this::teleportPush)); + addSubcommand(new Cmd("tpop", "cmd.mek.tpop", this::teleportPop)); + addSubcommand(new CommandChunk()); + } + + public static void register(FMLServerStartingEvent event) { + CommandMek cmd = new CommandMek(); + event.registerServerCommand(cmd); + event.registerServerCommand(new Cmd("mtp", "cmd.mek.tp", cmd::teleportPush)); + event.registerServerCommand(new Cmd("mtpop", "cmd.mek.tpop", cmd::teleportPop)); + } + + @Override + public String getName() { + return "mek"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "cmd.mek.usage"; + } + + + public void toggleDebug(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + MekanismAPI.debug = !MekanismAPI.debug; + CommandBase.notifyCommandListener(sender, this, "cmd.mek.debug", MekanismAPI.debug); + } + + public void setupTestRules(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + GameRules rules = server.getEntityWorld().getGameRules(); + rules.setOrCreateGameRule("doMobSpawning", "false"); + rules.setOrCreateGameRule("doDaylightCycle", "false"); + rules.setOrCreateGameRule("doWeatherCycle", "false"); + server.getEntityWorld().setWorldTime(2000); + CommandGameRule.notifyGameRuleChange(rules, "", server); + CommandBase.notifyCommandListener(sender, this, "cmd.mek.testrules"); + } + + public void teleportPush(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + if (args.length < 3) { + notifyCommandListener(sender, this, "cmd.mek.tp.missing.args"); + return; + } + + // Parse the target arguments from command line + CoordinateArg xArg = parseCoordinate(10, args[0], true); + CoordinateArg yArg = parseCoordinate(10, args[1], true); + CoordinateArg zArg = parseCoordinate(10, args[2], true); + + // Save the current location on the stack + UUID player = sender.getCommandSenderEntity().getUniqueID(); + Stack playerLocations = tpStack.getOrDefault(player, new Stack()); + playerLocations.push(sender.getPosition()); + tpStack.put(player, playerLocations); + + // Teleport user to new location + teleport(sender.getCommandSenderEntity(), xArg.getResult(), yArg.getResult(), zArg.getResult()); + notifyCommandListener(sender, this, "cmd.mek.tp", args[0], args[1], args[2]); + } + + public void teleportPop(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + UUID player = sender.getCommandSenderEntity().getUniqueID(); + + // Get stack of locations for the user; if there's at least one entry, pop it off + // and send the user back there + Stack playerLocations = tpStack.getOrDefault(player, new Stack()); + if (!playerLocations.isEmpty()) { + BlockPos lastPos = playerLocations.pop(); + tpStack.put(player, playerLocations); + teleport(sender.getCommandSenderEntity(), lastPos.getX(), lastPos.getY(), lastPos.getZ()); + notifyCommandListener(sender, this, "cmd.mek.tpop", + lastPos.getX(), lastPos.getY(), lastPos.getZ(), playerLocations.size()); + } else { + notifyCommandListener(sender, this, "cmd.mek.tpop.empty.stack"); + } + } + + private void teleport(Entity player, double x, double y, double z) { + if (player instanceof EntityPlayerMP) { + EntityPlayerMP mp = (EntityPlayerMP)player; + mp.connection.setPlayerLocation(x, y, z, mp.rotationYaw, mp.rotationPitch); + } else { + EntityPlayerSP sp = (EntityPlayerSP)player; + sp.setLocationAndAngles(x, y, z, sp.rotationYaw, sp.rotationPitch); + } + } + + // Wrapper class that makes it easier to create single method commands + public static class Cmd extends CommandBase { + + private String name; + private String usage; + private CmdExecute ex; + + Cmd(String name, String usage, CmdExecute ex) { + this.name = name; + this.usage = usage; + this.ex = ex; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getUsage(ICommandSender sender) { + return usage + ".usage"; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + ex.execute(server, sender, args); + } + } + + interface CmdExecute { + void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException; + } +} diff --git a/src/main/resources/assets/mekanism/lang/en_us.lang b/src/main/resources/assets/mekanism/lang/en_us.lang index 38667fd4f..ecbe4fcd5 100644 --- a/src/main/resources/assets/mekanism/lang/en_us.lang +++ b/src/main/resources/assets/mekanism/lang/en_us.lang @@ -1133,3 +1133,25 @@ mekanism.configgui.ctgy.tools.tools=Tools Balance mekanism.configgui.ctgy.tools.general.tooltip=Settings regarding MekanismTools' general configuration mekanism.configgui.ctgy.tools.armor.tooltip=Settings regarding the protection values and balance of MekanismTools armor sets mekanism.configgui.ctgy.tools.tools.tooltip=Settings regarding the efficiency values and balance of MekanismTools tool sets + +# Commands +cmd.mek.usage= +cmd.mek.debug.usage= +cmd.mek.debug= +cmd.mek.testrules.usage=Setup world rules for testing (no mob spawning, daylight cycle or weather) +cmd.mek.testrules=Mob spawning, daylight cycle and weather disabled! +cmd.mek.tp.usage=Teleport to new coordinates, saving last position on a stack +cmd.mek.tp=Teleported to %d,%d,%d - saved last position on stack +cmd.mek.tp.missing.args=Teleporting requires three coordinates: x, y, z +cmd.mek.tpop.usage=Return to last position on stack +cmd.mek.tpop=Returned to %d,%d,%d; %d positions on stack +cmd.mek.tpop.empty.stack=No positions on stack +cmd.mek. + +cmd.mek.chunk.usage= +cmd.mek.chunk.watch.usage=Add caller's chunk to watch list for loading/unloading events +cmd.mek.chunk.watch=Chunk %d,%d added to watch list +cmd.mek.chunk.unwatch.usage=Remove caller's chunk from watch list for chunk loading/unloading events +cmd.mek.chunk.unwatch=Chunk %d,%d removed from watch list +cmd.mek.chunk.clear.usage=Remove all chunks from watch list for chunk loading/unloading events +cmd.mek.chunk.clear=%d chunks removed from watch list \ No newline at end of file