Trying to copy and paste chests.

Discussion in 'Plugin Development' started by iceypotatoguy, Mar 8, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    iceypotatoguy

    I made a plugin where a wand selects a region and copies all the blocks in the region. For some reason, when it copies chests, the items inside are all zeroed when pasted. I broke the chest before I pasted the selection.

    Code:
    public class BlockClipboard {
    
        private ArrayList<SavedBlockInfo> blockList;
    
        public void addBlock(Block block) {
            if (block.getState() instanceof InventoryHolder) {
                InventoryHolder invHolder = (InventoryHolder) block.getState();
                blockList.add(new SavedBlockInfo(block, block.getType(), block.getData(), block.getState(), invHolder.getInventory(), invHolder.getInventory().getContents()));
            }
            else {
                blockList.add(new SavedBlockInfo(block, block.getType(), block.getData(), block.getState()));
            }
        }
    }
    
    Code:
    public class SavedBlockInfo {
       
        private Block block;
        private Material material;
        private byte blockData;
        private BlockState blockState;
        private MaterialData materialData;
        private Inventory blockInventory;
        private ItemStack[] itemStack;
        private boolean isContainer;
       
        //Regular Block
        public SavedBlockInfo(Block block, Material material, byte blockData, BlockState blockState) {
            this.block = block;
            this.material = material;
            this.blockData = blockData;
            this.blockState = blockState;
        }
       
        //container
        public SavedBlockInfo(Block block, Material material, byte blockData, BlockState blockState, Inventory blockInventory, ItemStack[] itemStack) {
            this.block = block;
            this.material = material;
            this.blockData = blockData;
            this.blockState = blockState;
            this.blockInventory = blockInventory;
            this.itemStack = itemStack;
            this.isContainer = true;
        }
    }
    
    I also noticed, that when I break the chest and read the contents of that broken chest, nothing is there. It should be saved in the SavedBlockInfo class in the itemStack field.

    I finally figured it out. It is apparently a bug. I had to clone each individual ItemStack and put it into a new ItemStack array.
    Code:
     private ItemStack[] cloneItemStack(ItemStack[] input) {
     ItemStack[] newItemStack = new ItemStack[input.length];
     int i = 0;
     for (ItemStack itemstack : input) {
     if (itemstack != null) {
     newItemStack[i] = itemstack.clone();
     }
     i++;
     }
     return newItemStack;
     }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 8, 2021
Thread Status:
Not open for further replies.

Share This Page