-
Notifications
You must be signed in to change notification settings - Fork 754
/
AdaptiveSmelteryLogic.java
586 lines (503 loc) · 16.2 KB
/
AdaptiveSmelteryLogic.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package tconstruct.blocks.logic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import tconstruct.TConstruct;
import tconstruct.blocks.component.SmelteryComponent;
import tconstruct.blocks.component.SmelteryScan;
import tconstruct.common.TContent;
import tconstruct.inventory.AdaptiveSmelteryContainer;
import tconstruct.library.blocks.AdaptiveInventoryLogic;
import tconstruct.library.component.IComponentHolder;
import tconstruct.library.component.LogicComponent;
import tconstruct.library.component.MultiFluidTank;
import tconstruct.library.util.CoordTuple;
import tconstruct.library.util.IActiveLogic;
import tconstruct.library.util.IMasterLogic;
import tconstruct.library.util.IServantLogic;
public class AdaptiveSmelteryLogic extends AdaptiveInventoryLogic implements IActiveLogic, IMasterLogic, IComponentHolder, IFluidHandler
{
byte direction;
boolean updateFluids = false;
boolean recheckStructure = false;
boolean updateAir = false;
SmelteryScan structure = new SmelteryScan(this, TContent.smeltery, TContent.lavaTank);
MultiFluidTank multitank = new MultiFluidTank();
SmelteryComponent smeltery = new SmelteryComponent(this, structure, multitank, 800);
HashMap<CoordTuple, LiquidDataInstance> airUpdates = new HashMap<CoordTuple, LiquidDataInstance>();
int tick = 0;
public MultiFluidTank getMultiTank ()
{
return multitank;
}
public SmelteryComponent getSmeltery ()
{
return smeltery;
}
public void updateEntity ()
{
tick++;
if (tick % 4 == 0)
smeltery.heatItems();
if (!worldObj.isRemote)
{
if (tick % 20 == 0)
{
if (structure.isComplete())
smeltery.update();
if (updateFluids)
{
distributeFluids();
updateFluids = false;
}
if (updateAir)
{
updateAir();
updateAir = false;
}
if (recheckStructure)
{
structure.recheckStructure();
recheckStructure = false;
}
}
if (tick >= 60)
{
if (!structure.isComplete())
{
structure.checkValidStructure();
if (structure.isComplete())
{
validateSmeltery();
}
}
tick = 0;
}
if (airUpdates.size() > 0)
updateFluidBlocks();
}
}
public void setUpdateFluids ()
{
updateFluids = true;
}
@Override
public List<LogicComponent> getComponents ()
{
ArrayList<LogicComponent> ret = new ArrayList<LogicComponent>(3);
ret.add(structure);
ret.add(multitank);
ret.add(smeltery);
return ret;
}
/* Structure */
@Override
public void setWorldObj (World world)
{
super.setWorldObj(world);
structure.setWorld(world);
smeltery.setWorld(world);
}
@Override
public void notifyChange (IServantLogic servant, int x, int y, int z)
{
if (!worldObj.isRemote)
{
//System.out.println("Notifying of change from "+new CoordTuple(x, y, z));
recheckStructure = true;
}
}
@Override
public void placeBlock (EntityLivingBase entity, ItemStack itemstack)
{
structure.checkValidStructure();
if (structure.isComplete())
{
validateSmeltery();
}
}
void validateSmeltery ()
{
adjustInventory(structure.getAirSize(), true);
smeltery.adjustSize(structure.getAirSize(), true);
multitank.setCapacity(structure.getAirSize() * (TConstruct.ingotLiquidValue * 18));
smeltery.setActiveLavaTank(structure.lavaTanks.get(0));
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
public void removeBlock ()
{
structure.cleanup();
}
/* Direction */
@Override
public byte getRenderDirection ()
{
return direction;
}
@Override
public ForgeDirection getForgeDirection ()
{
return ForgeDirection.VALID_DIRECTIONS[direction];
}
@Override
public void setDirection (int side)
{
}
@Override
public void setDirection (float yaw, float pitch, EntityLivingBase player)
{
int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3;
switch (facing)
{
case 0:
direction = 2;
break;
case 1:
direction = 5;
break;
case 2:
direction = 3;
break;
case 3:
direction = 4;
break;
}
}
@Override
public boolean getActive ()
{
return structure.isComplete();
}
@Override
public void setActive (boolean flag)
{
}
/* Inventory */
@Override
public int getInventoryStackLimit ()
{
return 1;
}
@Override
public void setInventorySlotContents (int slot, ItemStack itemstack)
{
inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
{
itemstack.stackSize = getInventoryStackLimit();
}
updateWorldBlock(slot, itemstack);
updateAir = true;
}
@Override
public ItemStack decrStackSize (int slot, int quantity)
{
if (inventory[slot] != null)
{
if (inventory[slot].stackSize <= quantity)
{
ItemStack stack = inventory[slot];
inventory[slot] = null;
updateWorldBlock(slot, inventory[slot]);
return stack;
}
ItemStack split = inventory[slot].splitStack(quantity);
if (inventory[slot].stackSize == 0)
{
inventory[slot] = null;
}
updateWorldBlock(slot, inventory[slot]);
return split;
}
else
{
return null;
}
}
@Override
public void onInventoryChanged ()
{
smeltery.updateTemperatures();
updateAir = true;
super.onInventoryChanged();
}
void updateWorldBlock (int slot, ItemStack itemstack)
{
CoordTuple air = structure.getAirByIndex(slot);
if (air != null)
{
TileEntity te = worldObj.getBlockTileEntity(air.x, air.y, air.z);
if (te != null && te instanceof TankAirLogic)
{
((TankAirLogic) te).setInventorySlotContents(0, itemstack);
}
}
}
static final int pixelLayer = 162;
//Do the Monster Mash~
public void distributeFluids ()
{
//Calculate liquids in each block
int size = structure.getAirLayerSize();
//HashMap<CoordTuple, LiquidDataInstance> blocks = new HashMap<CoordTuple, LiquidDataInstance>();
for (FluidStack fluid : multitank.fluidlist)
{
//Base calculations per liquid
LiquidData data = new LiquidData(fluid.amount, size);
int baseY = structure.airCoords.get(0).y;
int layerSize = structure.getAirLayerSize();
//Calculate where to distribute liquids
for (int i = 0; i < structure.airCoords.size(); i++)
{
LiquidDataInstance instance;
CoordTuple coord = structure.airCoords.get(i);
int height = 16 * (coord.y - baseY);
int position = i % layerSize;
if (!airUpdates.containsKey(coord))
{
instance = new LiquidDataInstance();
airUpdates.put(coord, instance);
}
else
{
instance = airUpdates.get(coord);
}
if (instance.openLayers() > 0) //This is capable of sending negative liquid sizes to blocks without the stopgap check
{
if (position > data.blocksWithExtra)
{
//Calculate open layers
int open = data.layers - height + 1;
if (open < 1) //Temporary stopgap check
continue;
if (open > instance.openLayers())
open = instance.openLayers();
//Copy fluid
FluidStack newFluid = fluid.copy();
newFluid.amount = pixelLayer * open;
instance.addFluid(open, newFluid);
//Subtract from total
data.totalAmount -= newFluid.amount;
if (data.totalAmount <= 0)
break;
}
else if (position == data.blocksWithExtra && data.leftovers > 0)
{
//Calculate open layers
int open = data.layers - height + 1;
if (open < 1) //Temporary stopgap check
continue;
boolean full = false;
if (open > instance.openLayers())
{
open = instance.openLayers();
full = true;
}
//Copy fluid
FluidStack newFluid = fluid.copy();
newFluid.amount = pixelLayer * open;
if (!full)
newFluid.amount += data.leftovers;
instance.addFluid(open, newFluid);
//Subtract from total
data.totalAmount -= newFluid.amount;
if (data.totalAmount <= 0)
break;
}
else
{
//Calculate open layers
int open = data.layers - height;
if (open < 1) //Temporary stopgap check
continue;
if (open > instance.openLayers())
open = instance.openLayers();
//Copy fluid
FluidStack newFluid = fluid.copy();
newFluid.amount = pixelLayer * open;
instance.addFluid(open, newFluid);
//Subtract from total
data.totalAmount -= newFluid.amount;
if (data.totalAmount <= 0)
break;
}
}
}
}
//Distribute liquids to each block
}
protected void updateFluidBlocks ()
{
Iterator iter = airUpdates.entrySet().iterator();
byte count = 0;
while (iter.hasNext() && count < 40)
{
Map.Entry pairs = (Map.Entry) iter.next();
CoordTuple coord = (CoordTuple) pairs.getKey();
TileEntity te = worldObj.getBlockTileEntity(coord.x, coord.y, coord.z);
if (te instanceof TankAirLogic)
{
((TankAirLogic) te).overrideFluids(((LiquidDataInstance) pairs.getValue()).fluids);
}
iter.remove();
count++;
}
}
public void updateAir ()
{
for (CoordTuple loc : structure.airCoords)
worldObj.markBlockForUpdate(loc.x, loc.y, loc.z);
}
class LiquidData
{
public int totalAmount;
public int layers;
public int leftovers;
public int blocksWithExtra;
LiquidData(int amount, int blocks)
{
totalAmount = amount;
int layerAmount = pixelLayer * blocks;
layers = amount / layerAmount;
leftovers = amount % pixelLayer;
blocksWithExtra = (amount % layerAmount) / pixelLayer;
}
}
class LiquidDataInstance
{
public ArrayList<FluidStack> fluids = new ArrayList<FluidStack>();
int layers = 0;
public int openLayers ()
{
return 16 - layers;
}
public void addFluid (int l, FluidStack fluid)
{
layers += l;
fluids.add(fluid);
}
}
/* Gui */
@Override
public Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z)
{
return new AdaptiveSmelteryContainer(inventoryplayer, this);
}
public int getTempForSlot (int slot)
{
return smeltery.activeTemps[slot];
}
public int getMeltingPointForSlot (int slot)
{
return smeltery.meltingTemps[slot];
}
@Override
public String getDefaultName ()
{
return "crafters.Smeltery";
}
/* Fluids */
@Override
public int fill (ForgeDirection from, FluidStack resource, boolean doFill)
{
return multitank.fill(resource, doFill);
}
@Override
public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain)
{
return multitank.drain(resource.amount, doDrain);
}
@Override
public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain)
{
return multitank.drain(maxDrain, doDrain);
}
@Override
public boolean canFill (ForgeDirection from, Fluid fluid)
{
return false;
}
@Override
public boolean canDrain (ForgeDirection from, Fluid fluid)
{
return false;
}
@Override
public FluidTankInfo[] getTankInfo (ForgeDirection from)
{
return multitank.getMultiTankInfo();
}
public int getFillState ()
{
return 1;
}
/* NBT */
@Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
readNetworkNBT(tags);
structure.readFromNBT(tags);
multitank.readFromNBT(tags);
smeltery.readFromNBT(tags);
}
public void readNetworkNBT (NBTTagCompound tags)
{
direction = tags.getByte("Direction");
adjustInventory(tags.getInteger("InvSize"), false);
super.readInventoryFromNBT(tags);
structure.readNetworkNBT(tags);
multitank.readNetworkNBT(tags);
smeltery.readNetworkNBT(tags);
}
@Override
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
writeNetworkNBT(tags);
structure.writeToNBT(tags);
multitank.writeToNBT(tags);
smeltery.writeToNBT(tags);
}
public void writeNetworkNBT (NBTTagCompound tags)
{
tags.setByte("Direction", direction);
tags.setInteger("InvSize", inventory.length);
super.writeInventoryToNBT(tags);
structure.writeNetworkNBT(tags);
multitank.writeNetworkNBT(tags);
smeltery.writeNetworkNBT(tags);
}
@Override
public void onDataPacket (INetworkManager net, Packet132TileEntityData packet)
{
readNetworkNBT(packet.data);
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
}
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeNetworkNBT(tag);
return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
}
}