Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow registration of materials via IMC
- Loading branch information
Showing
2 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |