Solved Clone all block data

Discussion in 'Plugin Development' started by LIMPIX31, Jul 1, 2021.

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

    LIMPIX31

    I would like to copy a block from one coordinate to another (including its direction, content, etc.) without thinking about whether it is a chest or a spawner.
    The following code copies the block and its direction, but not the contents of the chest and spawner. But I want to copy ALL data of a block, but without thinking about its type (Material)
    Code:
    block.setType(newblock.getType());
    block.setBlockData(newblock.getBlockData().clone());
    EDIT:
    Solution:
    Code:
    public void replaceBlock(Block oldBlock, Block newBlock) {
            if (newBlock.getState() instanceof Chest) {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
                int i = 0;
                for(ItemStack item : ((Chest)newBlock.getState()).getInventory()){
                    ((Chest) oldBlock.getState()).getInventory().setItem(i, item);
                    i++;
                }
            } else if (newBlock.getState() instanceof CreatureSpawner) {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
                CreatureSpawner spawner = ((CreatureSpawner) oldBlock.getState());
                spawner.setSpawnedType(((CreatureSpawner) newBlock.getState()).getSpawnedType());
                spawner.update(true);
            } else {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
            }
        }
     
    Last edited: Jul 6, 2021
  2. Offline

    c7dev

    Since chests and spawners have additional information than just the material ID and byte data, you'll have to add some extra code to support it if you want to clone them.

    For example, for chests, you would add a line of code that checks if the old block is instanceof Chest, then you would iterate through the old block's inventory and add those items to the new block's inventory, since they should both be able to be casted to the Chest class.

    For spawners, it is the same but it would be instanceof CreatureSpawner instead, and so you would cast the old and new block to the CreatureSpawner class, and then it's as simple as CreatureSpawner#getSpawnedType() and then setting that in the new (cloned) block.
     
  3. Offline

    LIMPIX31

    I''ll try to use this

    Code:
    public void replaceBlock(Block oldBlock, Block newBlock) {
            if (oldBlock instanceof Chest) {
                newBlock.setType(oldBlock.getType());
                newBlock.setBlockData(oldBlock.getBlockData());
                ((Chest) newBlock).getBlockInventory().setContents(((Chest) oldBlock).getBlockInventory().getContents());
            } else if (oldBlock instanceof CreatureSpawner) {
                newBlock.setType(oldBlock.getType());
                newBlock.setBlockData(oldBlock.getBlockData());
                ((CreatureSpawner) newBlock).setSpawnedType(((CreatureSpawner) oldBlock).getSpawnedType());
            } else {
                newBlock.setType(oldBlock.getType());
                newBlock.setBlockData(oldBlock.getBlockData());
            }
        }
    EDIT: this is is wrong, i will fix it

    UP
    I can't change the inventory of the chest.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 5, 2021
  4. Offline

    c7dev

    If filling the chests that way isn't working, try changing it to
    Code:
    ((Chest) newBlock.getState()).getInventory().setContents(ItemStack[]);
    Also, you'll need the lines where it says newBlock.setType(oldBlock.getType()); and the setdata line to run regardless of if the block is a chest or spawner, so it shouldn't be in an else statement.
     
  5. Offline

    LIMPIX31

    The following code worked for the chest, but doesn't work for the spawner
    Code:
    public void replaceBlock(Block oldBlock, Block newBlock) {
            if (newBlock.getState() instanceof Chest) {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
                int i = 0;
                for(ItemStack item : ((Chest)newBlock.getState()).getInventory()){
                    ((Chest) oldBlock.getState()).getInventory().setItem(i, item);
                    i++;
                }
            } else if (newBlock.getState() instanceof CreatureSpawner) {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
                ((CreatureSpawner) oldBlock.getState()).setSpawnedType(((CreatureSpawner) newBlock.getState()).getSpawnedType());
            } else {
                oldBlock.setType(newBlock.getType());
                oldBlock.setBlockData(newBlock.getBlockData());
            }
        }
    EDIT: Solved
     
    Last edited: Jul 6, 2021
Thread Status:
Not open for further replies.

Share This Page