Skip to content

Commit 8c60281

Browse files
authored
Add Void Fluid Pump (#328)
1 parent d696998 commit 8c60281

File tree

7 files changed

+786
-1
lines changed

7 files changed

+786
-1
lines changed
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
package com.github.gtexpert.core.api.capability.impl;
2+
3+
import com.github.gtexpert.core.common.metatileentities.multi.MetaTileEntityVoidFluidPump;
4+
5+
import gregtech.api.GTValues;
6+
import gregtech.api.capability.GregtechDataCodes;
7+
import gregtech.api.metatileentity.MetaTileEntity;
8+
import gregtech.api.worldgen.bedrockFluids.BedrockFluidVeinHandler;
9+
import gregtech.common.ConfigHolder;
10+
import gregtech.common.metatileentities.multi.electric.MetaTileEntityFluidDrill;
11+
12+
import net.minecraft.nbt.NBTTagCompound;
13+
import net.minecraft.network.PacketBuffer;
14+
import net.minecraftforge.fluids.Fluid;
15+
import net.minecraftforge.fluids.FluidStack;
16+
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public class VoidFluidPumpLogic {
20+
21+
public static final int MAX_PROGRESS = 20;
22+
23+
private int progressTime = 0;
24+
25+
private final MetaTileEntityVoidFluidPump metaTileEntity;
26+
27+
private boolean isActive;
28+
private boolean isWorkingEnabled = true;
29+
private boolean wasActiveAndNeedsUpdate;
30+
private boolean isDone = false;
31+
protected boolean isInventoryFull;
32+
33+
private boolean hasNotEnoughEnergy;
34+
35+
private Fluid veinFluid;
36+
37+
public VoidFluidPumpLogic(MetaTileEntityVoidFluidPump metaTileEntity) {
38+
this.metaTileEntity = metaTileEntity;
39+
this.veinFluid = null;
40+
}
41+
42+
/**
43+
* Performs the actual drilling
44+
* Call this method every tick in update
45+
*/
46+
public void performDrilling() {
47+
if (metaTileEntity.getWorld().isRemote) return;
48+
49+
// if we have no fluid, try to get a new one
50+
if (veinFluid == null)
51+
if (!acquireNewFluid())
52+
return; // stop if we still have no fluid
53+
54+
// drills that cannot work do nothing
55+
if (!this.isWorkingEnabled)
56+
return;
57+
58+
// check if drilling is possible
59+
if (!checkCanDrain())
60+
return;
61+
62+
// if the inventory is not full, drain energy etc. from the drill
63+
// the storages have already been checked earlier
64+
if (!isInventoryFull) {
65+
// actually drain the energy
66+
consumeEnergy(false);
67+
68+
// since energy is being consumed the rig is now active
69+
if (!this.isActive)
70+
setActive(true);
71+
} else {
72+
// the rig cannot drain, therefore it is inactive
73+
if (this.isActive)
74+
setActive(false);
75+
return;
76+
}
77+
78+
// increase progress
79+
progressTime++;
80+
if (progressTime % MAX_PROGRESS != 0)
81+
return;
82+
progressTime = 0;
83+
84+
int amount = getFluidToProduce();
85+
86+
if (metaTileEntity.fillTanks(new FluidStack(veinFluid, amount), true)) {
87+
metaTileEntity.fillTanks(new FluidStack(veinFluid, amount), false);
88+
depleteVein();
89+
} else {
90+
isInventoryFull = true;
91+
setActive(false);
92+
setWasActiveAndNeedsUpdate(true);
93+
}
94+
}
95+
96+
protected boolean consumeEnergy(boolean simulate) {
97+
return metaTileEntity.drainEnergy(simulate);
98+
}
99+
100+
private boolean acquireNewFluid() {
101+
this.veinFluid = BedrockFluidVeinHandler.getFluidInChunk(metaTileEntity.getWorld(), getChunkX(), getChunkZ());
102+
return this.veinFluid != null;
103+
}
104+
105+
public Fluid getDrilledFluid() {
106+
return veinFluid;
107+
}
108+
109+
protected void depleteVein() {
110+
BedrockFluidVeinHandler.depleteVein(metaTileEntity.getWorld(), getChunkX(), getChunkZ(), 0, true);
111+
}
112+
113+
public int getFluidToProduce() {
114+
int depletedYield = BedrockFluidVeinHandler.getDepletedFluidYield(metaTileEntity.getWorld(), getChunkX(),
115+
getChunkZ());
116+
int regularYield = BedrockFluidVeinHandler.getFluidYield(metaTileEntity.getWorld(), getChunkX(), getChunkZ());
117+
int remainingOperations = BedrockFluidVeinHandler.getOperationsRemaining(metaTileEntity.getWorld(), getChunkX(),
118+
getChunkZ());
119+
120+
int produced = Math.max(depletedYield,
121+
regularYield * remainingOperations / BedrockFluidVeinHandler.MAXIMUM_VEIN_OPERATIONS);
122+
produced *= metaTileEntity.getRigMultiplier();
123+
124+
return produced;
125+
}
126+
127+
/**
128+
*
129+
* @return true if the rig is able to drain, else false
130+
*/
131+
protected boolean checkCanDrain() {
132+
if (!consumeEnergy(true)) {
133+
if (progressTime >= 2) {
134+
if (ConfigHolder.machines.recipeProgressLowEnergy)
135+
this.progressTime = 1;
136+
else
137+
this.progressTime = Math.max(1, progressTime - 2);
138+
139+
hasNotEnoughEnergy = true;
140+
}
141+
return false;
142+
}
143+
144+
if (this.hasNotEnoughEnergy &&
145+
metaTileEntity.getEnergyInputPerSecond() > 19L * GTValues.VA[metaTileEntity.getEnergyTier()]) {
146+
this.hasNotEnoughEnergy = false;
147+
}
148+
149+
if (metaTileEntity.fillTanks(new FluidStack(veinFluid, getFluidToProduce()), true)) {
150+
this.isInventoryFull = false;
151+
return true;
152+
}
153+
this.isInventoryFull = true;
154+
155+
if (isActive()) {
156+
setActive(false);
157+
setWasActiveAndNeedsUpdate(true);
158+
}
159+
return false;
160+
}
161+
162+
public int getChunkX() {
163+
return Math.floorDiv(metaTileEntity.getPos().getX(), 16);
164+
}
165+
166+
public int getChunkZ() {
167+
return Math.floorDiv(metaTileEntity.getPos().getZ(), 16);
168+
}
169+
170+
/**
171+
*
172+
* @return true if the rig is active
173+
*/
174+
public boolean isActive() {
175+
return this.isActive;
176+
}
177+
178+
/**
179+
*
180+
* @param active the new state of the rig's activity: true to change to active, else false
181+
*/
182+
public void setActive(boolean active) {
183+
if (this.isActive != active) {
184+
this.isActive = active;
185+
this.metaTileEntity.markDirty();
186+
if (metaTileEntity.getWorld() != null && !metaTileEntity.getWorld().isRemote) {
187+
this.metaTileEntity.writeCustomData(GregtechDataCodes.WORKABLE_ACTIVE, buf -> buf.writeBoolean(active));
188+
}
189+
}
190+
}
191+
192+
/**
193+
*
194+
* @param isWorkingEnabled the new state of the rig's ability to work: true to change to enabled, else false
195+
*/
196+
public void setWorkingEnabled(boolean isWorkingEnabled) {
197+
if (this.isWorkingEnabled != isWorkingEnabled) {
198+
this.isWorkingEnabled = isWorkingEnabled;
199+
metaTileEntity.markDirty();
200+
if (metaTileEntity.getWorld() != null && !metaTileEntity.getWorld().isRemote) {
201+
this.metaTileEntity.writeCustomData(GregtechDataCodes.WORKING_ENABLED,
202+
buf -> buf.writeBoolean(isWorkingEnabled));
203+
}
204+
}
205+
}
206+
207+
/**
208+
*
209+
* @return whether working is enabled for the logic
210+
*/
211+
public boolean isWorkingEnabled() {
212+
return isWorkingEnabled;
213+
}
214+
215+
/**
216+
*
217+
* @return whether the rig is currently working
218+
*/
219+
public boolean isWorking() {
220+
return isActive && !hasNotEnoughEnergy && isWorkingEnabled;
221+
}
222+
223+
/**
224+
*
225+
* @return the current progress towards producing fluid of the rig
226+
*/
227+
public int getProgressTime() {
228+
return this.progressTime;
229+
}
230+
231+
public double getProgressPercent() {
232+
return getProgressTime() * 1.0 / MAX_PROGRESS;
233+
}
234+
235+
236+
/**
237+
*
238+
* @return whether the inventory is full
239+
*/
240+
public boolean isInventoryFull() {
241+
return this.isInventoryFull;
242+
}
243+
244+
/**
245+
* writes all needed values to NBT
246+
* This MUST be called and returned in the MetaTileEntity's {@link MetaTileEntity#writeToNBT(NBTTagCompound)} method
247+
*/
248+
public NBTTagCompound writeToNBT(@NotNull NBTTagCompound data) {
249+
data.setBoolean("isActive", this.isActive);
250+
data.setBoolean("isWorkingEnabled", this.isWorkingEnabled);
251+
data.setBoolean("wasActiveAndNeedsUpdate", this.wasActiveAndNeedsUpdate);
252+
data.setBoolean("isDone", isDone);
253+
data.setInteger("progressTime", progressTime);
254+
data.setBoolean("isInventoryFull", isInventoryFull);
255+
return data;
256+
}
257+
258+
/**
259+
* reads all needed values from NBT
260+
* This MUST be called and returned in the MetaTileEntity's {@link MetaTileEntity#readFromNBT(NBTTagCompound)}
261+
* method
262+
*/
263+
public void readFromNBT(@NotNull NBTTagCompound data) {
264+
this.isActive = data.getBoolean("isActive");
265+
this.isWorkingEnabled = data.getBoolean("isWorkingEnabled");
266+
this.wasActiveAndNeedsUpdate = data.getBoolean("wasActiveAndNeedsUpdate");
267+
this.isDone = data.getBoolean("isDone");
268+
this.progressTime = data.getInteger("progressTime");
269+
this.isInventoryFull = data.getBoolean("isInventoryFull");
270+
}
271+
272+
/**
273+
* writes all needed values to InitialSyncData
274+
* This MUST be called and returned in the MetaTileEntity's
275+
* {@link MetaTileEntity#writeInitialSyncData(PacketBuffer)} method
276+
*/
277+
public void writeInitialSyncData(@NotNull PacketBuffer buf) {
278+
buf.writeBoolean(this.isActive);
279+
buf.writeBoolean(this.isWorkingEnabled);
280+
buf.writeBoolean(this.wasActiveAndNeedsUpdate);
281+
buf.writeInt(this.progressTime);
282+
buf.writeBoolean(this.isInventoryFull);
283+
}
284+
285+
/**
286+
* reads all needed values from InitialSyncData
287+
* This MUST be called and returned in the MetaTileEntity's
288+
* {@link MetaTileEntity#receiveInitialSyncData(PacketBuffer)} method
289+
*/
290+
public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
291+
setActive(buf.readBoolean());
292+
setWorkingEnabled(buf.readBoolean());
293+
setWasActiveAndNeedsUpdate(buf.readBoolean());
294+
this.progressTime = buf.readInt();
295+
this.isInventoryFull = buf.readBoolean();
296+
}
297+
298+
/**
299+
* reads all needed values from CustomData
300+
* This MUST be called and returned in the MetaTileEntity's
301+
* {@link MetaTileEntity#receiveCustomData(int, PacketBuffer)} method
302+
*/
303+
public void receiveCustomData(int dataId, PacketBuffer buf) {
304+
if (dataId == GregtechDataCodes.WORKABLE_ACTIVE) {
305+
this.isActive = buf.readBoolean();
306+
metaTileEntity.scheduleRenderUpdate();
307+
} else if (dataId == GregtechDataCodes.WORKING_ENABLED) {
308+
this.isWorkingEnabled = buf.readBoolean();
309+
metaTileEntity.scheduleRenderUpdate();
310+
}
311+
}
312+
313+
/**
314+
*
315+
* @return whether the rig was active and needs an update
316+
*/
317+
public boolean wasActiveAndNeedsUpdate() {
318+
return this.wasActiveAndNeedsUpdate;
319+
}
320+
321+
/**
322+
* set whether the rig was active and needs an update
323+
*
324+
* @param wasActiveAndNeedsUpdate the state to set
325+
*/
326+
public void setWasActiveAndNeedsUpdate(boolean wasActiveAndNeedsUpdate) {
327+
this.wasActiveAndNeedsUpdate = wasActiveAndNeedsUpdate;
328+
}
329+
}

