package net.minecraft.world.inventory; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.EnderChestBlockEntity; import org.jetbrains.annotations.Nullable; public class PlayerEnderChestContainer extends SimpleContainer { @Nullable private EnderChestBlockEntity activeChest; public PlayerEnderChestContainer() { super(27); } public void setActiveChest(EnderChestBlockEntity enderChestBlockEntity) { this.activeChest = enderChestBlockEntity; } public boolean isActiveChest(EnderChestBlockEntity enderChest) { return this.activeChest == enderChest; } @Override public void fromTag(ListTag tag, HolderLookup.Provider levelRegistry) { for (int i = 0; i < this.getContainerSize(); i++) { this.setItem(i, ItemStack.EMPTY); } for (int i = 0; i < tag.size(); i++) { CompoundTag compoundTag = tag.getCompoundOrEmpty(i); int j = compoundTag.getByteOr("Slot", (byte)0) & 255; if (j >= 0 && j < this.getContainerSize()) { this.setItem(j, (ItemStack)ItemStack.parse(levelRegistry, compoundTag).orElse(ItemStack.EMPTY)); } } } @Override public ListTag createTag(HolderLookup.Provider levelRegistry) { ListTag listTag = new ListTag(); for (int i = 0; i < this.getContainerSize(); i++) { ItemStack itemStack = this.getItem(i); if (!itemStack.isEmpty()) { CompoundTag compoundTag = new CompoundTag(); compoundTag.putByte("Slot", (byte)i); listTag.add(itemStack.save(levelRegistry, compoundTag)); } } return listTag; } @Override public boolean stillValid(Player player) { return this.activeChest != null && !this.activeChest.stillValid(player) ? false : super.stillValid(player); } @Override public void startOpen(Player player) { if (this.activeChest != null) { this.activeChest.startOpen(player); } super.startOpen(player); } @Override public void stopOpen(Player player) { if (this.activeChest != null) { this.activeChest.stopOpen(player); } super.stopOpen(player); this.activeChest = null; } }