Skip to content

Commit

Permalink
fix negative eut
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude committed Nov 22, 2024
1 parent 067d32f commit c4aab5b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IMultiblockController;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.capability.IWorkable;
Expand Down Expand Up @@ -101,17 +102,25 @@ public AbstractRecipeLogic(MetaTileEntity tileEntity, RecipeMap<?> recipeMap, bo
/**
* @return the energy container's energy input per second
*/
protected abstract long getEnergyInputPerSecond();
protected long getEnergyInputPerSecond() {
return getEnergyContainer().getInputPerSec();
}

/**
* @return the energy container's current stored energy
*/
protected abstract long getEnergyStored();
protected long getEnergyStored() {
return getEnergyContainer().getEnergyStored();
}

/**
* @return the energy container's maximum energy capacity
*/
protected abstract long getEnergyCapacity();
protected long getEnergyCapacity() {
return getEnergyContainer().getEnergyCapacity();
}

protected abstract IEnergyContainer getEnergyContainer();

/**
* Draw energy from the energy container
Expand All @@ -120,12 +129,22 @@ public AbstractRecipeLogic(MetaTileEntity tileEntity, RecipeMap<?> recipeMap, bo
* @param simulate whether to simulate energy extraction or not
* @return true if the energy can/was drained, otherwise false
*/
protected abstract boolean drawEnergy(long recipeEUt, boolean simulate);
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
// this should be the ONLY time eut is negative!
if (consumesEnergy()) recipeEUt = -recipeEUt;
long resultEnergy = getEnergyStored() + recipeEUt;
if (resultEnergy >= 0L && resultEnergy <= getEnergyCapacity()) {
if (!simulate) getEnergyContainer().changeEnergy(recipeEUt);
return true;
} else return false;
}

/**
* @return the maximum voltage the machine can use/handle for recipe searching
*/
public abstract long getMaxVoltage();
public long getMaxVoltage() {
return Math.max(getEnergyContainer().getInputVoltage(), getEnergyContainer().getOutputVoltage());
}

