Skip to content

Commit

Permalink
Fix liquid and gas registry key functions overlapping (#2514)
Browse files Browse the repository at this point in the history
Co-authored-by: TechLord22 <[email protected]>
(cherry picked from commit 375af9e)
  • Loading branch information
bruberu authored and ALongStringOfNumbers committed Jul 6, 2024
1 parent cdf0e8b commit ec6b70e
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/fluids/FluidBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private static int convertViscosity(double viscosity) {
private void determineName(@Nullable Material material, @Nullable FluidStorageKey key) {
if (name != null) return;
if (material == null || key == null) throw new IllegalArgumentException("Fluid must have a name");
name = key.getRegistryNameFor(material.getName());
name = key.getRegistryNameFor(material);
}

private void determineTextures(@Nullable Material material, @Nullable FluidStorageKey key, @NotNull String modid) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/fluids/GTFluidRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void register() {
for (Material material : GregTechAPI.materialManager.getRegisteredMaterials()) {
FluidProperty property = material.getProperty(PropertyKey.FLUID);
if (property != null) {
property.getStorage().registerFluids(material);
property.registerFluids(material);
}
}
}
Expand Down
90 changes: 7 additions & 83 deletions src/main/java/gregtech/api/fluids/store/FluidStorage.java
Original file line number Diff line number Diff line change
@@ -1,117 +1,41 @@
package gregtech.api.fluids.store;

import gregtech.api.fluids.FluidBuilder;
import gregtech.api.unification.material.Material;
import gregtech.api.util.GTLog;

import net.minecraftforge.fluids.Fluid;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.Map;

public final class FluidStorage {

private final Map<FluidStorageKey, Fluid> map = new Object2ObjectOpenHashMap<>();
private Map<FluidStorageKey, FluidBuilder> toRegister = new Object2ObjectOpenHashMap<>();

private boolean registered = false;

public FluidStorage() {}
public interface FluidStorage {

/**
* Enqueue a fluid for registration
*
* @param key the key corresponding with the fluid
* @param builder the FluidBuilder to build
*/
public void enqueueRegistration(@NotNull FluidStorageKey key, @NotNull FluidBuilder builder) {
if (registered) {
throw new IllegalStateException("Cannot enqueue a builder after registration");
}

if (toRegister.containsKey(key)) {
throw new IllegalArgumentException("FluidStorageKey " + key + " is already queued");
}
toRegister.put(key, builder);
}
void enqueueRegistration(@NotNull FluidStorageKey key, @NotNull FluidBuilder builder);

/**
* @param key the key corresponding with the FluidBuilder
* @return the fluid builder queued to be registered
*/
public @Nullable FluidBuilder getQueuedBuilder(@NotNull FluidStorageKey key) {
if (registered) {
throw new IllegalArgumentException("FluidStorage has already been registered");
}
return toRegister.get(key);
}

/**
* Register the enqueued fluids
*
* @param material the material the fluid is based off of
*/
@ApiStatus.Internal
public void registerFluids(@NotNull Material material) {
if (registered) {
throw new IllegalStateException("FluidStorage has already been registered");
}

// If nothing is queued for registration and nothing is manually stored,
// we need something for the registry to handle this will prevent cases
// of a material having a fluid property but no fluids actually created
// for the material.
if (toRegister.isEmpty() && map.isEmpty()) {
enqueueRegistration(FluidStorageKeys.LIQUID, new FluidBuilder());
}

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;
}
@Nullable
FluidBuilder getQueuedBuilder(@NotNull FluidStorageKey key);

/**
* @param key the key corresponding with the fluid
* @return the fluid associated with the key
*/
public @Nullable Fluid get(@NotNull FluidStorageKey key) {
return map.get(key);
}

/**
* Will do nothing if an existing fluid association would be overwritten.
*
* @param key the key to associate with the fluid
* @param fluid the fluid to associate with the key
* @return if the associations were successfully updated
*/
public boolean storeNoOverwrites(@NotNull FluidStorageKey key, @NotNull Fluid fluid) {
if (map.containsKey(key)) {
return false;
}
store(key, fluid);
return true;
}
@Nullable
Fluid get(@NotNull FluidStorageKey key);

/**
* Will overwrite existing fluid associations.
*
* @param key the key to associate with the fluid
* @param fluid the fluid to associate with the key
*/
public void store(@NotNull FluidStorageKey key, @NotNull Fluid fluid) {
map.put(key, fluid);
}
void store(@NotNull FluidStorageKey key, @NotNull Fluid fluid);
}
101 changes: 101 additions & 0 deletions src/main/java/gregtech/api/fluids/store/FluidStorageImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package gregtech.api.fluids.store;

import gregtech.api.fluids.FluidBuilder;
import gregtech.api.unification.material.Material;
import gregtech.api.util.GTLog;

import net.minecraftforge.fluids.Fluid;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.Map;

public final class FluidStorageImpl implements FluidStorage {

private final Map<FluidStorageKey, Fluid> map = new Object2ObjectOpenHashMap<>();
private Map<FluidStorageKey, FluidBuilder> toRegister = new Object2ObjectOpenHashMap<>();

private boolean registered = false;

public FluidStorageImpl() {}

@Override
public void enqueueRegistration(@NotNull FluidStorageKey key, @NotNull FluidBuilder builder) {
if (registered) {
throw new IllegalStateException("Cannot enqueue a builder after registration");
}

if (toRegister.containsKey(key)) {
throw new IllegalArgumentException("FluidStorageKey " + key + " is already queued");
}
toRegister.put(key, builder);
}

@Override
public @Nullable FluidBuilder getQueuedBuilder(@NotNull FluidStorageKey key) {
if (registered) {
throw new IllegalArgumentException("FluidStorageImpl has already been registered");
}
return toRegister.get(key);
}

/**
* Register the enqueued fluids
*
* @param material the material the fluid is based off of
*/
@ApiStatus.Internal
public void registerFluids(@NotNull Material material) {
if (registered) {
throw new IllegalStateException("FluidStorageImpl has already been registered");
}

// If nothing is queued for registration and nothing is manually stored,
// we need something for the registry to handle this will prevent cases
// of a material having a fluid property but no fluids actually created
// for the material.
if (toRegister.isEmpty() && map.isEmpty()) {
enqueueRegistration(FluidStorageKeys.LIQUID, new FluidBuilder());
}

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;
}

@Override
public @Nullable Fluid get(@NotNull FluidStorageKey key) {
return map.get(key);
}

/**
* Will do nothing if an existing fluid association would be overwritten.
*
* @param key the key to associate with the fluid
* @param fluid the fluid to associate with the key
* @return if the associations were successfully updated
*/
private boolean storeNoOverwrites(@NotNull FluidStorageKey key, @NotNull Fluid fluid) {
if (map.containsKey(key)) {
return false;
}
store(key, fluid);
return true;
}

@Override
public void store(@NotNull FluidStorageKey key, @NotNull Fluid fluid) {
map.put(key, fluid);
}
}
19 changes: 9 additions & 10 deletions src/main/java/gregtech/api/fluids/store/FluidStorageKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,39 @@

import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;

public final class FluidStorageKey {

private static final Map<ResourceLocation, FluidStorageKey> keys = new Object2ObjectOpenHashMap<>();

private final ResourceLocation resourceLocation;
private final MaterialIconType iconType;
private final UnaryOperator<String> registryNameOperator;
private final Function<Material, String> registryNameFunction;
private final Function<Material, String> 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,
@NotNull Function<@NotNull Material, @NotNull String> registryNameFunction,
@NotNull Function<@NotNull Material, @NotNull String> translationKeyFunction) {
this(resourceLocation, iconType, registryNameOperator, translationKeyFunction, null);
this(resourceLocation, iconType, registryNameFunction, translationKeyFunction, null);
}

public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull MaterialIconType iconType,
@NotNull UnaryOperator<@NotNull String> registryNameOperator,
@NotNull Function<@NotNull Material, @NotNull String> registryNameFunction,
@NotNull Function<@NotNull Material, @NotNull String> translationKeyFunction,
@Nullable FluidState defaultFluidState) {
this(resourceLocation, iconType, registryNameOperator, translationKeyFunction, defaultFluidState, 0);
this(resourceLocation, iconType, registryNameFunction, translationKeyFunction, defaultFluidState, 0);
}

public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull MaterialIconType iconType,
@NotNull UnaryOperator<@NotNull String> registryNameOperator,
@NotNull Function<@NotNull Material, @NotNull String> registryNameFunction,
@NotNull Function<@NotNull Material, @NotNull String> translationKeyFunction,
@Nullable FluidState defaultFluidState, int registrationPriority) {
this.resourceLocation = resourceLocation;
this.iconType = iconType;
this.registryNameOperator = registryNameOperator;
this.registryNameFunction = registryNameFunction;
this.translationKeyFunction = translationKeyFunction;
this.hashCode = resourceLocation.hashCode();
this.defaultFluidState = defaultFluidState;
Expand All @@ -72,8 +71,8 @@ public FluidStorageKey(@NotNull ResourceLocation resourceLocation, @NotNull Mate
* @param baseName the base name of the fluid
* @return the registry name to use
*/
public @NotNull String getRegistryNameFor(@NotNull String baseName) {
return registryNameOperator.apply(baseName);
public @NotNull String getRegistryNameFor(@NotNull Material baseName) {
return registryNameFunction.apply(baseName);
}

/**
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/gregtech/api/fluids/store/FluidStorageKeys.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package gregtech.api.fluids.store;

import gregtech.api.fluids.FluidState;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.info.MaterialIconType;
import gregtech.api.unification.material.properties.FluidProperty;
import gregtech.api.unification.material.properties.PropertyKey;

import java.util.function.UnaryOperator;
import org.jetbrains.annotations.NotNull;

import static gregtech.api.util.GTUtility.gregtechId;

public final class FluidStorageKeys {

public static final FluidStorageKey LIQUID = new FluidStorageKey(gregtechId("liquid"),
MaterialIconType.liquid,
UnaryOperator.identity(),
m -> prefixedRegistryName("liquid.", FluidStorageKeys.LIQUID, m),
m -> m.hasProperty(PropertyKey.DUST) ? "gregtech.fluid.liquid_generic" : "gregtech.fluid.generic",
FluidState.LIQUID);

public static final FluidStorageKey GAS = new FluidStorageKey(gregtechId("gas"),
MaterialIconType.gas,
UnaryOperator.identity(),
m -> prefixedRegistryName("gas.", FluidStorageKeys.GAS, m),
m -> {
if (m.hasProperty(PropertyKey.DUST)) {
return "gregtech.fluid.gas_vapor";
}
if (m.isElement()) {

FluidProperty property = m.getProperty(PropertyKey.FLUID);
if (m.isElement() || (property != null && property.getPrimaryKey() != FluidStorageKeys.LIQUID)) {
return "gregtech.fluid.gas_generic";
}
return "gregtech.fluid.generic";
Expand All @@ -32,8 +36,24 @@ public final class FluidStorageKeys {

public static final FluidStorageKey PLASMA = new FluidStorageKey(gregtechId("plasma"),
MaterialIconType.plasma,
s -> "plasma." + s, m -> "gregtech.fluid.plasma",
m -> "plasma." + m.getName(),
m -> "gregtech.fluid.plasma",
FluidState.PLASMA, -1);

private FluidStorageKeys() {}

/**
* @param prefix the prefix string for the registry name
* @param key the key which does not require the prefix
* @param material the material to create a registry name for
* @return the registry name
*/
private static @NotNull String prefixedRegistryName(@NotNull String prefix, @NotNull FluidStorageKey key,
@NotNull Material material) {
FluidProperty property = material.getProperty(PropertyKey.FLUID);
if (property != null && property.getPrimaryKey() != key) {
return prefix + material.getName();
}
return material.getName();
}
}
Loading

0 comments on commit ec6b70e

Please sign in to comment.