From 3a753887c67abc3bded810ce4af6d15633013841 Mon Sep 17 00:00:00 2001 From: DaMachinator Date: Fri, 19 Jan 2018 21:07:54 -0500 Subject: [PATCH] Add event handlers and mod config --- .../civilengineering/CivilEngineering.java | 17 +++++- .../arcanitor/civilengineering/Config.java | 53 +++++++++++++++++++ .../eventhandlers/HandleChatMessage.java | 2 +- .../eventhandlers/HandleDeath.java | 26 +++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/main/java/arcanitor/civilengineering/Config.java create mode 100644 src/main/java/arcanitor/civilengineering/eventhandlers/HandleDeath.java diff --git a/src/main/java/arcanitor/civilengineering/CivilEngineering.java b/src/main/java/arcanitor/civilengineering/CivilEngineering.java index 453523e..b2241c1 100644 --- a/src/main/java/arcanitor/civilengineering/CivilEngineering.java +++ b/src/main/java/arcanitor/civilengineering/CivilEngineering.java @@ -1,17 +1,21 @@ package arcanitor.civilengineering; +import net.minecraftforge.common.config.Configuration; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import org.apache.logging.log4j.Logger; +import java.io.File; + @Mod( modid = CivilEngineering.MODID, name = CivilEngineering.NAME, version = CivilEngineering.VERSION, serverSideOnly = true, - useMetadata = true + useMetadata = true, + acceptableRemoteVersions = "*" ) public class CivilEngineering { public static final String MODID = "civilengineering"; @@ -22,11 +26,17 @@ public class CivilEngineering { public static CivilEngineering instance; public static Logger logger; + public static Configuration config; @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { logger = event.getModLog(); - logger.info("Bridge building pre-init."); + + logger.info("Reading bridge blueprints..."); + + File directory = event.getModConfigurationDirectory(); + config = new Configuration(new File(directory.getPath(), "CivilEngineering.cfg")); + Config.readConfig(); } @Mod.EventHandler @@ -37,6 +47,9 @@ public class CivilEngineering { @Mod.EventHandler public void init(FMLPostInitializationEvent event) { logger.info("Bridge building post-init."); + if (config.hasChanged()) { + config.save(); + } } } diff --git a/src/main/java/arcanitor/civilengineering/Config.java b/src/main/java/arcanitor/civilengineering/Config.java new file mode 100644 index 0000000..cf5b79b --- /dev/null +++ b/src/main/java/arcanitor/civilengineering/Config.java @@ -0,0 +1,53 @@ +package arcanitor.civilengineering; + +import net.minecraftforge.common.config.Configuration; +import org.apache.logging.log4j.Level; + +public class Config { + private static final String CATEGORY_RELAY_OPTIONS = "relay_options"; + private static final String CATEGORY_CONNECTION = "connection"; + + public static boolean relayDeathEvents = false; + public static boolean relayAdvancements = false; //unused for now + + public static String connectURL = "localhost"; + + public static void readConfig() { + Configuration config = CivilEngineering.config; + try { + config.load(); + initConfig(config); + } catch (Exception expt) { + CivilEngineering.logger.log(Level.ERROR,"Could not read config file!", expt); + } finally { + if (config.hasChanged()) { + config.save(); + } + } + } + + private static void initConfig(Configuration cfg) { + cfg.addCustomCategoryComment(CATEGORY_RELAY_OPTIONS,"Relay options"); + cfg.addCustomCategoryComment(CATEGORY_CONNECTION,"Connection settings"); + relayDeathEvents = cfg.getBoolean( + "relayDeathEvents", + CATEGORY_RELAY_OPTIONS, + false, + "Set to true to send death messages over the chat relay." + ); + relayAdvancements = cfg.getBoolean( + "relayAdvancements", + CATEGORY_RELAY_OPTIONS, + false, + "This option does nothing as advancement relays are not implemented." + ); + connectURL = cfg.getString( + "connectURL", + CATEGORY_CONNECTION, + "localhost", + "The URL or IP address of the bridge server" + ); + + } + +} \ No newline at end of file diff --git a/src/main/java/arcanitor/civilengineering/eventhandlers/HandleChatMessage.java b/src/main/java/arcanitor/civilengineering/eventhandlers/HandleChatMessage.java index e187efa..5486442 100644 --- a/src/main/java/arcanitor/civilengineering/eventhandlers/HandleChatMessage.java +++ b/src/main/java/arcanitor/civilengineering/eventhandlers/HandleChatMessage.java @@ -8,7 +8,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @Mod.EventBusSubscriber public class HandleChatMessage { @SubscribeEvent - public static void chatMessageGlobal(ServerChatEvent event) { + public static void handleServerChatEvent (ServerChatEvent event) { CivilEngineering.logger.info("Message on server: "+event.getMessage()+" sent by "+event.getUsername()); } } diff --git a/src/main/java/arcanitor/civilengineering/eventhandlers/HandleDeath.java b/src/main/java/arcanitor/civilengineering/eventhandlers/HandleDeath.java new file mode 100644 index 0000000..1e59d39 --- /dev/null +++ b/src/main/java/arcanitor/civilengineering/eventhandlers/HandleDeath.java @@ -0,0 +1,26 @@ +package arcanitor.civilengineering.eventhandlers; + +import arcanitor.civilengineering.CivilEngineering; +import arcanitor.civilengineering.Config; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.event.entity.living.LivingDeathEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +@Mod.EventBusSubscriber +public class HandleDeath { + @SubscribeEvent + public static void handleLivingDeathEvent (LivingDeathEvent event) { + if(Config.relayDeathEvents) { + EntityLivingBase entity = event.getEntityLiving(); + if (entity instanceof EntityPlayer) { + String message = entity.getCombatTracker().getDeathMessage().getUnformattedText(); + CivilEngineering.logger.info(message); + } + } + + } + + +}