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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
package ihl.flexible_cable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import ic2.api.network.INetworkClientTileEntityEventListener;
import ic2.api.network.INetworkTileEntityEventListener;
import ic2.api.recipe.IRecipeInput;
import ic2.api.recipe.RecipeInputItemStack;
import ic2.core.ContainerBase;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.Ic2Items;
import ic2.core.block.TileEntityInventory;
import ic2.core.block.invslot.InvSlot.Access;
import ihl.IHLMod;
import ihl.interfaces.IWire;
import ihl.items_blocks.FlexibleCableItem;
import ihl.processing.chemistry.GaedesMercuryRotaryPumpTileEntity;
import ihl.processing.metallurgy.GasWeldingStationTileEntity;
import ihl.recipes.IronWorkbenchRecipe;
import ihl.recipes.RecipeInputDie;
import ihl.recipes.RecipeInputObjectInstance;
import ihl.utils.IHLUtils;
public class IronWorkbenchTileEntity extends TileEntityInventory implements IHasGui, INetworkClientTileEntityEventListener, INetworkTileEntityEventListener{
public static List<IronWorkbenchRecipe> recipes = new ArrayList();
public int progress;
public int currentSlot=-1;
public final int maxProgress;
public final InvSlotTool tools;
public final InvSlotWorkspaceElement workspaceElements;
public final InvSlotProcessableIronWorkbench inputMaterial;
public final InvSlotOutputInProgress output;
public boolean isGuiScreenOpened=false;
private boolean startProcess=false;
private boolean outputDefined=false;
private EntityPlayer crafter;
public ContainerBase<?> container;
private Map<Integer, IronWorkbenchRecipe> slotRecipeMap = new HashMap();
public IronWorkbenchTileEntity()
{
this.maxProgress=80;
this.workspaceElements = new InvSlotWorkspaceElement(this, "workspaceElements", 3, Access.NONE, 6);
this.tools=new InvSlotTool(this, "tools", 0, Access.IO, 12);
this.inputMaterial=new InvSlotProcessableIronWorkbench(this, "input", 1, Access.IO, 12);
this.output=new InvSlotOutputInProgress(this, "output", 2, 18);
}
public static void addRecipe(IronWorkbenchRecipe recipe)
{
IronWorkbenchTileEntity.recipes.add(recipe);
}
@Override
public String getInventoryName()
{
return "ironWorkbench";
}
@Override
public ItemStack getWrenchDrop(EntityPlayer player)
{
return IHLUtils.getThisModItemStack("ironWorkbench");
}
@Override
public void updateEntityServer()
{
if(this.isGuiScreenOpened)
{
if(this.output.isEmpty() && !outputDefined)
{
this.workspaceElements.reset();
Iterator<IronWorkbenchRecipe> iwri=IronWorkbenchTileEntity.recipes.iterator();
while(iwri.hasNext())
{
IronWorkbenchRecipe recipe = iwri.next();
if(recipe.isCanBeCrafted(this.tools.getItemStackList(), this.inputMaterial.getItemStackList(), this.workspaceElements.getItemStackList()))
{
if(recipe.workspaceElements==null || recipe.workspaceElements.isEmpty() || this.workspaceElements.containsAndCanUse(recipe.workspaceElements))
{
List<ItemStack> newOutputs = recipe.outputs;
for(IRecipeInput rinput:recipe.tools)
{
if(rinput instanceof RecipeInputDie)
{
newOutputs = ((RecipeInputDie)rinput).transformOutput(this.getMatchedItemStack(rinput),recipe.outputs);
}
}
int slot = this.output.put(newOutputs);
if(slot<0)break;
slotRecipeMap.put(slot, recipe);
this.startProcess=false;
}
}
}
List<ItemStack> processingMaterials = new ArrayList<ItemStack>();
for(int i=0; i<this.inputMaterial.size();i++)
{
ItemStack stack = this.inputMaterial.get(i);
if(stack!=null && stack.getItem() instanceof IWire)
{
if(stack.stackTagCompound==null)
{
stack.stackTagCompound=new NBTTagCompound();
}
int fullLength = this.getFullLengthOfSameWires(stack);
List<RecipeInputObjectInstance> list = this.getListOfSameWires(stack);
ItemStack result = stack.copy();
result.stackTagCompound.setInteger("length", fullLength);
result.stackTagCompound.setInteger("fullLength", fullLength);
IronWorkbenchRecipe recipe = new IronWorkbenchRecipe(null, list, Arrays.asList(new ItemStack[] {result}));
int slot = this.output.put(recipe.outputs);
if(slot<0)break;
slotRecipeMap.put(slot, recipe);
this.startProcess=false;
break;
}
}
outputDefined=true;
}
else if(!this.output.isEmpty())
{
List<Integer> crafterEmptyInventorySlotsList = getCrafterEmptyInventorySlotsList();
if(startProcess && crafterEmptyInventorySlotsList.size()>=this.slotRecipeMap.get(currentSlot).outputs.size())
{
if(++this.progress>=this.maxProgress)
{
IronWorkbenchRecipe crecipe = this.slotRecipeMap.get(currentSlot);
List<ItemStack> opts = this.output.getRecipeOutputs(currentSlot);
int multiplier = this.inputMaterial.getMultiplier(crecipe.materials);
Iterator<ItemStack> optsi = opts.iterator();
Iterator<Integer> emptySlotsIterator = crafterEmptyInventorySlotsList.iterator();
while(optsi.hasNext())
{
int slot = emptySlotsIterator.next();
ItemStack stack = optsi.next();
this.crafter.inventory.mainInventory[slot]=stack.copy();
if(stack.getItem() instanceof IWire)
{
System.out.println(multiplier);
this.crafter.inventory.mainInventory[slot]=IHLUtils.getWireItemStackCopyWithLengthMultiplied(stack,multiplier);
}
else
{
this.crafter.inventory.mainInventory[slot]=stack.copy();
this.crafter.inventory.mainInventory[slot].stackSize*=multiplier;
}
}
this.crafter.inventoryContainer.detectAndSendChanges();
this.inputMaterial.substract(crecipe.materials, multiplier);
this.tools.damage(crecipe.tools);
if(!crecipe.workspaceElements.isEmpty())
{
this.workspaceElements.use(crecipe.workspaceElements);
}
this.resetOutput();
}
}
}
}
}
private ItemStack getMatchedItemStack(IRecipeInput rinput)
{
for(ItemStack tool:this.tools.getItemStackList())
{
if(rinput.matches(tool))
{
return tool;
}
}
for(ItemStack material:this.inputMaterial.getItemStackList())
{
if(rinput.matches(material))
{
return material;
}
}
return null;
}
private List<Integer> getCrafterEmptyInventorySlotsList()
{
List<Integer> list = new ArrayList<Integer>();
if(this.crafter!=null)
{
for (int var1 = 0; var1 < this.crafter.inventory.mainInventory.length; ++var1)
{
if (this.crafter.inventory.mainInventory[var1] == null)
{
list.add(var1);
}
}
}
return list;
}
private List<RecipeInputObjectInstance> getListOfSameWires(ItemStack stack1) {
List<RecipeInputObjectInstance> list = new ArrayList();
for(int i=0; i<this.inputMaterial.size();i++)
{
ItemStack stack = this.inputMaterial.get(i);
if(stack!=null && ((IWire)stack1.getItem()).isSameWire(stack1, stack))
{
list.add(new RecipeInputObjectInstance(stack));
}
}
return list;
}
private int getFullLengthOfSameWires(ItemStack stack1) {
int fullLength=0;
for(int i=0; i<this.inputMaterial.size();i++)
{
ItemStack stack = this.inputMaterial.get(i);
if(stack!=null && ((IWire)stack1.getItem()).isSameWire(stack1, stack))
{
fullLength += IHLUtils.getWireLength(stack);
}
}
return fullLength;
}
@Override
@SideOnly(Side.CLIENT)
public GuiScreen getGui(EntityPlayer player, boolean arg1) {
return new IronWorkbenchGui(new IronWorkbenchContainer(player, this));
}
@Override
public ContainerBase<?> getGuiContainer(EntityPlayer player) {
resetOutput();
this.isGuiScreenOpened=true;
this.crafter=player;
container = new IronWorkbenchContainer(player, this);
return container;
}
@Override
public void onGuiClosed(EntityPlayer arg0)
{
this.isGuiScreenOpened=false;
}
@Override
public void onNetworkEvent(EntityPlayer player, int event)
{
if(event==16)
{
this.isGuiScreenOpened=false;
this.crafter=null;
this.container=null;
return;
}
for(int i=event;i>=0;i--)
{
if(this.slotRecipeMap.containsKey(i))
{
if(!this.slotRecipeMap.get(i).isCanBeCrafted(this.tools.getItemStackList(), this.inputMaterial.getItemStackList(), this.workspaceElements.getItemStackList()))
{
resetOutput();
}
else
{
this.currentSlot=i;
this.startProcess=true;
return;
}
}
}
}
public void resetOutput()
{
this.output.clear();
this.slotRecipeMap.clear();
this.progress=0;
this.startProcess=false;
this.currentSlot=-1;
this.outputDefined=false;
}
public void dropContents() {
for(int i=0;i<this.tools.size();i++)
{
if(this.tools.get(i)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.tools.get(i)));
}
for(int i=0;i<this.inputMaterial.size();i++)
{
if(this.inputMaterial.get(i)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.inputMaterial.get(i)));
}
}
@Override
public void onNetworkEvent(int event)
{
}
public int gaugeProgressScaled(int i)
{
return this.progress * i / this.maxProgress;
}
@Override
public boolean shouldRenderInPass(int pass)
{
return pass==0;
}
public static void removeRecipeByOutput(List<ItemStack> recipeOutputsItems)
{
Iterator<IronWorkbenchRecipe> ri = recipes.iterator();
while(ri.hasNext())
{
IronWorkbenchRecipe recipe = ri.next();
boolean removeEntry=false;
Iterator<ItemStack> roi = recipe.outputs.iterator();
while(roi.hasNext())
{
if(IHLUtils.isItemStacksIsEqual(recipeOutputsItems.get(0), roi.next(), true))
{
removeEntry=true;
}
}
if(removeEntry)
{
ri.remove();
}
}
}
public static void removeRecipeByInput(List<IRecipeInput> recipeInputsTools1,List<IRecipeInput> recipeInputsItems1,List<ItemStack> recipeInputsMachines) {
List<ItemStack> recipeInputsTools = IHLUtils.convertRecipeInputToItemStackList(recipeInputsTools1);
List<ItemStack> recipeInputsItems = IHLUtils.convertRecipeInputToItemStackList(recipeInputsItems1);
Iterator<IronWorkbenchRecipe> ri = recipes.iterator();
while(ri.hasNext())
{
IronWorkbenchRecipe recipe = ri.next();
if(recipe.isCanBeCrafted(recipeInputsTools, recipeInputsItems, recipeInputsMachines))
{
ri.remove();
}
}
}
}
|