-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathRecipeLogicSteam.java
More file actions
262 lines (232 loc) · 9.97 KB
/
RecipeLogicSteam.java
File metadata and controls
262 lines (232 loc) · 9.97 KB
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
package gregtech.api.capability.impl;
import gregtech.api.GTValues;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.damagesources.DamageSources;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMap;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EntitySelectors;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.items.IItemHandler;
import java.util.Arrays;
import static gregtech.api.situation.Situations.*;
public class RecipeLogicSteam extends AbstractRecipeLogic {
private final IFluidTank steamFluidTank;
private final boolean isHighPressure;
private final double conversionRate; //energy units per millibucket
private boolean needsVenting;
private boolean ventingStuck;
private EnumFacing ventingSide;
public RecipeLogicSteam(MetaTileEntity tileEntity, RecipeMap<?> recipeMap, boolean isHighPressure, IFluidTank steamFluidTank, double conversionRate) {
super(tileEntity, recipeMap);
this.steamFluidTank = steamFluidTank;
this.conversionRate = conversionRate;
this.isHighPressure = isHighPressure;
}
public boolean isVentingStuck() {
return needsVenting && ventingStuck;
}
public boolean isNeedsVenting() {
return needsVenting;
}
@Override
public void onFrontFacingSet(EnumFacing newFrontFacing) {
if (ventingSide == null) {
setVentingSide(newFrontFacing.getOpposite());
}
}
public EnumFacing getVentingSide() {
return ventingSide == null ? EnumFacing.SOUTH : ventingSide;
}
public void setVentingStuck(boolean ventingStuck) {
this.ventingStuck = ventingStuck;
if (!metaTileEntity.getWorld().isRemote) {
metaTileEntity.markDirty();
writeCustomData(4, buf -> buf.writeBoolean(ventingStuck));
}
}
public void setNeedsVenting(boolean needsVenting) {
this.needsVenting = needsVenting;
if (!needsVenting && ventingStuck)
setVentingStuck(false);
if (!metaTileEntity.getWorld().isRemote) {
metaTileEntity.markDirty();
writeCustomData(2, buf -> buf.writeBoolean(needsVenting));
}
}
public void setVentingSide(EnumFacing ventingSide) {
this.ventingSide = ventingSide;
if (!metaTileEntity.getWorld().isRemote) {
metaTileEntity.markDirty();
writeCustomData(3, buf -> buf.writeByte(ventingSide.getIndex()));
}
}
@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
super.receiveCustomData(dataId, buf);
if (dataId == 2) {
this.needsVenting = buf.readBoolean();
} else if (dataId == 3) {
this.ventingSide = EnumFacing.VALUES[buf.readByte()];
getMetaTileEntity().getHolder().scheduleChunkForRenderUpdate();
} else if (dataId == 4) {
this.ventingStuck = buf.readBoolean();
}
}
@Override
public void writeInitialData(PacketBuffer buf) {
super.writeInitialData(buf);
buf.writeByte(getVentingSide().getIndex());
buf.writeBoolean(needsVenting);
buf.writeBoolean(ventingStuck);
}
@Override
public void receiveInitialData(PacketBuffer buf) {
super.receiveInitialData(buf);
this.ventingSide = EnumFacing.VALUES[buf.readByte()];
this.needsVenting = buf.readBoolean();
this.ventingStuck = buf.readBoolean();
}
protected void tryDoVenting() {
BlockPos machinePos = metaTileEntity.getPos();
EnumFacing ventingSide = getVentingSide();
BlockPos ventingBlockPos = machinePos.offset(ventingSide);
IBlockState blockOnPos = metaTileEntity.getWorld().getBlockState(ventingBlockPos);
if (blockOnPos.getCollisionBoundingBox(metaTileEntity.getWorld(), ventingBlockPos) == Block.NULL_AABB) {
metaTileEntity.getWorld()
.getEntitiesWithinAABB(EntityLivingBase.class, new AxisAlignedBB(ventingBlockPos), EntitySelectors.CAN_AI_TARGET)
.forEach(entity -> entity.attackEntityFrom(DamageSources.getHeatDamage(), 6.0f));
WorldServer world = (WorldServer) metaTileEntity.getWorld();
double posX = machinePos.getX() + 0.5 + ventingSide.getFrontOffsetX() * 0.6;
double posY = machinePos.getY() + 0.5 + ventingSide.getFrontOffsetY() * 0.6;
double posZ = machinePos.getZ() + 0.5 + ventingSide.getFrontOffsetZ() * 0.6;
world.spawnParticle(EnumParticleTypes.SMOKE_LARGE, posX, posY, posZ,
7 + world.rand.nextInt(3),
ventingSide.getFrontOffsetX() / 2.0,
ventingSide.getFrontOffsetY() / 2.0,
ventingSide.getFrontOffsetZ() / 2.0, 0.1);
world.playSound(null, posX, posY, posZ, SoundEvents.BLOCK_LAVA_EXTINGUISH, SoundCategory.BLOCKS, 1.0f, 1.0f);
setNeedsVenting(false);
} else if (!ventingStuck) {
setVentingStuck(true);
}
}
@Override
public void update() {
if (getMetaTileEntity().getWorld().isRemote)
return;
if (this.needsVenting && metaTileEntity.getTimer() % 10 == 0) {
tryDoVenting();
if (isVentingStuck()){
metaTileEntity.setSituation(BLOCKED_VENT);
}
}
super.update();
}
protected boolean checkRecipeInputsDirty(IItemHandler inputs, IMultipleTankHandler fluidInputs) {
boolean shouldRecheckRecipe = false;
this.isInputsEmpty = true;
if (lastItemInputs == null || lastItemInputs.length != inputs.getSlots()) {
this.lastItemInputs = new ItemStack[inputs.getSlots()];
Arrays.fill(lastItemInputs, ItemStack.EMPTY);
}
if (lastFluidInputs == null || lastFluidInputs.length != fluidInputs.getTanks()) {
this.lastFluidInputs = new FluidStack[fluidInputs.getTanks()];
}
for (int i = 0; i < lastItemInputs.length; i++) {
ItemStack currentStack = inputs.getStackInSlot(i);
ItemStack lastStack = lastItemInputs[i];
if (!areItemStacksEqual(currentStack, lastStack)) {
this.lastItemInputs[i] = currentStack.isEmpty() ? ItemStack.EMPTY : currentStack.copy();
shouldRecheckRecipe = true;
} else if (currentStack.getCount() != lastStack.getCount()) {
lastStack.setCount(currentStack.getCount());
shouldRecheckRecipe = true;
} else if (!currentStack.isEmpty()) {
this.isInputsEmpty = false;
}
}
for (int i = 0; i < lastFluidInputs.length; i++) {
if (fluidInputs.getTankAt(i) == steamFluidTank)
continue;
FluidStack currentStack = fluidInputs.getTankAt(i).getFluid();
FluidStack lastStack = lastFluidInputs[i];
if ((currentStack == null && lastStack != null) ||
(currentStack != null && !currentStack.isFluidEqual(lastStack))) {
this.lastFluidInputs[i] = currentStack == null ? null : currentStack.copy();
shouldRecheckRecipe = true;
} else if (currentStack != null && lastStack != null &&
currentStack.amount != lastStack.amount) {
lastStack.amount = currentStack.amount;
shouldRecheckRecipe = true;
} else if (currentStack != null) {
this.isInputsEmpty = false;
}
}
return shouldRecheckRecipe;
}
@Override
protected boolean setupAndConsumeRecipeInputs(Recipe recipe) {
return !this.needsVenting && super.setupAndConsumeRecipeInputs(recipe);
}
@Override
protected void completeRecipe() {
super.completeRecipe();
setNeedsVenting(true);
}
@Override
protected int[] calculateOverclock(int EUt, long voltage, int duration) {
if (!isHighPressure) {
//disallow overclocking for low pressure bronze machines
return new int[]{EUt, duration};
}
return super.calculateOverclock(EUt, voltage, duration);
}
@Override
protected long getEnergyStored() {
return (long) Math.ceil(steamFluidTank.getFluidAmount() * conversionRate);
}
@Override
protected long getEnergyCapacity() {
return (long) Math.floor(steamFluidTank.getCapacity() * conversionRate);
}
@Override
protected boolean drawEnergy(int recipeEUt) {
int resultDraw = (int) Math.ceil(recipeEUt / conversionRate);
return resultDraw >= 0 && steamFluidTank.getFluidAmount() >= resultDraw &&
steamFluidTank.drain(resultDraw, true) != null;
}
@Override
protected long getMaxVoltage() {
return GTValues.V[GTValues.LV];
}
@Override
public NBTTagCompound serializeNBT() {
NBTTagCompound compound = super.serializeNBT();
compound.setInteger("VentingSide", getVentingSide().getIndex());
compound.setBoolean("NeedsVenting", needsVenting);
compound.setBoolean("VentingStuck", ventingStuck);
return compound;
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
super.deserializeNBT(compound);
this.ventingSide = EnumFacing.VALUES[compound.getInteger("VentingSide")];
this.needsVenting = compound.getBoolean("NeedsVenting");
this.ventingStuck = compound.getBoolean("VentingStuck");
}
}