modifying drops in BlockBreakEvent

Discussion in 'Plugin Development' started by raGan., Jul 21, 2012.

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

    raGan.

    Is there proper way to do it ? My only thought was to cancel event and set block to air, then drop whatever I need.
     
  2. Get event.getBlock().getDrops() and change that list's contents.... to change the dropped block, just clear it and add whatever you want it to drop.
     
  3. Offline

    raGan.

    does that work ? hmm thanks, I was too lazy to try

    event.getBlock().getDrops().clear(); does not seem to be working
    event.getPlayer().sendMessage("Yo"); works

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. I never tested this myself, I've heard of it in another thread...
    Well, then I guess the only way would be to cancel the event just break the block manually.
     
  5. Offline

    Dreeass

    Check the api for the methods of the event.
     
  6. Offline

    lx3krypticx

    What I did to clear drops is:

    Code:
    Player player = event.getEntity();
        event.getDrops().clear();
    I assume you could then get the players location on death then drop custom items where he died?
     
  7. Offline

    WarmakerT

    BlockBreakEvent... :p
     
  8. Offline

    raGan.

    And that's the problem. There isn't method to get drops from event. At least I didn't find any.
     
  9. Well, you can just break the block manually (set it to air) and then drop the desired item there, no need for cancel :}
     
  10. Offline

    raGan.

    Ok I stayed with this
    Code:java
    1. //first get drops to modify (it does not work with leaves(shears in hand) though)
    2. event.getBlock().getDrops(event.getPlayer().getItemInHand()); //it needs to be manually dropped
    3. //then relace block with air
    4. event.getBlock().setType(Material.AIR);
     
  11. If this is your actual code:
    Code:
    event.getBlock().getDrops(event.getPlayer().getItemInHand());
    You're actually not doing anything in that expression.
     
  12. Offline

    raGan.

    It is not. I assign that to a variable to know what was about to be dropped.
     
  13. Offline

    MuisYa

    Do this:
    Code:
    event.getBlock().dropItemNaturally(new ItemStack(Material.LOG, 1);
     
  14. Offline

    raGan.

    Compiler complains about that. And what to do if I want to cancel all drops ?
     
  15. Offline

    MuisYa

    Code:
    event.getBlock().breakNaturally(new ItemStack(Material.LOG, 1));
    -___-
     
  16. Offline

    raGan.

    Code:java
    1. boolean breakNaturally(ItemStack tool)
    Breaks the block and spawns items as if a player had digged it with a specific tool
    I think this might not be what I really want here.
     
  17. Offline

    MuisYa

    What do you want then? Lol you asked for that if i see the first post?
     
  18. Offline

    raGan.

    breakNaturally acts like the block was broken with specified tool. I want to modify drops player is about to get from block he just destroyed. It sounds like totally different thing to me. I already know how to get those drops, all I want to know is how to change them.
    Code:java
    1. //first get drops to modify (it does not work with leaves(shears in hand) though)
    2. drops = event.getBlock().getDrops(event.getPlayer().getItemInHand());
    3. //then relace block with air
    4. event.getBlock().setType(Material.AIR);
    5. // then drop it manually
    This is what I got so far
     
  19. Offline

    MuisYa

    But i mean, with breakNaturally you can give the.........
    Never mind, i checked the Javadocs. I thought that the parameter you would give were the drops...

    Code:
    event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), ITEMS);
    That should do the trick, sorry for misunderstanding =)
     
  20. Offline

    raGan.

    Yea thanks, I know how to drop items, but I don't like setting block to AIR, because it may cause security issues with some plugins.
     
  21. Offline

    Lolmewn

    Heh, your question has just been answered.
    The reason why you can't alter the list with items being dropped is because the method is getDrops(), if you would want to change any drops there'd have to be a setDrops(List) method. Not sure if there is.
     
  22. Offline

    MuisYa

    raGan. Eeehr, dropItemNaturally will set it's drops to the things that you give.
    So when a player breaks it, (which is 00000.1 ms after you set it) they get the drops you want.
    So you will not need to change the block to air...
     
  23. Offline

    raGan.

    And how do I cancel items dropped from block the player broke 0.000001ms ago ? I would drop it along with the items being dropped from broken block.
     
  24. If you register it as highest priority there shouldn't be any problems, security plugins should register events as low priority so they can cancel it before other plugins. You should also use ignoreCancelled = true in @EventHandler().
     
  25. Offline

    hawkfalcon

    In the resources section I have a rut for making custom drops.
     
  26. Offline

    raGan.

    I did.
    I already do it that way, but I don't want to cancel event for logging reasons.
     
Thread Status:
Not open for further replies.

Share This Page