-
Notifications
You must be signed in to change notification settings - Fork 753
/
IMCHandler.java
263 lines (221 loc) · 10.9 KB
/
IMCHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package tconstruct.util;
import cpw.mods.fml.common.event.FMLInterModComms;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;
import tconstruct.TConstruct;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.crafting.CastingRecipe;
import tconstruct.library.crafting.PatternBuilder;
import tconstruct.library.crafting.Smeltery;
import tconstruct.library.crafting.StencilBuilder;
import tconstruct.library.tools.DynamicToolPart;
import tconstruct.library.tools.ToolMaterial;
import tconstruct.library.util.IPattern;
import tconstruct.library.util.IToolPart;
import tconstruct.smeltery.TinkerSmeltery;
import tconstruct.tools.TinkerTools;
import tconstruct.tools.items.Pattern;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
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);
}
}
else if(type.equals("addPartBuilderMaterial"))
{
if(!message.isNBTMessage())
{
logInvalidMessage(message);
continue;
}
NBTTagCompound tag = message.getNBTValue();
if(!checkRequiredTags("PartBuilder", tag, "MaterialId", "Item", "Value"))
continue;
int matID = tag.getInteger("MaterialId");
int value = tag.getInteger("Value");
if(TConstructRegistry.getMaterial(matID) == null)
{
TConstruct.logger.error("PartBuilder IMC: Unknown Material ID " + matID);
continue;
}
ItemStack item = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("Item"));
ItemStack shard = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("Shard")); // optional
ItemStack rod = new ItemStack(TinkerTools.toolRod, 1, matID);
// register the material
PatternBuilder.instance.registerFullMaterial(item, value, TConstructRegistry.getMaterial(matID).materialName, shard, rod, matID);
List<Item> addItems = new LinkedList<Item>();
List<Integer> addMetas = new LinkedList<Integer>();
List<ItemStack> addOUtputs = new LinkedList<ItemStack>();
// add mappings for everything that has stone tool mappings
for(Map.Entry<List, ItemStack> mappingEntry : TConstructRegistry.patternPartMapping.entrySet())
{
List mapping = mappingEntry.getKey();
// only stone mappings
if((Integer)mapping.get(2) != TinkerTools.MaterialID.Stone)
continue;
// only if the output is a dynamic part
if(!(mappingEntry.getValue().getItem() instanceof DynamicToolPart))
continue;
Item woodPattern = (Item) mapping.get(0);
Integer meta = (Integer) mapping.get(1);
ItemStack output = mappingEntry.getValue().copy();
output.setItemDamage(matID);
// save data, concurrent modification exception and i'm lazy
addItems.add(woodPattern);
addMetas.add(meta);
addOUtputs.add(output);
}
// add a part mapping for it
for(int i = 0; i < addItems.size(); i++)
TConstructRegistry.addPartMapping(addItems.get(i), addMetas.get(i), matID, addOUtputs.get(i));
TConstruct.logger.info("PartBuilder IMC: Added Part builder ampping for " + TConstructRegistry.getMaterial(matID).materialName);
}
else if(type.equals("addPartCastingMaterial"))
{
if(!message.isNBTMessage())
{
logInvalidMessage(message);
continue;
}
NBTTagCompound tag = message.getNBTValue();
if(!checkRequiredTags("Castingt", tag, "MaterialId", "FluidName"))
continue;
if(!tag.hasKey("MaterialId"))
{
TConstruct.logger.error("Casting IMC: Not material ID for the result present");
continue;
}
int matID = tag.getInteger("MaterialId");
FluidStack liquid = FluidStack.loadFluidStackFromNBT(tag);
if(liquid == null) {
TConstruct.logger.error("Casting IMC: No fluid found");
continue;
}
// we add the toolpart to all smeltery recipies that use iron and create a toolpart
List<CastingRecipe> newRecipies = new LinkedList<CastingRecipe>();
for(CastingRecipe recipe : TConstructRegistry.getTableCasting().getCastingRecipes())
{
if(recipe.castingMetal.getFluid() != TinkerSmeltery.moltenIronFluid)
continue;
if(recipe.cast == null || !(recipe.cast.getItem() instanceof IPattern))
continue;
if(!(recipe.getResult().getItem() instanceof DynamicToolPart)) // has to be dynamic toolpart to support automatic addition
continue;
newRecipies.add(recipe);
}
// has to be done separately so we have all checks and no concurrent modification exception
for(CastingRecipe recipe : newRecipies)
{
ItemStack output = recipe.getResult().copy();
output.setItemDamage(matID);
FluidStack liquid2 = new FluidStack(liquid, recipe.castingMetal.amount);
// ok, this recipe creates a toolpart and uses iron for it. add a new one for the IMC stuff!
TConstructRegistry.getTableCasting().addCastingRecipe(output, liquid2, recipe.cast, recipe.consumeCast, recipe.coolTime);
}
TConstruct.logger.info("Casting IMC: Added fluid " + tag.getString("FluidName") + " to part casting");
}
else if(type.equals("addSmelteryMelting")) {
if (!message.isNBTMessage()) {
logInvalidMessage(message);
continue;
}
NBTTagCompound tag = message.getNBTValue();
if (!checkRequiredTags("Smeltery", tag, "FluidName", "Temperature", "Item", "Block"))
continue;
FluidStack liquid = FluidStack.loadFluidStackFromNBT(tag);
if(liquid == null) {
TConstruct.logger.error("Smeltery IMC: No fluid found");
continue;
}
if(liquid.amount <= 0) {
TConstruct.logger.error("Smeltery IMC: Liquid has to have an amount greater than zero");
continue;
}
ItemStack item = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("Item"));
ItemStack block = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("Block"));
int temperature = tag.getInteger("Temperature");
Smeltery.addMelting(item, Block.getBlockFromItem(block.getItem()), block.getItemDamage(), temperature, liquid);
TConstruct.logger.info("Smeltery IMC: Added melting: " + item.getDisplayName() + " to " + liquid.amount + "mb " + liquid.getLocalizedName());
}
}
}
private static boolean checkRequiredTags(String prefix, NBTTagCompound tag, String... tags)
{
boolean ok = true;
for(String t : tags)
if(!tag.hasKey(t))
{
TConstruct.logger.error(String.format("%s IMC: Missing required NBT Tag %s", prefix, t));
ok = false; // don't abort, report all missing tags
}
return ok;
}
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);
}
}