From 62df136fae23396e8094a4260e7318d0898b3ac3 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 25 Nov 2019 15:15:17 +0900 Subject: [PATCH] Impl all commands but TBMEdit for 1.14 --- .../common/CommandTBMDisable.java | 61 ------------ .../common/CommandTBMDisableAll.java | 67 ------------- .../common/CommandTBMEnable.java | 61 ------------ .../common/CommandTBMEnableAll.java | 62 ------------ .../common/CommandTBMSet.java | 96 ------------------- .../common/TurnBasedMinecraftMod.java | 81 +++++++++++++++- 6 files changed, 76 insertions(+), 352 deletions(-) delete mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java delete mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisableAll.java delete mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java delete mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnableAll.java delete mode 100644 src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java deleted file mode 100644 index ac15f1e..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common; - -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -public class CommandTBMDisable extends CommandBase -{ - public static final String usage = "/tbm-disable (Disables turn-based-battle for the current player (only OP or anyone, depending on config))"; - private Config config; - - CommandTBMDisable(Config config) - { - this.config = config; - } - - @Override - public String getName() - { - return "tbm-disable"; - } - - @Override - public String getUsage(ICommandSender sender) - { - return usage; - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { - EntityPlayerMP player = null; - player = getCommandSenderAsPlayer(sender); - if(!config.getIfOnlyOPsCanDisableTurnBasedForSelf() || player.getServer().isSinglePlayer() || player.getServer().getPlayerList().getOppedPlayers().getEntry(player.getGameProfile()) != null) - { - config.addBattleIgnoringPlayer(player.getEntityId()); - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Disabled turn-based-combat for current player"), player); - TurnBasedMinecraftMod.logger.info("Disabled turn-based-combat for " + player.getName()); - } - else - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Only OPs can use this command (based on config)"), player); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 0; - } - - @Override - public boolean checkPermission(MinecraftServer server, ICommandSender sender) - { - return true; - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisableAll.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisableAll.java deleted file mode 100644 index 5914dbd..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisableAll.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common; - -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -public class CommandTBMDisableAll extends CommandBase -{ - public static final String usage = "/tbm-disable-all (Disables turn-based-battle for everyone"; - private Config config; - - public CommandTBMDisableAll(Config config) - { - this.config = config; - } - - @Override - public String getName() - { - return "tbm-disable-all"; - } - - @Override - public String getUsage(ICommandSender sender) - { - return usage; - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { - config.setBattleDisabledForAll(true); - for(Entity player : server.getPlayerList().getPlayers()) - { - config.addBattleIgnoringPlayer(player.getEntityId()); - } - EntityPlayerMP senderPlayer = null; - try { - senderPlayer = getCommandSenderAsPlayer(sender); - } catch(Throwable t) - { - // Ignore case when sender EntityPlayer cannot be found - } - if(senderPlayer != null) - { - TurnBasedMinecraftMod.logger.info(senderPlayer.getName() + " disabled turn-based-combat for everyone"); - TurnBasedMinecraftMod.NWINSTANCE.sendToAll(new PacketGeneralMessage(senderPlayer.getName() + " disabled turn-based-battle for everyone")); - } - else - { - TurnBasedMinecraftMod.logger.info("An admin disabled turn-based-combat for everyone"); - TurnBasedMinecraftMod.NWINSTANCE.sendToAll(new PacketGeneralMessage("An admin disabled turn-based-battle for everyone")); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 2; - } - -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java deleted file mode 100644 index bf47413..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common; - -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -public class CommandTBMEnable extends CommandBase -{ - public static final String usage = "/tbm-enable (Enables turn-based-battle for the current player (only OP or anyone, depending on config))"; - private Config config; - - CommandTBMEnable(Config config) - { - this.config = config; - } - - @Override - public String getName() - { - return "tbm-enable"; - } - - @Override - public String getUsage(ICommandSender sender) - { - return usage; - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { - EntityPlayerMP player = null; - player = getCommandSenderAsPlayer(sender); - if(!config.getIfOnlyOPsCanDisableTurnBasedForSelf() || player.getServer().isSinglePlayer() || player.getServer().getPlayerList().getOppedPlayers().getEntry(player.getGameProfile()) != null) - { - config.removeBattleIgnoringPlayer(player.getEntityId()); - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Enabled turn-based-combat for current player"), player); - TurnBasedMinecraftMod.logger.info("Enabled turn-based-combat for " + player.getName()); - } - else - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Only OPs can use this command (based on config)"), player); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 0; - } - - @Override - public boolean checkPermission(MinecraftServer server, ICommandSender sender) - { - return true; - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnableAll.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnableAll.java deleted file mode 100644 index 303ddb9..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnableAll.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common; - -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -public class CommandTBMEnableAll extends CommandBase -{ - public static final String usage = "/tbm-enable-all (enables turn-based-combat for everyone)"; - private Config config; - - public CommandTBMEnableAll(Config config) - { - this.config = config; - } - - @Override - public String getName() - { - return "tbm-enable-all"; - } - - @Override - public String getUsage(ICommandSender sender) - { - return usage; - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { - config.setBattleDisabledForAll(false); - config.clearBattleIgnoringPlayers(); - EntityPlayerMP senderPlayer = null; - try { - senderPlayer = getCommandSenderAsPlayer(sender); - } catch(Throwable t) - { - // Ignore case when sender EntityPlayer cannot be found - } - if(senderPlayer != null) - { - TurnBasedMinecraftMod.logger.info(senderPlayer.getName() + " enabled turn-based-combat for everyone"); - TurnBasedMinecraftMod.NWINSTANCE.sendToAll(new PacketGeneralMessage(senderPlayer.getName() + " enabled turn-based-combat for everyone")); - } - else - { - TurnBasedMinecraftMod.logger.info("An admin enabled turn-based-combat for everyone"); - TurnBasedMinecraftMod.NWINSTANCE.sendToAll(new PacketGeneralMessage("An admin enabled turn-based-combat for everyone")); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 2; - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java deleted file mode 100644 index f762c00..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common; - -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; - -import net.minecraft.command.CommandBase; -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.server.MinecraftServer; - -public class CommandTBMSet extends CommandBase -{ - public static final String usage = "/tbm-set "; - private Config config; - - public CommandTBMSet(Config config) - { - this.config = config; - } - - @Override - public String getName() - { - return "tbm-set"; - } - - @Override - public String getUsage(ICommandSender sender) - { - return usage; - } - - @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { - if(args.length != 2) - { - throw new WrongUsageException(usage, new Object[0]); - } - EntityPlayerMP target = getPlayer(server, sender, args[0]); - EntityPlayerMP senderPlayer = null; - try { - senderPlayer = getCommandSenderAsPlayer(sender); - } catch(Throwable t) - { - // Ignore case when sender EntityPlayer cannot be found - } - if(args[1].toLowerCase().equals("true")) - { - config.removeBattleIgnoringPlayer(target.getEntityId()); - if(senderPlayer != null) - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage(senderPlayer.getName() + " enabled turn-based-combat for you"), target); - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("You enabled turn-based-combat for " + target.getName()), senderPlayer); - TurnBasedMinecraftMod.logger.info(senderPlayer.getName() + " enabled turn-based-combat for " + target.getName()); - } - else - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("An admin enabled turn-based-combat for you"), target); - TurnBasedMinecraftMod.logger.info("Enabled turn-based-combat for " + target.getName()); - } - } - else if(args[1].toLowerCase().equals("false")) - { - config.addBattleIgnoringPlayer(target.getEntityId()); - if(senderPlayer != null) - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage(senderPlayer.getName() + " disabled turn-based-combat for you"), target); - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("You disabled turn-based-combat for " + target.getName()), senderPlayer); - TurnBasedMinecraftMod.logger.info(senderPlayer.getName() + " disabled turn-based-combat for " + target.getName()); - } - else - { - TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("An admin disabled turn-based-combat for you"), target); - TurnBasedMinecraftMod.logger.info("Disabled turn-based-combat for " + target.getName()); - } - } - else - { - throw new WrongUsageException(usage, new Object[0]); - } - } - - @Override - public int getRequiredPermissionLevel() - { - return 2; - } - - @Override - public boolean isUsernameIndex(String[] args, int index) - { - return index == 0; - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 70d3067..93d4f28 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -2,7 +2,11 @@ package com.seodisparate.TurnBasedMinecraft.common; import com.seodisparate.TurnBasedMinecraft.client.ClientProxy; import com.seodisparate.TurnBasedMinecraft.common.networking.*; +import net.minecraft.command.Commands; +import net.minecraft.command.arguments.EntityArgument; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.StringTextComponent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DistExecutor; @@ -13,6 +17,7 @@ import net.minecraftforge.fml.event.lifecycle.FMLDedicatedServerSetupEvent; import net.minecraftforge.fml.event.server.FMLServerStartingEvent; import net.minecraftforge.fml.event.server.FMLServerStoppingEvent; import net.minecraftforge.fml.network.NetworkRegistry; +import net.minecraftforge.fml.network.PacketDistributor; import net.minecraftforge.fml.network.simple.SimpleChannel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -128,11 +133,77 @@ public class TurnBasedMinecraftMod proxy.getConfig().clearBattleIgnoringPlayers(); // register commands - event.registerServerCommand(new CommandTBMDisable(proxy.getConfig())); - event.registerServerCommand(new CommandTBMEnable(proxy.getConfig())); - event.registerServerCommand(new CommandTBMSet(proxy.getConfig())); - event.registerServerCommand(new CommandTBMDisableAll(proxy.getConfig())); - event.registerServerCommand(new CommandTBMEnableAll(proxy.getConfig())); + // tbm-disable + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-disable") + .requires(c -> { + return !proxy.getConfig().getIfOnlyOPsCanDisableTurnBasedForSelf() || c.hasPermissionLevel(2); + }) + .executes( c -> { + proxy.getConfig().addBattleIgnoringPlayer(c.getSource().asPlayer().getEntityId()); + c.getSource().sendFeedback(new StringTextComponent("Disabled turn-based-combat for current player"), true); + return 1; + })); + // tbm-disable-all + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-disable-all") + .requires(c -> { + return c.hasPermissionLevel(2); + }) + .executes(c -> { + proxy.getConfig().setBattleDisabledForAll(true); + for(ServerPlayerEntity player : c.getSource().getServer().getPlayerList().getPlayers()) { + proxy.getConfig().addBattleIgnoringPlayer(player.getEntityId()); + getHandler().send(PacketDistributor.PLAYER.with(() -> player), new PacketGeneralMessage("OP disabled turn-based-combat for everyone")); + } + return 1; + })); + // tbm-enable + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-enable") + .requires(c -> !proxy.getConfig().getIfOnlyOPsCanDisableTurnBasedForSelf() || c.hasPermissionLevel(2)) + .executes(c -> { + proxy.getConfig().removeBattleIgnoringPlayer(c.getSource().asPlayer().getEntityId()); + c.getSource().sendFeedback(new StringTextComponent("Enabled turn-based-combat for current player"), true)); + return 1; + })); + // tbm-enable-all + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-enable-all") + .requires(c -> c.hasPermissionLevel(2)) + .executes(c -> { + proxy.getConfig().setBattleDisabledForAll(false); + proxy.getConfig().clearBattleIgnoringPlayers(); + for(ServerPlayerEntity player: c.getSource().getServer().getPlayerList().getPlayers()) { + getHandler().send(PacketDistributor.PLAYER.with(() -> player), new PacketGeneralMessage("OP enabled turn-based-combat for everyone")); + } + return 1; + })); + // tbm-set-enable + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-set-enable") + .requires(c -> c.hasPermissionLevel(2)) + .then(Commands.argument("targets", EntityArgument.players()).executes(c -> { + for(ServerPlayerEntity player : EntityArgument.getPlayers(c, "targets")) { + proxy.getConfig().addBattleIgnoringPlayer(player.getEntityId()); + getHandler().send(PacketDistributor.PLAYER.with(() -> player), new PacketGeneralMessage("OP enabled turn-based-combat for you")); + c.getSource().sendFeedback(new StringTextComponent("Enabled turn-based-combat for " + player.getDisplayName().getUnformattedComponentText()), true); + } + return 1; + }))); + // tbm-set-disable + event.getServer().getCommandManager().getDispatcher().register( + Commands.literal("tbm-set-disable") + .requires(c -> c.hasPermissionLevel(2)) + .then(Commands.argument("targets", EntityArgument.players()).executes(c -> { + for(ServerPlayerEntity player : EntityArgument.getPlayers(c, "targets")) { + proxy.getConfig().removeBattleIgnoringPlayer(player.getEntityId()); + getHandler().send(PacketDistributor.PLAYER.with(() -> player), new PacketGeneralMessage("OP disabled turn-based-combat for you")); + c.getSource().sendFeedback(new StringTextComponent("Disabled turn-based-combat for " + player.getDisplayName().getUnformattedComponentText()), true); + } + return 1; + }))); + // tbm-edit event.registerServerCommand(new CommandTBMEdit(proxy.getConfig())); }