Remove placed Blocks from one Player

Discussion in 'Spigot Plugin Development' started by rakso10000, May 17, 2016.

  1. Hey,
    do you know how to remove all blocks placed by one Player after he dies?
    -rakso
     
  2. Could try putting all the blocks he placed into a hashmap, and when he dies get the blocks location, and set it to air..
    Probably a better method of doing this.

    -- Edit --
    I was thinking, you might want to put the blocks he breaks into the map also(If its not his) And restore them when he dies, that way if he digs into the ground and such, its all restored.
     
    #2 aaron1998ish, May 17, 2016
    Last edited: May 17, 2016
    • Agree Agree x 1
  3. Yeah, I think it's the only way to make it. :D
     
  4. Ok, thank you for the fast answers.
     
  5. And how to exactly do it then (in code) I don't get it :3
     
  6. I don't give you the entire code, just "step" to follow! :D

    Code (Text):
    Map<Player, Block> blocksPlaced = new HashMap<>();
    Map<Player, Block> blocksBreaked = new HashMap<>();

    PlayerBlockPlaceEvent event {
        blocksPlaced.put(event.getPlayer(), event.getBlock());
    }

    PlayerBlockBreakEvent event {
        Block block = event.getBlock();

        // Add metadata to the block, next you had to use it to replace original block.

        blocksBreaked.put(event.getPlayer(), block);
    }

    PlayerDeathEvent event {
        // iterate the placed blocks and setMaterial air
        // iterate the breacked blocks and setMaterial the material store in the metadata :D
    }

    Enjoy your coding time.
     
    • Like Like x 1
  7. It's now working, but only replacing the last placed Block :D I was never working with HashMaps before, so sorry if it is to easy^^
     
  8. You want help for replace blocks destroyed? Enjoy my help it's free. :)
     
  9. Do you know how to remove ALL placed blocks? :D
     
  10. If you iterate correctly the Map, you normally have all the placed blocks. :eek:
     
  11. Uhm no? A map has a unique key. You're assigning 1 single block to a player, that's not right. What you want is:
    Code (Text):
    HashMap<UUID, ArrayList<Block>>
     
  12. And you'd replace
    Code (Text):
    blocksBreaked.put(event.getPlayer(), block);
    with
    Code (Text):
    List<Block> blocks = blockBreaked.get(event.getPlayer().getUniqueId();
    blocks.add(yourNewBlock);
    blocksBreaked.put(event.getPlayer().getUniqueId(), blocks);
     
    • Like Like x 1
  13. Yes, my bad.. sorry!

    So, my code change.

    Map<Player, ArrayList<Block>> blocksPlaced = new HashMap<>();
    Map<Player, ArrayList<Block>> blocksBreaked = new HashMap<>();

    PlayerBlockPlaceEvent event {
    blocksPlaced.get(event.getPlayer()).add(event.getBlock());
    }

    PlayerBlockBreakEvent event {
    Block block = event.getBlock();

    // Add metadata to the block, next you had to use it to replace original block.

    blocksBreake.get(event.getPlayer()).add(block);
    }

    PlayerDeathEvent event {
    // iterate the placed blocks and setMaterial air
    // iterate the breacked blocks and setMaterial the material store in the metadata :D
    }
     
  14. No utility to store the List...
     
  15. Just making it a bit clearer
     
  16. I would make a custom class that stores a location, previous block, and new block. This way, you can store a list of those objects mapped to a player. Something like this:
    Code (Text):
    public class BlockNode{

        private Location location;
        private Block oldBlock;
        private Block newBlock;

        public BlockNode(Block oldBlock, Block newBlock){
            this.location = oldBlock.getLocation();
            this.oldBlock = oldBlock;
            this.newBlock = newBlock;
        }
    }
    And then add appropriate getters and setters.