Add commands/config to disable turnbased on demand

This commit is contained in:
Stephen Seo 2018-10-16 14:02:44 +09:00
parent 7532f2c506
commit 761ab5a5f0
9 changed files with 326 additions and 4 deletions

View file

@ -10,7 +10,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle.forge' 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. //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 group = "com.seodisparate.TurnBasedMinecraft" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft"

View file

@ -64,7 +64,12 @@ public class AttackEventHandler
return; 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()); // TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.commonProxy.getAttackingEntity() + " and " + event.getEntity());
event.setCanceled(true); event.setCanceled(true);

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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 <player> <true or false; if true turn-based-battle is enabled for that player>";
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;
}
}

View file

@ -8,6 +8,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -44,6 +45,8 @@ public class Config
private boolean freezeCombatantsInBattle = false; private boolean freezeCombatantsInBattle = false;
private int sillyMusicThreshold = 40; private int sillyMusicThreshold = 40;
private int configVersion = 0; private int configVersion = 0;
private Set<Integer> battleIgnoringPlayers = null;
private boolean onlyOPsSelfDisableTB = true;
public Config(Logger logger) public Config(Logger logger)
{ {
@ -52,6 +55,7 @@ public class Config
this.logger = logger; this.logger = logger;
musicBattleTypes = new HashSet<String>(); musicBattleTypes = new HashSet<String>();
musicSillyTypes = new HashSet<String>(); musicSillyTypes = new HashSet<String>();
battleIgnoringPlayers = new HashSet<Integer>();
int internalVersion = 0; int internalVersion = 0;
try try
@ -169,6 +173,17 @@ public class Config
{ {
continue; continue;
} }
else if(xmlReader.getLocalName().equals("WhoCanDisableTurnBasedForSelf"))
{
if(xmlReader.getElementText().toLowerCase().equals("any"))
{
onlyOPsSelfDisableTB = false;
}
else
{
onlyOPsSelfDisableTB = true;
}
}
else if(xmlReader.getLocalName().equals("MaxInBattle")) else if(xmlReader.getLocalName().equals("MaxInBattle"))
{ {
maxInBattle = Integer.parseInt(xmlReader.getElementText()); maxInBattle = Integer.parseInt(xmlReader.getElementText());
@ -566,4 +581,29 @@ public class Config
{ {
return (int)(battleDecisionDurationNanos / 1000000000L); 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<Integer> getBattleIgnoringPlayers()
{
return battleIgnoringPlayers;
}
public boolean getIfOnlyOPsCanDisableTurnBasedForSelf()
{
return onlyOPsSelfDisableTB;
}
} }

View file

@ -6,6 +6,7 @@ import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecisio
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequestInfo; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequestInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod; 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 MODID = "com.seodisparate.turnbasedminecraft";
public static final String NAME = "Turn Based Minecraft Mod"; 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_FILENAME = "TBM_Config.xml";
public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";
public static final String CONFIG_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME; public static final String CONFIG_FILE_PATH = CONFIG_DIRECTORY + CONFIG_FILENAME;
@ -74,6 +75,11 @@ public class TurnBasedMinecraftMod
PacketBattleMessage.class, PacketBattleMessage.class,
packetHandlerID++, packetHandlerID++,
Side.CLIENT); Side.CLIENT);
NWINSTANCE.registerMessage(
PacketGeneralMessage.HandlerGeneralMessage.class,
PacketGeneralMessage.class,
packetHandlerID++,
Side.CLIENT);
// register event handler(s) // register event handler(s)
MinecraftForge.EVENT_BUS.register(new AttackEventHandler()); MinecraftForge.EVENT_BUS.register(new AttackEventHandler());
@ -93,6 +99,13 @@ public class TurnBasedMinecraftMod
{ {
logger.debug("Initialized BattleManager"); 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 @EventHandler

View file

@ -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<PacketGeneralMessage, IMessage>
{
@Override
public IMessage onMessage(PacketGeneralMessage message, MessageContext ctx)
{
TurnBasedMinecraftMod.proxy.displayString(message.message);
return null;
}
}
}

View file

@ -1,6 +1,8 @@
<TurnBasedMinecraftConfig> <TurnBasedMinecraftConfig>
<!-- If the mod has a newer version config, it will rename the existing config and place the new config --> <!-- If the mod has a newer version config, it will rename the existing config and place the new config -->
<Version>3</Version> <Version>4</Version>
<!-- Determines who can disable turn-based-battle for themselves via command. Must be "op" or "any". If neither, defaults to "op"-->
<WhoCanDisableTurnBasedForSelf>op</WhoCanDisableTurnBasedForSelf>
<!-- If there are "MaxInBattle" amount of entities in battle, other entities cannot join until combatants leave battle. --> <!-- If there are "MaxInBattle" amount of entities in battle, other entities cannot join until combatants leave battle. -->
<MaxInBattle>8</MaxInBattle> <MaxInBattle>8</MaxInBattle>
<!-- If not set to "false", then when battle starts all combatants will remain in their starting position. --> <!-- If not set to "false", then when battle starts all combatants will remain in their starting position. -->