-
Notifications
You must be signed in to change notification settings - Fork 754
/
CastingChannelLogic.java
584 lines (529 loc) · 20 KB
/
CastingChannelLogic.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
package tconstruct.blocks.logic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import mantle.blocks.iface.IActiveLogic;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.IFluidTank;
import tconstruct.TConstruct;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* @author BluSunrize
*/
public class CastingChannelLogic extends TileEntity implements IFluidHandler
{
public final static int fillMax = TConstruct.ingotLiquidValue;
public final static int outputMax = TConstruct.ingotLiquidValue;
FluidTank internalTank = new FluidTank(fillMax);
HashMap<ForgeDirection, FluidTank> subTanks = new HashMap();
public ForgeDirection lastProvider;
public ArrayList<ForgeDirection> validOutputs = new ArrayList();
public int ticks = 0;
public int recentlyFilledDelay;
/* UNUSED
public CastingChannelMode mode = CastingChannelMode.SINGLEDRAIN;
*/
public CastingChannelLogic()
{
lastProvider = ForgeDirection.UNKNOWN;
validOutputs.add(ForgeDirection.DOWN);
validOutputs.add(ForgeDirection.NORTH);
validOutputs.add(ForgeDirection.SOUTH);
validOutputs.add(ForgeDirection.EAST);
validOutputs.add(ForgeDirection.WEST);
subTanks.put(ForgeDirection.NORTH, new FluidTank(TConstruct.ingotLiquidValue / 4));
subTanks.put(ForgeDirection.SOUTH, new FluidTank(TConstruct.ingotLiquidValue / 4));
subTanks.put(ForgeDirection.WEST, new FluidTank(TConstruct.ingotLiquidValue / 4));
subTanks.put(ForgeDirection.EAST, new FluidTank(TConstruct.ingotLiquidValue / 4));
}
@Override
public void updateEntity ()
{
ticks++;
boolean flagActiveFaucet = false;
TileEntity possibleFaucet = worldObj.getTileEntity(xCoord, yCoord + 1, zCoord);
if (possibleFaucet != null && possibleFaucet instanceof FaucetLogic)
flagActiveFaucet = ((FaucetLogic) possibleFaucet).active;
if (ticks == 6 && !flagActiveFaucet)
this.distributeFluids();
if (ticks >= 12 && !flagActiveFaucet)
{
if (recentlyFilledDelay != 0)
recentlyFilledDelay--;
if (recentlyFilledDelay == 0 || lastProvider == ForgeDirection.UP)
this.outputFluids();
ticks = 0;
}
}
public void changeOutputs (EntityPlayer player, int side, float hitX, float hitY, float hitZ)
{
/*
* UNUSED
if(player.isSneaking())
{
if(mode == null || mode == CastingChannelMode.SINGLEDRAIN)
mode = CastingChannelMode.CONTINUEDRAIN;
else
mode = CastingChannelMode.SINGLEDRAIN;
String s = "Casting Channel Mode "+(worldObj.isRemote?"client side: ":"server side: ");
player.addChatMessage(s+mode.toString());
return;
}
*/
ForgeDirection toggle = ForgeDirection.UNKNOWN;
if (side == 0 || side == 1)
{
if (hitX > 0.3125 && hitX < 0.6875)
if (hitZ > 0 && hitZ < 0.3125)
toggle = ForgeDirection.NORTH;
if (hitX > 0.3125 && hitX < 0.6875)
if (hitZ > 0.6875 && hitZ < 1)
toggle = ForgeDirection.SOUTH;
if (hitZ > 0.3125 && hitZ < 0.6875)
if (hitX > 0 && hitX < 0.3125)
toggle = ForgeDirection.WEST;
if (hitZ > 0.3125 && hitZ < 0.6875)
if (hitX > 0.6875 && hitX < 1)
toggle = ForgeDirection.EAST;
if (side == 0)
if (hitX > 0.3125 && hitX < 0.6875)
if (hitZ > 0.3125 && hitZ < 0.6875)
toggle = ForgeDirection.DOWN;
}
else
{
if (side == 2)
{
if (hitX > 0 && hitX < 0.3125)
toggle = ForgeDirection.WEST;
if (hitX > 0.6875 && hitX < 1)
toggle = ForgeDirection.EAST;
}
if (side == 3)
{
if (hitX > 0 && hitX < 0.3125)
toggle = ForgeDirection.WEST;
if (hitX > 0.6875 && hitX < 1)
toggle = ForgeDirection.EAST;
}
if (side == 4)
{
if (hitZ > 0 && hitZ < 0.3125)
toggle = ForgeDirection.NORTH;
if (hitZ > 0.6875 && hitZ < 1)
toggle = ForgeDirection.SOUTH;
}
if (side == 5)
{
if (hitZ > 0 && hitZ < 0.3125)
toggle = ForgeDirection.NORTH;
if (hitZ > 0.6875 && hitZ < 1)
toggle = ForgeDirection.SOUTH;
}
}
if (toggle != ForgeDirection.UNKNOWN && toggle != ForgeDirection.UP)
{
TileEntity tile = worldObj.getTileEntity(xCoord + toggle.offsetX, yCoord + toggle.offsetY, zCoord + toggle.offsetZ);
if (tile instanceof IFluidHandler)
if (validOutputs.contains(toggle))
{
validOutputs.remove(toggle);
if (tile instanceof CastingChannelLogic && toggle != ForgeDirection.DOWN)
((CastingChannelLogic) tile).validOutputs.remove(toggle.getOpposite());
}
else
{
validOutputs.add(toggle);
if (tile instanceof CastingChannelLogic && toggle != ForgeDirection.DOWN)
if (!((CastingChannelLogic) tile).validOutputs.contains(toggle.getOpposite()))
((CastingChannelLogic) tile).validOutputs.add(toggle.getOpposite());
}
}
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
private void distributeFluids ()
{
/* DEBUG
boolean printDebugLog = false;
ArrayList<String> debugLog = new ArrayList();
*/
//Move Fluid to Output Tanks
Set<ForgeDirection> connected = this.getOutputs().keySet();
if (connected.contains(ForgeDirection.DOWN))
connected.remove(ForgeDirection.DOWN);
if (connected.contains(lastProvider))
connected.remove(lastProvider);
int output = Math.min(internalTank.getFluidAmount(), 12);
int connectedAmount = connected.size();
if (connectedAmount < 1)
connectedAmount = 1;
int scaledAmount = output / connectedAmount;
for (ForgeDirection dirOut : connected)
{
if (this.internalTank.getFluid() == null)
break;
/* DEBUG
debugLog.add("Moving to "+dirOut+"ern SubTank");
*/
FluidStack tempFS = new FluidStack(internalTank.getFluid().getFluid(), scaledAmount);
int fit = subTanks.get(dirOut).fill(tempFS, false);
/* DEBUG
debugLog.add("SubTank will accept "+fit+"mb");
*/
if (fit > 0)
fit = internalTank.drain(fit, true).amount;
/* DEBUG
debugLog.add("Internal Tank was drained by "+fit+"mb");
*/
tempFS.amount = fit;
fit = subTanks.get(dirOut).fill(tempFS, true);
/* DEBUG
debugLog.add("SubTank was filled by "+fit+"mb");
if(fit > 0)
printDebugLog = true;
debugLog.add("Internal Tank now contains: "+internalTank.getFluidAmount()+"mb");
debugLog.add("SubTank now contains: "+subTanks.get(dirOut).getFluidAmount()+"mb");
*/
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
/* DEBUG
if(printDebugLog)
{
for(String s: debugLog)
System.out.println(s);
debugLog.clear();
}
printDebugLog = false;
*/
//Get Fluid from most recent InputTank
FluidTank inputTank = subTanks.get(lastProvider);
if (inputTank != null && inputTank.getFluid() != null) //Tank can be null if input was received from top
{
/* DEBUG
debugLog.add("Importing from "+lastProvider+"ern SubTank");
*/
FluidStack tempFS = new FluidStack(inputTank.getFluid().getFluid(), Math.min(inputTank.getFluidAmount(), 12));
int fit = internalTank.fill(tempFS, false);
/* DEBUG
debugLog.add("Internal Tank will accept "+fit+"mb");
*/
if (fit > 0)
fit = inputTank.drain(fit, true).amount;
/* DEBUG
debugLog.add("Import Tank was drained by "+fit+"mb");
*/
tempFS.amount = fit;
fit = internalTank.fill(tempFS, true);
/* DEBUG
debugLog.add("Internal Tank was filled by "+fit+"mb");
if(fit > 0)
printDebugLog = true;
debugLog.add("Internal Tank now contains: "+internalTank.getFluidAmount()+"mb");
debugLog.add("SubTank now contains: "+inputTank.getFluidAmount()+"mb");
*/
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
/* DEBUG
if(printDebugLog)
{
for(String s: debugLog)
System.out.println(s);
debugLog.clear();
}
*/
}
private void outputFluids ()
{
/* DEBUG
boolean printDebugLog = false;
ArrayList<String> debugLog = new ArrayList();
*/
HashMap<ForgeDirection, TileEntity> connected = this.getOutputs();
if (connected.containsKey(lastProvider))
connected.remove(lastProvider);
if (connected.containsKey(ForgeDirection.DOWN) && this.internalTank.getFluid() != null) // Prioritizes FluidHandlers below
{
int output = Math.min(internalTank.getFluid().amount, 12);
FluidStack tempFS = new FluidStack(internalTank.getFluid().getFluid(), output);
int fittingBelow = ((IFluidHandler) connected.get(ForgeDirection.DOWN)).fill(ForgeDirection.UP, tempFS, false);
if (fittingBelow > 0)
fittingBelow = this.drain(ForgeDirection.DOWN, fittingBelow, true).amount;
tempFS.amount = fittingBelow;
fittingBelow = ((IFluidHandler) connected.get(ForgeDirection.DOWN)).fill(ForgeDirection.UP, tempFS, true);
connected.remove(ForgeDirection.DOWN);
}
if (connected.size() != 0)
{
for (ForgeDirection dir : connected.keySet()) //Iterates to connected FluidHandlers
{
if (subTanks.get(dir) != null && subTanks.get(dir).getFluid() != null)
{
/* DEBUG
debugLog.add("Exporting from "+dir+"ern SubTank");
*/
FluidStack tempFS = new FluidStack(subTanks.get(dir).getFluid().getFluid(), Math.min(subTanks.get(dir).getFluidAmount(), 12));
int fit = ((IFluidHandler) connected.get(dir)).fill(dir.getOpposite(), tempFS, false);
/* DEBUG
debugLog.add("New Channel will accept "+fit+"mb");
*/
if (fit > 0)
fit = this.drain(dir, fit, true).amount;
/* DEBUG
debugLog.add("OldSubTank was drained by "+fit+"mb");
*/
tempFS.amount = fit;
fit = ((IFluidHandler) connected.get(dir)).fill(dir.getOpposite(), tempFS, true);
/* DEBUG
debugLog.add("New Channel was filled by "+fit+"mb");
debugLog.add("OldSubTank now contains: "+drain(dir, 100000, false).amount+"mb");
if(fit > 0)
printDebugLog = true;
*/
}
/* DEBUG
for(String s: debugLog)
System.out.println(s);
debugLog.clear();
*/
}
}
}
@SideOnly(Side.CLIENT)
public float tankBelow ()
{
TileEntity te = this.worldObj.getTileEntity(this.xCoord, this.yCoord - 1, this.zCoord);
if (te instanceof CastingChannelLogic)
return -0.5f;
if (te instanceof LavaTankLogic)
return -1.0f;
if (te instanceof CastingBasinLogic)
return -0.75f;
if (te instanceof CastingTableLogic)
return -0.125f;
if (te instanceof IFluidHandler)
return 0f;
return 0.5f;
}
public HashMap<ForgeDirection, TileEntity> getOutputs ()
{
HashMap<ForgeDirection, TileEntity> map = new HashMap();
for (ForgeDirection fd : this.validOutputs)
{
int tX = this.xCoord + fd.offsetX;
int tY = this.yCoord + fd.offsetY;
int tZ = this.zCoord + fd.offsetZ;
TileEntity tile = this.worldObj.getTileEntity(tX, tY, tZ);
if (tile != null && tile instanceof IFluidHandler)
map.put(fd, tile);
}
return map;
}
@Override
public int fill (ForgeDirection from, FluidStack resource, boolean doFill)
{
if (doFill)
{
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
this.lastProvider = from;
if (this.internalTank.getFluid() == null)
this.recentlyFilledDelay = 2;
}
if (from == ForgeDirection.UP)
{
return this.internalTank.fill(resource, doFill);
}
else if (from == ForgeDirection.NORTH || from == ForgeDirection.SOUTH || from == ForgeDirection.WEST || from == ForgeDirection.EAST)
{
return this.subTanks.get(from).fill(resource, doFill);
}
return 0;
}
@Override
public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain)
{
return drain(from, resource.amount, doDrain);
}
@Override
public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain)
{
//if(doDrain)
// this.worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
if (from == ForgeDirection.DOWN)
return this.internalTank.drain(maxDrain, doDrain);
else if (from == ForgeDirection.NORTH || from == ForgeDirection.SOUTH || from == ForgeDirection.WEST || from == ForgeDirection.EAST)
{
return this.subTanks.get(from).drain(maxDrain, doDrain);
}
return null;
}
@Override
public boolean canFill (ForgeDirection from, Fluid fluid)
{
if (from == ForgeDirection.DOWN)
return false;
if (from == ForgeDirection.UP)
return true;
return validOutputs.contains(from);
}
@Override
public boolean canDrain (ForgeDirection from, Fluid fluid)
{
if (from == ForgeDirection.UP)
return false;
return validOutputs.contains(from);
}
@Override
public FluidTankInfo[] getTankInfo (ForgeDirection from)
{
if (from == null || from == ForgeDirection.UP || from == ForgeDirection.DOWN || from == ForgeDirection.UNKNOWN)
return new FluidTankInfo[] { new FluidTankInfo(internalTank) };
else
return new FluidTankInfo[] { new FluidTankInfo(subTanks.get(from)) };
}
@Override
public void readFromNBT (NBTTagCompound tags)
{
super.readFromNBT(tags);
readCustomNBT(tags);
}
public void readCustomNBT (NBTTagCompound tags)
{
NBTTagCompound nbtTank = tags.getCompoundTag("internalTank");
if (nbtTank != null)
internalTank.readFromNBT(nbtTank);
for (ForgeDirection fdSub : subTanks.keySet())
{
NBTTagCompound nbtSubTank = tags.getCompoundTag("subTank_" + fdSub.name());
if (nbtSubTank != null)
subTanks.get(fdSub).readFromNBT(nbtSubTank);
}
int[] validFDs = tags.getIntArray("validOutputs");
if (validFDs != null)
{
validOutputs = new ArrayList();
for (int i : validFDs)
validOutputs.add(convertIntToFD(i));
}
/* UNUSED
this.mode = CastingChannelMode.readFromNBT(tags);
*/
this.lastProvider = this.convertIntToFD(tags.getInteger("LastProvider"));
}
@Override
public void writeToNBT (NBTTagCompound tags)
{
super.writeToNBT(tags);
writeCustomNBT(tags);
}
public void writeCustomNBT (NBTTagCompound tags)
{
NBTTagCompound nbtTank = new NBTTagCompound();
internalTank.writeToNBT(nbtTank);
tags.setTag("internalTank", nbtTank);
for (ForgeDirection fdSub : subTanks.keySet())
{
NBTTagCompound nbtSubTank = new NBTTagCompound();
subTanks.get(fdSub).writeToNBT(nbtSubTank);
tags.setTag("subTank_" + fdSub.name(), nbtSubTank);
}
int[] validFDs = new int[validOutputs.size()];
int it = 0;
for (ForgeDirection fd : validOutputs)
{
if (fd != null)
validFDs[it] = convertFDToInt(fd);
it++;
}
tags.setIntArray("validOutputs", validFDs);
/* UNUSED
CastingChannelMode.writeToNBT(tags, this.mode);
*/
tags.setInteger("LastProvider", this.convertFDToInt(this.lastProvider));
}
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, tag);
}
@Override
public void onDataPacket (NetworkManager net, S35PacketUpdateTileEntity packet)
{
readCustomNBT(packet.func_148857_g());
this.worldObj.func_147479_m(this.xCoord, this.yCoord, this.zCoord);
}
public int convertFDToInt (ForgeDirection dir)
{
switch (dir)
{
case DOWN:
return 0;
case UP:
return 1;
case NORTH:
return 2;
case SOUTH:
return 3;
case WEST:
return 4;
case EAST:
return 5;
default:
return -1;
}
}
public ForgeDirection convertIntToFD (int i)
{
switch (i)
{
case 0:
return ForgeDirection.DOWN;
case 1:
return ForgeDirection.UP;
case 2:
return ForgeDirection.NORTH;
case 3:
return ForgeDirection.SOUTH;
case 4:
return ForgeDirection.WEST;
case 5:
return ForgeDirection.EAST;
default:
return ForgeDirection.UNKNOWN;
}
}
/* UNUSED
public enum CastingChannelMode
{
//Will not output while being filled by Faucet, hence only taking one ingots worth from the Smeltery
SINGLEDRAIN,
//Will keep the Faucet above it open
CONTINUEDRAIN;
public static void writeToNBT(NBTTagCompound tag, CastingChannelMode mode)
{
if(mode == SINGLEDRAIN)
tag.setString("CastingChannelMode","single");
else
tag.setString("CastingChannelMode","continue");
}
public static CastingChannelMode readFromNBT(NBTTagCompound tag)
{
return (!tag.hasKey("CastingChannelMode") || tag.getString("CastingChannelMode")=="single") ? SINGLEDRAIN : CONTINUEDRAIN ;
}
}
*/
}