diff --git a/src/main/java/tconstruct/TConstruct.java b/src/main/java/tconstruct/TConstruct.java index 95ac8c35a62..993438647ca 100644 --- a/src/main/java/tconstruct/TConstruct.java +++ b/src/main/java/tconstruct/TConstruct.java @@ -3,6 +3,7 @@ import java.util.Map; import java.util.Random; +import cpw.mods.fml.common.event.*; import mantle.pulsar.config.ForgeCFG; import mantle.pulsar.control.PulseManager; import net.minecraft.world.gen.structure.MapGenStructureIO; @@ -36,6 +37,7 @@ import tconstruct.smeltery.TinkerSmeltery; import tconstruct.tools.TinkerTools; import tconstruct.util.EnvironmentChecks; +import tconstruct.util.IMCHandler; import tconstruct.util.config.DimensionBlacklist; import tconstruct.util.config.PHConstruct; import tconstruct.util.network.PacketPipeline; @@ -52,9 +54,6 @@ import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.SidedProxy; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkCheckHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; @@ -211,6 +210,19 @@ public void postInit (FMLPostInitializationEvent event) } } + /* IMC Mod Support */ + @EventHandler + public void handleIMC(FMLInterModComms.IMCEvent e) + { + IMCHandler.processIMC(e.getMessages()); + } + + @EventHandler + public void loadComplete(FMLLoadCompleteEvent evt) + { + IMCHandler.processIMC(FMLInterModComms.fetchRuntimeMessages(this)); + } + public static LiquidCasting getTableCasting () { return tableCasting; diff --git a/src/main/java/tconstruct/util/IMCHandler.java b/src/main/java/tconstruct/util/IMCHandler.java new file mode 100644 index 00000000000..e4407cc19f0 --- /dev/null +++ b/src/main/java/tconstruct/util/IMCHandler.java @@ -0,0 +1,89 @@ +package tconstruct.util; + +import cpw.mods.fml.common.event.FMLInterModComms; +import net.minecraft.nbt.NBTTagCompound; +import tconstruct.TConstruct; +import tconstruct.library.TConstructRegistry; +import tconstruct.library.tools.ToolMaterial; + +import java.util.List; + +public final class IMCHandler { + private IMCHandler() {} + + public static void processIMC(List messages) + { + for(FMLInterModComms.IMCMessage message : messages) + { + String type = message.key; + if(type == null || type.isEmpty()) + continue; + + // process materials added from mods + if(type.equals("addMaterial")) + { + if(!message.isNBTMessage()) + { + logInvalidMessage(message); + continue; + } + + NBTTagCompound tag = message.getNBTValue(); + ToolMaterial mat = scanMaterial(tag); + if(mat != null) { + TConstructRegistry.addtoolMaterial(tag.getInteger("Id"), mat); + TConstruct.logger.info("IMC: Added material " + mat.materialName); + } + } + } + } + + private static void logInvalidMessage(FMLInterModComms.IMCMessage message) + { + TConstruct.logger.error(String.format("Received invalid IMC '%s' from %s. Not a NBT Message.", message.key, message.getSender())); + } + + private static ToolMaterial scanMaterial(NBTTagCompound tag) + { + if(!tag.hasKey("Name")) { + TConstruct.logger.error("Material IMC: Material has no name"); + return null; + } + String name = tag.getString("Name"); + + if(!tag.hasKey("Id")) { + TConstruct.logger.error("Material IMC: Materials need a unique id. " + name); + return null; + } + else if(!tag.hasKey("Durability")) { + TConstruct.logger.error("Material IMC: Materials need a durability. " + name); + return null; + } + else if(!tag.hasKey("MiningSpeed")) { + TConstruct.logger.error("Material IMC: Materials need a mining speed. " + name); + return null; + } + else if(tag.hasKey("Stonebound") && tag.hasKey("Jagged")) { + TConstruct.logger.error("Material IMC: Materials can only be Stonebound or Jagged. " + name); + return null; + } + + int hlvl = tag.getInteger("HarvestLevel"); + int durability = tag.getInteger("Durability"); + int speed = tag.getInteger("MiningSpeed"); + int attack = tag.getInteger("Attack"); + float handle = tag.getFloat("HandleModifier"); + int reinforced = tag.getInteger("Reinforced"); + float shoddy = tag.getFloat("Stonebound"); + String style = tag.getString("Style"); + int color = tag.getInteger("Color"); + + if(tag.hasKey("Jagged")) + shoddy = tag.getFloat("Jagged"); + + if(tag.hasKey("localizationString")) + return new ToolMaterial(name, tag.getString("localizationString"), hlvl, durability, speed, attack, handle, reinforced, shoddy, style, color); + else + return new ToolMaterial(name, hlvl, durability, speed, attack, handle, reinforced, shoddy, style, color); + } +}