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 /src/main/java/gregtech/common/fluid | |
| 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.
Diffstat (limited to 'src/main/java/gregtech/common/fluid')
| -rw-r--r-- | src/main/java/gregtech/common/fluid/GT_Fluid.java | 205 | ||||
| -rw-r--r-- | src/main/java/gregtech/common/fluid/GT_FluidBuilder.java | 39 |
2 files changed, 125 insertions, 119 deletions
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(); } } |
