Disable item drops

Discussion in 'Plugin Development' started by Moon_werewolf, Dec 24, 2012.

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

    Moon_werewolf

    I'm working on a plugin that add the item drop directly in to the players inventory when they mine. But i don't know how to disable the normal item drop.
     
  2. Offline

    caseif

    Code:java
    1. public void onBlockBreak(BlockBreakEvent e){
    2. e.getBlock().getDrops().clear();
    3. e.getPlayer().getInventory().addItem(e.getBlock().getDrops());
    4. }
     
  3. Offline

    Moon_werewolf

    I changed my code a bit. But i still get the item drops
    Code:java
    1. public void onBlockBreak(BlockBreakEvent event)
    2. {
    3. Block block = event.getBlock();
    4. Player player = event.getPlayer();
    5.  
    6. block.getDrops().clear();
    7. for(ItemStack items : block.getDrops())
    8. {
    9. player.getInventory().addItem(items);
    10. }
    11. }
    [
     
  4. Offline

    fireblast709

    Moon_werewolf he thought you ment entity drops. You should cancel the event, set the block to AIR, optionally play the Effect.STEP_SOUND and give the drop. The code is in the spoiler (in case the instructions are enough, and you want to make it yourself ;3)
    Show Spoiler
    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent e)
    3. {
    4. e.setCancelled(true);
    5. ItemStack[] drops = e.getBlock().getDrops().toArray(new ItemStack[0]);
    6. int id = e.getBlock().getTypeId();
    7. e.getBlock().setType(Material.AIR);
    8. e.getBlock().getWorld().playEffect(e.getBlock().getLocation(), Effect.STEP_SOUND, id);
    9. for(ItemStack drop : drops)
    10. {
    11. e.getPlayer().getInventory().addItem(drop);
    12. }
    13. }
     
  5. Offline

    Moon_werewolf

    But wouldn't that cause the tools to last forever? and If i use "item.setDurability((short)(item.getDurability() + 1));" would ignore enchantments.
     
  6. Offline

    fireblast709

    Yep that would require some mathematics to get the correct durability
     
  7. Offline

    caseif

    The primary problem that I see with that is the fact that it could potentially bypass logging plugins, and depending on the priority, some plugins which listen to BlockBreakEvent.
     
  8. Offline

    fireblast709

    Other suggestions are welcome ;3
     
Thread Status:
Not open for further replies.

Share This Page