diff --git a/src/main/java/com/cricketcraft/chisel/Chisel.java b/src/main/java/com/cricketcraft/chisel/Chisel.java index 0aad18e9..0c30017c 100644 --- a/src/main/java/com/cricketcraft/chisel/Chisel.java +++ b/src/main/java/com/cricketcraft/chisel/Chisel.java @@ -12,6 +12,7 @@ import org.apache.logging.log4j.Logger; import com.cricketcraft.chisel.block.BlockCarvable; import com.cricketcraft.chisel.compat.Compatibility; +import com.cricketcraft.chisel.compat.IMCHandler; import com.cricketcraft.chisel.config.Configurations; import com.cricketcraft.chisel.init.ChiselBlocks; import com.cricketcraft.chisel.init.ChiselTabs; @@ -31,7 +32,9 @@ 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.FMLInterModComms; +import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; import cpw.mods.fml.common.event.FMLMissingMappingsEvent; +import cpw.mods.fml.common.event.FMLInterModComms.IMCEvent; import cpw.mods.fml.common.event.FMLMissingMappingsEvent.MissingMapping; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; @@ -158,6 +161,13 @@ public class Chisel { Compatibility.init(event); } + @EventHandler + public void onIMC(IMCEvent event) { + for (IMCMessage msg : event.getMessages()) { + IMCHandler.INSTANCE.handleMessage(msg); + } + } + @SubscribeEvent public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { if (event.modID.equals("chisel")) { diff --git a/src/main/java/com/cricketcraft/chisel/api/IMC.java b/src/main/java/com/cricketcraft/chisel/api/IMC.java new file mode 100644 index 00000000..30f162c0 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/api/IMC.java @@ -0,0 +1,59 @@ +package com.cricketcraft.chisel.api; + +import com.cricketcraft.chisel.Chisel; + +/** + * Use the enum constants (using {@link #key} or calling {@link #toString()}) in this class as keys for IMC messages sent to chisel + */ +public enum IMC { + + /** + * Adds a variation to a group. + * + * Use this to add a variation to a group. String syntax: + *

+ * groupname|blockname|meta + *

+ * An example would be {@code "mygroup|minecraft:dirt|1"} and this will add the vanilla dirt block with metadata 1 to the "mygroup" group, creating that group if need be. + */ + ADD_VARIATION("variation:add"), + + /** + * Removes a variation from a group. + * + * Use this to remove a variation from a group. String syntax: + *

+ * groupname|blockname|meta + *

+ * An example would be {@code "mygroup|minecraft:dirt|1"} and this will add the vanilla dirt block with metadata 1 to the "mygroup" group, creating that group if need be. + */ + REMOVE_VARIATION("variation:remove"), + + /** + * Registers an oredict name to a group. This can be used to automatically add all blocks with this oredict name to a group. String syntax: + *

+ * groupname|oredictname + *

+ * An example would be {@code "mygroup|plankWood"} which will add all blocks registered in the oredict as "plankWood" to your group called "mygroup". + */ + REGISTER_GROUP_ORE("group:ore"); + + /** + * The IMC message key for this message type. + */ + public final String key; + + IMC(String key) { + this.key = key; + } + + @Override + public String toString() { + return key; + } + + /** + * The modid of Chisel so you can easily send IMC to this mod. + */ + public static final String CHISEL_MODID = Chisel.MOD_ID; +} diff --git a/src/main/java/com/cricketcraft/chisel/compat/IMCHandler.java b/src/main/java/com/cricketcraft/chisel/compat/IMCHandler.java new file mode 100644 index 00000000..33225588 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/compat/IMCHandler.java @@ -0,0 +1,55 @@ +package com.cricketcraft.chisel.compat; + +import net.minecraft.block.Block; + +import com.cricketcraft.chisel.Chisel; +import com.cricketcraft.chisel.api.IMC; +import com.cricketcraft.chisel.api.carving.ICarvingRegistry; +import com.cricketcraft.chisel.carving.Carving; + +import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage; + +public class IMCHandler { + + public static final IMCHandler INSTANCE = new IMCHandler(); + + private int order = 1000; + + private IMCHandler() { + } + + public void handleMessage(IMCMessage message) { + for (IMC imc : IMC.values()) { + if (imc.key.equals(message.key)) { + handle(message, imc, message.getStringValue()); + } + } + } + + private void handle(IMCMessage message, IMC type, String value) { + ICarvingRegistry reg = Carving.chisel; + String[] data = value.split("\\|"); + try { + switch (type) { + case ADD_VARIATION: + case REMOVE_VARIATION: + Block block = (Block) Block.blockRegistry.getObject(data[1]); + int meta = Short.parseShort(data[2]); + if (type == IMC.ADD_VARIATION) { + reg.addVariation(data[0], block, meta, order++); + } else { + reg.removeVariation(block, meta, data[0]); + } + case REGISTER_GROUP_ORE: + reg.registerOre(data[0], data[1]); + default: + throw new IllegalArgumentException("Invalid IMC constant! How...what...?"); + } + } catch (Exception e) { + Chisel.logger.error("Could not handle data {} for IMC type {}. This was sent from mod {}.\n" + + "!! This is a bug in that mod !!\nSwallowing error and continuing...", + value, type.name(), message.getSender()); + e.printStackTrace(); + } + } +}