diff --git a/src/main/java/com/buuz135/functionalstorage/block/DrawerControllerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/DrawerControllerBlock.java index 5422302..ebf0539 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/DrawerControllerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/DrawerControllerBlock.java @@ -2,6 +2,7 @@ package com.buuz135.functionalstorage.block; import com.buuz135.functionalstorage.FunctionalStorage; import com.buuz135.functionalstorage.block.tile.CompactingDrawerTile; +import com.buuz135.functionalstorage.block.tile.ControllableDrawerTile; import com.buuz135.functionalstorage.block.tile.DrawerControllerTile; import com.hrznstudio.titanium.block.RotatableBlock; import com.hrznstudio.titanium.util.TileUtil; @@ -19,6 +20,8 @@ import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.phys.BlockHitResult; import org.jetbrains.annotations.NotNull; +import java.util.Optional; + public class DrawerControllerBlock extends RotatableBlock { public DrawerControllerBlock() { @@ -48,4 +51,18 @@ public class DrawerControllerBlock extends RotatableBlock public InteractionResult use(BlockState state, Level worldIn, BlockPos pos, Player player, InteractionHand hand, BlockHitResult ray) { return TileUtil.getTileEntity(worldIn, pos, DrawerControllerTile.class).map(drawerTile -> drawerTile.onSlotActivated(player, hand, ray.getDirection(), ray.getLocation().x, ray.getLocation().y, ray.getLocation().z)).orElse(InteractionResult.PASS); } + + @Override + public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) { + TileUtil.getTileEntity(worldIn, pos, DrawerControllerTile.class).ifPresent(drawerControllerTile -> { + drawerControllerTile.getConnectedDrawers().getConnectedDrawers().stream() + .map(BlockPos::of) + .map(blockPos -> TileUtil.getTileEntity(worldIn, blockPos, ControllableDrawerTile.class)) + .filter(Optional::isPresent) + .map(Optional::get) + .forEach(controllableDrawerTile -> controllableDrawerTile.setControllerPos(null)); + }); + super.onRemove(state, worldIn, pos, newState, isMoving); + + } } diff --git a/src/main/java/com/buuz135/functionalstorage/block/tile/DrawerControllerTile.java b/src/main/java/com/buuz135/functionalstorage/block/tile/DrawerControllerTile.java index 9da60b6..35161a2 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/tile/DrawerControllerTile.java +++ b/src/main/java/com/buuz135/functionalstorage/block/tile/DrawerControllerTile.java @@ -168,13 +168,15 @@ public class DrawerControllerTile extends ControllableDrawerTile) { + if (area.contains(Vec3.atCenterOf(position)) && this.getLevel().getBlockEntity(position) instanceof ControllableDrawerTile controllableDrawerTile) { if (action == LinkingToolItem.ActionMode.ADD) { - if (!connectedDrawers.getConnectedDrawers().contains(position.asLong())) + controllableDrawerTile.setControllerPos(this.getBlockPos()); + if (!connectedDrawers.getConnectedDrawers().contains(position.asLong())){ this.connectedDrawers.getConnectedDrawers().add(position.asLong()); + } } } if (action == LinkingToolItem.ActionMode.REMOVE) { diff --git a/src/main/java/com/buuz135/functionalstorage/client/ControllerRenderer.java b/src/main/java/com/buuz135/functionalstorage/client/ControllerRenderer.java index dcb6e64..b0346f2 100644 --- a/src/main/java/com/buuz135/functionalstorage/client/ControllerRenderer.java +++ b/src/main/java/com/buuz135/functionalstorage/client/ControllerRenderer.java @@ -80,7 +80,7 @@ public class ControllerRenderer implements BlockEntityRenderer) blockEntity).setControllerPos(controller.getBlockPos()); ((DrawerControllerTile) controller).addConnectedDrawers(linkingAction, pos); if (linkingAction == ActionMode.ADD){ context.getPlayer().displayClientMessage(new TextComponent("Linked drawer to the controller").setStyle(Style.EMPTY.withColor(linkingMode.color)), true);