How do I paste chest contents?

Discussion in 'Plugin Development' started by iceypotatoguy, Sep 10, 2020.

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

    iceypotatoguy

    Hello, I have been working on copying and pasting blocks using code for a little while now. I was able to copy blocks using code that I wrote, but when I paste blocks such as chests, this happens:


    Does anyone know why this is? How do I fix this?

    Code:
    public class BlockClipboard {
      
        private ArrayList<SavedBlockInfo> blockList;
      
      
        public BlockClipboard() {
            blockList = new ArrayList<SavedBlockInfo>();
        }
      
        public void addBlock(Block block) {
            if (block.getState() instanceof InventoryHolder) {
                InventoryHolder inventory = (InventoryHolder) block.getState();
                blockList.add(new SavedBlockInfo(block, block.getType(), block.getData(), inventory.getInventory(), inventory.getInventory().getContents()));
            }
            blockList.add(new SavedBlockInfo(block, block.getType(), block.getData()));
        }
      
        public void pasteBlocksInClipboard() {
            for (int i = 0; i < getBlockList().size(); i++) {
                blockList.get(i).getBlock().setType(blockList.get(i).getMaterial());
                blockList.get(i).getBlock().setData(blockList.get(i).getBlockData());
                if (blockList.get(i).isContainer()) {
                    InventoryHolder inv = (InventoryHolder) blockList.get(i).getBlock().getState();
                    inv.getInventory().setContents(blockList.get(i).getItemStack());
                }
            }
        }
    ...
    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) {
            this.block = block;
            this.material = material;
            this.blockData = blockData;
        }
      
        //container
        public SavedBlockInfo(Block block, Material material, byte blockData, Inventory blockInventory, ItemStack[] itemStack) {
            this.block = block;
            this.material = material;
            this.blockData = blockData;
            this.blockInventory = blockInventory;
            this.itemStack = itemStack;
            this.isContainer = true;
        }
    ...
    I am using bukkit 1.8.8
    I left out the imports, getters/setters so we can focus on the code that deals with pasting. If anyone needs the full version, please ask me.
     
    Last edited: Sep 10, 2020
  2. Offline

    Kars

  3. Offline

    iceypotatoguy

    I'm using bukkit 1.8.8, and I don't see setItemStack in the javadocs.
     
  4. Offline

    Kars

    @iceypotatoguy forget that.
    PHP:
    public void addBlock(Block block) {
            if (
    block.getState() instanceof InventoryHolder) {
                
    InventoryHolder inventory = (InventoryHolderblock.getState();
                
    blockList.add(new SavedBlockInfo(blockblock.getType(), block.getData(), inventory.getInventory(), inventory.getInventory().getContents()));
            }
            
    blockList.add(new SavedBlockInfo(blockblock.getType(), block.getData()));
        }
    This here adds the chest to the list with your non-container constructor after adding it with the container one. Add an else block.
     
  5. Offline

    iceypotatoguy

    I added an else, but it still didn't fix the chest issue.
     
  6. Offline

    Kars

    Try printing information about the itemstacks that are in the SavedBlockInfo objects at the time that you have it copied.
     
  7. Offline

    iceypotatoguy

    I wrote this code in the BlockClipboard class:
    Code:
        public String listItemStacks() {
            String output = "";
            for (int i = 0; i < blockList.size(); i++) {
                for (int j = 0; j < blockList.get(i).getItemStack().length; j++) {
                    output += blockList.get(i).getItemStack()[i] + "\n";
                }
            }
            return output;
        }
    I also wrote a command to execute that code. The image is what happened.
     

    Attached Files:

  8. Offline

    Kars

    Print only the itemstacks if they are not null.. use getMaterial and getAmount this time.

    to be safe, print it using the same check as when you copy the inventory, like so.
    PHP:
    public void listItemStacks() {
        for (for 
    Block block blockList) {
            if (
    block.getState() instanceof InventoryHolder) {
                
    InventoryHolder inventory = (InventoryHolderblock.getState();
              
                for (
    ItemStack stack inventory.getItemStack()) {
                    
    Bukkit.broadcastMessage("Type: " stack.getType().getName() + " Amount: " stack.getAmount());
                }
            }
        }
    }
    I did not test this and i'm assuming that your code that checks inventory through state works.
     
    Last edited: Sep 11, 2020
  9. Offline

    iceypotatoguy

    Code:
        public void listItemStacks() {
            for (SavedBlockInfo block : blockList) {
                if (block.getBlockState() instanceof InventoryHolder) {
                    InventoryHolder inventory = (InventoryHolder) block.getBlockState();
                    for (ItemStack stack : inventory.getInventory().getContents()) {
                        Bukkit.broadcastMessage("Type: " + stack.getType().toString() + " Amount: " + stack.getAmount());
                    }
                }
            }
        }
    I did this, and an internal error occurred at this line: "Bukkit.broadcastMessage("Type: " + stack.getType().toString() + " Amount: " + stack.getAmount());" It said "null." I have a feeling that the code might be broken or something, it is not doing what it is supposed to be doing.

    Code:
     
        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 added `BlockState blockState` to my constructor in class SavedBlockInfo.
     
    Last edited: Sep 12, 2020
  10. Offline

    Kars

  11. Offline

    iceypotatoguy

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'walls' in plugin WallsMinigame v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_241]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_241]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at com.icey.walls.framework.BlockClipboard.listItemStacks(BlockClipboard.java:62) ~[?:?]
        at com.icey.walls.commands.Walls.onCommand(Walls.java:100) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        ... 15 more
     
  12. Offline

    xelatercero

    Could you post your code , and highlight that line?

    Code:
    at com.icey.walls.commands.Walls.onCommand(Walls.java:100)
     
  13. Offline

    iceypotatoguy

    Code:
    public class Walls implements CommandExecutor, TabCompleter {
        private MainPluginClass myplugin;
        private ArenaManager arenaManager;
        private FileConfiguration arenaConfig;
        private WallsTool wallsTool;
        private BlockClipboard protectedBlocks;
                else if (args[0].equalsIgnoreCase("listitems")) {
                    if (protectedBlocks == null || protectedBlocks.getBlockList().size() == 0) {
                        sender.sendMessage("nothing");
                    }
                    else {
                        for (int i = 0; i < protectedBlocks.getBlockList().size(); i++) {
                            protectedBlocks.listItemStacks(); <--
                        }
                    }
                }
    The arrow is line 100.
     
    Last edited: Sep 13, 2020
  14. Offline

    xelatercero

    line 63 of BlockClipboard.java please
     
  15. Offline

    iceypotatoguy

    Code:
        public void listItemStacks() {
            for (SavedBlockInfo block : blockList) {
                if (block.getBlockState() instanceof InventoryHolder) {
                    InventoryHolder inventory = (InventoryHolder) block.getBlockState();
                    for (ItemStack stack : inventory.getInventory().getContents()) {
                        Bukkit.broadcastMessage("Type: " + stack.getType().toString() + " Amount: " + stack.getAmount()); //This is line 62.
                    }
                }
            }
        }
    Line 62 will have a comment. Scroll to the right.
     
Thread Status:
Not open for further replies.

Share This Page