diff --git a/build.gradle b/build.gradle index e543377..37b7acc 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { apply plugin: 'net.minecraftforge.gradle.forge' //Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. -version = "1.0" +version = "1.1" group = "com.seodisparate.TurnBasedMinecraft" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "TurnBasedMinecraft" diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java index 037bf6f..0a7a1a9 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java @@ -64,7 +64,12 @@ public class AttackEventHandler return; } - if(!isAttackerValid(event) && event.getEntity() != null && event.getSource().getTrueSource() != null && TurnBasedMinecraftMod.proxy.getBattleManager().checkAttack(event)) + if(!isAttackerValid(event) + && event.getEntity() != null + && event.getSource().getTrueSource() != null + && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getSource().getTrueSource().getEntityId()) + && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getEntity().getEntityId()) + && TurnBasedMinecraftMod.proxy.getBattleManager().checkAttack(event)) { // TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.commonProxy.getAttackingEntity() + " and " + event.getEntity()); event.setCanceled(true); diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java new file mode 100644 index 0000000..710498e --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMDisable.java @@ -0,0 +1,60 @@ +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().getPlayerList().getOppedPlayers().getEntry(player.getGameProfile()) != null) + { + config.addBattleIgnoringPlayer(player.getEntityId()); + TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Disabled turn-based-combat for current player"), player); + } + 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/CommandTBMEnable.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java new file mode 100644 index 0000000..3316a57 --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMEnable.java @@ -0,0 +1,60 @@ +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().getPlayerList().getOppedPlayers().getEntry(player.getGameProfile()) != null) + { + config.removeBattleIgnoringPlayer(player.getEntityId()); + TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Enabled turn-based-combat for current player"), player); + } + 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/CommandTBMSet.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java new file mode 100644 index 0000000..f762c00 --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommandTBMSet.java @@ -0,0 +1,96 @@ +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/Config.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java index 20baf39..313a169 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -44,6 +45,8 @@ public class Config private boolean freezeCombatantsInBattle = false; private int sillyMusicThreshold = 40; private int configVersion = 0; + private Set battleIgnoringPlayers = null; + private boolean onlyOPsSelfDisableTB = true; public Config(Logger logger) { @@ -52,6 +55,7 @@ public class Config this.logger = logger; musicBattleTypes = new HashSet(); musicSillyTypes = new HashSet(); + battleIgnoringPlayers = new HashSet(); int internalVersion = 0; try @@ -169,6 +173,17 @@ public class Config { continue; } + else if(xmlReader.getLocalName().equals("WhoCanDisableTurnBasedForSelf")) + { + if(xmlReader.getElementText().toLowerCase().equals("any")) + { + onlyOPsSelfDisableTB = false; + } + else + { + onlyOPsSelfDisableTB = true; + } + } else if(xmlReader.getLocalName().equals("MaxInBattle")) { maxInBattle = Integer.parseInt(xmlReader.getElementText()); @@ -566,4 +581,29 @@ public class Config { return (int)(battleDecisionDurationNanos / 1000000000L); } + + protected void addBattleIgnoringPlayer(int id) + { + battleIgnoringPlayers.add(id); + } + + protected void removeBattleIgnoringPlayer(int id) + { + battleIgnoringPlayers.remove(id); + } + + protected void clearBattleIgnoringPlayers() + { + battleIgnoringPlayers.clear(); + } + + protected Set getBattleIgnoringPlayers() + { + return battleIgnoringPlayers; + } + + public boolean getIfOnlyOPsCanDisableTurnBasedForSelf() + { + return onlyOPsSelfDisableTB; + } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index efee62a..ed994e7 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -6,6 +6,7 @@ import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecisio import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequestInfo; +import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; @@ -25,7 +26,7 @@ public class TurnBasedMinecraftMod { public static final String MODID = "com.seodisparate.turnbasedminecraft"; public static final String NAME = "Turn Based Minecraft Mod"; - public static final String VERSION = "1.0"; + public static final String VERSION = "1.1"; public static final String CONFIG_FILENAME = "TBM_Config.xml"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME; @@ -74,6 +75,11 @@ public class TurnBasedMinecraftMod PacketBattleMessage.class, packetHandlerID++, Side.CLIENT); + NWINSTANCE.registerMessage( + PacketGeneralMessage.HandlerGeneralMessage.class, + PacketGeneralMessage.class, + packetHandlerID++, + Side.CLIENT); // register event handler(s) MinecraftForge.EVENT_BUS.register(new AttackEventHandler()); @@ -93,6 +99,13 @@ public class TurnBasedMinecraftMod { logger.debug("Initialized BattleManager"); } + + proxy.getConfig().clearBattleIgnoringPlayers(); + + // register commands + event.registerServerCommand(new CommandTBMDisable(proxy.getConfig())); + event.registerServerCommand(new CommandTBMEnable(proxy.getConfig())); + event.registerServerCommand(new CommandTBMSet(proxy.getConfig())); } @EventHandler diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketGeneralMessage.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketGeneralMessage.java new file mode 100644 index 0000000..a731a2a --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketGeneralMessage.java @@ -0,0 +1,46 @@ +package com.seodisparate.TurnBasedMinecraft.common.networking; + +import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod; + +import io.netty.buffer.ByteBuf; +import net.minecraftforge.fml.common.network.ByteBufUtils; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +public class PacketGeneralMessage implements IMessage +{ + String message; + + public PacketGeneralMessage() + { + message = new String(); + } + + public PacketGeneralMessage(String message) + { + this.message = message; + } + + @Override + public void fromBytes(ByteBuf buf) + { + message = ByteBufUtils.readUTF8String(buf); + } + + @Override + public void toBytes(ByteBuf buf) + { + ByteBufUtils.writeUTF8String(buf, message); + } + + public static class HandlerGeneralMessage implements IMessageHandler + { + @Override + public IMessage onMessage(PacketGeneralMessage message, MessageContext ctx) + { + TurnBasedMinecraftMod.proxy.displayString(message.message); + return null; + } + } +} diff --git a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml index fb217c0..afcea5d 100644 --- a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml +++ b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml @@ -1,6 +1,8 @@ - 3 + 4 + + op 8