/**
*
Expand Down Expand Up @@ -941,7 +960,7 @@ public String[] getAvailableOverclockingTiers() {
protected void setupRecipe(@NotNull Recipe recipe) {
this.progressTime = 1;
setMaxProgress(ocResult.duration());
this.recipeEUt = consumesEnergy() ? ocResult.eut() : -ocResult.eut();
this.recipeEUt = ocResult.eut();

int recipeTier = GTUtility.getTierByVoltage(recipe.getEUt());
int machineTier = getOverclockForTier(getMaximumOverclockVoltage());
Expand Down Expand Up @@ -1226,7 +1245,7 @@ public void deserializeNBT(@NotNull NBTTagCompound compound) {
if (progressTime > 0) {
this.isActive = true;
this.maxProgressTime = compound.getInteger("MaxProgress");
this.recipeEUt = compound.getLong("RecipeEUt");
this.recipeEUt = Math.abs(compound.getLong("RecipeEUt"));
NBTTagList itemOutputsList = compound.getTagList("ItemOutputs", Constants.NBT.TAG_COMPOUND);
this.itemOutputs = new ArrayList<>(itemOutputsList.tagCount());
for (int i = 0; i < itemOutputsList.tagCount(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.GTValues;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IMultiblockController;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.recipes.Recipe;
Expand Down Expand Up @@ -330,6 +331,12 @@ public long getMaxVoltage() {
return 0;
}

@Override
protected IEnergyContainer getEnergyContainer() {
GTLog.logger.error("Large Boiler called getEnergyContainer(), this should not be possible!");
return IEnergyContainer.DEFAULT;
}

/**
* @param fluidHandler the handler to drain from
* @param amount the amount to drain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,9 @@ protected long boostProduction(long production) {
}

@Override
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
long euToDraw = boostProduction(recipeEUt);
long resultEnergy = getEnergyStored() - euToDraw;
if (resultEnergy >= 0L && resultEnergy <= getEnergyCapacity()) {
if (!simulate) getEnergyContainer().changeEnergy(-euToDraw);
return true;
}
return false;
}

@Override
public long getInfoProviderEUt() {
return boostProduction(super.getInfoProviderEUt());
protected void setupRecipe(@NotNull Recipe recipe) {
super.setupRecipe(recipe);
this.recipeEUt = boostProduction(this.recipeEUt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,35 +384,11 @@ protected void performMufflerOperations() {
}
}

@Override
protected long getEnergyInputPerSecond() {
return getEnergyContainer().getInputPerSec();
}

@Override
protected long getEnergyStored() {
return getEnergyContainer().getEnergyStored();
}

@Override
protected long getEnergyCapacity() {
return getEnergyContainer().getEnergyCapacity();
}

@Override
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
long resultEnergy = getEnergyStored() - recipeEUt;
if (resultEnergy >= 0L && resultEnergy <= getEnergyCapacity()) {
if (!simulate) getEnergyContainer().changeEnergy(-recipeEUt);
return true;
} else return false;
}

@Override
public long getMaxVoltage() {
IEnergyContainer energyContainer = getEnergyContainer();
if (!consumesEnergy()) {
// Generators
// Generator Multiblocks
long voltage = energyContainer.getOutputVoltage();
long amperage = energyContainer.getOutputAmperage();
if (energyContainer instanceof EnergyContainerList && amperage == 1) {
Expand All @@ -424,7 +400,7 @@ public long getMaxVoltage() {
}
return voltage;
} else {
// Machines
// Machine Multiblocks
if (energyContainer instanceof EnergyContainerList energyList) {
long highestVoltage = energyList.getHighestInputVoltage();
if (energyList.getNumHighestInputContainers() > 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.GTValues;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.metatileentity.multiblock.RecipeMapPrimitiveMultiblockController;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.recipes.logic.OCParams;
Expand Down Expand Up @@ -33,6 +34,11 @@ protected long getEnergyCapacity() {
return Integer.MAX_VALUE;
}

@Override
protected IEnergyContainer getEnergyContainer() {
return IEnergyContainer.DEFAULT;
}

@Override
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
return true; // spoof energy being drawn
Expand Down
29 changes: 2 additions & 27 deletions src/main/java/gregtech/api/capability/impl/RecipeLogicEnergy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,8 @@ public RecipeLogicEnergy(MetaTileEntity tileEntity, RecipeMap<?> recipeMap,
}

@Override
protected long getEnergyInputPerSecond() {
return energyContainer.get().getInputPerSec();
}

@Override
protected long getEnergyStored() {
return energyContainer.get().getEnergyStored();
}

@Override
protected long getEnergyCapacity() {
return energyContainer.get().getEnergyCapacity();
}

@Override
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
long resultEnergy = getEnergyStored() - recipeEUt;
if (resultEnergy >= 0L && resultEnergy <= getEnergyCapacity()) {
if (!simulate) energyContainer.get().changeEnergy(-recipeEUt);
return true;
}
return false;
}

@Override
public long getMaxVoltage() {
return Math.max(energyContainer.get().getInputVoltage(), energyContainer.get().getOutputVoltage());
protected IEnergyContainer getEnergyContainer() {
return energyContainer.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gregtech.api.GTValues;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IVentable;
import gregtech.api.damagesources.DamageSources;
import gregtech.api.metatileentity.MetaTileEntity;
Expand Down Expand Up @@ -215,6 +216,11 @@ protected long getEnergyCapacity() {
return (long) Math.floor(steamFluidTank.getCapacity() * conversionRate);
}

@Override
protected IEnergyContainer getEnergyContainer() {
return IEnergyContainer.DEFAULT;
}

@Override
protected boolean drawEnergy(long recipeEUt, boolean simulate) {
int resultDraw = GTUtility.safeCastLongToInt((long) Math.ceil(recipeEUt / conversionRate));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.GTValues;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.metatileentity.multiblock.RecipeMapSteamMultiblockController;
import gregtech.api.recipes.Recipe;
Expand Down Expand Up @@ -47,6 +48,11 @@ public IFluidTank getSteamFluidTankCombined() {
return steamFluidTankCombined;
}

@Override
protected IEnergyContainer getEnergyContainer() {
return IEnergyContainer.DEFAULT;
}

@Override
protected IItemHandlerModifiable getInputInventory() {
RecipeMapSteamMultiblockController controller = (RecipeMapSteamMultiblockController) metaTileEntity;
Expand Down

0 comments on commit c4aab5b

Please sign in to comment.