From 9d528f3ba69cfdd1a9ead54e49d253038313bf97 Mon Sep 17 00:00:00 2001 From: UnlikePaladin <36827970+UnlikePaladin@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:04:29 -0600 Subject: [PATCH] Rework light switch and shower handle --- .../pfm/blocks/BasicShowerHandleBlock.java | 3 ++- .../pfm/blocks/LightSwitchBlock.java | 3 ++- .../blockentities/LightSwitchBlockEntity.java | 13 ++++++---- .../ShowerHandleBlockEntity.java | 25 ++++++++++--------- .../pfm/items/LightSwitchItem.java | 15 ++++++++--- .../pfm/items/ShowerHandleItem.java | 12 +++++---- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/com/unlikepaladin/pfm/blocks/BasicShowerHandleBlock.java b/common/src/main/java/com/unlikepaladin/pfm/blocks/BasicShowerHandleBlock.java index c32a43336..33d68227f 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/blocks/BasicShowerHandleBlock.java +++ b/common/src/main/java/com/unlikepaladin/pfm/blocks/BasicShowerHandleBlock.java @@ -107,7 +107,8 @@ public BlockState toggleOpen(BlockState state, World world, BlockPos pos, boolea state = state.cycle(POWERED);} world.setBlockState(pos, state, Block.NOTIFY_ALL); this.updateNeighbors(state, world, pos); - ((ShowerHandleBlockEntity)(world.getBlockEntity(pos))).setState(state.get(POWERED)); + if (world.getBlockEntity(pos) instanceof ShowerHandleBlockEntity) + ((ShowerHandleBlockEntity)(world.getBlockEntity(pos))).setState(state.get(POWERED)); return state; } diff --git a/common/src/main/java/com/unlikepaladin/pfm/blocks/LightSwitchBlock.java b/common/src/main/java/com/unlikepaladin/pfm/blocks/LightSwitchBlock.java index cdf5b51c0..08b3c05ec 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/blocks/LightSwitchBlock.java +++ b/common/src/main/java/com/unlikepaladin/pfm/blocks/LightSwitchBlock.java @@ -112,7 +112,8 @@ public BlockState togglePower(BlockState state, World world, BlockPos pos, boole state = state.cycle(POWERED);} world.setBlockState(pos, state, Block.NOTIFY_ALL); this.updateNeighbors(state, world, pos); - ((LightSwitchBlockEntity)world.getBlockEntity(pos)).setState(state.get(POWERED)); + if (world.getBlockEntity(pos) instanceof LightSwitchBlockEntity) + ((LightSwitchBlockEntity)world.getBlockEntity(pos)).setState(state.get(POWERED)); return state; } diff --git a/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/LightSwitchBlockEntity.java b/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/LightSwitchBlockEntity.java index feb2ea815..f9b49e486 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/LightSwitchBlockEntity.java +++ b/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/LightSwitchBlockEntity.java @@ -48,18 +48,21 @@ public void addLight(long pos) } } + + public void setState(boolean powered) { if(!lights.isEmpty()) { - lights.removeIf(lightPos -> + lights.removeIf(offset -> { - BlockState state = world.getBlockState(lightPos); + BlockState state = world.getBlockState(this.pos.subtract(offset)); return !(state.getBlock() instanceof PowerableBlock); }); - lights.forEach(lightPos -> + lights.forEach(offset -> { - BlockState state = world.getBlockState(lightPos); - ((PowerableBlock) state.getBlock()).setPowered(world, lightPos, powered); + BlockPos actualPos = this.pos.subtract(offset); + BlockState state = world.getBlockState(actualPos); + ((PowerableBlock) state.getBlock()).setPowered(world, actualPos, powered); }); diff --git a/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/ShowerHandleBlockEntity.java b/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/ShowerHandleBlockEntity.java index 5bf981f47..17b31324c 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/ShowerHandleBlockEntity.java +++ b/common/src/main/java/com/unlikepaladin/pfm/blocks/blockentities/ShowerHandleBlockEntity.java @@ -10,17 +10,17 @@ import net.minecraft.util.math.BlockPos; public class ShowerHandleBlockEntity extends BlockEntity { - protected BlockPos showerHead; + protected BlockPos showerOffset; public ShowerHandleBlockEntity(BlockPos pos, BlockState state) { super(BlockEntities.SHOWER_HANDLE_BLOCK_ENTITY, pos, state); - this.showerHead = null; + this.showerOffset = null; } @Override public NbtCompound writeNbt(NbtCompound nbt) { super.writeNbt(nbt); - if (this.showerHead != null) { - NbtLong showerHeadPos = NbtLong.of(this.showerHead.asLong()); + if (this.showerOffset != null) { + NbtLong showerHeadPos = NbtLong.of(this.showerOffset.asLong()); nbt.put("showerHead", showerHeadPos); } return nbt; @@ -30,21 +30,22 @@ public NbtCompound writeNbt(NbtCompound nbt) { public void readNbt(NbtCompound nbt) { super.readNbt(nbt); if(nbt.contains("showerHead", NbtElement.LONG_TYPE)){ - this.showerHead = BlockPos.fromLong(nbt.getLong("showerHead")); + this.showerOffset = BlockPos.fromLong(nbt.getLong("showerHead")); } } public void setState(boolean open) { - if (this.showerHead != null) { - if(this.world.getBlockEntity(this.showerHead) != null) { + if (this.showerOffset != null) { + BlockPos showerHeadPos = this.pos.subtract(this.showerOffset); + if(this.world.getBlockEntity(showerHeadPos) != null) { - BlockState state = world.getBlockState(this.showerHead); - ((ShowerHeadBlockEntity)world.getBlockEntity(this.showerHead)).setOpen(open); + BlockState state = world.getBlockState(showerHeadPos); + ((ShowerHeadBlockEntity)world.getBlockEntity(showerHeadPos)).setOpen(open); - world.updateListeners(this.showerHead, state, state, Block.NOTIFY_LISTENERS); - } else if (this.world.getBlockEntity(this.showerHead) == null) { - this.showerHead = null; + world.updateListeners(showerHeadPos, state, state, Block.NOTIFY_LISTENERS); + } else if (this.world.getBlockEntity(showerHeadPos) == null) { + this.showerOffset = null; } } } diff --git a/common/src/main/java/com/unlikepaladin/pfm/items/LightSwitchItem.java b/common/src/main/java/com/unlikepaladin/pfm/items/LightSwitchItem.java index fde1c37b0..4fe648344 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/items/LightSwitchItem.java +++ b/common/src/main/java/com/unlikepaladin/pfm/items/LightSwitchItem.java @@ -83,20 +83,27 @@ protected boolean canPlace(ItemPlacementContext context, BlockState state) { WorldView world = context.getWorld(); Direction side = context.getSide(); NbtList lights = getLights(context.getStack()); + if (!side.getAxis().isHorizontal()) { + return false; + } if (lights != null) { ArrayList removedLights = new ArrayList<>(); - Direction facing = context.getPlayerFacing(); - + ArrayList lightOffsets = new ArrayList<>(); for (Iterator iterator = lights.iterator(); iterator.hasNext();) { NbtElement nbtElement = iterator.next(); BlockPos lightPos = BlockPos.fromLong(((NbtLong) nbtElement).longValue()); - BlockPos placedPos = pos.offset(facing); - double distance = Math.sqrt(lightPos.getSquaredDistance(placedPos.getX() + 0.5, placedPos.getY() + 0.5, placedPos.getZ() + 0.5, true)); + double distance = Math.sqrt(lightPos.getSquaredDistance(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, true)); if (distance > 16) { removedLights.add(BlockPos.fromLong(((NbtLong) nbtElement).longValue())); iterator.remove(); + } else { + lightOffsets.add(pos.subtract(lightPos)); } } + context.getStack().setNbt(new NbtCompound()); + for (BlockPos blockPos : lightOffsets) { + addLight(context.getStack(), blockPos); + } if (!removedLights.isEmpty() && context.getWorld().isClient){ context.getPlayer().sendMessage(new TranslatableText("message.pfm.light_switch_far", removedLights.toString()), false); diff --git a/common/src/main/java/com/unlikepaladin/pfm/items/ShowerHandleItem.java b/common/src/main/java/com/unlikepaladin/pfm/items/ShowerHandleItem.java index e74dbc5ef..42410a61b 100644 --- a/common/src/main/java/com/unlikepaladin/pfm/items/ShowerHandleItem.java +++ b/common/src/main/java/com/unlikepaladin/pfm/items/ShowerHandleItem.java @@ -63,20 +63,22 @@ public ActionResult useOnBlock(ItemUsageContext context) { protected boolean canPlace(ItemPlacementContext context, BlockState state) { BlockPos pos = context.getBlockPos(); WorldView world = context.getWorld(); - NbtLong showerHeadPos = getShowerHead(context.getStack()); + NbtLong showerHeadLong = getShowerHead(context.getStack()); Direction playerFacing = context.getPlayerFacing(); Direction placeDirection = context.getSide(); - if (showerHeadPos != null) { - BlockPos lightPos = BlockPos.fromLong(showerHeadPos.longValue()); + if (showerHeadLong != null) { + BlockPos headPos = BlockPos.fromLong(showerHeadLong.longValue()); BlockPos placedPos = pos.offset(playerFacing); - double distance = Math.sqrt(lightPos.getSquaredDistance(placedPos.getX() + 0.5, placedPos.getY() + 0.5, placedPos.getZ() + 0.5, true)); + double distance = Math.sqrt(headPos.getSquaredDistance(placedPos.getX() + 0.5, placedPos.getY() + 0.5, placedPos.getZ() + 0.5, true)); if (distance > 16 && world.isClient()){ - context.getPlayer().sendMessage(new TranslatableText("message.pfm.shower_handle_far", lightPos.toString()), false); + context.getPlayer().sendMessage(new TranslatableText("message.pfm.shower_handle_far", headPos.toString()), false); } if (distance > 16) { context.getStack().setNbt(null); + } else { + setShowerHeadPosNBT(context.getStack(), pos.subtract(headPos)); } return state.getBlock().canPlaceAt(state, world, pos) && placeDirection.getAxis().isHorizontal(); }