mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 19:15:32 +00:00
[1.19.x] Fluid API Overhaul (#8695)
This commit is contained in:
1 parent
e386229b66
commit
2a05c7c01d
67 files changed
+3856
-740
No files matched your search
@@ -0,0 +1,218 @@
|
||||
package net.minecraftforge.debug.fluid;
|
||||
|
||||
import com.mojang.blaze3d.shaders.FogShape;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.math.Vector3f;
|
||||
import net.minecraft.client.Camera;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ClientLevel;
|
||||
import net.minecraft.client.renderer.FogRenderer;
|
||||
import net.minecraft.client.renderer.ItemBlockRenderTypes;
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.BucketItem;
|
||||
import net.minecraft.world.item.CreativeModeTab;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.LiquidBlock;
|
||||
import net.minecraft.world.level.block.state.BlockBehaviour;
|
||||
import net.minecraft.world.level.material.FlowingFluid;
|
||||
import net.minecraft.world.level.material.Fluid;
|
||||
import net.minecraft.world.level.material.FluidState;
|
||||
import net.minecraft.world.level.material.Material;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.IFluidTypeRenderProperties;
|
||||
import net.minecraftforge.client.RenderProperties;
|
||||
import net.minecraftforge.client.event.ColorHandlerEvent;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fluids.FluidInteractionRegistry;
|
||||
import net.minecraftforge.fluids.FluidType;
|
||||
import net.minecraftforge.fluids.ForgeFlowingFluid;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A test case used to define and test fluid type integration into fluids.
|
||||
*
|
||||
* <ul>
|
||||
* <li>Checks that each fluid has a fluid type</li>
|
||||
* <li>Adds a new fluid with its type, source/flowing, block, and bucket</li>
|
||||
* <li>Sets properties to test out fluid logic</li>
|
||||
* <li>Overrides fluid rendering methods</li>
|
||||
* <li>Adds block color and render layer</li>
|
||||
* <li>Adds fluid interaction definitions</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Mod(FluidTypeTest.ID)
|
||||
public class FluidTypeTest
|
||||
{
|
||||
private static final boolean ENABLE = true;
|
||||
|
||||
protected static final String ID = "fluid_type_test";
|
||||
private static Logger logger;
|
||||
|
||||
private static final DeferredRegister<FluidType> FLUID_TYPES = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, ID);
|
||||
private static final DeferredRegister<Fluid> FLUIDS = DeferredRegister.create(ForgeRegistries.FLUIDS, ID);
|
||||
private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, ID);
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ID);
|
||||
|
||||
private static ForgeFlowingFluid.Properties fluidProperties()
|
||||
{
|
||||
return new ForgeFlowingFluid.Properties(TEST_FLUID_TYPE, TEST_FLUID, TEST_FLUID_FLOWING)
|
||||
.block(TEST_FLUID_BLOCK)
|
||||
.bucket(TEST_FLUID_BUCKET);
|
||||
}
|
||||
|
||||
private static final RegistryObject<FluidType> TEST_FLUID_TYPE = FLUID_TYPES.register("test_fluid", () ->
|
||||
new FluidType(FluidType.Properties.create().supportsBoating(true).canHydrate(true))
|
||||
{
|
||||
@Override
|
||||
public void initializeClient(Consumer<IFluidTypeRenderProperties> consumer)
|
||||
{
|
||||
consumer.accept(new IFluidTypeRenderProperties()
|
||||
{
|
||||
private static final ResourceLocation STILL = new ResourceLocation("block/water_still"),
|
||||
FLOW = new ResourceLocation("block/water_flow"),
|
||||
OVERLAY = new ResourceLocation("block/obsidian"),
|
||||
VIEW_OVERLAY = new ResourceLocation("textures/block/obsidian.png");
|
||||
|
||||
@Override
|
||||
public ResourceLocation getStillTexture()
|
||||
{
|
||||
return STILL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getFlowingTexture()
|
||||
{
|
||||
return FLOW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getOverlayTexture()
|
||||
{
|
||||
return OVERLAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getRenderOverlayTexture(Minecraft mc)
|
||||
{
|
||||
return VIEW_OVERLAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColorTint()
|
||||
{
|
||||
return 0xAF7FFFD4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Vector3f modifyFogColor(Camera camera, float partialTick, ClientLevel level, int renderDistance, float darkenWorldAmount, Vector3f fluidFogColor)
|
||||
{
|
||||
int color = this.getColorTint();
|
||||
return new Vector3f((color >> 16 & 0xFF) / 255F, (color >> 8 & 0xFF) / 255F, (color & 0xFF) / 255F);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float renderDistance, float partialTick, float nearDistance, float farDistance, FogShape shape)
|
||||
{
|
||||
nearDistance = -8F;
|
||||
farDistance = 24F;
|
||||
|
||||
if (farDistance > renderDistance)
|
||||
{
|
||||
farDistance = renderDistance;
|
||||
shape = FogShape.CYLINDER;
|
||||
}
|
||||
|
||||
RenderSystem.setShaderFogStart(nearDistance);
|
||||
RenderSystem.setShaderFogEnd(farDistance);
|
||||
RenderSystem.setShaderFogShape(shape);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
private static final RegistryObject<FlowingFluid> TEST_FLUID = FLUIDS.register("test_fluid", () ->
|
||||
new ForgeFlowingFluid.Source(fluidProperties()));
|
||||
private static final RegistryObject<Fluid> TEST_FLUID_FLOWING = FLUIDS.register("test_fluid_flowing", () ->
|
||||
new ForgeFlowingFluid.Flowing(fluidProperties()));
|
||||
private static final RegistryObject<LiquidBlock> TEST_FLUID_BLOCK = BLOCKS.register("test_fluid_block", () ->
|
||||
new LiquidBlock(TEST_FLUID, BlockBehaviour.Properties.of(Material.WATER).noCollission().strength(100.0F).noLootTable()));
|
||||
private static final RegistryObject<Item> TEST_FLUID_BUCKET = ITEMS.register("test_fluid_bucket", () ->
|
||||
new BucketItem(TEST_FLUID, new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1).tab(CreativeModeTab.TAB_MISC)));
|
||||
|
||||
public FluidTypeTest()
|
||||
{
|
||||
if (ENABLE)
|
||||
{
|
||||
logger = LogManager.getLogger();
|
||||
ForgeMod.enableMilkFluid();
|
||||
|
||||
var modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
|
||||
FLUID_TYPES.register(modEventBus);
|
||||
FLUIDS.register(modEventBus);
|
||||
BLOCKS.register(modEventBus);
|
||||
ITEMS.register(modEventBus);
|
||||
|
||||
modEventBus.addListener(this::commonSetup);
|
||||
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> new FluidTypeTestClient(modEventBus));
|
||||
}
|
||||
}
|
||||
|
||||
private void commonSetup(FMLCommonSetupEvent event)
|
||||
{
|
||||
// Add Interactions for sources
|
||||
FluidInteractionRegistry.addInteraction(TEST_FLUID_TYPE.get(), new FluidInteractionRegistry.InteractionInformation(ForgeMod.LAVA_TYPE.get(), Blocks.GOLD_BLOCK.defaultBlockState()));
|
||||
FluidInteractionRegistry.addInteraction(ForgeMod.WATER_TYPE.get(), new FluidInteractionRegistry.InteractionInformation(TEST_FLUID_TYPE.get(), state -> state.isSource() ? Blocks.DIAMOND_BLOCK.defaultBlockState() : Blocks.IRON_BLOCK.defaultBlockState()));
|
||||
|
||||
// Log Fluid Types for all fluids
|
||||
event.enqueueWork(() ->
|
||||
ForgeRegistries.FLUIDS.forEach(fluid ->
|
||||
logger.info("Fluid {} has FluidType {}", ForgeRegistries.FLUIDS.getKey(fluid), ForgeRegistries.FLUID_TYPES.get().getKey(fluid.getFluidType())))
|
||||
);
|
||||
}
|
||||
|
||||
private static class FluidTypeTestClient
|
||||
{
|
||||
private FluidTypeTestClient(IEventBus modEventBus)
|
||||
{
|
||||
modEventBus.addListener(this::clientSetup);
|
||||
modEventBus.addListener(this::registerBlockColors);
|
||||
}
|
||||
|
||||
private void clientSetup(FMLClientSetupEvent event)
|
||||
{
|
||||
Stream.of(TEST_FLUID, TEST_FLUID_FLOWING).map(RegistryObject::get)
|
||||
.forEach(fluid -> ItemBlockRenderTypes.setRenderLayer(fluid, RenderType.translucent()));
|
||||
}
|
||||
|
||||
private void registerBlockColors(ColorHandlerEvent.Block event)
|
||||
{
|
||||
event.getBlockColors().register((state, getter, pos, index) ->
|
||||
{
|
||||
if (getter != null && pos != null)
|
||||
{
|
||||
FluidState fluidState = getter.getFluidState(pos);
|
||||
return RenderProperties.get(fluidState).getColorTint(fluidState, getter, pos);
|
||||
} else return 0xAF7FFFD4;
|
||||
}, TEST_FLUID_BLOCK.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,9 @@ import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraftforge.client.IFluidTypeRenderProperties;
|
||||
import net.minecraftforge.fluids.DispenseFluidContainer;
|
||||
import net.minecraftforge.fluids.FluidType;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import net.minecraft.world.level.material.FlowingFluid;
|
||||
@@ -20,7 +22,6 @@ import net.minecraft.world.level.material.Fluid;
|
||||
import net.minecraft.world.level.material.Fluids;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fluids.FluidAttributes;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.ForgeFlowingFluid;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
@@ -44,6 +45,9 @@ import net.minecraft.world.level.block.DispenserBlock;
|
||||
import net.minecraft.world.level.block.LiquidBlock;
|
||||
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Mod(NewFluidTest.MODID)
|
||||
public class NewFluidTest
|
||||
@@ -57,15 +61,50 @@ public class NewFluidTest
|
||||
|
||||
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
|
||||
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
|
||||
public static final DeferredRegister<FluidType> FLUID_TYPES = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, MODID);
|
||||
public static final DeferredRegister<Fluid> FLUIDS = DeferredRegister.create(ForgeRegistries.FLUIDS, MODID);
|
||||
|
||||
private static ForgeFlowingFluid.Properties makeProperties()
|
||||
{
|
||||
return new ForgeFlowingFluid.Properties(test_fluid, test_fluid_flowing,
|
||||
FluidAttributes.builder(FLUID_STILL, FLUID_FLOWING).overlay(FLUID_OVERLAY).color(0x3F1080FF))
|
||||
return new ForgeFlowingFluid.Properties(test_fluid_type, test_fluid, test_fluid_flowing)
|
||||
.bucket(test_fluid_bucket).block(test_fluid_block);
|
||||
}
|
||||
|
||||
public static RegistryObject<FluidType> test_fluid_type = FLUID_TYPES.register("test_fluid", () -> new FluidType(FluidType.Properties.create())
|
||||
{
|
||||
@Override
|
||||
public void initializeClient(Consumer<IFluidTypeRenderProperties> consumer)
|
||||
{
|
||||
consumer.accept(new IFluidTypeRenderProperties()
|
||||
{
|
||||
@Override
|
||||
public ResourceLocation getStillTexture()
|
||||
{
|
||||
return FLUID_STILL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getFlowingTexture()
|
||||
{
|
||||
return FLUID_FLOWING;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ResourceLocation getOverlayTexture()
|
||||
{
|
||||
return FLUID_OVERLAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColorTint()
|
||||
{
|
||||
return 0x3F1080FF;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
public static RegistryObject<FlowingFluid> test_fluid = FLUIDS.register("test_fluid", () ->
|
||||
new ForgeFlowingFluid.Source(makeProperties())
|
||||
);
|
||||
@@ -98,6 +137,7 @@ public class NewFluidTest
|
||||
|
||||
BLOCKS.register(modEventBus);
|
||||
ITEMS.register(modEventBus);
|
||||
FLUID_TYPES.register(modEventBus);
|
||||
FLUIDS.register(modEventBus);
|
||||
}
|
||||
}
|
||||
@@ -106,9 +146,9 @@ public class NewFluidTest
|
||||
{
|
||||
// some sanity checks
|
||||
BlockState state = Fluids.WATER.defaultFluidState().createLegacyBlock();
|
||||
BlockState state2 = Fluids.WATER.getAttributes().getBlock(null,null,Fluids.WATER.defaultFluidState());
|
||||
BlockState state2 = Fluids.WATER.getFluidType().getBlockForFluidState(null,null,Fluids.WATER.defaultFluidState());
|
||||
Validate.isTrue(state.getBlock() == Blocks.WATER && state2 == state);
|
||||
ItemStack stack = Fluids.WATER.getAttributes().getBucket(new FluidStack(Fluids.WATER, 1));
|
||||
ItemStack stack = Fluids.WATER.getFluidType().getBucket(new FluidStack(Fluids.WATER, 1));
|
||||
Validate.isTrue(stack.getItem() == Fluids.WATER.getBucket());
|
||||
event.enqueueWork(() -> DispenserBlock.registerBehavior(test_fluid_bucket.get(), DispenseFluidContainer.getInstance()));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user