From 71d21cd98a984cbc97ac4e0e67ae95ce5f756edf Mon Sep 17 00:00:00 2001 From: quat1024 Date: Thu, 6 Jul 2023 01:14:50 -0400 Subject: [PATCH] Waterlog the slope blocks --- .../api/TemplateInteractionUtil.java | 5 ++- .../templates/block/TemplateSlopeBlock.java | 19 ++++---- .../block/WaterloggableTemplateBlock.java | 45 +++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/templates/block/WaterloggableTemplateBlock.java diff --git a/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java b/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java index 4b3a53f..9c059f0 100644 --- a/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java +++ b/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java @@ -50,7 +50,10 @@ public class TemplateInteractionUtil { } public static BlockState setDefaultStates(BlockState in) { - return in.with(LIGHT, false).with(REDSTONE, false).with(SOLID, true); + if(in.contains(LIGHT)) in = in.with(LIGHT, false); + if(in.contains(REDSTONE)) in = in.with(REDSTONE, false); + if(in.contains(SOLID)) in = in.with(SOLID, true); + return in; } public static ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java index bae06a3..fc0494c 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java @@ -21,7 +21,7 @@ import net.minecraft.world.BlockView; import javax.annotation.Nullable; -public class TemplateSlopeBlock extends TemplateBlock { +public class TemplateSlopeBlock extends WaterloggableTemplateBlock { public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; public static final EnumProperty HALF = Properties.BLOCK_HALF; @@ -51,15 +51,16 @@ public class TemplateSlopeBlock extends TemplateBlock { @Nullable @Override public BlockState getPlacementState(ItemPlacementContext ctx) { - BlockHalf half = switch(ctx.getSide()) { - case UP -> BlockHalf.BOTTOM; - case DOWN -> BlockHalf.TOP; - default -> (ctx.getHitPos().getY() - (double) ctx.getBlockPos().getY() < 0.5) ? BlockHalf.BOTTOM : BlockHalf.TOP; - }; - - return getDefaultState() + BlockState sup = super.getPlacementState(ctx); + if(sup != null) sup = sup .with(FACING, ctx.getHorizontalPlayerFacing()) - .with(HALF, half); + .with(HALF, switch(ctx.getSide()) { + case UP -> BlockHalf.BOTTOM; + case DOWN -> BlockHalf.TOP; + default -> (ctx.getHitPos().getY() - (double) ctx.getBlockPos().getY() < 0.5) ? BlockHalf.BOTTOM : BlockHalf.TOP; + }); + + return sup; } @Override diff --git a/src/main/java/io/github/cottonmc/templates/block/WaterloggableTemplateBlock.java b/src/main/java/io/github/cottonmc/templates/block/WaterloggableTemplateBlock.java new file mode 100644 index 0000000..eeb7b5d --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/block/WaterloggableTemplateBlock.java @@ -0,0 +1,45 @@ +package io.github.cottonmc.templates.block; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Waterloggable; +import net.minecraft.fluid.FluidState; +import net.minecraft.fluid.Fluids; +import net.minecraft.item.ItemPlacementContext; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.Properties; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.WorldAccess; +import org.jetbrains.annotations.Nullable; + +public class WaterloggableTemplateBlock extends TemplateBlock implements Waterloggable { + public WaterloggableTemplateBlock(Settings settings) { + super(settings); + + setDefaultState(getDefaultState().with(Properties.WATERLOGGED, false)); + } + + @Override + protected void appendProperties(StateManager.Builder builder) { + super.appendProperties(builder.add(Properties.WATERLOGGED)); + } + + @Nullable + @Override + public BlockState getPlacementState(ItemPlacementContext ctx) { + BlockState sup = super.getPlacementState(ctx); + if(sup != null) sup = sup.with(Properties.WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).isOf(Fluids.WATER)); + return sup; + } + + @Override + public FluidState getFluidState(BlockState state) { + return state.get(Properties.WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state); + } + + public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState otherState, WorldAccess world, BlockPos pos, BlockPos moved) { + if(state.get(Properties.WATERLOGGED)) world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); + return super.getStateForNeighborUpdate(state, direction, otherState, world, pos, moved); + } +}