From e758126529019b5bc644beb626ffe775f751af91 Mon Sep 17 00:00:00 2001 From: David Quintana Date: Wed, 6 May 2020 13:56:03 +0200 Subject: [PATCH] Backport a7e4cdcc49afb5bd68d252e54a0207830e36f964 (Bucket unable to place custom fluid on block with custom fluid-loggable implementation) --- .../net/minecraft/item/BucketItem.java.patch | 35 +++++- .../debug/fluid/NewFluidTest.java | 104 ++++++++++++++++-- .../blockstates/fluidloggable_block.json | 5 + 3 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/assets/new_fluid_test/blockstates/fluidloggable_block.json diff --git a/patches/minecraft/net/minecraft/item/BucketItem.java.patch b/patches/minecraft/net/minecraft/item/BucketItem.java.patch index d253969e76..5e842e1045 100644 --- a/patches/minecraft/net/minecraft/item/BucketItem.java.patch +++ b/patches/minecraft/net/minecraft/item/BucketItem.java.patch @@ -41,7 +41,35 @@ ItemStack itemstack1 = this.func_150910_a(itemstack, p_77659_2_, fluid.func_204524_b()); if (!p_77659_1_.field_72995_K) { CriteriaTriggers.field_204813_j.func_204817_a((ServerPlayerEntity)p_77659_2_, new ItemStack(fluid.func_204524_b())); -@@ -147,7 +164,19 @@ +@@ -65,7 +82,7 @@ + return new ActionResult<>(ActionResultType.FAIL, itemstack); + } else { + BlockState blockstate = p_77659_1_.func_180495_p(blockpos); +- BlockPos blockpos1 = blockstate.func_177230_c() instanceof ILiquidContainer && this.field_77876_a == Fluids.field_204546_a ? blockpos : blockraytraceresult.func_216350_a().func_177972_a(blockraytraceresult.func_216354_b()); ++ BlockPos blockpos1 = canBlockContainFluid(p_77659_1_, blockpos, blockstate) ? blockpos : blockraytraceresult.func_216350_a().func_177972_a(blockraytraceresult.func_216354_b()); + if (this.func_180616_a(p_77659_2_, p_77659_1_, blockpos1, blockraytraceresult)) { + this.func_203792_a(p_77659_1_, itemstack, blockpos1); + if (p_77659_2_ instanceof ServerPlayerEntity) { +@@ -116,7 +133,8 @@ + Material material = blockstate.func_185904_a(); + boolean flag = !material.func_76220_a(); + boolean flag1 = material.func_76222_j(); +- if (p_180616_2_.func_175623_d(p_180616_3_) || flag || flag1 || blockstate.func_177230_c() instanceof ILiquidContainer && ((ILiquidContainer)blockstate.func_177230_c()).func_204510_a(p_180616_2_, p_180616_3_, blockstate, this.field_77876_a)) { ++ boolean canContainFluid = canBlockContainFluid(p_180616_2_, p_180616_3_, blockstate); ++ if (p_180616_2_.func_175623_d(p_180616_3_) || flag || flag1 || canContainFluid) { + if (p_180616_2_.field_73011_w.func_177500_n() && this.field_77876_a.func_207185_a(FluidTags.field_206959_a)) { + int i = p_180616_3_.func_177958_n(); + int j = p_180616_3_.func_177956_o(); +@@ -126,7 +144,7 @@ + for(int l = 0; l < 8; ++l) { + p_180616_2_.func_195594_a(ParticleTypes.field_197594_E, (double)i + Math.random(), (double)j + Math.random(), (double)k + Math.random(), 0.0D, 0.0D, 0.0D); + } +- } else if (blockstate.func_177230_c() instanceof ILiquidContainer && this.field_77876_a == Fluids.field_204546_a) { ++ } else if (canContainFluid) { + if (((ILiquidContainer)blockstate.func_177230_c()).func_204509_a(p_180616_2_, p_180616_3_, blockstate, ((FlowingFluid)this.field_77876_a).func_207204_a(false))) { + this.func_203791_b(p_180616_1_, p_180616_2_, p_180616_3_); + } +@@ -147,7 +165,24 @@ } protected void func_203791_b(@Nullable PlayerEntity p_203791_1_, IWorld p_203791_2_, BlockPos p_203791_3_) { @@ -61,4 +89,9 @@ + + private final java.util.function.Supplier fluidSupplier; + public Fluid getFluid() { return fluidSupplier.get(); } ++ ++ private boolean canBlockContainFluid(World worldIn, BlockPos posIn, BlockState blockstate) ++ { ++ return blockstate.func_177230_c() instanceof ILiquidContainer && ((ILiquidContainer)blockstate.func_177230_c()).func_204510_a(worldIn, posIn, blockstate, this.field_77876_a); ++ } } diff --git a/src/test/java/net/minecraftforge/debug/fluid/NewFluidTest.java b/src/test/java/net/minecraftforge/debug/fluid/NewFluidTest.java index 914c791936..a5eba738f7 100644 --- a/src/test/java/net/minecraftforge/debug/fluid/NewFluidTest.java +++ b/src/test/java/net/minecraftforge/debug/fluid/NewFluidTest.java @@ -19,16 +19,20 @@ package net.minecraftforge.debug.fluid; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; -import net.minecraft.block.FlowingFluidBlock; +import net.minecraft.block.*; import net.minecraft.block.material.Material; import net.minecraft.fluid.FlowingFluid; import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluids; +import net.minecraft.fluid.IFluidState; import net.minecraft.item.*; +import net.minecraft.state.BooleanProperty; +import net.minecraft.state.StateContainer; +import net.minecraft.util.Direction; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockReader; +import net.minecraft.world.IWorld; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fluids.FluidAttributes; import net.minecraftforge.fluids.FluidStack; @@ -41,6 +45,8 @@ import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import org.apache.commons.lang3.Validate; +import javax.annotation.Nullable; + @Mod(NewFluidTest.MODID) public class NewFluidTest { @@ -53,11 +59,18 @@ public class NewFluidTest public static final DeferredRegister ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, MODID); public static final DeferredRegister FLUIDS = new DeferredRegister<>(ForgeRegistries.FLUIDS, MODID); + private static ForgeFlowingFluid.Properties makeProperties() + { + return new ForgeFlowingFluid.Properties(test_fluid, test_fluid_flowing, + FluidAttributes.builder(FLUID_STILL, FLUID_FLOWING).color(0x3F1080FF)) + .bucket(test_fluid_bucket).block(test_fluid_block); + } + public static RegistryObject test_fluid = FLUIDS.register("test_fluid", () -> - new ForgeFlowingFluid.Source(NewFluidTest.test_fluid_properties) + new ForgeFlowingFluid.Source(makeProperties()) ); public static RegistryObject test_fluid_flowing = FLUIDS.register("test_fluid_flowing", () -> - new ForgeFlowingFluid.Flowing(NewFluidTest.test_fluid_properties) + new ForgeFlowingFluid.Flowing(makeProperties()) ); public static RegistryObject test_fluid_block = BLOCKS.register("test_fluid_block", () -> @@ -67,9 +80,13 @@ public class NewFluidTest new BucketItem(test_fluid, new Item.Properties().containerItem(Items.BUCKET).maxStackSize(1).group(ItemGroup.MISC)) ); - public static final ForgeFlowingFluid.Properties test_fluid_properties = - new ForgeFlowingFluid.Properties(test_fluid, test_fluid_flowing, FluidAttributes.builder(FLUID_STILL, FLUID_FLOWING).color(0x3F1080FF)) - .bucket(test_fluid_bucket).block(test_fluid_block); + // WARNING: this doesn't allow "any fluid", only the fluid from this test mod! + public static RegistryObject fluidloggable_block = BLOCKS.register("fluidloggable_block", () -> + new FluidloggableBlock(Block.Properties.create(Material.WOOD).doesNotBlockMovement().hardnessAndResistance(100.0F).noDrops()) + ); + public static RegistryObject fluidloggable_blockitem = ITEMS.register("fluidloggable_block", () -> + new BlockItem(fluidloggable_block.get(), new Item.Properties().group(ItemGroup.MISC)) + ); public NewFluidTest() { @@ -91,4 +108,73 @@ public class NewFluidTest ItemStack stack = Fluids.WATER.getAttributes().getBucket(new FluidStack(Fluids.WATER, 1)); Validate.isTrue(stack.getItem() == Fluids.WATER.getFilledBucket()); } + + // WARNING: this doesn't allow "any fluid", only the fluid from this test mod! + private static class FluidloggableBlock extends Block implements IWaterLoggable + { + public static final BooleanProperty FLUIDLOGGED = BooleanProperty.create("fluidlogged"); + + public FluidloggableBlock(Properties properties) + { + super(properties); + setDefaultState(getStateContainer().getBaseState().with(FLUIDLOGGED, false)); + } + + @Override + protected void fillStateContainer(StateContainer.Builder builder) + { + builder.add(FLUIDLOGGED); + } + + @Nullable + @Override + public BlockState getStateForPlacement(BlockItemUseContext context) + { + IFluidState state = context.getWorld().getFluidState(context.getPos()); + return getDefaultState().with(FLUIDLOGGED, state.getFluid() == test_fluid.get()); + } + + public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) { + if (stateIn.get(FLUIDLOGGED)) { + worldIn.getPendingFluidTicks().scheduleTick(currentPos, test_fluid.get(), test_fluid.get().getTickRate(worldIn)); + } + + return super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos); + } + + @Override + public boolean canContainFluid(IBlockReader worldIn, BlockPos pos, BlockState state, Fluid fluidIn) { + return !state.get(FLUIDLOGGED) && fluidIn == test_fluid.get(); + } + + @Override + public boolean receiveFluid(IWorld worldIn, BlockPos pos, BlockState state, IFluidState fluidStateIn) { + if (canContainFluid(worldIn, pos, state, fluidStateIn.getFluid())) { + if (!worldIn.isRemote()) { + worldIn.setBlockState(pos, state.with(FLUIDLOGGED, true), 3); + worldIn.getPendingFluidTicks().scheduleTick(pos, fluidStateIn.getFluid(), fluidStateIn.getFluid().getTickRate(worldIn)); + } + + return true; + } else { + return false; + } + } + + @Override + public Fluid pickupFluid(IWorld worldIn, BlockPos pos, BlockState state) { + if (state.get(FLUIDLOGGED)) { + worldIn.setBlockState(pos, state.with(FLUIDLOGGED, false), 3); + return test_fluid.get(); + } else { + return Fluids.EMPTY; + } + } + + @Override + public IFluidState getFluidState(BlockState state) + { + return state.get(FLUIDLOGGED) ? test_fluid.get().getDefaultState() : Fluids.EMPTY.getDefaultState(); + } + } } diff --git a/src/test/resources/assets/new_fluid_test/blockstates/fluidloggable_block.json b/src/test/resources/assets/new_fluid_test/blockstates/fluidloggable_block.json new file mode 100644 index 0000000000..a627c6b913 --- /dev/null +++ b/src/test/resources/assets/new_fluid_test/blockstates/fluidloggable_block.json @@ -0,0 +1,5 @@ +{ + "variants": { + "": { "model": "minecraft:block/stone_brick_wall_post"} + } +} \ No newline at end of file