Skip to content

Commit

Permalink
Fix Assembler deleting Items
Browse files Browse the repository at this point in the history
  • Loading branch information
TheidenHD committed Oct 28, 2024
1 parent 4325a77 commit 7ca6118
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
81 changes: 49 additions & 32 deletions src/main/java/vazkii/psi/common/block/tile/TileCADAssembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,8 @@ public class TileCADAssembler extends BlockEntity implements ITileCADAssembler,
@ObjectHolder(registryName = "minecraft:block_entity_type", value = LibMisc.PREFIX_MOD + LibBlockNames.CAD_ASSEMBLER)
public static BlockEntityType<TileCADAssembler> TYPE;

private final IItemHandlerModifiable inventory = new ItemStackHandler(6) {
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
if(0 < slot && slot < 6) {
clearCachedCAD();
}
}
private final CADStackHandler inventory = new CADStackHandler(6);

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
if(stack.isEmpty()) {
return true;
}

if(slot == 0) {
return ISocketable.isSocketable(stack);
} else if(slot < 6) {
return stack.getItem() instanceof ICADComponent &&
((ICADComponent) stack.getItem()).getComponentType(stack) == EnumCADComponent.values()[slot - 1];
}

return false;
}
};
private final IItemHandler publicInv = new IItemHandler() {
@Override
public int getSlots() {
Expand Down Expand Up @@ -237,11 +214,7 @@ public boolean isBulletSlotEnabled(int slot) {
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
NonNullList<ItemStack> items = NonNullList.withSize(inventory.getSlots(), ItemStack.EMPTY);
for(int i = 0; i < inventory.getSlots(); i++) {
items.set(i, inventory.getStackInSlot(i));
}
ContainerHelper.saveAllItems(tag, items);
ContainerHelper.saveAllItems(tag, inventory.getItems());
}

@Override
Expand All @@ -250,10 +223,13 @@ public void load(CompoundTag cmp) {
readPacketNBT(cmp);
}



public void readPacketNBT(@Nonnull CompoundTag tag) {
// Migrate old CAD assemblers to the new format
if(tag.getInt("version") < 1) {
ListTag items = tag.getList("Items", 10);
ListTag items = tag.getList("Items", 10);
if(items.size() == 19) {

for(int i = 0; i < inventory.getSlots(); i++) {
inventory.setStackInSlot(i, ItemStack.EMPTY);
}
Expand Down Expand Up @@ -295,7 +271,7 @@ public void readPacketNBT(@Nonnull CompoundTag tag) {
}
}
} else {
//CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(inventory, null, tag.getList("Items", Tag.TAG_COMPOUND)); //TODO Fix this but we need answers
ContainerHelper.loadAllItems(tag, inventory.getItems());
}
}

Expand Down Expand Up @@ -324,4 +300,45 @@ public Component getDisplayName() {
public AbstractContainerMenu createMenu(int i, Inventory playerInventory, Player playerEntity) {
return new ContainerCADAssembler(i, playerInventory, this);
}

private class CADStackHandler extends ItemStackHandler {


private CADStackHandler(int size) {
super(size);
}

private NonNullList<ItemStack> getItems() {
return this.stacks;
}

private void setItems(NonNullList<ItemStack> pItems) {
this.stacks = pItems;
}

@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
if (0 < slot && slot < 6) {
clearCachedCAD();
}
setChanged();
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
if (stack.isEmpty()) {
return true;
}

if (slot == 0) {
return ISocketable.isSocketable(stack);
} else if (slot < 6) {
return stack.getItem() instanceof ICADComponent &&
((ICADComponent) stack.getItem()).getComponentType(stack).ordinal() == slot - 1;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public int getColor(@Nonnull ItemStack stack) {
return ICADColorizer.DEFAULT_SPELL_COLOR;
}

public void setColor(ItemStack stack, int color) {
}

@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
consumer.accept(new IClientItemExtensions() {
Expand Down

0 comments on commit 7ca6118

Please sign in to comment.