TurnBasedMinecraftMod/src/main/java/com/seodisparate/TurnBasedMinecraft/TurnBasedMinecraftMod.java

135 lines
4.7 KiB
Java
Raw Normal View History

package com.seodisparate.TurnBasedMinecraft;
import java.time.Duration;
import org.apache.logging.log4j.Logger;
2018-09-10 05:59:56 +00:00
import com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.BattleManager;
import com.seodisparate.TurnBasedMinecraft.common.Config;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecision;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
2018-09-06 08:08:36 +00:00
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequestInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler;
import net.minecraft.entity.Entity;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
2018-08-28 02:51:26 +00:00
@Mod(modid = TurnBasedMinecraftMod.MODID, name = TurnBasedMinecraftMod.NAME, version = TurnBasedMinecraftMod.VERSION)
public class TurnBasedMinecraftMod
{
2018-08-28 02:51:26 +00:00
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 Duration BattleDecisionTime = Duration.ofSeconds(15);
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;
public static final String CONFIG_INTERNAL_PATH = "/assets/TurnBasedMinecraft/" + CONFIG_FILENAME;
2018-09-10 05:59:56 +00:00
private static int CONFIG_FILE_VERSION = 0;
private static Logger logger;
private static BattleManager battleManager;
private static int packetHandlerID = 0;
public static Entity attackingEntity;
2018-09-06 08:08:36 +00:00
public static int attackingDamage = 0;
public static Config config;
2018-09-10 05:59:56 +00:00
public static Battle currentBattle = null;
public static BattleGui currentBattleGui = null;
@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
logger = event.getModLog();
logger.debug("PREINIT");
}
@EventHandler
public void init(FMLInitializationEvent event)
{
currentBattle = null;
if(event.getSide() == Side.SERVER)
{
battleManager = new BattleManager();
}
else
{
battleManager = null;
}
// register packets
PacketHandler.INSTANCE.registerMessage(
PacketBattleInfo.HandlerBattleInfo.class,
PacketBattleInfo.class,
packetHandlerID++,
Side.CLIENT);
PacketHandler.INSTANCE.registerMessage(
PacketBattleRequestInfo.HandlerBattleRequestInfo.class,
PacketBattleRequestInfo.class,
packetHandlerID++,
Side.SERVER);
PacketHandler.INSTANCE.registerMessage(
PacketBattleDecision.HandleBattleDecision.class,
PacketBattleDecision.class,
packetHandlerID++,
Side.SERVER);
2018-09-06 08:08:36 +00:00
PacketHandler.INSTANCE.registerMessage(
PacketBattleMessage.HandlerBattleMessage.class,
PacketBattleMessage.class,
packetHandlerID++,
Side.CLIENT);
logger.debug("INIT");
}
@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
if(battleManager != null)
{
config = new Config(logger);
}
logger.debug("POSTINIT");
}
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
2018-09-10 05:59:56 +00:00
if(battleManager == null || event.getEntity().world.isRemote)
{
return;
}
if(!event.getSource().getTrueSource().equals(attackingEntity) && battleManager.checkAttack(event))
{
logger.debug("Canceled LivingAttackEvent between " + attackingEntity + " and " + event.getEntity());
event.setCanceled(true);
}
2018-09-06 08:08:36 +00:00
attackingDamage = (int) event.getAmount();
}
public static BattleManager getBattleManager()
{
return battleManager;
}
public static void setConfigVersion(int version)
{
CONFIG_FILE_VERSION = version;
}
public static int getConfigVersion()
{
return CONFIG_FILE_VERSION;
}
}