Solved Delete Item by block breaking

Discussion in 'Plugin Development' started by jersogamer, Jul 13, 2019.

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

    jersogamer

    Hello, first of all sorry for my bad english, I have a problem that I dont know how te correct way to do it would be, as you can read on the title I want that when a player breaks a block for example iron ore, the item of the iron ore dont drop, I mean when a player breaks the block, the item block disappears.

    I already try to do it with this code:

    Code:
    @EventHandler
        public void mine(BlockBreakEvent e) {
            Material block = e.getBlock().getType();
            Player miner = e.getPlayer();
           
           
            if(miner != null && miner.getType().equals(EntityType.PLAYER) && block.equals(Material.IRON_ORE)) {
            e.getBlock().getDrops().clear();
            miner.sendMessage(ChatColor.YELLOW+"You broke an iron ore");
            }
           
        }
    I added that message because in that way I can see that the condition works. I usted the metod getDrops.clear because i did some related but using entities like a cow, If a player kills a cow any drop will appear.

    I hope you understand what I tried to mean
     
  2. Offline

    KarimAKL

    @jersogamer Try using this.
    1. e.getPlayer() won't return null, so there's no need to check that.
    2. You are checking if a player is a player, just remove that check.
    3. Compare enums with '==' instead of '.equals()'
     
  3. Offline

    jersogamer

    Thank you, but how do i delete the drop from the block I mined?
     
  4. Offline

    KarimAKL

     
  5. Offline

    jersogamer

    Sorry... I didn't understand how to do it, I mean I don't know yet how to delete the drop from the item. I'm a little new at these things of coding
     
    Last edited: Jul 14, 2019
  6. Offline

    KarimAKL

    @jersogamer You can press 'this' in my previous post, it's a link. Anyway, try using the 'setDropItems(boolean)' method from 'BlockBreakEvent'.
     
  7. Offline

    jersogamer

    This is the event so I tried this:

    public void mine(BlockBreakEvent e) {
    e.setDropItems(false);
    }

    but It told me that that method doesn't exists
    the only one related is "setExpToDrop();"

    I'm working on a plugin for 1.8.x

    I'm working on a plugin for 1.8.9

    SORRY, I reply instead of edit, how do i delete this one?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  8. Offline

    KarimAKL

    @jersogamer I think the 'setDropItems(boolean)' method was added in 1.13, and if i remember correctly, getDrops().clear() doesn't work; just cancel the event and set the block to air.
     
  9. Offline

    jersogamer

    Thanks for helping me, this is the code that helped me:

    Code:
    @EventHandler
    public void onBreak(BlockBreakEvent e) {
    e.setCancelled(true);
    e.getBlock().setType(Material.AIR);
    }
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page