src/main/java/com/github/gtexpert/core/common/GTEConfigHolder.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ public static class ModpackFlag {
5353
"3. EIO: Capacitor Bank",
5454
"Default: true" })
5555
public boolean addCreativeRecipe = true;
56+
57+
@Config.Comment({ "The base voltage of Void Fluid Pump",
58+
"Minimum: 0 (ULV)",
59+
"Maximum: 9 (UHV)",
60+
"Default: 4 (EV)"})
61+
@Config.RangeInt(min = 0, max = 9)
62+
public int vfpBaseVoltage = 4;
63+
64+
@Config.Comment({ "The base of Production Rate of Void Fluid Pump",
65+
"Total Production Multiplier = Base Production Rate * (Current Voltage - Base Voltage)",
66+
"Minimum: 1",
67+
"Default: 16"})
68+
@Config.RangeInt(min = 1)
69+
public int vfpBaseProductionRate = 16;
5670
}
5771

5872
public static class GregtechOverride {

src/main/java/com/github/gtexpert/core/common/metatileentities/GTEMetaTileEntities.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class GTEMetaTileEntities {
2525
public static MetaTileEntityAdvancedChemicalPlant ADVANCED_CHEMICAL_PLANT;
2626
public static MetaTileEntityLargeGasCollector LARGE_GAS_COLLECTOR;
2727
public static MetaTileEntityLargeTransformer LARGE_TRANSFORMER;
28+
public static MetaTileEntityVoidFluidPump VOID_FLUID_PUMP;
2829

2930
public static void init() {
3031
// Single Machine
@@ -45,6 +46,8 @@ public static void init() {
4546
"large_gas_collector" : "advanced_gas_collector")));
4647
LARGE_TRANSFORMER = registerMetaTileEntity(12008,
4748
new MetaTileEntityLargeTransformer(gteId("large_transformer")));
49+
VOID_FLUID_PUMP = registerMetaTileEntity(12009,
50+
new MetaTileEntityVoidFluidPump((gteId("void_fluid_pump"))));
4851
}
4952

5053
public static void registerGTESimpleMetaTileEntity(GTESimpleMachineMetaTileEntity[] machines, int startId,

0 commit comments

Comments
 (0)