diff options
author | Léa Gris <lea.gris@noiraude.net> | 2022-10-01 11:01:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-01 11:01:53 +0200 |
commit | 51a41123b0ccdf10cb7b311f8d87d250f78d1b89 (patch) | |
tree | 92b545c7768232ee09c89e39281aebff30c470f2 | |
parent | ad7f7b95550d8a27cc5cddc8c419898d8d836713 (diff) | |
download | GT5-Unofficial-51a41123b0ccdf10cb7b311f8d87d250f78d1b89.tar.gz GT5-Unofficial-51a41123b0ccdf10cb7b311f8d87d250f78d1b89.tar.bz2 GT5-Unofficial-51a41123b0ccdf10cb7b311f8d87d250f78d1b89.zip |
refactor(fluid_api): fluent interface (#1407)
* refactor(fluid_api): fluent interface
Improves the initial construction model into a fluent interface.
See: https://java-design-patterns.com/patterns/fluentinterface/
This change provides the built and saved states of a GT_Fluid,
with their own interface, so that: object state validations are
performed at build time, rather than causing an `IllegalStateException`
to be thrown at runtime, with the previous implementation.
This also allows the IDE to display and check the applicable methods
for the GT_Fluid object's state, as it moves through the call chain.
* hotfix off-by-one in FluidState.fromValue
* minor: deduplicate buildAndRegister action
* fix(withIconsFrom): needs dependency management
Cracked fluid Icons were copied too early from non-cracked fluid
within the `IGT_FluidBuilder`'s implementation.
At this stage, the source Fluid has not registered its own icons yet,
so the Cracked fluid got null Icons (fallback to Error checkerboard).
This commit delegates the copy of the source fluid's Icons, to the
`run` Icons texture's registration task; ensuring the source Fluid
runs its own Icons textures registration before copying them,
as a light-weight dependency management.
9 files changed, 254 insertions, 297 deletions
diff --git a/src/main/java/gregtech/api/enums/FluidState.java b/src/main/java/gregtech/api/enums/FluidState.java index ad27047a1c..a6e81ab43d 100644 --- a/src/main/java/gregtech/api/enums/FluidState.java +++ b/src/main/java/gregtech/api/enums/FluidState.java @@ -6,5 +6,12 @@ public enum FluidState { MOLTEN, PLASMA, SLURRY; - public static final FluidState[] VALUES = new FluidState[] {SLURRY, LIQUID, GAS, PLASMA, MOLTEN}; + + public static final FluidState[] VALID_STATES = new FluidState[] {SLURRY, LIQUID, GAS, PLASMA, MOLTEN}; + + public static FluidState fromValue(int stateValue) { + return stateValue >= 0 && stateValue < FluidState.VALID_STATES.length + ? FluidState.VALID_STATES[stateValue] + : FluidState.LIQUID; + } } diff --git a/src/main/java/gregtech/api/fluid/GT_FluidFactory.java b/src/main/java/gregtech/api/fluid/GT_FluidFactory.java index 4aa39095ec..1622aa2e05 100644 --- a/src/main/java/gregtech/api/fluid/GT_FluidFactory.java +++ b/src/main/java/gregtech/api/fluid/GT_FluidFactory.java @@ -39,9 +39,9 @@ public class GT_FluidFactory { * @param material The {@link Materials} of this {@link IGT_Fluid} * @param state The {@link FluidState} of this {@link IGT_Fluid} * @param temperature The fluid temperature in Kelvin - * @return the registered {@link IGT_Fluid} + * @return the registered {@link Fluid} */ - public static IGT_Fluid of( + public static Fluid of( final String fluidName, final String localizedName, final Materials material, @@ -51,7 +51,8 @@ public class GT_FluidFactory { .withLocalizedName(localizedName) .withStateAndTemperature(state, temperature) .buildAndRegister() - .configureMaterials(material); + .configureMaterials(material) + .asFluid(); } /** @@ -60,14 +61,15 @@ public class GT_FluidFactory { * @param localizedName The localized name of this {@link IGT_Fluid} * @param state The {@link FluidState} of this {@link IGT_Fluid} * @param temperature The fluid temperature in Kelvin - * @return the registered {@link IGT_Fluid} + * @return the registered {@link Fluid} */ - public static IGT_Fluid of( + public static Fluid of( final String fluidName, final String localizedName, final FluidState state, final int temperature) { return builder(fluidName) .withLocalizedName(localizedName) .withStateAndTemperature(state, temperature) - .buildAndRegister(); + .buildAndRegister() + .asFluid(); } /** diff --git a/src/main/java/gregtech/api/interfaces/fluid/IGT_Fluid.java b/src/main/java/gregtech/api/interfaces/fluid/IGT_Fluid.java index 5529e111bb..7c8b2b3f11 100644 --- a/src/main/java/gregtech/api/interfaces/fluid/IGT_Fluid.java +++ b/src/main/java/gregtech/api/interfaces/fluid/IGT_Fluid.java @@ -1,11 +1,5 @@ package gregtech.api.interfaces.fluid; -import gregtech.api.enums.FluidState; -import gregtech.api.enums.Materials; -import net.minecraft.item.ItemStack; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidRegistry; @SuppressWarnings("unused") // API might legitimately expose unused methods within this local project's scope @@ -14,68 +8,7 @@ public interface IGT_Fluid { /** * Adds this {@link IGT_Fluid} to the {@link FluidRegistry} and internally-implemented registrations * - * @return {@link IGT_Fluid} self for call chaining + * @return {@link IGT_RegisteredFluid} The GregTech registered fluid */ - IGT_Fluid addFluid(); - - /** - * Registers the containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} - * - * @param fullContainer The full fluid container - * @param emptyContainer The empty fluid container - * @param containerSize The size of the container - * @return The {@link IGT_Fluid} for chaining - * - * @throws IllegalStateException on attempt to register containers for an unregistered fluid - */ - IGT_Fluid registerContainers( - final ItemStack fullContainer, final ItemStack emptyContainer, final int containerSize); - - /** - * Registers the bucket-sized 1000L containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} - * - * @param fullContainer The full container to associate with this {@link IGT_Fluid} - * @param emptyContainer The empty container associate with this {@link IGT_Fluid} - * @return {@link IGT_Fluid} self for call chaining - * - * @throws IllegalStateException on attempt to register containers for an unregistered fluid - */ - IGT_Fluid registerBContainers(final ItemStack fullContainer, final ItemStack emptyContainer); - - /** - * Registers the potion-sized 250L containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} - * - * @param fullContainer The full container to associate with this {@link IGT_Fluid} - * @param emptyContainer The empty container associate with this {@link IGT_Fluid} - * @return {@link IGT_Fluid} self for call chaining - * - * @throws IllegalStateException on attempt to register containers for an unregistered fluid - */ - IGT_Fluid registerPContainers(final ItemStack fullContainer, final ItemStack emptyContainer); - - /** - * Updates the {@link Materials}'s fluids from this {@link IGT_Fluid}'s state - * - * @param material the {@link Materials} to configure based on this {@link IGT_Fluid} and {@link FluidState} - * @return The {@link IGT_Fluid} for chaining - * - * @throws IllegalStateException on unknown {@link FluidState} - * @throws IllegalStateException on attempt to register containers for an unregistered fluid - */ - IGT_Fluid configureMaterials(final Materials material); - - /** - * @return this {@link IGT_Fluid} cast to {@link Fluid} - */ - Fluid asFluid(); - - /** - * @return the {@link ResourceLocation} of the still fluid texture - */ - ResourceLocation getStillIconResourceLocation(); - - /** - * @return the {@link ResourceLocation} of the flowing fluid texture - */ - ResourceLocation getFlowingIconResourceLocation(); + IGT_RegisteredFluid addFluid(); } diff --git a/src/main/java/gregtech/api/interfaces/fluid/IGT_FluidBuilder.java b/src/main/java/gregtech/api/interfaces/fluid/IGT_FluidBuilder.java index 4010a465ce..a643b8aace 100644 --- a/src/main/java/gregtech/api/interfaces/fluid/IGT_FluidBuilder.java +++ b/src/main/java/gregtech/api/interfaces/fluid/IGT_FluidBuilder.java @@ -1,6 +1,7 @@ package gregtech.api.interfaces.fluid; import gregtech.api.enums.FluidState; +import javax.annotation.Nonnull; import net.minecraft.block.Block; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fluids.Fluid; @@ -12,12 +13,14 @@ public interface IGT_FluidBuilder { * @param colorRGBA The {@code short[]} RGBA color of the {@link Fluid} or {@code null} for no defined RGBA color * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withColorRGBA(final short[] colorRGBA); /** * @param localizedName The localized name of this {@link IGT_FluidBuilder} * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withLocalizedName(final String localizedName); /** @@ -25,43 +28,50 @@ public interface IGT_FluidBuilder { * @param temperature The Kelvin temperature of this {@link IGT_FluidBuilder} * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withStateAndTemperature(final FluidState fluidState, final int temperature); /** * @param stillIconResourceLocation the {@link ResourceLocation} of the still fluid icon * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withStillIconResourceLocation(final ResourceLocation stillIconResourceLocation); /** * @param flowingIconResourceLocation the {@link ResourceLocation} of the flowing fluid icon * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withFlowingIconResourceLocation(final ResourceLocation flowingIconResourceLocation); /** * @param textureName The name of the GregTech mod texture of this {@link IGT_FluidBuilder} * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withTextureName(final String textureName); /** - * @param fromGTFluid the {@link IGT_Fluid} to copy the texture from + * @param fluidBlock the {@link Block} implementation of the {@link IGT_Fluid} * @return {@link IGT_FluidBuilder} self for call chaining */ - IGT_FluidBuilder withTextureFrom(final IGT_Fluid fromGTFluid); + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_FluidBuilder withFluidBlock(final Block fluidBlock); /** - * @param fluidBlock the {@link Block} implementation of the {@link IGT_Fluid} + * @param fromFluid the {@link Fluid} to copy the icons from * @return {@link IGT_FluidBuilder} self for call chaining */ - IGT_FluidBuilder withFluidBlock(final Block fluidBlock); + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_FluidBuilder withIconsFrom(@Nonnull final Fluid fromFluid); /** * @param stillIconResourceLocation The {@link ResourceLocation} of the still fluid texture * @param flowingIconResourceLocation The {@link ResourceLocation} of the flowing fluid texture * @return {@link IGT_FluidBuilder} self for call chaining */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value IGT_FluidBuilder withTextures( final ResourceLocation stillIconResourceLocation, final ResourceLocation flowingIconResourceLocation); @@ -79,5 +89,5 @@ public interface IGT_FluidBuilder { * @see #build() * @see IGT_Fluid#addFluid() */ - IGT_Fluid buildAndRegister(); + IGT_RegisteredFluid buildAndRegister(); } diff --git a/src/main/java/gregtech/api/interfaces/fluid/IGT_RegisteredFluid.java b/src/main/java/gregtech/api/interfaces/fluid/IGT_RegisteredFluid.java new file mode 100644 index 0000000000..1d8cd2384f --- /dev/null +++ b/src/main/java/gregtech/api/interfaces/fluid/IGT_RegisteredFluid.java @@ -0,0 +1,56 @@ +package gregtech.api.interfaces.fluid; + +import gregtech.api.enums.FluidState; +import gregtech.api.enums.Materials; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidContainerRegistry; + +public interface IGT_RegisteredFluid { + + /** + * Registers the containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} + * + * @param fullContainer The full fluid container + * @param emptyContainer The empty fluid container + * @param containerSize The size of the container + * @return The {@link IGT_RegisteredFluid} for call chaining + */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_RegisteredFluid registerContainers( + final ItemStack fullContainer, final ItemStack emptyContainer, final int containerSize); + + /** + * Registers the bucket-sized 1000L containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} + * + * @param fullContainer The full container to associate with this {@link IGT_Fluid} + * @param emptyContainer The empty container associate with this {@link IGT_Fluid} + * @return {@link IGT_RegisteredFluid} for call chaining + */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_RegisteredFluid registerBContainers(final ItemStack fullContainer, final ItemStack emptyContainer); + + /** + * Registers the potion-sized 250L containers in the {@link FluidContainerRegistry} for this {@link IGT_Fluid} + * + * @param fullContainer The full container to associate with this {@link IGT_Fluid} + * @param emptyContainer The empty container associate with this {@link IGT_Fluid} + * @return {@link IGT_RegisteredFluid} self for call chaining + */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_RegisteredFluid registerPContainers(final ItemStack fullContainer, final ItemStack emptyContainer); + + /** + * Updates the {@link Materials}'s fluids from this {@link IGT_Fluid}'s state + * + * @param material the {@link Materials} to configure based on this {@link IGT_Fluid} and {@link FluidState} + * @return The {@link IGT_RegisteredFluid} for call chaining + */ + @SuppressWarnings("UnusedReturnValue") // Last call in chain, may not use this returned value + IGT_RegisteredFluid configureMaterials(final Materials material); + + /** + * @return this {@link IGT_Fluid} cast to {@link Fluid} + */ + Fluid asFluid(); +} diff --git a/src/main/java/gregtech/common/GT_Proxy.java b/src/main/java/gregtech/common/GT_Proxy.java index 1f0eda5034..3bc5557fac 100644 --- a/src/main/java/gregtech/common/GT_Proxy.java +++ b/src/main/java/gregtech/common/GT_Proxy.java @@ -5,6 +5,7 @@ import static gregtech.api.enums.FluidState.GAS; import static gregtech.api.enums.FluidState.LIQUID; import static gregtech.api.enums.FluidState.MOLTEN; import static gregtech.api.enums.FluidState.PLASMA; +import static gregtech.api.enums.GT_Values.MOD_ID; import static gregtech.api.enums.GT_Values.MOD_ID_RC; import static gregtech.api.enums.GT_Values.MOD_ID_TC; import static gregtech.api.enums.GT_Values.MOD_ID_TE; @@ -43,7 +44,6 @@ import gregtech.api.fluid.GT_FluidFactory; import gregtech.api.interfaces.IBlockOnWalkOver; import gregtech.api.interfaces.IGlobalWirelessEnergy; import gregtech.api.interfaces.IProjectileItem; -import gregtech.api.interfaces.fluid.IGT_Fluid; import gregtech.api.interfaces.internal.IGT_Mod; import gregtech.api.interfaces.internal.IThaumcraftCompat; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; @@ -51,7 +51,6 @@ import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.items.GT_MetaGenerated_Item; import gregtech.api.items.GT_MetaGenerated_Tool; import gregtech.api.objects.GT_ChunkManager; -import gregtech.api.objects.GT_Fluid; import gregtech.api.objects.GT_FluidStack; import gregtech.api.objects.GT_ItemStack; import gregtech.api.objects.GT_UO_DimensionList; @@ -119,6 +118,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.MathHelper; +import net.minecraft.util.ResourceLocation; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; import net.minecraft.world.WorldSettings.GameType; @@ -138,7 +138,6 @@ import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.event.world.ChunkDataEvent; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidContainerRegistry; -import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.RecipeSorter; @@ -2552,7 +2551,7 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG } else return; for (int i = 0; i < 3; i++) { crackedFluids[i] = GT_FluidFactory.builder(namePrefixes[i] + aMaterial.mName.toLowerCase(Locale.ENGLISH)) - .withTextureFrom((IGT_Fluid) uncrackedFluid) + .withIconsFrom(uncrackedFluid) .withLocalizedName(orePrefixes[i].mLocalizedMaterialPre + aMaterial.mDefaultLocalName) .withColorRGBA(aMaterial.mRGBa) .withStateAndTemperature(GAS, 775) @@ -2603,7 +2602,7 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG } else return; for (int i = 0; i < 3; i++) { crackedFluids[i] = GT_FluidFactory.builder(namePrefixes[i] + aMaterial.mName.toLowerCase(Locale.ENGLISH)) - .withTextureFrom((IGT_Fluid) uncrackedFluid) + .withIconsFrom(uncrackedFluid) .withLocalizedName(orePrefixes[i].mLocalizedMaterialPre + aMaterial.mDefaultLocalName) .withColorRGBA(aMaterial.mRGBa) .withStateAndTemperature(GAS, 775) @@ -2640,19 +2639,19 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG } /** - * @deprecated use {@link IGT_Fluid#addFluid} + * @deprecated use {@link GT_FluidFactory#builder} * @see GT_FluidFactory#of(String, String, Materials, FluidState, int) * @see GT_FluidFactory#of(String, String, FluidState, int) */ @Deprecated public Fluid addFluid(String aName, String aLocalized, Materials aMaterial, int aState, int aTemperatureK) { - return addFluid(aName, aLocalized, aMaterial, aState, aTemperatureK, null, null, 0); + return GT_FluidFactory.of(aName, aLocalized, aMaterial, FluidState.VALID_STATES[aState], aTemperatureK); } /** - * @deprecated use {@link IGT_Fluid#addFluid} - * @see GT_FluidFactory#builder + * @deprecated use {@link GT_FluidFactory#builder} */ + @SuppressWarnings({"MethodWithTooManyParameters"}) // Deprecated method @Deprecated public Fluid addFluid( String aName, @@ -2663,24 +2662,20 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG ItemStack aFullContainer, ItemStack aEmptyContainer, int aFluidAmount) { - return addFluid( - aName, - aName.toLowerCase(Locale.ENGLISH), - aLocalized, - aMaterial, - null, - aState, - aTemperatureK, - aFullContainer, - aEmptyContainer, - aFluidAmount); + return GT_FluidFactory.builder(aName) + .withLocalizedName(aLocalized) + .withStateAndTemperature(FluidState.fromValue(aState), aTemperatureK) + .buildAndRegister() + .configureMaterials(aMaterial) + .registerContainers(aFullContainer, aEmptyContainer, aFluidAmount) + .asFluid(); } /** - * @deprecated use {@link IGT_Fluid#addFluid} - * @see GT_FluidFactory#builder + * @deprecated use {@link GT_FluidFactory#builder} */ @Deprecated + @SuppressWarnings({"MethodWithTooManyParameters"}) // Deprecated method public Fluid addFluid( String aName, String aTexture, @@ -2692,67 +2687,15 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG ItemStack aFullContainer, ItemStack aEmptyContainer, int aFluidAmount) { - aName = aName.toLowerCase(Locale.ENGLISH); - - Fluid rFluid = new GT_Fluid(aName, aTexture, aRGBa != null ? aRGBa : Dyes._NULL.getRGBA()); - GT_LanguageManager.addStringLocalization(rFluid.getUnlocalizedName(), aLocalized == null ? aName : aLocalized); - if (FluidRegistry.registerFluid(rFluid)) { - switch (aState) { - case 0: - rFluid.setGaseous(false); - rFluid.setViscosity(10000); - break; - case 1: - case 4: - rFluid.setGaseous(false); - rFluid.setViscosity(1000); - break; - case 2: - rFluid.setGaseous(true); - rFluid.setDensity(-100); - rFluid.setViscosity(200); - break; - case 3: - rFluid.setGaseous(true); - rFluid.setDensity(55536); - rFluid.setViscosity(10); - rFluid.setLuminosity(15); - } - } else { - rFluid = FluidRegistry.getFluid(aName); - } - if (rFluid.getTemperature() == new Fluid("test").getTemperature()) { - rFluid.setTemperature(aTemperatureK); - } - if (aMaterial != null) { - switch (aState) { - case 0: - aMaterial.mSolid = rFluid; - break; - case 1: - aMaterial.mFluid = rFluid; - break; - case 2: - aMaterial.mGas = rFluid; - break; - case 3: - aMaterial.mPlasma = rFluid; - break; - case 4: - aMaterial.mStandardMoltenFluid = rFluid; - } - } - if ((aFullContainer != null) - && (aEmptyContainer != null) - && (!FluidContainerRegistry.registerFluidContainer( - new FluidStack(rFluid, aFluidAmount), aFullContainer, aEmptyContainer))) { - GT_Values.RA.addFluidCannerRecipe( - aFullContainer, - GT_Utility.getContainerItem(aFullContainer, false), - null, - new FluidStack(rFluid, aFluidAmount)); - } - return rFluid; + return GT_FluidFactory.builder(aName) + .withLocalizedName(aLocalized) + .withStillIconResourceLocation(new ResourceLocation(MOD_ID, "fluids/fluid." + aTexture)) + .withColorRGBA(aRGBa) + .withStateAndTemperature(FluidState.fromValue(aState), aTemperatureK) + .buildAndRegister() + .configureMaterials(aMaterial) + .registerContainers(aFullContainer, aEmptyContainer, aFluidAmount) + .asFluid(); } public File getSaveDirectory() { diff --git a/src/main/java/gregtech/common/fluid/GT_Fluid.java b/src/main/java/gregtech/common/fluid/GT_Fluid.java index b8bca3116a..e768358503 100644 --- a/src/main/java/gregtech/common/fluid/GT_Fluid.java +++ b/src/main/java/gregtech/common/fluid/GT_Fluid.java @@ -5,34 +5,38 @@ import gregtech.api.enums.FluidState; import gregtech.api.enums.GT_Values; import gregtech.api.enums.Materials; import gregtech.api.interfaces.fluid.IGT_Fluid; +import gregtech.api.interfaces.fluid.IGT_RegisteredFluid; import gregtech.api.util.GT_LanguageManager; import gregtech.api.util.GT_Utility; +import javax.annotation.Nonnull; import net.minecraft.item.ItemStack; -import net.minecraft.util.IIcon; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; -public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { +public class GT_Fluid extends Fluid implements IGT_Fluid, IGT_RegisteredFluid, Runnable { private final String localizedName; private final ResourceLocation stillIconResourceLocation; private final ResourceLocation flowingIconResourceLocation; private final short[] colorRGBA; private final FluidState fluidState; + private final Fluid iconsFrom; private Fluid registeredFluid; + private boolean hasRun = false; /** * Constructs this {@link IGT_Fluid} implementation from an {@link GT_FluidBuilder} instance * * @param builder The {@link GT_FluidBuilder} instance to construct this {@link IGT_Fluid} implementation */ - protected GT_Fluid(final GT_FluidBuilder builder) { + protected GT_Fluid(@Nonnull final GT_FluidBuilder builder) { super(builder.fluidName); this.localizedName = builder.localizedName; this.stillIconResourceLocation = builder.stillIconResourceLocation; this.flowingIconResourceLocation = builder.flowingIconResourceLocation; + this.iconsFrom = builder.iconsFrom; this.block = builder.fluidBlock; this.colorRGBA = builder.colorRGBA; this.fluidState = builder.fluidState; @@ -41,6 +45,37 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { } /** + * Adjusts this {@link Fluid}'s settings based on this {@link IGT_Fluid}'s state + */ + protected void configureFromStateTemperature() { + switch (fluidState) { + case SLURRY: + setGaseous(false).setViscosity(10000); + break; + case GAS: + setGaseous(true).setDensity(-100).setViscosity(200); + break; + case PLASMA: + setGaseous(true).setDensity(55536).setViscosity(10).setLuminosity(15); + break; + case MOLTEN: + final int luminosity; + if (temperature >= 3500) { + luminosity = 15; + } else { + luminosity = temperature < 1000 ? 0 : 14 * (temperature - 1000) / 2500 + 1; + } + setLuminosity(luminosity); + case LIQUID: + default: + setGaseous(false).setViscosity(1000); + break; + } + } + + // ----- Fluid implementations ----- + + /** * @inheritDoc */ @Override @@ -50,61 +85,41 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { | Math.max(0, Math.min(255, colorRGBA[2])); } - /** - * This {@link Runnable#run()} implementation is scheduled within the {@link GregTech_API#sGTBlockIconload} - * to load this {@link IGT_Fluid}'s texture icons. - * - * @see Runnable#run() - */ - @Override - public void run() { - final IIcon stillIcon = GregTech_API.sBlockIcons.registerIcon(stillIconResourceLocation.toString()); - if (flowingIconResourceLocation == null) { - setIcons(stillIcon); - } else { - final IIcon flowingIcon = GregTech_API.sBlockIcons.registerIcon(flowingIconResourceLocation.toString()); - setIcons(stillIcon, flowingIcon); - } - } + // ----- IGT_Fluid interface implementations ----- - /** - * @inheritDoc - */ - @Override - public IGT_Fluid addFluid() { - - if (FluidRegistry.registerFluid(this)) { + public IGT_RegisteredFluid addFluid() { + if (FluidRegistry.registerFluid(GT_Fluid.this)) { // Registered as a new Fluid - // Adds self as Runnable to the block icons loader run() tasks + registeredFluid = this; + // Schedules the gtFluid for the block icons loader run() tasks GregTech_API.sGTBlockIconload.add(this); // Adds a server-side localized-name - GT_LanguageManager.addStringLocalization(this.getUnlocalizedName(), localizedName); - registeredFluid = this; + GT_LanguageManager.addStringLocalization(getUnlocalizedName(), localizedName); } else { - // Promotes Fluid from the registry to enable GT_Fluid methods - registeredFluid = FluidRegistry.getFluid(fluidName); + // Fluid already registered, get it from the registry + registeredFluid = FluidRegistry.getFluid(GT_Fluid.this.fluidName); // Sets temperature of already registered fluids if they use the default (temperature = 300) if (registeredFluid.getTemperature() == new Fluid("test").getTemperature()) { - registeredFluid.setTemperature(temperature); + registeredFluid.setTemperature(GT_Fluid.this.temperature); } } return this; } + // ----- IGT_RegisteredFluid interface implementations ----- + /** * @inheritDoc */ @Override - public IGT_Fluid registerContainers( + public IGT_RegisteredFluid registerContainers( final ItemStack fullContainer, final ItemStack emptyContainer, final int containerSize) { - if (fullContainer == null || emptyContainer == null) return this; - if (registeredFluid == null) { - throw new IllegalStateException("Cannot register containers for an unregistered fluid"); - } - final FluidStack fluidStack = new FluidStack(registeredFluid, containerSize); - if (!FluidContainerRegistry.registerFluidContainer(fluidStack, fullContainer, emptyContainer)) { - GT_Values.RA.addFluidCannerRecipe( - fullContainer, GT_Utility.getContainerItem(fullContainer, false), null, fluidStack); + if (fullContainer != null && emptyContainer != null) { + final FluidStack fluidStack = new FluidStack(registeredFluid, containerSize); + if (!FluidContainerRegistry.registerFluidContainer(fluidStack, fullContainer, emptyContainer)) { + GT_Values.RA.addFluidCannerRecipe( + fullContainer, GT_Utility.getContainerItem(fullContainer, false), null, fluidStack); + } } return this; } @@ -113,7 +128,7 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { * @inheritDoc */ @Override - public IGT_Fluid registerBContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { + public IGT_RegisteredFluid registerBContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { return registerContainers(fullContainer, emptyContainer, 1000); } @@ -121,7 +136,7 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { * @inheritDoc */ @Override - public IGT_Fluid registerPContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { + public IGT_RegisteredFluid registerPContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { return registerContainers(fullContainer, emptyContainer, 250); } @@ -129,45 +144,26 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { * @inheritDoc */ @Override - public ResourceLocation getStillIconResourceLocation() { - return stillIconResourceLocation; - } - - /** - * @inheritDoc - */ - @Override - public ResourceLocation getFlowingIconResourceLocation() { - return flowingIconResourceLocation; - } - - /** - * @inheritDoc - */ - @Override - public IGT_Fluid configureMaterials(final Materials material) { - if (registeredFluid == null) { - throw new IllegalStateException("Cannot configure Materials with an unregistered fluid"); - } - - switch (fluidState) { - case SLURRY: - material.mSolid = registeredFluid; - break; - case LIQUID: - material.mFluid = registeredFluid; - break; - case GAS: - material.mGas = registeredFluid; - break; - case PLASMA: - material.mPlasma = registeredFluid; - break; - case MOLTEN: - material.mStandardMoltenFluid = registeredFluid; - break; - default: - throw new IllegalStateException("Unexpected FluidState: " + fluidState); + public IGT_RegisteredFluid configureMaterials(final Materials material) { + if (material != null) { + switch (fluidState) { + case SLURRY: + material.mSolid = registeredFluid; + break; + case GAS: + material.mGas = registeredFluid; + break; + case PLASMA: + material.mPlasma = registeredFluid; + break; + case MOLTEN: + material.mStandardMoltenFluid = registeredFluid; + break; + case LIQUID: + default: + material.mFluid = registeredFluid; + break; + } } return this; } @@ -177,33 +173,34 @@ public class GT_Fluid extends Fluid implements IGT_Fluid, Runnable { */ @Override public Fluid asFluid() { - return registeredFluid == null ? this : registeredFluid; + return registeredFluid; } + // ----- Runnable interface implementations ----- + /** - * Adjusts this {@link Fluid}'s settings based on this {@link IGT_Fluid}'s state + * This {@link Runnable#run()} implementation is scheduled within the {@link GregTech_API#sGTBlockIconload} + * to load this {@link IGT_Fluid}'s texture icons. * - * @throws IllegalStateException if {@link FluidState} is unknown + * @see Runnable#run() */ - protected void configureFromStateTemperature() { - switch (fluidState) { - case SLURRY: - setGaseous(false).setViscosity(10000); - break; - case LIQUID: - case MOLTEN: - final int luminosity = - temperature >= 3500 ? 15 : temperature < 1000 ? 0 : 14 * (temperature - 1000) / 2500 + 1; - setGaseous(false).setViscosity(1000).setLuminosity(luminosity); - break; - case GAS: - setGaseous(true).setDensity(-100).setViscosity(200); - break; - case PLASMA: - setGaseous(true).setDensity(55536).setViscosity(10).setLuminosity(15); - break; - default: - throw new IllegalStateException("Unexpected FluidState: " + fluidState); + @Override + public void run() { + if (!hasRun) { + if (iconsFrom instanceof GT_Fluid) { + // Needs the GT_Fluid to have registered its icons + ((GT_Fluid) iconsFrom).run(); + stillIcon = iconsFrom.getStillIcon(); + flowingIcon = iconsFrom.getFlowingIcon(); + } else { + if (stillIconResourceLocation != null) { + stillIcon = GregTech_API.sBlockIcons.registerIcon(stillIconResourceLocation.toString()); + } + if (flowingIconResourceLocation != null) { + flowingIcon = GregTech_API.sBlockIcons.registerIcon(flowingIconResourceLocation.toString()); + } + } + hasRun = true; } } } diff --git a/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java b/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java index b484f7aacb..48d4e0e95e 100644 --- a/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java +++ b/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java @@ -6,21 +6,28 @@ import gregtech.api.enums.Dyes; import gregtech.api.enums.FluidState; import gregtech.api.interfaces.fluid.IGT_Fluid; import gregtech.api.interfaces.fluid.IGT_FluidBuilder; +import gregtech.api.interfaces.fluid.IGT_RegisteredFluid; import java.util.Locale; +import javax.annotation.Nonnull; import net.minecraft.block.Block; +import net.minecraft.util.IIcon; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fluids.Fluid; public class GT_FluidBuilder implements IGT_FluidBuilder { - protected final String fluidName; - protected String localizedName; - protected ResourceLocation stillIconResourceLocation = null, flowingIconResourceLocation = null; - protected short[] colorRGBA = Dyes._NULL.getRGBA(); - protected Block fluidBlock = null; - protected FluidState fluidState; - protected int temperature; + final String fluidName; + String localizedName; + ResourceLocation stillIconResourceLocation = null, flowingIconResourceLocation = null; + short[] colorRGBA = Dyes._NULL.getRGBA(); + Block fluidBlock = null; + FluidState fluidState; + int temperature; + IIcon stillIcon; + IIcon flowingIcon; + Fluid iconsFrom; public GT_FluidBuilder(final String fluidName) { - this.fluidName = fluidName; + this.fluidName = fluidName.toLowerCase(Locale.ENGLISH); } /** @@ -83,9 +90,8 @@ public class GT_FluidBuilder implements IGT_FluidBuilder { * @inheritDoc */ @Override - public IGT_FluidBuilder withTextureFrom(final IGT_Fluid fromGTFluid) { - this.stillIconResourceLocation = fromGTFluid.getStillIconResourceLocation(); - this.flowingIconResourceLocation = fromGTFluid.getFlowingIconResourceLocation(); + public IGT_FluidBuilder withIconsFrom(@Nonnull final Fluid fromFluid) { + this.iconsFrom = fromFluid; return this; } @@ -114,9 +120,15 @@ public class GT_FluidBuilder implements IGT_FluidBuilder { */ @Override public IGT_Fluid build() { + if (colorRGBA == null) { + colorRGBA = Dyes._NULL.getRGBA(); + } if (stillIconResourceLocation == null) { withTextureName(fluidName.toLowerCase(Locale.ENGLISH)); } + if (localizedName == null) { + localizedName = fluidName; + } return new GT_Fluid(this); } @@ -124,10 +136,7 @@ public class GT_FluidBuilder implements IGT_FluidBuilder { * @inheritDoc */ @Override - public IGT_Fluid buildAndRegister() { - if (stillIconResourceLocation == null) { - withTextureName(fluidName.toLowerCase(Locale.ENGLISH)); - } + public IGT_RegisteredFluid buildAndRegister() { return build().addFluid(); } } diff --git a/src/main/java/gregtech/loaders/preload/GT_Loader_Item_Block_And_Fluid.java b/src/main/java/gregtech/loaders/preload/GT_Loader_Item_Block_And_Fluid.java index 633a8d77ae..ad85963075 100644 --- a/src/main/java/gregtech/loaders/preload/GT_Loader_Item_Block_And_Fluid.java +++ b/src/main/java/gregtech/loaders/preload/GT_Loader_Item_Block_And_Fluid.java @@ -915,8 +915,7 @@ public class GT_Loader_Item_Block_And_Fluid implements Runnable { Materials.Ice.mGas = Materials.Water.mGas; Materials.Water.mGas.setTemperature(375).setGaseous(true); - ItemList.sOilExtraHeavy = GT_FluidFactory.of("liquid_extra_heavy_oil", "Very Heavy Oil", LIQUID, 295) - .asFluid(); + ItemList.sOilExtraHeavy = GT_FluidFactory.of("liquid_extra_heavy_oil", "Very Heavy Oil", LIQUID, 295); ItemList.sEpichlorhydrin = GT_FluidFactory.builder("liquid_epichlorhydrin") .withLocalizedName("Epichlorohydrin") .withStateAndTemperature(LIQUID, 295) @@ -924,8 +923,7 @@ public class GT_Loader_Item_Block_And_Fluid implements Runnable { .configureMaterials(Materials.Epichlorohydrin) .registerBContainers(Materials.Epichlorohydrin.getCells(1), Materials.Empty.getCells(1)) .asFluid(); - ItemList.sDrillingFluid = GT_FluidFactory.of("liquid_drillingfluid", "Drilling Fluid", LIQUID, 295) - .asFluid(); + ItemList.sDrillingFluid = GT_FluidFactory.of("liquid_drillingfluid", "Drilling Fluid", LIQUID, 295); ItemList.sToluene = GT_FluidFactory.builder("liquid_toluene") .withLocalizedName("Toluene") .withStateAndTemperature(LIQUID, 295) @@ -1321,21 +1319,23 @@ public class GT_Loader_Item_Block_And_Fluid implements Runnable { for (byte i = 0; i < Dyes.VALUES.length; i = (byte) (i + 1)) { Dyes tDye = Dyes.VALUES[i]; Fluid tFluid; - tDye.addFluidDye((Fluid) + tDye.addFluidDye( GT_FluidFactory.builder("dye.watermixed." + tDye.name().toLowerCase(Locale.ENGLISH)) .withTextureName("dyes") .withLocalizedName("Water Mixed " + tDye.mName + " Dye") .withColorRGBA(tDye.getRGBA()) .withStateAndTemperature(LIQUID, 295) - .buildAndRegister()); - tDye.addFluidDye((Fluid) GT_FluidFactory.builder( - "dye.chemical." + tDye.name().toLowerCase(Locale.ENGLISH)) - .withTextureName("dyes") - .withLocalizedName("Chemical " + tDye.mName + " Dye") - .withColorRGBA(tDye.getRGBA()) - .withStateAndTemperature(LIQUID, 295) - .buildAndRegister() - .registerContainers(ItemList.SPRAY_CAN_DYES[i].get(1L), ItemList.Spray_Empty.get(1L), 2304)); + .buildAndRegister() + .asFluid()); + tDye.addFluidDye( + GT_FluidFactory.builder("dye.chemical." + tDye.name().toLowerCase(Locale.ENGLISH)) + .withTextureName("dyes") + .withLocalizedName("Chemical " + tDye.mName + " Dye") + .withColorRGBA(tDye.getRGBA()) + .withStateAndTemperature(LIQUID, 295) + .buildAndRegister() + .registerContainers(ItemList.SPRAY_CAN_DYES[i].get(1L), ItemList.Spray_Empty.get(1L), 2304) + .asFluid()); } GT_FluidFactory.builder("ice") .withLocalizedName("Crushed Ice") |