Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
Expand Down Expand Up @@ -76,7 +77,35 @@ public ControllableDrawerTile(BasicTileBlock<T> base, BlockEntityType<T> entityT
}

}

@Override
public void onLoad() {
super.onLoad();
if (level == null || level.isClientSide()) return;

// Loaded at a new location after block movement
// If controllerPos exists but current position is missing from Controller list -> Moved
if (controllerPos != null) {
BlockEntity be = level.getBlockEntity(controllerPos);
if (be instanceof StorageControllerTile<?> controllerTile) {
boolean isInController = controllerTile.getConnectedDrawers()
.getConnectedDrawers()
.contains(this.getBlockPos().asLong());

if (!isInController) {
// Moved Drawer: Remove old link + Force cache reconstruction
controllerTile.getConnectedDrawers()
.getConnectedDrawers()
.removeIf(aLong -> aLong == this.getBlockPos().asLong());
this.controllerPos = null;
controllerTile.getConnectedDrawers().rebuild(); // selectors[] 재구성
controllerTile.markForUpdate();
}
} else {
// No Controller found
this.controllerPos = null;
}
}
}
@Override
@OnlyIn(Dist.CLIENT)
public void initClient() {
Expand Down Expand Up @@ -298,11 +327,6 @@ public InventoryComponent<ControllableDrawerTile<T>> getStorageUpgrades() {
return storageUpgrades;
}

@Override
public void invalidateCaps() {
super.invalidateCaps();
}

public boolean isEverythingEmpty() {
for (int i = 0; i < getStorageUpgrades().getSlots(); i++) {
if (!getStorageUpgrades().getStackInSlot(i).isEmpty()) {
Expand All @@ -316,6 +340,17 @@ public boolean isEverythingEmpty() {
}
return true;
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
// Rebuild Controller cache immediately on caps invalidation
if (level != null && !level.isClientSide() && controllerPos != null) {
BlockEntity be = level.getBlockEntity(controllerPos);
if (be instanceof StorageControllerTile<?> controllerTile) {
controllerTile.getConnectedDrawers().rebuild();
}
}
}

public abstract InventoryComponent<ControllableDrawerTile<T>> getStorageUpgradesConstructor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ private HandlerSlotSelector selectorForSlot(int slot) {
@Override
public ItemStack getStackInSlot(int slot) {
HandlerSlotSelector selector = selectorForSlot(slot);
return null != selector ? selector.getStackInSlot() : ItemStack.EMPTY;
if (null == selector) return ItemStack.EMPTY;

// Block access to invalid handlers
if (!getDrawers().getItemHandlers().contains(selector.handler)) {
invalidateSlots();
return ItemStack.EMPTY;
}

return selector.getStackInSlot();
}

@NotNull
Expand All @@ -89,7 +97,16 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
HandlerSlotSelector selector = selectorForSlot(slot);
return null != selector ? selector.extractItem(amount, simulate) : ItemStack.EMPTY;
if (null == selector) return ItemStack.EMPTY;

//Verify if the handler is still valid before extraction
if (!getDrawers().getItemHandlers().contains(selector.handler)) {
// Invalid handler: Rebuild slots and return empty stack
invalidateSlots();
return ItemStack.EMPTY;
}

return selector.extractItem(amount, simulate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public void rebuild() {
}
var area = new AABB(controllerTile.getBlockPos()).inflate(FunctionalStorageConfig.DRAWER_CONTROLLER_LINKING_RANGE + extraRange);
this.connectedDrawers.removeIf(aLong -> !area.contains(Vec3.atCenterOf(BlockPos.of(aLong))));

//Check if the Drawer is present within the range.
this.connectedDrawers.removeIf(aLong -> {
BlockEntity entity = level.getBlockEntity(BlockPos.of(aLong));
return !(entity instanceof ItemControllableDrawerTile<?>)
&& !(entity instanceof FluidDrawerTile)
&& !(entity instanceof StorageControllerExtensionTile);
});

this.connectedDrawers.sort(Comparator.comparingDouble(value -> BlockPos.of(value).distSqr(controllerTile.getBlockPos())));
for (Long connectedDrawer : this.connectedDrawers) {
BlockPos pos = BlockPos.of(connectedDrawer);
Expand Down