How do I make blocks copy and paste?

Discussion in 'Plugin Development' started by iceypotatoguy, Jul 13, 2020.

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

    iceypotatoguy

    I have this minigame plugin where players destroy blocks and such, and I was working on the part where the arena restores itself. Each minigame has its own arena, so I need my arena to copy all the blocks in a certain region, then paste it when the arena is over. The problem is, I don't know how to set chest contents, signs, etc. While working with chests, however, I was able to copy the contents of it, but when I restore it, the blocks were there but had a "0" right next to the items. How do I fix this? How do I restore blocks like how they were?

    I'm using commands for now so I can actively debug it.

    This is my copy and paste commands.
    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")) {
                    protectedBlocks.pasteBlocksInClipboard();
                    sender.sendMessage("pasted");
                }
    This is the code that stores the blocks.


    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);
            }
        }
      
        public void pasteBlocksInClipboard() {
            for (int i = 0; i < getBlockList().size(); i++) {
                //World world = ((CraftWorld) blockList.get(i).getWorld()).getHandle();
                //world.getType(new BlockPosition(blockList.get(i).getX(), blockList.get(i).getY(), blockList.get(i).getZ()));
                getBlock(i).setType(getMaterial(i));
                getBlock(i).setData(getBlockData(i));
                if (getBlock(i).getState() instanceof InventoryHolder) {
                    InventoryHolder protectedContainer = (InventoryHolder) getBlock(i).getState();
                    protectedContainer.getInventory().setContents(getInventory(i).getContents());
                }
            }
        }
     

    Attached Files:

  2. Offline

    timtower Administrator Administrator Moderator

    @iceypotatoguy 1. Please make a Map<Location, Blockinformation>, lists don't do the trick here.
    2. Why not use a worldedit schematic?
     
  3. Offline

    iceypotatoguy

    I don't use a WE schem because I would have to use the worldedit API. Wouldn't I? And also I'm using 1.8.8.
     
  4. Offline

    timtower Administrator Administrator Moderator

    You would, but why make something that has been made before?
     
  5. Offline

    iceypotatoguy

    The worldedit API is made for users using the latest versions of Minecraft. So I cannot use it. Also for BlockInformation, does it exist?
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    iceypotatoguy

    The API documentation is made for newer versions of MC. And I can't find one for 1.8.8. Also, does BlockInformation exist?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    iceypotatoguy

    Yes, the javadocs. But does it include documentation on how to copy and paste? I also do not want to have worldedit as a requrement for my plugin.
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    iceypotatoguy

    This only allows me to paste. How do I copy a schematic?
     
    Last edited: Jul 23, 2020
  12. Offline

    iceypotatoguy

    Do I have to use WorldEdit to save a schematic before I paste it? This code only allows me to paste schematic files.
     
Thread Status:
Not open for further replies.

Share This Page