Some (sadly breaking) improvements to the Fluid API (#6110)

* Some (sadly breaking) improvements to the API, after feedback and concerns provided by users:
  - Moved calculation of the translation key to the FluidAttributes constructor, so that builders can be shared between still & flowing sub-fluids.
  - Moved biome-based coloring to a dedicated FluidAttributes.Water variant, which is not used by default.
  - Added logic to automatically gather fluid textures into the list of textures to bake.
  - Patched BucketItem and FlowingFluidBlock to avoid eager access to the fluid objects.
  - Added a ForgeFlowingFluid class, as a more user-friendly way to construct a new FlowingFluid.
This commit is contained in:
David Quintana authored and GitHub committed 2019-09-11 16:00:32 +02:00
1 parent 281ef5a152
commit b0c9d7cce9
17 files changed
+537 -277

No files matched your search

@@ -146,6 +146,7 @@ import net.minecraftforge.resource.ReloadRequirements;
import net.minecraftforge.resource.SelectiveReloadStateHandler;
import net.minecraftforge.resource.VanillaResourceType;
import net.minecraftforge.versions.forge.ForgeVersion;
import net.minecraftforge.registries.ForgeRegistries;
public class ForgeHooksClient
{
@@ -380,9 +381,9 @@ public class ForgeHooksClient
}
@SuppressWarnings("deprecation")
public static Matrix4f getMatrix(net.minecraft.client.renderer.model.ItemTransformVec3f transform)
public static Matrix4f getMatrix(ItemTransformVec3f transform)
{
javax.vecmath.Matrix4f m = new javax.vecmath.Matrix4f(), t = new javax.vecmath.Matrix4f();
Matrix4f m = new Matrix4f(), t = new Matrix4f();
m.setIdentity();
m.setTranslation(TRSRTransformation.toVecmath(transform.translation));
t.setIdentity();
@@ -569,6 +570,13 @@ public class ForgeHooksClient
};
}
public static void gatherFluidTextures(Set<ResourceLocation> textures)
{
ForgeRegistries.FLUIDS.getValues().stream()
.flatMap(f -> f.getAttributes().getTextures())
.forEach(textures::add);
}
private static class LightGatheringTransformer extends QuadGatheringTransformer {
private static final VertexFormat FORMAT = new VertexFormat().addElement(DefaultVertexFormats.TEX_2F).addElement(DefaultVertexFormats.TEX_2S);
@@ -702,7 +710,7 @@ public class ForgeHooksClient
private static void drawSegment(ItemRenderer ri, int baseColor, ItemStack stack, List<BakedQuad> segment, int bl, int sl, boolean shade, boolean updateLighting, boolean updateShading)
{
BufferBuilder bufferbuilder = Tessellator.getInstance().getBuffer();
bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.ITEM);
bufferbuilder.begin(GL_QUADS, DefaultVertexFormats.ITEM);
float lastBl = GLX.lastBrightnessX;
float lastSl = GLX.lastBrightnessY;
@@ -442,7 +442,7 @@ public final class ModelDynBucket implements IUnbakedModel
BakedDynBucket model = (BakedDynBucket)originalModel;
Fluid fluid = fluidStack.getFluid();
String name = fluid.getAttributes().getName();
String name = fluid.getRegistryName().toString();
if (!model.cache.containsKey(name))
{
@@ -45,6 +45,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.advancements.Advancement;
import net.minecraft.block.Block;
import net.minecraft.fluid.*;
import net.minecraft.util.CachedBlockInfo;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
@@ -59,9 +60,7 @@ import net.minecraft.entity.item.minecart.ContainerMinecartEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.IFluidState;
import net.minecraft.block.Blocks;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.container.RepairContainer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
@@ -141,6 +140,7 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.NoteBlockEvent;
import net.minecraftforge.eventbus.api.Event.Result;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.registries.DataSerializerEntry;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
@@ -831,6 +831,30 @@ public class ForgeHooks
return ret;
}
public static FluidAttributes createVanillaFluidAttributes(Fluid fluid)
{
if (fluid instanceof EmptyFluid)
return net.minecraftforge.fluids.FluidAttributes.builder(
new net.minecraft.util.ResourceLocation("white"),
new net.minecraft.util.ResourceLocation("white"))
.translationKey("block.minecraft.air")
.color(0).density(0).temperature(0).luminosity(0).viscosity(0).build(fluid);
if (fluid instanceof WaterFluid)
return net.minecraftforge.fluids.FluidAttributes.Water.builder(
new net.minecraft.util.ResourceLocation("block/water_still"),
new net.minecraft.util.ResourceLocation("block/water_flow"))
.overlay(new net.minecraft.util.ResourceLocation("block/water_overlay"))
.translationKey("block.minecraft.water")
.color(0x3F76E4).build(fluid);
if (fluid instanceof LavaFluid)
return net.minecraftforge.fluids.FluidAttributes.builder(
new net.minecraft.util.ResourceLocation("block/lava_still"),
new net.minecraft.util.ResourceLocation("block/lava_flow"))
.translationKey("block.minecraft.lava")
.luminosity(15).density(3000).viscosity(6000).temperature(1300).build(fluid);
throw new RuntimeException("Mod fluids must override createAttributes.");
}
private static class LootTableContext
{
public final ResourceLocation name;
@@ -22,30 +22,25 @@ package net.minecraftforge.fluids;
import javax.annotation.Nullable;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.IFluidState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IEnviromentBlockReader;
import java.util.Locale;
import java.util.function.Supplier;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.translation.LanguageMap;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraft.item.Rarity;
import net.minecraft.world.biome.BiomeColors;
/**
* Minecraft Forge Fluid Implementation
@@ -67,10 +62,6 @@ public class FluidAttributes
{
public static final int BUCKET_VOLUME = 1000;
/** The unique identification name for this fluid. */
private final String fluidName;
/** The translation key of this fluid. */
private String translationKey;
private final ResourceLocation stillTexture;
@@ -141,10 +132,9 @@ public class FluidAttributes
*/
private final int color;
protected FluidAttributes(Builder builder)
protected FluidAttributes(Builder builder, Fluid fluid)
{
this.fluidName = builder.name;
this.translationKey = builder.translationKey;
this.translationKey = builder.translationKey != null ? builder.translationKey : Util.makeTranslationKey("fluid", fluid.getRegistryName());
this.stillTexture = builder.stillTexture;
this.flowingTexture = builder.flowingTexture;
this.overlayTexture = builder.overlayTexture;
@@ -159,11 +149,6 @@ public class FluidAttributes
this.rarity = builder.rarity;
}
public final String getName()
{
return this.fluidName;
}
public ItemStack getBucket(FluidStack stack)
{
return new ItemStack(stack.getFluid().getFilledBucket());
@@ -240,7 +225,7 @@ public class FluidAttributes
}
/**
* A FluidStack sensitive version of getUnlocalizedName
* A FluidStack sensitive version of getTranslationKey
*/
public String getTranslationKey(FluidStack stack)
{
@@ -248,11 +233,11 @@ public class FluidAttributes
}
/**
* Returns the unlocalized name of this fluid.
* Returns the translation key of this fluid.
*/
public String getTranslationKey()
{
return "fluid." + this.translationKey;
return this.translationKey;
}
/* Default Accessors */
@@ -343,17 +328,23 @@ public class FluidAttributes
public SoundEvent getFillSound(IEnviromentBlockReader world, BlockPos pos) { return getFillSound(); }
public SoundEvent getEmptySound(IEnviromentBlockReader world, BlockPos pos) { return getEmptySound(); }
public static Builder builder(String name, ResourceLocation stillTexture, ResourceLocation flowingTexture) {
return new Builder(name, stillTexture, flowingTexture);
public static Builder builder(ResourceLocation stillTexture, ResourceLocation flowingTexture) {
return new Builder(stillTexture, flowingTexture, FluidAttributes::new);
}
public Stream<ResourceLocation> getTextures()
{
if (overlayTexture != null)
return Stream.of(stillTexture, flowingTexture, overlayTexture);
return Stream.of(stillTexture, flowingTexture);
}
public static class Builder
{
private final String name;
private final ResourceLocation stillTexture;
private final ResourceLocation flowingTexture;
private ResourceLocation overlayTexture;
private int color = 0xFFFFFF;
private int color = 0xFFFFFFFF;
private String translationKey;
private SoundEvent fillSound;
private SoundEvent emptySound;
@@ -363,12 +354,12 @@ public class FluidAttributes
private int viscosity = 1000;
private boolean isGaseous;
private Rarity rarity = Rarity.COMMON;
private BiFunction<Builder,Fluid,FluidAttributes> factory;
protected Builder(String name, ResourceLocation stillTexture, ResourceLocation flowingTexture) {
this.name = name.toLowerCase(Locale.ENGLISH);
protected Builder(ResourceLocation stillTexture, ResourceLocation flowingTexture, BiFunction<Builder,Fluid,FluidAttributes> factory) {
this.factory = factory;
this.stillTexture = stillTexture;
this.flowingTexture = flowingTexture;
this.translationKey = "fluid." + this.name + ".name";
}
public final Builder translationKey(String translationKey)
@@ -383,12 +374,6 @@ public class FluidAttributes
return this;
}
public final Builder vanillaColor()
{
this.color = -1;
return this;
}
public final Builder overlay(ResourceLocation texture)
{
overlayTexture = texture;
@@ -444,9 +429,27 @@ public class FluidAttributes
return this;
}
public FluidAttributes build()
public FluidAttributes build(Fluid fluid)
{
return new FluidAttributes(this);
return factory.apply(this, fluid);
}
}
public static class Water extends FluidAttributes
{
protected Water(Builder builder, Fluid fluid)
{
super(builder, fluid);
}
@Override
public int getColor(IEnviromentBlockReader world, BlockPos pos)
{
return BiomeColors.getWaterColor(world, pos);
}
public static Builder builder(ResourceLocation stillTexture, ResourceLocation flowingTexture) {
return new Builder(stillTexture, flowingTexture, Water::new);
}
}
}
@@ -161,7 +161,7 @@ public class FluidStack
}
protected void updateEmpty() {
isEmpty = getRawFluid() == Fluids.EMPTY || amount == 0;
isEmpty = getRawFluid() == Fluids.EMPTY || amount <= 0;
}
public int getAmount()
@@ -0,0 +1,263 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fluids;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.FlowingFluidBlock;
import net.minecraft.fluid.FlowingFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.IFluidState;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.state.StateContainer;
import net.minecraft.tags.FluidTags;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import javax.annotation.Nullable;
import java.util.function.Supplier;
public abstract class ForgeFlowingFluid extends FlowingFluid
{
private final Supplier<? extends Fluid> flowing;
private final Supplier<? extends Fluid> still;
@Nullable
private final Supplier<? extends Item> bucket;
@Nullable
private final Supplier<? extends FlowingFluidBlock> block;
private final FluidAttributes.Builder builder;
private final boolean canMultiply;
private final int slopeFindDistance;
private final int levelDecreasePerBlock;
private final float explosionResistance;
private final BlockRenderLayer renderLayer;
private final int tickRate;
protected ForgeFlowingFluid(Properties properties)
{
this.flowing = properties.flowing;
this.still = properties.still;
this.builder = properties.attributes;
this.canMultiply = properties.canMultiply;
this.bucket = properties.bucket;
this.block = properties.block;
this.slopeFindDistance = properties.slopeFindDistance;
this.levelDecreasePerBlock = properties.levelDecreasePerBlock;
this.explosionResistance = properties.explosionResistance;
this.renderLayer = properties.renderLayer;
this.tickRate = properties.tickRate;
}
@Override
public Fluid getFlowingFluid()
{
return flowing.get();
}
@Override
public Fluid getStillFluid()
{
return still.get();
}
@Override
protected boolean canSourcesMultiply()
{
return canMultiply;
}
@Override
protected void beforeReplacingBlock(IWorld worldIn, BlockPos pos, BlockState state)
{
TileEntity tileentity = state.getBlock().hasTileEntity() ? worldIn.getTileEntity(pos) : null;
Block.spawnDrops(state, worldIn.getWorld(), pos, tileentity);
}
@Override
protected int getSlopeFindDistance(IWorldReader worldIn)
{
return slopeFindDistance;
}
@Override
protected int getLevelDecreasePerBlock(IWorldReader worldIn)
{
return levelDecreasePerBlock;
}
@Override
public BlockRenderLayer getRenderLayer()
{
return renderLayer;
}
@Override
public Item getFilledBucket()
{
return bucket != null ? bucket.get() : Items.AIR;
}
@Override
protected boolean func_215665_a(IFluidState state, IBlockReader world, BlockPos pos, Fluid fluidIn, Direction direction)
{
// Based on the water implementation, may need to be overriden for mod fluids that shouldn't behave like water.
return direction == Direction.DOWN && !isEquivalentTo(fluidIn);
}
@Override
public int getTickRate(IWorldReader world)
{
return tickRate;
}
@Override
protected float getExplosionResistance()
{
return explosionResistance;
}
@Override
protected BlockState getBlockState(IFluidState state)
{
if (block != null)
return block.get().getDefaultState().with(FlowingFluidBlock.LEVEL, getLevelFromState(state));
return Blocks.AIR.getDefaultState();
}
@Override
public boolean isEquivalentTo(Fluid fluidIn) {
return fluidIn == still.get() || fluidIn == flowing.get();
}
@Override
protected FluidAttributes createAttributes()
{
return builder.build(this);
}
public static class Flowing extends ForgeFlowingFluid
{
public Flowing(Properties properties)
{
super(properties);
setDefaultState(getStateContainer().getBaseState().with(LEVEL_1_8, 7));
}
protected void fillStateContainer(StateContainer.Builder<Fluid, IFluidState> builder) {
super.fillStateContainer(builder);
builder.add(LEVEL_1_8);
}
public int getLevel(IFluidState state) {
return state.get(LEVEL_1_8);
}
public boolean isSource(IFluidState state) {
return false;
}
}
public static class Source extends ForgeFlowingFluid
{
public Source(Properties properties)
{
super(properties);
}
public int getLevel(IFluidState state) {
return 8;
}
public boolean isSource(IFluidState state) {
return true;
}
}
public static class Properties
{
private Supplier<? extends Fluid> still;
private Supplier<? extends Fluid> flowing;
private FluidAttributes.Builder attributes;
private boolean canMultiply;
private Supplier<? extends Item> bucket;
private Supplier<? extends FlowingFluidBlock> block;
private int slopeFindDistance = 4;
private int levelDecreasePerBlock = 1;
private float explosionResistance = 1;
private BlockRenderLayer renderLayer = BlockRenderLayer.TRANSLUCENT;
private int tickRate = 5;
public Properties(Supplier<? extends Fluid> still, Supplier<? extends Fluid> flowing, FluidAttributes.Builder attributes)
{
this.still = still;
this.flowing = flowing;
this.attributes = attributes;
}
public Properties canMultiply()
{
canMultiply = true;
return this;
}
public Properties bucket(Supplier<? extends Item> bucket)
{
this.bucket = bucket;
return this;
}
public Properties block(Supplier<? extends FlowingFluidBlock> block)
{
this.block = block;
return this;
}
public Properties slopeFindDistance(int slopeFindDistance)
{
this.slopeFindDistance = slopeFindDistance;
return this;
}
public Properties levelDecreasePerBlock(int levelDecreasePerBlock)
{
this.levelDecreasePerBlock = levelDecreasePerBlock;
return this;
}
public Properties explosionResistance(float explosionResistance)
{
this.explosionResistance = explosionResistance;
return this;
}
public Properties renderLayer(BlockRenderLayer layer)
{
this.renderLayer = layer;
return this;
}
}
}
@@ -19,10 +19,10 @@
package net.minecraftforge.fluids.capability;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.INBT;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
@@ -39,7 +39,7 @@ public class CapabilityFluidHandler
@CapabilityInject(IFluidHandlerItem.class)
public static Capability<IFluidHandlerItem> FLUID_HANDLER_ITEM_CAPABILITY = null;
public static void register()
public static void register()
{
CapabilityManager.INSTANCE.register(IFluidHandler.class, new DefaultFluidHandlerStorage<>(), () -> new FluidTank(FluidAttributes.BUCKET_VOLUME));
@@ -48,27 +48,27 @@ public class CapabilityFluidHandler
private static class DefaultFluidHandlerStorage<T extends IFluidHandler> implements Capability.IStorage<T> {
@Override
public INBT writeNBT(Capability<T> capability, T instance, Direction side)
{
if (!(instance instanceof FluidTank))
throw new RuntimeException("Cannot serialize to an instance that isn't the default implementation");
CompoundNBT nbt = new CompoundNBT();
FluidTank tank = (FluidTank) instance;
FluidStack fluid = tank.getFluid();
fluid.writeToNBT(nbt);
nbt.putInt("Capacity", tank.getCapacity());
return nbt;
}
public INBT writeNBT(Capability<T> capability, T instance, Direction side)
{
if (!(instance instanceof FluidTank))
throw new RuntimeException("Cannot serialize to an instance that isn't the default implementation");
CompoundNBT nbt = new CompoundNBT();
FluidTank tank = (FluidTank) instance;
FluidStack fluid = tank.getFluid();
fluid.writeToNBT(nbt);
nbt.putInt("Capacity", tank.getCapacity());
return nbt;
}
@Override
public void readNBT(Capability<T> capability, T instance, Direction side, INBT nbt)
{
if (!(instance instanceof FluidTank))
throw new RuntimeException("Cannot deserialize to an instance that isn't the default implementation");
CompoundNBT tags = (CompoundNBT) nbt;
FluidTank tank = (FluidTank) instance;
tank.setCapacity(tags.getInt("Capacity"));
tank.readFromNBT(tags);
}
public void readNBT(Capability<T> capability, T instance, Direction side, INBT nbt)
{
if (!(instance instanceof FluidTank))
throw new RuntimeException("Cannot deserialize to an instance that isn't the default implementation");
CompoundNBT tags = (CompoundNBT) nbt;
FluidTank tank = (FluidTank) instance;
tank.setCapacity(tags.getInt("Capacity"));
tank.readFromNBT(tags);
}
}
}
@@ -65,7 +65,7 @@ public interface IFluidHandler
* </p>
*
* @param tank Tank to query.
* @return FluidStack in a given tank. NULL if the tank is empty.
* @return FluidStack in a given tank. FluidStack.EMPTY if the tank is empty.
*/
@Nonnull
FluidStack getFluidInTank(int tank);
@@ -35,7 +35,7 @@ import java.util.stream.Stream;
import javax.annotation.Nullable;
public final class RegistryObject<T extends IForgeRegistryEntry<? super T>>
public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> implements Supplier<T>
{
private final ResourceLocation name;
@Nullable
@@ -21,6 +21,8 @@ package net.minecraftforge.registries;
import net.minecraft.util.ResourceLocation;
import java.util.function.Supplier;
/**
* A registry delegate for holding references to items or blocks
@@ -31,7 +33,7 @@ import net.minecraft.util.ResourceLocation;
*
* @param <T> the type of thing we're holding onto
*/
public interface IRegistryDelegate<T> {
public interface IRegistryDelegate<T> extends Supplier<T> {
/**
* Get the referent pointed at by this delegate. This will be the currently active item or block, and will change
* as world saves come and go. Note that item.delegate.get() may NOT be the same object as item, due to item and