From 79836c35a9fb9315e6e5dfa820bfc6ff02948b06 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:49:31 +0100 Subject: [PATCH] Fixes --- .../block/AbstractCauldronBlock.java.patch | 3 +- .../capabilities/BlockCapability.java | 3 ++ .../neoforge/fluids/CauldronFluidContent.java | 6 ++-- .../capability/wrappers/CauldronWrapper.java | 34 +++++++++++++------ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/patches/net/minecraft/world/level/block/AbstractCauldronBlock.java.patch b/patches/net/minecraft/world/level/block/AbstractCauldronBlock.java.patch index 2cc9a096a6..3ca300683b 100644 --- a/patches/net/minecraft/world/level/block/AbstractCauldronBlock.java.patch +++ b/patches/net/minecraft/world/level/block/AbstractCauldronBlock.java.patch @@ -1,12 +1,13 @@ --- a/net/minecraft/world/level/block/AbstractCauldronBlock.java +++ b/net/minecraft/world/level/block/AbstractCauldronBlock.java -@@ -102,4 +_,21 @@ +@@ -102,4 +_,22 @@ protected void receiveStalactiteDrip(BlockState p_151975_, Level p_151976_, BlockPos p_151977_, Fluid p_151978_) { } + + @Override + public void onPlace(BlockState p_51978_, Level p_51979_, BlockPos p_51980_, BlockState p_51981_, boolean p_51982_) { ++ super.onPlace(p_51978_, p_51979_, p_51980_, p_51981_, p_51982_); + // Neo: Invalidate cauldron capabilities when a cauldron is added + if (net.neoforged.neoforge.fluids.CauldronFluidContent.getForBlock(p_51981_.getBlock()) == null) { + p_51979_.invalidateCapabilities(p_51980_); diff --git a/src/main/java/net/neoforged/neoforge/capabilities/BlockCapability.java b/src/main/java/net/neoforged/neoforge/capabilities/BlockCapability.java index 564283229f..52b7aa223c 100644 --- a/src/main/java/net/neoforged/neoforge/capabilities/BlockCapability.java +++ b/src/main/java/net/neoforged/neoforge/capabilities/BlockCapability.java @@ -137,6 +137,9 @@ private BlockCapability(ResourceLocation name, Class typeClass, Class cont @ApiStatus.Internal @Nullable public T getCapability(Level level, BlockPos pos, @Nullable BlockState state, @Nullable BlockEntity blockEntity, C context) { + // Convert pos to immutable, it's easy to forget otherwise + pos = pos.immutable(); + // Get block state and block entity if they were not provided if (blockEntity == null) { if (state == null) diff --git a/src/main/java/net/neoforged/neoforge/fluids/CauldronFluidContent.java b/src/main/java/net/neoforged/neoforge/fluids/CauldronFluidContent.java index a332ba043b..a2f0ea44fe 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/CauldronFluidContent.java +++ b/src/main/java/net/neoforged/neoforge/fluids/CauldronFluidContent.java @@ -108,9 +108,9 @@ public static CauldronFluidContent getForFluid(Fluid fluid) { public static void init() { var registerEvent = new RegisterCauldronFluidContentEvent(); // Vanilla registrations - registerEvent.register(Blocks.CAULDRON, Fluids.EMPTY, 1000, null); - registerEvent.register(Blocks.WATER_CAULDRON, Fluids.WATER, 1000, LayeredCauldronBlock.LEVEL); - registerEvent.register(Blocks.LAVA_CAULDRON, Fluids.LAVA, 1000, null); + registerEvent.register(Blocks.CAULDRON, Fluids.EMPTY, FluidType.BUCKET_VOLUME, null); + registerEvent.register(Blocks.WATER_CAULDRON, Fluids.WATER, FluidType.BUCKET_VOLUME, LayeredCauldronBlock.LEVEL); + registerEvent.register(Blocks.LAVA_CAULDRON, Fluids.LAVA, FluidType.BUCKET_VOLUME, null); // Modded registrations ModLoader.get().postEvent(registerEvent); } diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java index 0a60f9cdd8..8056c808ae 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java @@ -24,7 +24,7 @@ public class CauldronWrapper implements IFluidHandler { public CauldronWrapper(Level level, BlockPos pos) { this.level = level; - this.pos = pos.immutable(); + this.pos = pos; } @Override @@ -95,9 +95,10 @@ public int fill(FluidStack resource, FluidAction action) { int amountIncrements = insertContent.totalAmount / d; int levelIncrements = insertContent.maxLevel / d; - int insertedIncrements = Math.min(resource.getAmount() / amountIncrements, (insertContent.maxLevel - currentContent.currentLevel(state)) / levelIncrements); + int currentLevel = currentContent.currentLevel(state); + int insertedIncrements = Math.min(resource.getAmount() / amountIncrements, (insertContent.maxLevel - currentLevel) / levelIncrements); if (insertedIncrements > 0) { - updateLevel(insertContent, currentContent.currentLevel(state) + insertedIncrements * levelIncrements, action); + updateLevel(insertContent, currentLevel + insertedIncrements * levelIncrements, action); } return insertedIncrements * amountIncrements; @@ -105,21 +106,29 @@ public int fill(FluidStack resource, FluidAction action) { @Override public FluidStack drain(FluidStack resource, FluidAction action) { - FluidStack current = getFluidInTank(0); - if (current.isFluidEqual(resource) && Objects.equals(current.getTag(), resource.getTag())) { - return drain(resource.getAmount(), action); + if (resource.isEmpty()) { + return FluidStack.EMPTY; + } + + BlockState state = level.getBlockState(pos); + if (getContent(state).fluid == resource.getFluid() && null == resource.getTag()) { + return drain(state, resource.getAmount(), action); } else { return FluidStack.EMPTY; } } + @Override public FluidStack drain(int maxDrain, FluidAction action) { if (maxDrain <= 0) { return FluidStack.EMPTY; } - BlockState state = level.getBlockState(pos); + return drain(level.getBlockState(pos), maxDrain, action); + } + + private FluidStack drain(BlockState state, int maxDrain, FluidAction action) { CauldronFluidContent content = getContent(state); // We can only extract increments based on the GCD between the number of levels and the total amount. @@ -127,15 +136,18 @@ public FluidStack drain(int maxDrain, FluidAction action) { int amountIncrements = content.totalAmount / d; int levelIncrements = content.maxLevel / d; - int extractedIncrements = Math.min(maxDrain / amountIncrements, content.currentLevel(state) / levelIncrements); + int currentLevel = content.currentLevel(state); + int extractedIncrements = Math.min(maxDrain / amountIncrements, currentLevel / levelIncrements); if (extractedIncrements > 0) { - int newLevel = content.currentLevel(state) - extractedIncrements * levelIncrements; + int newLevel = currentLevel - extractedIncrements * levelIncrements; if (newLevel == 0) { // Fully extract -> back to empty cauldron - level.setBlockAndUpdate(pos, Blocks.CAULDRON.defaultBlockState()); + if (action.execute()) { + level.setBlockAndUpdate(pos, Blocks.CAULDRON.defaultBlockState()); + } } else { // Otherwise just decrease levels - updateLevel(content, content.currentLevel(state) - extractedIncrements * levelIncrements, action); + updateLevel(content, newLevel, action); } }