Skip to content

Commit

Permalink
Rework light switch and shower handle
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlikePaladin committed Jan 2, 2024
1 parent bc99cdc commit 9d528f3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockPos> removedLights = new ArrayList<>();
Direction facing = context.getPlayerFacing();

ArrayList<BlockPos> lightOffsets = new ArrayList<>();
for (Iterator<NbtElement> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 9d528f3

Please sign in to comment.