My TileEntity implements IInventory (I know it's outdated, but I don't think IItemHandler would solve my problem) and I want to change things in the inventory if setItem is called. However, the container only shows the change after one action delay or if I reopen the container.
Code (not finished):
@Override
public void setItem(int index, ItemStack stack) {
this.items.set(index, stack);
if (stack.getCount() > this.getMaxStackSize()) {
stack.setCount(this.getMaxStackSize());
}
//This is were the changes are not recognised instantaneously
//Important is that I change the list that stores the items
if(index == 9 || index == 10) {
ItemStack input = this.items.get(9);
int in = input.getCount();
if(in > 0) {
ItemStack output = this.items.get(10);
int out = output.getCount();
Item item;
if(input.getItem().equals(Items.BUCKET)) item = ItemInit.CREOSOTE_BUCKET.get();
else item = ItemInit.CREOSOTE_GLASS.get();
if(output.getItem().equals(item) || output.isEmpty()) {
ItemStack filledItem = new ItemStack(item);
int amount = Math.min(filledItem.getMaxStackSize() - out, in);
input.shrink(amount);
filledItem.setCount(out + amount);
items.set(10, filledItem);
}
}
}
this.setChanged();
}
I also did this:
//-----------------------------------------------------Synchronization-----------------------------------------------------
@Override
public SUpdateTileEntityPacket getUpdatePacket() {
return new SUpdateTileEntityPacket(this.getBlockPos(), 1, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
this.load(this.level.getBlockState(this.getBlockPos()), pkt.getTag());
}
@Override
public CompoundNBT getUpdateTag() {
return this.save(new CompoundNBT());
}
@Override
public void handleUpdateTag(BlockState state, CompoundNBT tag) {
this.load(state, tag);
}
What can I do to solve this problem? I wanted to use packets to sync server and client, but I don't have a player in this context.