-
Notifications
You must be signed in to change notification settings - Fork 497
/
EngineIron.java
420 lines (353 loc) · 11.5 KB
/
EngineIron.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
/**
* Copyright (c) SpaceToad, 2011
* 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.minecraftforge.common.ForgeDirection;
import net.minecraftforge.liquids.ILiquidTank;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidTank;
import buildcraft.api.fuels.IronEngineCoolant;
import buildcraft.api.fuels.IronEngineFuel;
import buildcraft.core.DefaultProps;
import buildcraft.core.utils.Utils;
import buildcraft.energy.gui.ContainerEngine;
import net.minecraft.src.Block;
import net.minecraft.src.ICrafting;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
public class EngineIron extends Engine {
public static int MAX_LIQUID = LiquidContainerRegistry.BUCKET_VOLUME * 10;
public static int MAX_HEAT = 100000;
public static int COOLANT_THRESHOLD = 49000;
private ItemStack itemInInventory;
int burnTime = 0;
int heat = 0;
private LiquidTank fuelTank;
private LiquidTank coolantTank;
public int penaltyCooling = 0;
boolean lastPowered = false;
public EngineIron(TileEngine engine) {
super(engine);
maxEnergy = 100000;
maxEnergyExtracted = 500;
fuelTank = new LiquidTank(MAX_LIQUID);
coolantTank = new LiquidTank(MAX_LIQUID);
}
@Override
public String getTextureFile() {
return DefaultProps.TEXTURE_PATH_BLOCKS + "/base_iron.png";
}
@Override
public int explosionRange() {
return 8;
}
@Override
public int maxEnergyReceived() {
return 2000;
}
@Override
public float getPistonSpeed() {
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.0f;
}
}
@Override
public boolean isBurning() {
LiquidStack fuel = fuelTank.getLiquid();
return fuel != null && fuel.amount > 0 && penaltyCooling == 0 && tile.isRedstonePowered;
}
@Override
public void burn() {
currentOutput = 0;
LiquidStack fuel = this.fuelTank.getLiquid();
IronEngineFuel currentFuel = IronEngineFuel.getFuelForLiquid(fuel);
if (currentFuel == null) {
return;
}
if (penaltyCooling <= 0 && tile.isRedstonePowered) {
lastPowered = true;
if (burnTime > 0 || fuel.amount > 0) {
if (burnTime > 0) {
burnTime--;
} else {
fuel.amount--;
burnTime = currentFuel.totalBurningTime / LiquidContainerRegistry.BUCKET_VOLUME;
}
currentOutput = currentFuel.powerPerCycle;
addEnergy(currentFuel.powerPerCycle);
heat += currentFuel.powerPerCycle;
}
} else if (penaltyCooling <= 0) {
if (lastPowered) {
lastPowered = false;
penaltyCooling = 30 * 20;
// 30 sec of penalty on top of the cooling
}
}
}
@Override
public void update() {
super.update();
if (itemInInventory != null) {
LiquidStack liquid = null;
if (Block.ice.blockID == itemInInventory.itemID && heat > COOLANT_THRESHOLD)
{
liquid = LiquidContainerRegistry.getLiquidForFilledItem(new ItemStack(Item.bucketWater));
}
else
{
liquid = LiquidContainerRegistry.getLiquidForFilledItem(itemInInventory);
}
if (liquid != null) {
if (fill(ForgeDirection.UNKNOWN, liquid, false) == liquid.amount) {
fill(ForgeDirection.UNKNOWN, liquid, true);
tile.setInventorySlotContents(0, Utils.consumeItem(itemInInventory));
}
}
}
if (heat > COOLANT_THRESHOLD) {
int extraHeat = heat - COOLANT_THRESHOLD;
LiquidStack coolant = this.coolantTank.getLiquid();
IronEngineCoolant currentCoolant = IronEngineCoolant.getCoolantForLiquid(coolant);
if (currentCoolant != null)
{
if(coolant.amount * currentCoolant.coolingPerUnit > extraHeat) {
coolant.amount -= Math.round(extraHeat / currentCoolant.coolingPerUnit);
heat = COOLANT_THRESHOLD;
} else {
heat -= coolant.amount * currentCoolant.coolingPerUnit;
coolant.amount = 0;
}
}
}
if (heat > 0 && (penaltyCooling > 0 || !tile.isRedstonePowered)) {
heat -= 10;
}
if (heat <= 0) heat = 0;
if (heat == 0 && penaltyCooling > 0) {
penaltyCooling--;
}
}
@Override
public void computeEnergyStage() {
if (heat <= MAX_HEAT / 4) {
energyStage = EnergyStage.Blue;
} else if (heat <= MAX_HEAT / 2) {
energyStage = EnergyStage.Green;
} else if (heat <= MAX_HEAT * 3F / 4F) {
energyStage = EnergyStage.Yellow;
} else if (heat <= MAX_HEAT) {
energyStage = EnergyStage.Red;
} else {
energyStage = EnergyStage.Explosion;
}
}
@Override
public int getScaledBurnTime(int i) {
return this.fuelTank.getLiquid() != null ? (int) (((float) this.fuelTank.getLiquid().amount / (float) (MAX_LIQUID)) * i) : 0;
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
if(nbttagcompound.hasKey("liquidId")) {
fuelTank.setLiquid(
new LiquidStack(
nbttagcompound.getInteger("liquidId"),
nbttagcompound.getInteger("liquidQty"),
nbttagcompound.getInteger("liquidMeta")));
} else if(nbttagcompound.hasKey("fuelTank")) {
fuelTank.setLiquid(
LiquidStack.loadLiquidStackFromNBT(
nbttagcompound.getCompoundTag("fuelTank")));
}
burnTime = nbttagcompound.getInteger("burnTime");
if(nbttagcompound.hasKey("coolantId")) {
coolantTank.setLiquid(
new LiquidStack(
nbttagcompound.getInteger("coolantId"),
nbttagcompound.getInteger("coolantQty"),
nbttagcompound.getInteger("coolantMeta")));
} else if(nbttagcompound.hasKey("coolantTank")) {
coolantTank.setLiquid(
LiquidStack.loadLiquidStackFromNBT(
nbttagcompound.getCompoundTag("coolantTank")));
}
heat = nbttagcompound.getInteger("heat");
penaltyCooling = nbttagcompound.getInteger("penaltyCooling");
if (nbttagcompound.hasKey("itemInInventory")) {
NBTTagCompound cpt = nbttagcompound.getCompoundTag("itemInInventory");
itemInInventory = ItemStack.loadItemStackFromNBT(cpt);
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
if(fuelTank.getLiquid() != null) {
nbttagcompound.setTag("fuelTank", fuelTank.getLiquid().writeToNBT(new NBTTagCompound()));
}
if(coolantTank.getLiquid() != null) {
nbttagcompound.setTag("coolantTank", coolantTank.getLiquid().writeToNBT(new NBTTagCompound()));
}
nbttagcompound.setInteger("burnTime", burnTime);
nbttagcompound.setInteger("heat", heat);
nbttagcompound.setInteger("penaltyCooling", penaltyCooling);
if (itemInInventory != null) {
NBTTagCompound cpt = new NBTTagCompound();
itemInInventory.writeToNBT(cpt);
nbttagcompound.setTag("itemInInventory", cpt);
}
}
public int getScaledCoolant(int i) {
return coolantTank.getLiquid() != null ? (int) (((float) coolantTank.getLiquid().amount / (float) (MAX_LIQUID)) * i) : 0;
}
@Override
public void delete() {
}
@Override
public void getGUINetworkData(int i, int j) {
switch (i) {
case 0:
int iEnergy = Math.round(energy * 10);
iEnergy = (iEnergy & 0xff00) | (j & 0xff);
energy = iEnergy / 10;
break;
case 1:
iEnergy = Math.round(energy * 10);
iEnergy = (iEnergy & 0xff) | ((j & 0xff) << 8);
energy = iEnergy / 10;
break;
case 2:
currentOutput = j / 10;
break;
case 3:
heat = (heat & 0xff00) | (j & 0xff);
break;
case 4:
heat = (heat & 0xff) | ((j & 0xff) << 8 );
break;
case 5:
if(fuelTank.getLiquid() == null) fuelTank.setLiquid(new LiquidStack(0, j));
else fuelTank.getLiquid().amount = j;
break;
case 6:
if(fuelTank.getLiquid() == null) fuelTank.setLiquid(new LiquidStack(j, 0));
else fuelTank.getLiquid().itemID = j;
break;
case 7:
if(coolantTank.getLiquid() == null) coolantTank.setLiquid(new LiquidStack(0, j));
else coolantTank.getLiquid().amount = j;
break;
case 8:
if(coolantTank.getLiquid() == null) coolantTank.setLiquid(new LiquidStack(j, 0));
else coolantTank.getLiquid().itemID = j;
break;
case 9:
if(fuelTank.getLiquid() == null) fuelTank.setLiquid(new LiquidStack(0, 0, j));
else fuelTank.getLiquid().itemMeta = j;
break;
case 10:
if(coolantTank.getLiquid() == null) coolantTank.setLiquid(new LiquidStack(0, 0, j));
else coolantTank.getLiquid().itemMeta = j;
}
}
@Override
public void sendGUINetworkData(ContainerEngine containerEngine, ICrafting iCrafting) {
iCrafting.sendProgressBarUpdate(containerEngine, 0, Math.round(energy * 10) & 0xff);
iCrafting.sendProgressBarUpdate(containerEngine, 1, (Math.round(energy * 10) & 0xff00) >> 8 );
iCrafting.sendProgressBarUpdate(containerEngine, 2, Math.round(currentOutput * 10));
iCrafting.sendProgressBarUpdate(containerEngine, 3, heat & 0xff);
iCrafting.sendProgressBarUpdate(containerEngine, 4, (heat & 0xff00) >> 8);
iCrafting.sendProgressBarUpdate(containerEngine, 5, fuelTank.getLiquid() != null ? fuelTank.getLiquid().amount : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 6, fuelTank.getLiquid() != null ? fuelTank.getLiquid().itemID : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 7, coolantTank.getLiquid() != null ? coolantTank.getLiquid().amount : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 8, coolantTank.getLiquid() != null ? coolantTank.getLiquid().itemID : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 9, fuelTank.getLiquid() != null ? fuelTank.getLiquid().itemMeta : 0);
iCrafting.sendProgressBarUpdate(containerEngine, 10, coolantTank.getLiquid() != null ? coolantTank.getLiquid().itemMeta : 0);
}
@Override
public boolean isActive() {
return penaltyCooling <= 0;
}
@Override
public int getHeat() {
return heat;
}
/* ITANKCONTAINER */
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill) {
// Handle coolant
if (IronEngineCoolant.getCoolantForLiquid(resource) != null)
return fillCoolant(from, resource, doFill);
if (IronEngineFuel.getFuelForLiquid(resource) != null)
return fuelTank.fill(resource, doFill);
return 0;
}
private int fillCoolant(ForgeDirection from, LiquidStack resource, boolean doFill) {
return coolantTank.fill(resource, doFill);
}
@Override
public LiquidTank[] getLiquidSlots() {
return new LiquidTank[] { fuelTank, coolantTank };
}
/* IINVENTORY */
@Override public int getSizeInventory() { return 1; }
@Override public ItemStack getStackInSlot(int i) { return itemInInventory; }
@Override public void setInventorySlotContents(int i, ItemStack itemstack) { itemInInventory = itemstack; }
@Override
public ItemStack decrStackSize(int i, int j) {
if (itemInInventory != null) {
ItemStack newStack = itemInInventory.splitStack(j);
if (itemInInventory.stackSize == 0) {
itemInInventory = null;
}
return newStack;
} else {
return null;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int var1) {
if (itemInInventory == null)
return null;
ItemStack toReturn = itemInInventory;
itemInInventory = null;
return toReturn;
}
@Override
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type) {
switch (direction)
{
case UP:
return fuelTank;
case DOWN:
return coolantTank;
default:
return null;
}
}
public int getFuelId() {
return fuelTank.getLiquid() != null ? fuelTank.getLiquid().itemID : 0;
}
public int getFuelMeta() {
return fuelTank.getLiquid() != null ? fuelTank.getLiquid().itemMeta : 0;
}
public int getCoolantId() {
return coolantTank.getLiquid() != null ? coolantTank.getLiquid().itemID : 0;
}
public int getCoolantMeta() {
return coolantTank.getLiquid() != null ? coolantTank.getLiquid().itemMeta : 0;
}
}