diff options
author | Léa Gris <lea.gris@noiraude.net> | 2022-09-05 17:43:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-05 17:43:09 +0200 |
commit | 7caea6daefcbffbc102741ed09daac9d6439824d (patch) | |
tree | d223491481aeafd36418a62cb43250703b1cc987 /src/main/java/gregtech/common/fluid | |
parent | f97a1861751aa9431a7c36eb4ea061f902e9f255 (diff) | |
download | GT5-Unofficial-7caea6daefcbffbc102741ed09daac9d6439824d.tar.gz GT5-Unofficial-7caea6daefcbffbc102741ed09daac9d6439824d.tar.bz2 GT5-Unofficial-7caea6daefcbffbc102741ed09daac9d6439824d.zip |
feat(API): Implements a featured API for GT_Fluid (#1345)
* feat(API): Implements a featured API for GT_Fluid
*** Rationale
The current implementation, which is based on the `GT_Fluid` object,
does not allow for the evolution of the functionalities, or the
variation of the fluid-related implementations in GregTech.
*** Objectives
This replacement API should free from these constraints, by providing :
1. The separation of responsibilities of the different tasks and steps:
- The definition and progressive construction of an `IGT_Fluid`,
- Registration of the `IGT_Fluid`,
- Configuration of related equipment, such as containers,
- Propagation of properties of an `IGT_Fluid` to related services
such as Materials
2. The separation of interfaces exposed to the API from their internal
implementations to allow:
- Evolve the implementations in the most transparent way possible
- To have internal GregTech implementations or outsourced
implementations coexist in its extensions.
*** Specificity of this new API
- Provides a new interface to build and interact with fluid related records
- Deprecates the old `api/objects/GT_Fluid` object and the
`common/GT_Proxy.addFluid` record methods
* fix(conversations): addresses @Glease review comments
https://github.com/GTNewHorizons/GT5-Unofficial/pull/1345#pullrequestreview-1096261703
* ./gradlew :spotlessApply
* fix(review): add review comments from @eigenraven
Added missing final qualifiers on methods parameters.
https://github.com/GTNewHorizons/GT5-Unofficial/pull/1345#pullrequestreview-1096318523
* fix(review) address remaining review comments from @eigenraven
Diffstat (limited to 'src/main/java/gregtech/common/fluid')
-rw-r--r-- | src/main/java/gregtech/common/fluid/GT_Fluid.java | 207 | ||||
-rw-r--r-- | src/main/java/gregtech/common/fluid/GT_FluidBuilder.java | 133 |
2 files changed, 340 insertions, 0 deletions
diff --git a/src/main/java/gregtech/common/fluid/GT_Fluid.java b/src/main/java/gregtech/common/fluid/GT_Fluid.java new file mode 100644 index 0000000000..4e4131bb2f --- /dev/null +++ b/src/main/java/gregtech/common/fluid/GT_Fluid.java @@ -0,0 +1,207 @@ +package gregtech.common.fluid; + +import gregtech.api.GregTech_API; +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.util.GT_LanguageManager; +import gregtech.api.util.GT_Utility; +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 { + private final String localizedName; + private final ResourceLocation stillIconResourceLocation; + private final ResourceLocation flowingIconResourceLocation; + private final short[] colorRGBA; + private final FluidState fluidState; + private final int temperature; + + /** + * 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) { + super(builder.fluidName); + this.localizedName = builder.localizedName; + this.stillIconResourceLocation = builder.stillIconResourceLocation; + this.flowingIconResourceLocation = builder.flowingIconResourceLocation; + this.block = builder.fluidBlock; + this.colorRGBA = builder.colorRGBA; + this.fluidState = builder.fluidState; + this.temperature = builder.temperature; + configureFromStateTemperature(); + } + + /** + * @inheritDoc from {@link Fluid#getColor()} + */ + @Override + public int getColor() { + return (Math.max(0, Math.min(255, colorRGBA[0])) << 16) + | (Math.max(0, Math.min(255, colorRGBA[1])) << 8) + | 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. + * + * @inheritDoc from {@link 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); + } + } + + /** + * @inheritDoc from {@link IGT_Fluid#addFluid()} + */ + @Override + public IGT_Fluid addFluid() { + // Adds self the block icons loader run() tasks + GregTech_API.sGTBlockIconload.add(this); + GT_LanguageManager.addStringLocalization(this.getUnlocalizedName(), localizedName); + + final Fluid registeredFluid = registerFluid(); + if (registeredFluid.getTemperature() == new Fluid("test").getTemperature()) { + registeredFluid.setTemperature(temperature); + } + return this; + } + + /** + * @inheritDoc from {@link IGT_Fluid#registerContainers(ItemStack, ItemStack, int)} + */ + @Override + public IGT_Fluid registerContainers( + final ItemStack fullContainer, final ItemStack emptyContainer, final int containerSize) { + if (fullContainer == null || emptyContainer == null) return this; + final FluidStack fluidStack = new FluidStack(this, containerSize); + if (!FluidContainerRegistry.registerFluidContainer(fluidStack, fullContainer, emptyContainer)) { + GT_Values.RA.addFluidCannerRecipe( + fullContainer, GT_Utility.getContainerItem(fullContainer, false), null, fluidStack); + } + return this; + } + + /** + * @inheritDoc from {@link IGT_Fluid#registerBContainers(ItemStack, ItemStack)} + */ + @Override + public IGT_Fluid registerBContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { + return registerContainers(fullContainer, emptyContainer, 1000); + } + + /** + * @inheritDoc from {@link IGT_Fluid#registerPContainers(ItemStack, ItemStack)} + */ + @Override + public IGT_Fluid registerPContainers(final ItemStack fullContainer, final ItemStack emptyContainer) { + return registerContainers(fullContainer, emptyContainer, 250); + } + + /** + * @inheritDoc from {@link IGT_Fluid#getStillIconResourceLocation()} + */ + @Override + public ResourceLocation getStillIconResourceLocation() { + return stillIconResourceLocation; + } + + /** + * @inheritDoc from {@link IGT_Fluid#getFlowingIconResourceLocation()} + */ + @Override + public ResourceLocation getFlowingIconResourceLocation() { + return flowingIconResourceLocation; + } + + /** + * @throws IllegalStateException if {@link FluidState} in invalid + * @inheritDoc from {@link IGT_Fluid#configureMaterials(Materials)} + */ + @Override + public IGT_Fluid configureMaterials(final Materials material) { + switch (fluidState) { + case SLURRY: + material.mSolid = this; + break; + case LIQUID: + material.mFluid = this; + break; + case GAS: + material.mGas = this; + break; + case PLASMA: + material.mPlasma = this; + break; + case MOLTEN: + material.mStandardMoltenFluid = this; + break; + default: + throw new IllegalStateException("Unexpected FluidState: " + fluidState); + } + return this; + } + + /** + * @inheritDoc from {@link IGT_Fluid#asFluid()} + */ + @Override + public Fluid asFluid() { + return this; + } + + /** + * Adjusts this {@link Fluid}'s settings based on this {@link IGT_Fluid}'s state + * + * @throws IllegalStateException if {@link FluidState} in invalid + */ + protected void configureFromStateTemperature() { + switch (fluidState) { + case SLURRY: // Solid + setGaseous(false).setViscosity(10000); + break; + case LIQUID: // Fluid + case MOLTEN: // Molten + setGaseous(false) + .setViscosity(1000) + .setLuminosity( + temperature >= 5000 + ? 15 + : temperature < 1000 ? 0 : 14 * (temperature - 1000) / 4000 + 1); + break; + case GAS: // Gas + setGaseous(true).setDensity(-100).setViscosity(200); + break; + case PLASMA: // Plasma + setGaseous(true).setDensity(55536).setViscosity(10).setLuminosity(15); + break; + default: + throw new IllegalStateException("Unexpected FluidState: " + fluidState); + } + } + + /** + * Registers this {@link IGT_Fluid} to the {@link FluidRegistry} + * + * @return the {@link Fluid} from the {@link FluidRegistry} + */ + protected Fluid registerFluid() { + return FluidRegistry.registerFluid(this) ? this : FluidRegistry.getFluid(this.fluidName); + } +} diff --git a/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java b/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java new file mode 100644 index 0000000000..91db377ce0 --- /dev/null +++ b/src/main/java/gregtech/common/fluid/GT_FluidBuilder.java @@ -0,0 +1,133 @@ +package gregtech.common.fluid; + +import static gregtech.api.enums.GT_Values.MOD_ID; + +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 java.util.Locale; +import net.minecraft.block.Block; +import net.minecraft.util.ResourceLocation; + +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; + + public GT_FluidBuilder(final String fluidName) { + this.fluidName = fluidName; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withColorRGBA(short[])} + */ + @Override + public IGT_FluidBuilder withColorRGBA(final short[] colorRGBA) { + this.colorRGBA = colorRGBA; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withLocalizedName(String)} + */ + @Override + public IGT_FluidBuilder withLocalizedName(final String localizedName) { + this.localizedName = localizedName; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withStateAndTemperature(FluidState, int)} + */ + @Override + public IGT_FluidBuilder withStateAndTemperature(final FluidState fluidState, final int temperature) { + this.fluidState = fluidState; + this.temperature = temperature; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withStillIconResourceLocation(ResourceLocation)} + */ + @Override + public IGT_FluidBuilder withStillIconResourceLocation(final ResourceLocation stillIconResourceLocation) { + this.stillIconResourceLocation = stillIconResourceLocation; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withFlowingIconResourceLocation(ResourceLocation)} + */ + @Override + public IGT_FluidBuilder withFlowingIconResourceLocation(final ResourceLocation flowingIconResourceLocation) { + this.flowingIconResourceLocation = flowingIconResourceLocation; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withTextureName(String)} + */ + @Override + public IGT_FluidBuilder withTextureName(final String textureName) { + this.stillIconResourceLocation = new ResourceLocation(MOD_ID, "fluids/fluid." + textureName); + this.flowingIconResourceLocation = null; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withTextureFrom(IGT_Fluid)} + */ + @Override + public IGT_FluidBuilder withTextureFrom(final IGT_Fluid fromGTFluid) { + this.stillIconResourceLocation = fromGTFluid.getStillIconResourceLocation(); + this.flowingIconResourceLocation = fromGTFluid.getFlowingIconResourceLocation(); + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withFluidBlock} + */ + @Override + public IGT_FluidBuilder withFluidBlock(final Block fluidBlock) { + this.fluidBlock = fluidBlock; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#withTextures(ResourceLocation, ResourceLocation)} + */ + @Override + public IGT_FluidBuilder withTextures( + final ResourceLocation stillIconResourceLocation, final ResourceLocation flowingIconResourceLocation) { + this.stillIconResourceLocation = stillIconResourceLocation; + this.flowingIconResourceLocation = flowingIconResourceLocation; + return this; + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#build() + */ + @Override + public IGT_Fluid build() { + if (stillIconResourceLocation == null) { + withTextureName(fluidName.toLowerCase(Locale.ENGLISH)); + } + return new GT_Fluid(this); + } + + /** + * @inheritDoc from {@link IGT_FluidBuilder#buildAndRegister() + */ + @Override + public IGT_Fluid buildAndRegister() { + if (stillIconResourceLocation == null) { + withTextureName(fluidName.toLowerCase(Locale.ENGLISH)); + } + return build().addFluid(); + } +} |