Modifying drops in BlockBreakEvent

Discussion in 'Plugin Development' started by Assist, Dec 19, 2015.

Thread Status:
Not open for further replies.
  1. I am aware that this question has been asked and answered several times, however in each thread, the answer has been something along the lines of "Cancel the event and drop the items manually". In my case, this is not an option. If I use any of these methods,
    • Cancel the event
    • Set the block type to air
    • Use Block#breakNaturally
    the held item durability will not be changed.

    I might just manually change the durability based on this: http://minecraft.gamepedia.com/Item_durability,
    but I'm here to ask if there is an easier way to achieve this.

    Thanks in advance.
     
  2. Offline

    Javlin

    I suppose you could:
    Code:
    e.getPlayer().getItemInHand().setDurability((short) (e.getPlayer().getItemInHand().getDurability() - 1));
    e.setCancelled(true);
    And then manually drop the items.
    But then you would have to deal with breaking items and unbreakable items.
     
  3. Sure, but the amount of durability the item loses varies based on a few different variables:
    http://minecraft.gamepedia.com/Item_durability#Tool_durability

    This is what I'll most likely end up using though, unless someone suggests a better way.
     
  4. Offline

    teej107

    @Assist So what do you want to achieve?
     
  5. @teej107
    Isn't it obvious from the thread title? I want to modify the block drops in BlockBreakEvent, while still making the held item lose durability as it would normally do. This does not happen if I cancel the event, set the block to air or use Block#breakNaturally.
     
  6. Offline

    Javlin

    How about:
    Code:
    e.getBlock().breakNaturally(e.getPlayer().getItemInHand());
    for(Entity d : e.getPlayer().getWorld().getEntities()) {
           if(d.getLocation().distance(e.getBlock().getLocation()) <= 1) {
               d.remove();
           }
    }
    // Drop items
    e.setCancelled(true);
    
    Not very efficient though.
     
  7. Offline

    Zombie_Striker

    @Assist
    If you want to modify the dropped items, why can't you just use:
    Code:
    Block.getDrops().add/remove/clear();
    BTW: If someone asks a question, you should just answer the question.
     
    Nibbit likes this.
  8. Because that doesn't seem to work, at least for the item that the block itself drops.

    What post are you referring to?

    Works as a temporary solution.
    Code:
    ItemStack first = new ArrayList<ItemStack>(drops).get(0);
    
    for (Item item : block.getLocation().getWorld().getEntitiesByClass(Item.class)) {
        if (item.getItemStack() == first) {
            item.remove();
            break;
        }
    }
    Edit: ^ I run it 1 tick later.
     
    Last edited: Dec 19, 2015
  9. Offline

    teej107

    oh yeah. Completely forgot about that title :p
    This was from the JavaDocs.
    So I am certain you have to change the durability yourself.
     
  10. Offline

    Nibbit

    Code:
        @EventHandler
        public void BreakABlock(BlockBreakEvent e){
            Player p = e.getPlayer();
            Block b = e.getBlock();
            e.setCancelled(true);
            b.setType(Material.AIR);
            p.getItemInHand().setDurability((short) (p.getItemInHand().getDurability() - 1));
            p.getWorld().dropItem(b.getLocation(), new ItemStack(Material.DIAMOND, 1));
        }
    Only drops a diamond and takes some durability

    EDIT : weird... Doesn't take any durability it seems...
     
  11. Offline

    Javlin

    This is because the durability starts at 0 and increases, therefore you need +1 instead of -1.
    Although I suggested this before @Assist pointed out that durability loss is not always 1:
     
  12. Offline

    Nibbit

    Oh it looked like 1 with every pickaxe I used :p Maybe because mining stone with a sword uses more durability? Idk
    I don't know if there is something to get how much durability it would cost :/
     
  13. Unbreakable enchantment :)
     
  14. Offline

    teej107

    Similar to this less efficient way, you could always listen for the ItemSpawnEvent.
     
    Javlin and Nibbit like this.
  15. Offline

    Nibbit

    Oh, Didn't think of that xD
     
Thread Status:
Not open for further replies.

Share This Page