Skip to content

Commit

Permalink
Fix Negative EUt in TOP (#2666)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude authored Feb 3, 2025
1 parent 225a53f commit 32d2c2d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 75 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,27 @@ 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 IEnergyContainer getEnergyContainer() {
return IEnergyContainer.DEFAULT;
}

/**
* Draw energy from the energy container
Expand All @@ -120,12 +131,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 +962,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 +1247,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 super.getEnergyContainer();
}

/**
* @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
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 @@ -72,7 +72,7 @@ protected boolean isDynamoTierTooLow() {
IEnergyContainer energyContainer = recipeMapWorkable.getEnergyContainer();
if (energyContainer != null && energyContainer.getEnergyCapacity() > 0) {
long maxVoltage = Math.max(energyContainer.getInputVoltage(), energyContainer.getOutputVoltage());
return maxVoltage < -recipeMapWorkable.getRecipeEUt();
return maxVoltage < recipeMapWorkable.getRecipeEUt();
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Builder addEnergyUsageExactLine(long energyUsage) {
*/
public Builder addEnergyProductionLine(long maxVoltage, long recipeEUt) {
if (!isStructureFormed) return this;
if (maxVoltage != 0 && maxVoltage >= -recipeEUt) {
if (maxVoltage != 0 && maxVoltage >= recipeEUt) {
String energyFormatted = TextFormattingUtil.formatNumbers(maxVoltage);
// wrap in text component to keep it from being formatted
ITextComponent voltageName = new TextComponentString(
Expand Down

0 comments on commit 32d2c2d

Please sign in to comment.