Make sure that drawers get disconnected from their old drawer controller properly, closes #64 closes #58 closes #52

This commit is contained in:
Buuz135 2022-08-07 19:02:17 +02:00
parent bc92c9084c
commit df75dfa8f0
5 changed files with 27 additions and 5 deletions

View File

@ -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<DrawerControllerTile> {
public DrawerControllerBlock() {
@ -48,4 +51,18 @@ public class DrawerControllerBlock extends RotatableBlock<DrawerControllerTile>
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);
}
}

View File

@ -168,15 +168,17 @@ public class DrawerControllerTile extends ControllableDrawerTile<DrawerControlle
}
public void addConnectedDrawers(LinkingToolItem.ActionMode action, BlockPos... positions) {
var area = new AABB(this.getBlockPos()).inflate( FunctionalStorageConfig.DRAWER_CONTROLLER_LINKING_RANGE);
var area = new AABB(this.getBlockPos()).inflate(FunctionalStorageConfig.DRAWER_CONTROLLER_LINKING_RANGE);
for (BlockPos position : positions) {
if (level.getBlockState(position).is(FunctionalStorage.DRAWER_CONTROLLER.getLeft().get())) continue;
if (area.contains(Vec3.atCenterOf(position)) && this.getLevel().getBlockEntity(position) instanceof 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) {
this.connectedDrawers.getConnectedDrawers().removeIf(aLong -> aLong == position.asLong());
}

View File

@ -80,7 +80,7 @@ public class ControllerRenderer implements BlockEntityRenderer<DrawerControllerT
BlockPos hit = ((BlockHitResult)result).getBlockPos();
AABB aabb = new AABB(Math.min(firstPos.getX(), hit.getX()), Math.min(firstPos.getY(), hit.getY()), Math.min(firstPos.getZ(), hit.getZ()), Math.max(firstPos.getX(), hit.getX()) + 1, Math.max(firstPos.getY(), hit.getY()) + 1, Math.max(firstPos.getZ(), hit.getZ()) + 1);
VoxelShape shape = Shapes.create(aabb);
LevelRenderer.renderVoxelShape(matrixStack, bufferIn.getBuffer(TYPE), shape, -controller.getX(), -controller.getY(), -controller.getZ(), 1f, 1f, 1f, 1f);
renderShape(matrixStack, bufferIn.getBuffer(TYPE), shape, -controller.getX(), -controller.getY(), -controller.getZ(), 1f, 1f, 1f, 1f);
return;
}
}

View File

@ -41,6 +41,7 @@ public abstract class ControllerInventoryHandler implements IItemHandler {
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
int index = 0;
for (IItemHandler handler : getDrawers().getHandlers()) {
if (handler instanceof ControllerInventoryHandler) continue;
int relativeIndex = slot - index;
if (relativeIndex < handler.getSlots()){
return handler.insertItem(relativeIndex, stack, simulate);
@ -55,6 +56,7 @@ public abstract class ControllerInventoryHandler implements IItemHandler {
public ItemStack extractItem(int slot, int amount, boolean simulate) {
int index = 0;
for (IItemHandler handler : getDrawers().getHandlers()) {
if (handler instanceof ControllerInventoryHandler) continue;
int relativeIndex = slot - index;
if (relativeIndex < handler.getSlots()){
return handler.extractItem(relativeIndex, amount, simulate);
@ -68,6 +70,7 @@ public abstract class ControllerInventoryHandler implements IItemHandler {
public int getSlotLimit(int slot) {
int index = 0;
for (IItemHandler handler : getDrawers().getHandlers()) {
if (handler instanceof ControllerInventoryHandler) continue;
int relativeIndex = slot - index;
if (relativeIndex < handler.getSlots()){
return handler.getSlotLimit(relativeIndex);
@ -81,6 +84,7 @@ public abstract class ControllerInventoryHandler implements IItemHandler {
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
int index = 0;
for (IItemHandler handler : getDrawers().getHandlers()) {
if (handler instanceof ControllerInventoryHandler) continue;
int relativeIndex = slot - index;
if (relativeIndex < handler.getSlots()){
return handler.isItemValid(relativeIndex, stack);

View File

@ -149,7 +149,6 @@ public class LinkingToolItem extends BasicItem {
BlockEntity controller = level.getBlockEntity(new BlockPos(controllerNBT.getInt("X"), controllerNBT.getInt("Y"), controllerNBT.getInt("Z")));
if (controller instanceof DrawerControllerTile) {
if (linkingMode == LinkingMode.SINGLE) {
((ControllableDrawerTile<?>) 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);