From 118a73168c4c76548138c0b316d754a1373443f3 Mon Sep 17 00:00:00 2001 From: serenibyss <10861407+serenibyss@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:42:08 -0600 Subject: [PATCH] Fix plasma temp NPE for plasma-only materials --- .../java/gregtech/api/fluids/FluidBuilder.java | 2 +- .../gregtech/api/fluids/store/FluidStorage.java | 15 +++++++++------ .../api/fluids/store/FluidStorageKey.java | 17 +++++++++++++++++ .../api/fluids/store/FluidStorageKeys.java | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/gregtech/api/fluids/FluidBuilder.java b/src/main/java/gregtech/api/fluids/FluidBuilder.java index 757369b75ae..f7a75111cc4 100644 --- a/src/main/java/gregtech/api/fluids/FluidBuilder.java +++ b/src/main/java/gregtech/api/fluids/FluidBuilder.java @@ -428,7 +428,7 @@ private void determineTemperature(@Nullable Material material) { } case GAS -> ROOM_TEMPERATURE; case PLASMA -> { - if (material.hasFluid()) { + if (material.hasFluid() && material.getFluid() != null) { yield BASE_PLASMA_TEMPERATURE + material.getFluid().getTemperature(); } yield BASE_PLASMA_TEMPERATURE; diff --git a/src/main/java/gregtech/api/fluids/store/FluidStorage.java b/src/main/java/gregtech/api/fluids/store/FluidStorage.java index f9a232b0d27..fe7505423d0 100644 --- a/src/main/java/gregtech/api/fluids/store/FluidStorage.java +++ b/src/main/java/gregtech/api/fluids/store/FluidStorage.java @@ -11,6 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Comparator; import java.util.Map; public final class FluidStorage { @@ -69,12 +70,14 @@ public void registerFluids(@NotNull Material material) { enqueueRegistration(FluidStorageKeys.LIQUID, new FluidBuilder()); } - for (var entry : toRegister.entrySet()) { - Fluid fluid = entry.getValue().build(material.getModid(), material, entry.getKey()); - if (!storeNoOverwrites(entry.getKey(), fluid)) { - GTLog.logger.error("{} already has an associated fluid for material {}", material); - } - } + toRegister.entrySet().stream() + .sorted(Comparator.comparingInt(e -> -e.getKey().getRegistrationPriority())) + .forEach(entry -> { + Fluid fluid = entry.getValue().build(material.getModid(), material, entry.getKey()); + if (!storeNoOverwrites(entry.getKey(), fluid)) { + GTLog.logger.error("{} already has an associated fluid for material {}", material); + } + }); toRegister = null; registered = true; } diff --git a/src/main/java/gregtech/api/fluids/store/FluidStorageKey.java b/src/main/java/gregtech/api/fluids/store/FluidStorageKey.java index 01f44dcc8da..7b58514aa35 100644 --- a/src/main/java/gregtech/api/fluids/store/FluidStorageKey.java +++ b/src/main/java/gregtech/api/fluids/store/FluidStorageKey.java @@ -24,6 +24,7 @@ public final class FluidStorageKey { private final Function translationKeyFunction; private final int hashCode; private final FluidState defaultFluidState; + private final int registrationPriority; public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull MaterialIconType iconType, @NotNull UnaryOperator<@NotNull String> registryNameOperator, @@ -35,12 +36,20 @@ public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull Mate @NotNull UnaryOperator<@NotNull String> registryNameOperator, @NotNull Function<@NotNull Material, @NotNull String> translationKeyFunction, @Nullable FluidState defaultFluidState) { + this(resourceLocation, iconType, registryNameOperator, translationKeyFunction, defaultFluidState, 0); + } + + public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull MaterialIconType iconType, + @NotNull UnaryOperator<@NotNull String> registryNameOperator, + @NotNull Function<@NotNull Material, @NotNull String> translationKeyFunction, + @Nullable FluidState defaultFluidState, int registrationPriority) { this.resourceLocation = resourceLocation; this.iconType = iconType; this.registryNameOperator = registryNameOperator; this.translationKeyFunction = translationKeyFunction; this.hashCode = resourceLocation.hashCode(); this.defaultFluidState = defaultFluidState; + this.registrationPriority = registrationPriority; if (keys.containsKey(resourceLocation)) { throw new IllegalArgumentException("Cannot create duplicate keys"); } @@ -81,6 +90,14 @@ public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull Mate return defaultFluidState; } + /** + * @return The registration priority for this fluid type, determining the build order for fluids. + * Useful for when your fluid building requires some properties from previous fluids. + */ + public int getRegistrationPriority() { + return registrationPriority; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/gregtech/api/fluids/store/FluidStorageKeys.java b/src/main/java/gregtech/api/fluids/store/FluidStorageKeys.java index 10407e03b03..3b9a6be51e8 100644 --- a/src/main/java/gregtech/api/fluids/store/FluidStorageKeys.java +++ b/src/main/java/gregtech/api/fluids/store/FluidStorageKeys.java @@ -33,7 +33,7 @@ public final class FluidStorageKeys { public static final FluidStorageKey PLASMA = new FluidStorageKey(gregtechId("plasma"), MaterialIconType.plasma, s -> "plasma." + s, m -> "gregtech.fluid.plasma", - FluidState.PLASMA); + FluidState.PLASMA, -1); private FluidStorageKeys() {} }