-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileEngineIron.java
450 lines (391 loc) · 11.7 KB
/
TileEngineIron.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.energy;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ICrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import buildcraft.BuildCraftEnergy;
import buildcraft.api.core.StackKey;
import buildcraft.api.fuels.BuildcraftFuelRegistry;
import buildcraft.api.fuels.ICoolant;
import buildcraft.api.fuels.IFuel;
import buildcraft.api.fuels.ISolidCoolant;
import buildcraft.core.GuiIds;
import buildcraft.core.IItemPipe;
import buildcraft.core.fluids.Tank;
import buildcraft.core.fluids.TankManager;
import buildcraft.core.fluids.TankUtils;
import buildcraft.core.inventory.InvUtils;
import buildcraft.energy.gui.ContainerEngine;
public class TileEngineIron extends TileEngineWithInventory implements IFluidHandler {
public static int MAX_LIQUID = FluidContainerRegistry.BUCKET_VOLUME * 10;
public static float HEAT_PER_RF = 0.00023F;
public static float COOLDOWN_RATE = 0.05F;
public static int MAX_COOLANT_PER_TICK = 40;
public Tank tankFuel = new Tank("tankFuel", MAX_LIQUID, this);
public Tank tankCoolant = new Tank("tankCoolant", MAX_LIQUID, this);
private int burnTime = 0;
private TankManager<Tank> tankManager = new TankManager<Tank>();
private IFuel currentFuel;
private int penaltyCooling = 0;
private boolean lastPowered = false;
private BiomeGenBase biomeCache;
public TileEngineIron() {
super(1);
tankManager.add(tankFuel);
tankManager.add(tankCoolant);
}
@Override
public ResourceLocation getBaseTexture() {
return BASE_TEXTURES[2];
}
@Override
public ResourceLocation getChamberTexture() {
return CHAMBER_TEXTURES[2];
}
@Override
public boolean onBlockActivated(EntityPlayer player, ForgeDirection side) {
if (super.onBlockActivated(player, side)) {
return true;
}
ItemStack current = player.getCurrentEquippedItem();
if (current != null) {
if (current.getItem() instanceof IItemPipe) {
return false;
}
if (!worldObj.isRemote) {
if (TankUtils.handleRightClick(this, side, player, true, true)) {
return true;
}
} else {
if (FluidContainerRegistry.isContainer(current)) {
return true;
}
}
}
if (!worldObj.isRemote) {
player.openGui(BuildCraftEnergy.instance, GuiIds.ENGINE_IRON, worldObj, xCoord, yCoord, zCoord);
}
return true;
}
@Override
public float getPistonSpeed() {
if (!worldObj.isRemote) {
return Math.max(0.07f * getHeatLevel(), 0.01f);
}
switch (getEnergyStage()) {
case BLUE:
return 0.04F;
case GREEN:
return 0.05F;
case YELLOW:
return 0.06F;
case RED:
return 0.07F;
default:
return 0;
}
}
private float getBiomeTempScalar() {
if (biomeCache == null) {
biomeCache = worldObj.getBiomeGenForCoords(xCoord, zCoord);
}
float tempScalar = biomeCache.temperature - 1.0F;
tempScalar *= 0.5F;
tempScalar += 1.0F;
return tempScalar;
}
@Override
public void invalidate() {
super.invalidate();
biomeCache = null;
}
@Override
public boolean isBurning() {
if (getEnergyStage() == EnergyStage.OVERHEAT) {
return false;
}
FluidStack fuel = tankFuel.getFluid();
return fuel != null && fuel.amount > 0 && penaltyCooling == 0 && isRedstonePowered;
}
@Override
public void overheat() {
super.overheat();
// Evaporate all remaining coolant as a form of penalty.
tankCoolant.setFluid(null);
}
@Override
public void burn() {
FluidStack fuel = this.tankFuel.getFluid();
if (currentFuel == null && fuel != null) {
currentFuel = BuildcraftFuelRegistry.fuel.getFuel(fuel.getFluid());
}
if (currentFuel == null) {
return;
}
if (penaltyCooling <= 0 && isRedstonePowered) {
lastPowered = true;
if (burnTime > 0 || (fuel != null && fuel.amount > 0)) {
if (burnTime > 0) {
burnTime--;
}
if (burnTime <= 0) {
if (fuel != null) {
if (--fuel.amount <= 0) {
tankFuel.setFluid(null);
}
burnTime = currentFuel.getTotalBurningTime() / FluidContainerRegistry.BUCKET_VOLUME;
} else {
currentFuel = null;
return;
}
}
currentOutput = currentFuel.getPowerPerCycle();
addEnergy(currentFuel.getPowerPerCycle());
heat += currentFuel.getPowerPerCycle() * HEAT_PER_RF * getBiomeTempScalar();
}
} else if (penaltyCooling <= 0) {
if (lastPowered) {
lastPowered = false;
penaltyCooling = 10;
// 10 tick of penalty on top of the cooling
}
}
}
@Override
public void updateHeat() {
if (energyStage == EnergyStage.OVERHEAT && heat > MIN_HEAT) {
heat -= COOLDOWN_RATE;
}
}
@Override
public void engineUpdate() {
super.engineUpdate();
ItemStack stack = getStackInSlot(0);
if (stack != null) {
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(stack);
if (liquid == null && heat > MIN_HEAT * 2) {
final ItemStack stackOne = stack.copy();
stackOne.stackSize = 1;
ISolidCoolant coolant = BuildcraftFuelRegistry.coolant.getSolidCoolant(StackKey.stack(stackOne));
if (coolant != null) {
liquid = coolant.getFluidFromSolidCoolant(stackOne);
}
}
if (liquid != null) {
if (fill(ForgeDirection.UNKNOWN, liquid, false) == liquid.amount) {
fill(ForgeDirection.UNKNOWN, liquid, true);
setInventorySlotContents(0, InvUtils.consumeItem(stack));
}
}
}
if (heat > MIN_HEAT && (penaltyCooling > 0 || !isRedstonePowered)) {
heat -= COOLDOWN_RATE;
coolEngine(MIN_HEAT);
getEnergyStage();
} else if (heat > IDEAL_HEAT) {
coolEngine(IDEAL_HEAT);
}
if (heat <= MIN_HEAT && penaltyCooling > 0) {
penaltyCooling--;
}
if (heat <= MIN_HEAT) {
heat = MIN_HEAT;
}
}
private void coolEngine(float idealHeat) {
float extraHeat = heat - idealHeat;
FluidStack coolant = this.tankCoolant.getFluid();
if (coolant == null) {
return;
}
int coolantAmount = Math.min(MAX_COOLANT_PER_TICK, coolant.amount);
ICoolant currentCoolant = BuildcraftFuelRegistry.coolant.getCoolant(coolant.getFluid());
if (currentCoolant != null) {
float cooling = currentCoolant.getDegreesCoolingPerMB(heat);
cooling /= getBiomeTempScalar();
if (coolantAmount * cooling > extraHeat) {
tankCoolant.drain(Math.round(extraHeat / cooling), true);
heat -= extraHeat;
} else {
tankCoolant.drain(coolantAmount, true);
heat -= coolantAmount * cooling;
}
}
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
tankManager.readFromNBT(data);
burnTime = data.getInteger("burnTime");
penaltyCooling = data.getInteger("penaltyCooling");
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
tankManager.writeToNBT(data);
data.setInteger("burnTime", burnTime);
data.setInteger("penaltyCooling", penaltyCooling);
}
@Override
public void getGUINetworkData(int id, int value) {
super.getGUINetworkData(id, value);
switch (id) {
// Fluid Fuel ID
case 15:
if (tankFuel.getFluid() == null) {
tankFuel.setFluid(new FluidStack(value, 0));
} else {
tankFuel.getFluid().fluidID = value;
}
break;
// Fluid Coolant ID
case 16:
if (tankCoolant.getFluid() == null) {
tankCoolant.setFluid(new FluidStack(value, 0));
} else {
tankCoolant.getFluid().fluidID = value;
}
break;
// Fluid Fuel amount
case 17:
if (tankFuel.getFluid() == null) {
tankFuel.setFluid(new FluidStack(0, value));
} else {
tankFuel.getFluid().amount = value;
}
break;
// Fluid Coolant amount
case 18:
if (tankCoolant.getFluid() == null) {
tankCoolant.setFluid(new FluidStack(0, value));
} else {
tankCoolant.getFluid().amount = value;
}
break;
//Fluid Fuel color
case 19:
tankFuel.colorRenderCache = value;
break;
//Fluid Coolant color
case 20:
tankCoolant.colorRenderCache = value;
break;
}
}
@Override
public void sendGUINetworkData(ContainerEngine containerEngine, ICrafting iCrafting) {
super.sendGUINetworkData(containerEngine, iCrafting);
iCrafting.sendProgressBarUpdate(containerEngine, 15, tankFuel.getFluid() != null ? tankFuel.getFluid().fluidID : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 16, tankCoolant.getFluid() != null ? tankCoolant.getFluid().fluidID : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 17, tankFuel.getFluid() != null ? tankFuel.getFluid().amount : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 18, tankCoolant.getFluid() != null ? tankCoolant.getFluid().amount : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 19, tankFuel.colorRenderCache);
iCrafting.sendProgressBarUpdate(containerEngine, 20, tankCoolant.colorRenderCache);
}
@Override
public boolean isActive() {
return penaltyCooling <= 0;
}
/* ITANKCONTAINER */
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return tankFuel.drain(maxDrain, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
if (resource == null) {
return null;
}
if (tankCoolant.getFluidType() == resource.getFluid()) {
return tankCoolant.drain(resource.amount, doDrain);
}
if (tankFuel.getFluidType() == resource.getFluid()) {
return tankFuel.drain(resource.amount, doDrain);
}
return null;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return from != orientation;
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
if (BuildcraftFuelRegistry.coolant.getCoolant(resource.getFluid()) != null) {
return tankCoolant.fill(resource, doFill);
} else if (BuildcraftFuelRegistry.fuel.getFuel(resource.getFluid()) != null) {
return tankFuel.fill(resource, doFill);
} else {
return 0;
}
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return from != orientation &&
(BuildcraftFuelRegistry.coolant.getCoolant(fluid) != null ||
BuildcraftFuelRegistry.fuel.getFuel(fluid) != null);
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection direction) {
if (direction == orientation) {
return null;
}
return tankManager.getTankInfo(direction);
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
if (itemstack == null) {
return false;
} else if (BuildcraftFuelRegistry.coolant.getSolidCoolant(StackKey.stack(itemstack)) != null) {
return true;
} else {
FluidStack fluidStack = FluidContainerRegistry.getFluidForFilledItem(itemstack);
return fluidStack != null && canFill(ForgeDirection.UNKNOWN, fluidStack.getFluid());
}
}
public FluidStack getFuel() {
return tankFuel.getFluid();
}
public FluidStack getCoolant() {
return tankCoolant.getFluid();
}
@Override
public int maxEnergyReceived() {
return 20000;
}
@Override
public int maxEnergyExtracted() {
return 5000;
}
@Override
public int getMaxEnergy() {
return 100000;
}
@Override
public int calculateCurrentOutput() {
if (currentFuel == null) {
return 0;
} else {
return currentFuel.getPowerPerCycle();
}
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
}