How do I store blocks in a variable and paste them for later?

Discussion in 'Plugin Development' started by iceypotatoguy, Jun 25, 2020.

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

    iceypotatoguy

    I am creating a minigame plugin that has arenas, and each arena has build regions. The build regions are supposed to copy all the blocks, and then when the minigame is over, I need to restore those blocks.

    People have suggested resetting the world by copying and pasting, but my minigame plugin is supposed to handle multiple games in the same world. I also was suggested to use the WorldEdit API, but the documentation is for WorldEdit 7.0, which does not work on 1.8.8.

    I wrote my own code, but I'm stuck at copying the chest's contents.

    EDIT: Copying the blocks and the chest's inventory worked, but when I ran the "paste" command, all the blocks were there, but when I opened the chest, the items were there, but they had zeros in front. How do I fix this?

    This is only part of my BlockClipboard object.
    Code:
    public class BlockClipboard {
    
        private ArrayList<Block> blockList;
        private ArrayList<Material> materialList;
        private ArrayList<BlockState> blockStates;
        private ArrayList<Byte> blockData;
        private ArrayList<MaterialData> materialDatas;
        private ArrayList<Inventory> blockInventories;
      
        public BlockClipboard() {
            blockList = new ArrayList<Block>();
            materialList = new ArrayList<Material>();
            blockStates = new ArrayList<BlockState>();
            blockData = new ArrayList<Byte>();
            materialDatas = new ArrayList<MaterialData>();
            blockInventories = new ArrayList<Inventory>();
        }
      
        public void addBlock(Block block) {
            for (int i = 0; i < blockList.size(); i++) {
                if (getBlock(i).getLocation().getBlockX() == block.getLocation().getBlockX() && getBlock(i).getLocation().getBlockY() == block.getLocation().getBlockY() && getBlock(i).getLocation().getBlockZ() == block.getLocation().getBlockZ()) {
                    blockList.set(i, block);
                    materialList.set(i, block.getType());
                    blockStates.set(i, block.getState());
                    blockData.set(i, block.getData());
                    if (block.getState() instanceof InventoryHolder) {
                        InventoryHolder container = (InventoryHolder) block.getState();
                        blockInventories.set(i, container.getInventory());
                      
                    }
                    else {
                        blockInventories.set(i, null);
                    }
                    return;
                }
            }
            blockList.add(block);
            materialList.add(block.getType());
            blockStates.add(block.getState());
            blockData.add(block.getData());
            if (block.getState() instanceof InventoryHolder) {
                InventoryHolder container = (InventoryHolder) block.getState();
                blockInventories.add(container.getInventory());
            }
            else {
                blockInventories.add(null);
            }
        }
    This is the code that handles the command to copy and paste.
    Code:
                else if (args[0].equalsIgnoreCase("addcopy")) {
                    if (protectedBlocks == null) {
                        protectedBlocks = new BlockClipboard();
                    }
                    if (!protectedBlocks.getBlockList().isEmpty()) {
                        sender.sendMessage("Clipboard was not empty");
                    }
                    for (int x = Math.min(wallsTool.getPos1().getBlockX(), wallsTool.getPos2().getBlockX()); x <= Math.max(wallsTool.getPos1().getBlockX(), wallsTool.getPos2().getBlockX()); x++) {
                        for (int y = Math.min(wallsTool.getPos1().getBlockY(), wallsTool.getPos2().getBlockY()); y <= Math.max(wallsTool.getPos1().getBlockY(), wallsTool.getPos2().getBlockY()); y++) {
                            for (int z = Math.min(wallsTool.getPos1().getBlockZ(), wallsTool.getPos2().getBlockZ()); z <= Math.max(wallsTool.getPos1().getBlockZ(), wallsTool.getPos2().getBlockZ()); z++) {
                                Location loc = new Location(wallsTool.getWorld(), x, y, z);
                                sender.sendMessage(loc.toString());
                                protectedBlocks.addBlock(loc.getBlock());
                            }
                        }
                    }
                    sender.sendMessage("copied");
                }
                else if (args[0].equalsIgnoreCase("paste")) {
                    for (int i = 0; i < protectedBlocks.getBlockList().size(); i++) {
                        protectedBlocks.getBlock(i).setType(protectedBlocks.getMaterial(i));
                        protectedBlocks.getBlock(i).setData(protectedBlocks.getBlockData(i));
                        if (protectedBlocks.getBlock(i).getState() instanceof InventoryHolder) {
                            InventoryHolder protectedContainer = (InventoryHolder) protectedBlocks.getBlock(i).getState();
                            protectedContainer.getInventory().setContents(protectedBlocks.getInventory(i).getContents());
                        }
    
                    }
                    sender.sendMessage("pasted");
                }
                else if (args[0].equalsIgnoreCase("listcopy")) {
                    if (protectedBlocks == null || protectedBlocks.getBlockList().size() == 0) {
                        sender.sendMessage("nothing");
                    }
                    else {
                        for (int i = 0; i < protectedBlocks.getBlockList().size(); i++) {
                            sender.sendMessage(protectedBlocks.getBlock(i).toString());
                            sender.sendMessage(protectedBlocks.getBlock(i).getState().toString());
                        }
                    }
                }
                else if (args[0].equalsIgnoreCase("clear")) {
                    if (protectedBlocks == null || protectedBlocks.getBlockList().size() == 0) {
                        sender.sendMessage("Empty");
                    }
                    else {
                        protectedBlocks.clear();
                        sender.sendMessage("cleared.");
                    }
                }
     
    Last edited: Jun 28, 2020
  2. Offline

    travja

    Simply stating that you're stuck on inventories can only help direct us to a problem so much. Would you mind explaining what you expect to happen and what result is happening instead?
     
  3. Offline

    iceypotatoguy

    Sorry I didn't explain it clearly enough. Copying the blocks and the chest's inventory worked, but when I ran the "paste" command, all the blocks were there, but when I opened the chest, the items were there, but they had zeros in front.

    Click Here for what I mean by zeros.
     
  4. Offline

    mAndAle

    You need to save the inventory and when do /paste load the inventory inside the chest, if i well interpetraded you no need to save permanently so you can use an hashmap and save the inventory in to.

    Example:

    Code:
    HashMap<UUID, List<ItemStack>> items = new HashMap<UUID, List<ItemStack>>();
    and after you put all of itemStack in the inventory
     
    Last edited: Jun 28, 2020
  5. Offline

    iceypotatoguy

    Code:
    protectedContainer.getInventory().setContents(protectedBlocks.getInventory(i).getContents());
    Doesn't this code already load the inventory into the chest?
     
Thread Status:
Not open for further replies.

Share This Page