Skip to content

Commit

Permalink
Fix plasma mixer power consumption and overflow (GTNewHorizons#2778)
Browse files Browse the repository at this point in the history
* fix

* better fix
  • Loading branch information
HoleFish authored Jul 28, 2024
1 parent b67f335 commit f877131
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.recipe.check;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -142,11 +143,17 @@ public static CheckRecipeResult insufficientStartupPower(int required) {
return new ResultInsufficientStartupPower(required);
}

@Nonnull
public static CheckRecipeResult insufficientStartupPower(BigInteger required) {
return new ResultInsufficientStartupPowerBigInt(required);
}

static {
register(new SimpleCheckRecipeResult(false, "", false));
register(new ResultInsufficientPower(0));
register(new ResultInsufficientHeat(0));
register(new ResultInsufficientMachineTier(0));
register(new ResultInsufficientStartupPower(0));
register(new ResultInsufficientStartupPowerBigInt(BigInteger.ZERO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gregtech.api.recipe.check;

import static util.Util.toStandardForm;

import java.math.BigInteger;
import java.util.Objects;

import net.minecraft.network.PacketBuffer;
import net.minecraft.util.StatCollector;

import org.jetbrains.annotations.NotNull;

public class ResultInsufficientStartupPowerBigInt implements CheckRecipeResult {

private String required;

public ResultInsufficientStartupPowerBigInt(BigInteger required) {
this.required = toStandardForm(required);
}

@NotNull
@Override
public String getID() {
return "insufficient_startup_power_bigint";
}

@Override
public boolean wasSuccessful() {
return false;
}

@NotNull
@Override
public String getDisplayString() {
return Objects.requireNonNull(
StatCollector.translateToLocalFormatted("GT5U.gui.text.insufficient_startup_power", required));
}

@NotNull
@Override
public CheckRecipeResult newInstance() {
return new ResultInsufficientStartupPowerBigInt(BigInteger.ZERO);
}

@Override
public void encode(@NotNull PacketBuffer buffer) {
try {
buffer.writeStringToBuffer(required);
} catch (Exception ignored) {}

}

@Override
public void decode(PacketBuffer buffer) {
try {
required = buffer.readStringFromBuffer(32768);
} catch (Exception ignored) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,30 @@ public RecipeMap<?> getRecipeMap() {
protected ProcessingLogic createProcessingLogic() {
return new ProcessingLogic() {

BigInteger recipeEU;

@NotNull
@Override
protected CheckRecipeResult validateRecipe(@Nonnull GT_Recipe recipe) {
mWirelessEUt = 10L * (long) recipe.mEUt * (long) multiplier;
if (getUserEU(ownerUUID).compareTo(BigInteger.valueOf(mWirelessEUt * recipe.mDuration)) < 0) {
return CheckRecipeResultRegistry.insufficientPower(mWirelessEUt * recipe.mDuration);
BigInteger availableEU = getUserEU(ownerUUID);
recipeEU = BigInteger.valueOf(10L * recipe.mEUt * recipe.mDuration);
if (availableEU.compareTo(recipeEU) < 0) {
return CheckRecipeResultRegistry.insufficientStartupPower(recipeEU);
}
maxParallel = availableEU.divide(recipeEU)
.min(BigInteger.valueOf(maxParallel))
.intValue();
return CheckRecipeResultRegistry.SUCCESSFUL;
}

@NotNull
@Override
protected CheckRecipeResult onRecipeStart(@Nonnull GT_Recipe recipe) {
mWirelessEUt = 10L * (long) recipe.mEUt * (long) multiplier;
BigInteger finalConsumption = recipeEU.multiply(BigInteger.valueOf(-calculatedParallels));
// This will void the inputs if wireless energy has dropped
// below the required amount between validateRecipe and here.
if (!addEUToGlobalEnergyMap(ownerUUID, -mWirelessEUt * recipe.mDuration)) {
return CheckRecipeResultRegistry.insufficientPower(mWirelessEUt * recipe.mDuration);
if (!addEUToGlobalEnergyMap(ownerUUID, finalConsumption)) {
return CheckRecipeResultRegistry.insufficientStartupPower(finalConsumption);
}
// Energy consumed all at once from wireless net.
setCalculatedEut(0);
Expand Down

0 comments on commit f877131

Please sign in to comment.