Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
package com.github.gtexpert.core.api.capability.impl;

import com.github.gtexpert.core.common.metatileentities.multi.MetaTileEntityVoidFluidPump;

import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.worldgen.bedrockFluids.BedrockFluidVeinHandler;
import gregtech.common.ConfigHolder;
import gregtech.common.metatileentities.multi.electric.MetaTileEntityFluidDrill;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

public class VoidFluidPumpLogic {

public static final int MAX_PROGRESS = 20;

private int progressTime = 0;

private final MetaTileEntityVoidFluidPump metaTileEntity;

private boolean isActive;
private boolean isWorkingEnabled = true;
private boolean wasActiveAndNeedsUpdate;
private boolean isDone = false;
protected boolean isInventoryFull;

private boolean hasNotEnoughEnergy;

private Fluid veinFluid;

public VoidFluidPumpLogic(MetaTileEntityVoidFluidPump metaTileEntity) {
this.metaTileEntity = metaTileEntity;
this.veinFluid = null;
}

/**
* Performs the actual drilling
* Call this method every tick in update
*/
public void performDrilling() {
if (metaTileEntity.getWorld().isRemote) return;

// if we have no fluid, try to get a new one
if (veinFluid == null)
if (!acquireNewFluid())
return; // stop if we still have no fluid

// drills that cannot work do nothing
if (!this.isWorkingEnabled)
return;

// check if drilling is possible
if (!checkCanDrain())
return;

// if the inventory is not full, drain energy etc. from the drill
// the storages have already been checked earlier
if (!isInventoryFull) {
// actually drain the energy
consumeEnergy(false);

// since energy is being consumed the rig is now active
if (!this.isActive)
setActive(true);
} else {
// the rig cannot drain, therefore it is inactive
if (this.isActive)
setActive(false);
return;
}

// increase progress
progressTime++;
if (progressTime % MAX_PROGRESS != 0)
return;
progressTime = 0;

int amount = getFluidToProduce();

if (metaTileEntity.fillTanks(new FluidStack(veinFluid, amount), true)) {
metaTileEntity.fillTanks(new FluidStack(veinFluid, amount), false);
depleteVein();
} else {
isInventoryFull = true;
setActive(false);
setWasActiveAndNeedsUpdate(true);
}
}

protected boolean consumeEnergy(boolean simulate) {
return metaTileEntity.drainEnergy(simulate);
}

private boolean acquireNewFluid() {
this.veinFluid = BedrockFluidVeinHandler.getFluidInChunk(metaTileEntity.getWorld(), getChunkX(), getChunkZ());
return this.veinFluid != null;
}

public Fluid getDrilledFluid() {
return veinFluid;
}

protected void depleteVein() {
BedrockFluidVeinHandler.depleteVein(metaTileEntity.getWorld(), getChunkX(), getChunkZ(), 0, true);
}

public int getFluidToProduce() {
int depletedYield = BedrockFluidVeinHandler.getDepletedFluidYield(metaTileEntity.getWorld(), getChunkX(),
getChunkZ());
int regularYield = BedrockFluidVeinHandler.getFluidYield(metaTileEntity.getWorld(), getChunkX(), getChunkZ());
int remainingOperations = BedrockFluidVeinHandler.getOperationsRemaining(metaTileEntity.getWorld(), getChunkX(),
getChunkZ());

int produced = Math.max(depletedYield,
regularYield * remainingOperations / BedrockFluidVeinHandler.MAXIMUM_VEIN_OPERATIONS);
produced *= metaTileEntity.getRigMultiplier();

return produced;
}

/**
*
* @return true if the rig is able to drain, else false
*/
protected boolean checkCanDrain() {
if (!consumeEnergy(true)) {
if (progressTime >= 2) {
if (ConfigHolder.machines.recipeProgressLowEnergy)
this.progressTime = 1;
else
this.progressTime = Math.max(1, progressTime - 2);

hasNotEnoughEnergy = true;
}
return false;
}

if (this.hasNotEnoughEnergy &&
metaTileEntity.getEnergyInputPerSecond() > 19L * GTValues.VA[metaTileEntity.getEnergyTier()]) {
this.hasNotEnoughEnergy = false;
}

if (metaTileEntity.fillTanks(new FluidStack(veinFluid, getFluidToProduce()), true)) {
this.isInventoryFull = false;
return true;
}
this.isInventoryFull = true;

if (isActive()) {
setActive(false);
setWasActiveAndNeedsUpdate(true);
}
return false;
}

public int getChunkX() {
return Math.floorDiv(metaTileEntity.getPos().getX(), 16);
}

public int getChunkZ() {
return Math.floorDiv(metaTileEntity.getPos().getZ(), 16);
}

/**
*
* @return true if the rig is active
*/
public boolean isActive() {
return this.isActive;
}

/**
*
* @param active the new state of the rig's activity: true to change to active, else false
*/
public void setActive(boolean active) {
if (this.isActive != active) {
this.isActive = active;
this.metaTileEntity.markDirty();
if (metaTileEntity.getWorld() != null && !metaTileEntity.getWorld().isRemote) {
this.metaTileEntity.writeCustomData(GregtechDataCodes.WORKABLE_ACTIVE, buf -> buf.writeBoolean(active));
}
}
}

/**
*
* @param isWorkingEnabled the new state of the rig's ability to work: true to change to enabled, else false
*/
public void setWorkingEnabled(boolean isWorkingEnabled) {
if (this.isWorkingEnabled != isWorkingEnabled) {
this.isWorkingEnabled = isWorkingEnabled;
metaTileEntity.markDirty();
if (metaTileEntity.getWorld() != null && !metaTileEntity.getWorld().isRemote) {
this.metaTileEntity.writeCustomData(GregtechDataCodes.WORKING_ENABLED,
buf -> buf.writeBoolean(isWorkingEnabled));
}
}
}

/**
*
* @return whether working is enabled for the logic
*/
public boolean isWorkingEnabled() {
return isWorkingEnabled;
}

/**
*
* @return whether the rig is currently working
*/
public boolean isWorking() {
return isActive && !hasNotEnoughEnergy && isWorkingEnabled;
}

/**
*
* @return the current progress towards producing fluid of the rig
*/
public int getProgressTime() {
return this.progressTime;
}

public double getProgressPercent() {
return getProgressTime() * 1.0 / MAX_PROGRESS;
}


/**
*
* @return whether the inventory is full
*/
public boolean isInventoryFull() {
return this.isInventoryFull;
}

/**
* writes all needed values to NBT
* This MUST be called and returned in the MetaTileEntity's {@link MetaTileEntity#writeToNBT(NBTTagCompound)} method
*/
public NBTTagCompound writeToNBT(@NotNull NBTTagCompound data) {
data.setBoolean("isActive", this.isActive);
data.setBoolean("isWorkingEnabled", this.isWorkingEnabled);
data.setBoolean("wasActiveAndNeedsUpdate", this.wasActiveAndNeedsUpdate);
data.setBoolean("isDone", isDone);
data.setInteger("progressTime", progressTime);
data.setBoolean("isInventoryFull", isInventoryFull);
return data;
}

/**
* reads all needed values from NBT
* This MUST be called and returned in the MetaTileEntity's {@link MetaTileEntity#readFromNBT(NBTTagCompound)}
* method
*/
public void readFromNBT(@NotNull NBTTagCompound data) {
this.isActive = data.getBoolean("isActive");
this.isWorkingEnabled = data.getBoolean("isWorkingEnabled");
this.wasActiveAndNeedsUpdate = data.getBoolean("wasActiveAndNeedsUpdate");
this.isDone = data.getBoolean("isDone");
this.progressTime = data.getInteger("progressTime");
this.isInventoryFull = data.getBoolean("isInventoryFull");
}

/**
* writes all needed values to InitialSyncData
* This MUST be called and returned in the MetaTileEntity's
* {@link MetaTileEntity#writeInitialSyncData(PacketBuffer)} method
*/
public void writeInitialSyncData(@NotNull PacketBuffer buf) {
buf.writeBoolean(this.isActive);
buf.writeBoolean(this.isWorkingEnabled);
buf.writeBoolean(this.wasActiveAndNeedsUpdate);
buf.writeInt(this.progressTime);
buf.writeBoolean(this.isInventoryFull);
}

/**
* reads all needed values from InitialSyncData
* This MUST be called and returned in the MetaTileEntity's
* {@link MetaTileEntity#receiveInitialSyncData(PacketBuffer)} method
*/
public void receiveInitialSyncData(@NotNull PacketBuffer buf) {
setActive(buf.readBoolean());
setWorkingEnabled(buf.readBoolean());
setWasActiveAndNeedsUpdate(buf.readBoolean());
this.progressTime = buf.readInt();
this.isInventoryFull = buf.readBoolean();
}

/**
* reads all needed values from CustomData
* This MUST be called and returned in the MetaTileEntity's
* {@link MetaTileEntity#receiveCustomData(int, PacketBuffer)} method
*/
public void receiveCustomData(int dataId, PacketBuffer buf) {
if (dataId == GregtechDataCodes.WORKABLE_ACTIVE) {
this.isActive = buf.readBoolean();
metaTileEntity.scheduleRenderUpdate();
} else if (dataId == GregtechDataCodes.WORKING_ENABLED) {
this.isWorkingEnabled = buf.readBoolean();
metaTileEntity.scheduleRenderUpdate();
}
}

/**
*
* @return whether the rig was active and needs an update
*/
public boolean wasActiveAndNeedsUpdate() {
return this.wasActiveAndNeedsUpdate;
}

/**
* set whether the rig was active and needs an update
*
* @param wasActiveAndNeedsUpdate the state to set
*/
public void setWasActiveAndNeedsUpdate(boolean wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = wasActiveAndNeedsUpdate;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/github/gtexpert/core/common/GTEConfigHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public static class ModpackFlag {
"3. EIO: Capacitor Bank",
"Default: true" })
public boolean addCreativeRecipe = true;

@Config.Comment({ "The base voltage of Void Fluid Pump",
"Minimum: 0 (ULV)",
"Maximum: 9 (UHV)",
"Default: 4 (EV)"})
@Config.RangeInt(min = 0, max = 9)
public int vfpBaseVoltage = 4;

@Config.Comment({ "The base of Production Rate of Void Fluid Pump",
"Total Production Multiplier = Base Production Rate * (Current Voltage - Base Voltage)",
"Minimum: 1",
"Default: 16"})
@Config.RangeInt(min = 1)
public int vfpBaseProductionRate = 16;
}

public static class GregtechOverride {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class GTEMetaTileEntities {
public static MetaTileEntityAdvancedChemicalPlant ADVANCED_CHEMICAL_PLANT;
public static MetaTileEntityLargeGasCollector LARGE_GAS_COLLECTOR;
public static MetaTileEntityLargeTransformer LARGE_TRANSFORMER;
public static MetaTileEntityVoidFluidPump VOID_FLUID_PUMP;

public static void init() {
// Single Machine
Expand All @@ -45,6 +46,8 @@ public static void init() {
"large_gas_collector" : "advanced_gas_collector")));
LARGE_TRANSFORMER = registerMetaTileEntity(12008,
new MetaTileEntityLargeTransformer(gteId("large_transformer")));
VOID_FLUID_PUMP = registerMetaTileEntity(12009,
new MetaTileEntityVoidFluidPump((gteId("void_fluid_pump"))));
}

public static void registerGTESimpleMetaTileEntity(GTESimpleMachineMetaTileEntity[] machines, int startId,
Expand Down
Loading
Loading