BlockBreakEvent

Discussion in 'Spigot Plugin Development' started by RayoxHD, Apr 30, 2017.

  1. Hello,

    I want to store the mined block in a Map and then set it to Bedrock. After x Secs or Reload the server goes through the map and replaces these block but for some reason its sets the Bedrock block to the Map and noit the previous block like for example LOG.

    Code (blockbreaklistenerjava (Unknown Language)):

    public class BlockBreakListener implements Listener {
     
        public static Map<Location, Block> blocks = new HashMap<>();
     
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Player player = e.getPlayer();
            Block block = e.getBlock();
         
            if(e.isCancelled()) {
             
                e.setCancelled(true);
             
            }else if(e.isCancelled() == false) {
         
                if(e.getBlock().getDrops().size() > 0) {
                 
                    if(!(e.getPlayer().getInventory().firstEmpty() == -1)){
                       
                        for(ItemStack item : e.getBlock().getDrops()){
                         
                            player.getInventory().addItem(item);
                        }
                     
                        blocks.put(block.getLocation(), block);
                        Core.resetBlock(block.getLocation(), block);
                        block.getLocation().getBlock().setType(Material.BEDROCK);
                        e.setCancelled(true);
                    }else if(e.getPlayer().getInventory().firstEmpty() == -1) {
                     
                        for(ItemStack item : e.getBlock().getDrops()){
                         
                           player.getWorld().dropItem(player.getLocation(), item);
                        }
                     
                        blocks.put(block.getLocation(), block);
                        Core.resetBlock(block.getLocation(), block);
                        block.getLocation().getBlock().setType(Material.BEDROCK);
                        player.sendMessage("§5SC §7- §cYour Inventory is Full! Items have been droped.");
                        e.setCancelled(true);
                    }
                 
                }
             
                Core.notifyServer(NotificationType.Warning, "" + blocks);
            }
         
        }

    }
     
    Code (mainjavaondisable (Unknown Language)):

    @SuppressWarnings("deprecation")
        @Override
        public void onDisable() {
         
            int amount = 0;
         
            for(Entry<Location, Block> str : BlockBreakListener.blocks.entrySet()) {
                Location loc = str.getKey();
                Block mat = str.getValue();
             
                loc.getBlock().setType(mat.getType());
                loc.getBlock().setData(mat.getData());  
             
                amount++;
            }
         
            Core.notifyServer(NotificationType.Normal, "§2 " + amount + " Blocks have been Replaced!");
         
        }
     
  2. sothatsit

    Patron

    Block objects update as the block changes. What you want to store is the BlockState before you change it to bedrock.

    For example:
    Code (Text):
    // Store a list of the old block states
    Set<BlockState> blockStates = new HashSet<>();

    ...

    // Get the block state before you change it, add it to the blockStates set
    BlockState state = block.getBlockState();
    block.setType(Material.BEDROCK);
    blockStates.add(state);

    ...

    // Loop through all the block states and update the blocks back to this state.
    for(BlockState state : blockStates) {
        state.update(true);
    }