Overhaul /mek commands; add support for teleporting, test rules and chunk watching

This commit is contained in:
Dave Smith committed 2019-03-17 11:28:09 -06:00
1 parent 2b8411bdd0
commit 88b52a6f64
5 files changed
+248 -100

No files matched your search

@@ -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 <parameters>";
}
@Nonnull
@Override
public List<String> 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."));
}
}
}
}
+2 -1
View File
@@ -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
@@ -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<Long> 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);
}
}
@@ -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<UUID, Stack<BlockPos>> 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<BlockPos> playerLocations = tpStack.getOrDefault(player, new Stack<BlockPos>());
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<BlockPos> playerLocations = tpStack.getOrDefault(player, new Stack<BlockPos>());
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;
}
}