Skip to content

Commit

Permalink
Allow registration of materials via IMC
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Sep 22, 2014
1 parent 8e0bfdd commit 2610428
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/java/tconstruct/TConstruct.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
89 changes: 89 additions & 0 deletions 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<FMLInterModComms.IMCMessage> 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);
}
}

0 comments on commit 2610428

Please sign in to comment.