From 2499d4e0c284677aa0d86ef1b43130a0dfdc6c47 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Sat, 23 Dec 2023 20:46:15 -0500 Subject: [PATCH] Restrict ornament placement positions that would intersect with a block --- .../java/xyz/nucleoid/extras/lobby/NEItems.java | 2 +- .../lobby/block/TreeDecorationBlockEntity.java | 15 +++++++++++++++ .../nucleoid/extras/lobby/tree/OrnamentModel.java | 14 ++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java b/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java index 118e7be..df3d413 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/NEItems.java @@ -856,7 +856,7 @@ private static ActionResult onUseBlock(PlayerEntity player, World world, Hand ha var lobbyState = PlayerLobbyState.get(player); - if (lobbyState.collectTaterFromBlock(world, pos, stack, player) == ActionResult.PASS && !(stack.getItem() instanceof TaterBoxItem) && hitResult.getSide() != Direction.UP) { + if (lobbyState.collectTaterFromBlock(world, pos, stack, player) == ActionResult.PASS && !(stack.getItem() instanceof TaterBoxItem)) { var state = world.getBlockState(pos); if (state.isIn(BlockTags.LEAVES)) { diff --git a/src/main/java/xyz/nucleoid/extras/lobby/block/TreeDecorationBlockEntity.java b/src/main/java/xyz/nucleoid/extras/lobby/block/TreeDecorationBlockEntity.java index 89fc463..7a677d7 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/block/TreeDecorationBlockEntity.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/block/TreeDecorationBlockEntity.java @@ -25,6 +25,7 @@ import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; @@ -97,6 +98,20 @@ public boolean placeOrnament(ServerPlayerEntity player, ServerWorld world, Hand if (item == Items.AIR) return false; var pos = hitResult.getPos(); + var side = hitResult.getSide(); + + if (side == Direction.UP) { + return false; + } else if (side != Direction.DOWN) { + var blockPos = hitResult.getBlockPos(); + var belowPos = blockPos.add(side.getOffsetX(), side.getOffsetY() - 1, side.getOffsetZ()); + + if (world.getBlockState(belowPos).isFullCube(world, belowPos)) { + double minY = blockPos.getY() + OrnamentModel.HEIGHT; + pos = pos.withAxis(Direction.Axis.Y, Math.max(minY, pos.getY())); + } + } + var offset = pos.subtract(this.pos.toCenterPos()); float yaw = player.getYaw() - 180; diff --git a/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java b/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java index 75da964..a398d29 100644 --- a/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java +++ b/src/main/java/xyz/nucleoid/extras/lobby/tree/OrnamentModel.java @@ -15,9 +15,13 @@ import net.minecraft.text.Text; import net.minecraft.util.Hand; import net.minecraft.util.math.MathHelper; +import net.minecraft.world.World; import xyz.nucleoid.extras.lobby.block.TreeDecorationBlockEntity; public final class OrnamentModel implements InteractionHandler { + public static final float WIDTH = 8 / 16f; + public static final float HEIGHT = 11 / 16f; + private final TreeDecorationBlockEntity blockEntity; private final Ornament ornament; @@ -48,7 +52,7 @@ public OrnamentModel(TreeDecorationBlockEntity blockEntity, Ornament ornament) { this.interaction = new InteractionElement(this); this.interaction.setOffset(ornament.offset().subtract(0, 0.6, 0)); - this.interaction.setSize(8 / 16f, 11 / 16f); + this.interaction.setSize(WIDTH, HEIGHT); this.updateTransformations(true); } @@ -85,8 +89,14 @@ public void attack(ServerPlayerEntity player) { } } + private long getTime() { + World world = this.blockEntity.getWorld(); + + return world == null ? 0 : world.getTime(); + } + private void updateTransformations(boolean initial) { - long time = this.blockEntity.getWorld().getTime(); + long time = this.getTime(); float rotation; if (this.wobbleTicks > 0) {