[TUTORIAL|EASY] How to make a custom drop

Discussion in 'Resources' started by hawkfalcon, Jun 3, 2012.

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

    hawkfalcon

    This is how you can set the drop for any block:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. if(event.getBlock().getType() == Material.BREAKMATERIAL) {
    4. Block block = event.getBlock();
    5. block.setType(Material.AIR);
    6. block.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.DROPMATERIAL, 1));
    7. event.setCancelled(true); // Cancel the event because you've done it yourself.
    8. }
    9. }

    Have fun!
     
  2. Offline

    McLuke500

    With this what happens if you break a block under water?
     
  3. Offline

    hawkfalcon

    Haven't tested, good question;o
     
  4. Offline

    Iron_Crystal

    Well, looking at the code he posted, he is turning the block to air. So what do you think will happen if you make a block air that is fully enclosed in water?

    PS. It fills in.
     
  5. The same thing as if it wasn't under water?
     
  6. Offline

    hawkfalcon

    It does set it to air though ;p
     
  7. Offline

    Technius

    They really need to add a drops list for BlockBreakEvent. It should be an ArrayList...
     
    hawkfalcon likes this.
  8. Offline

    hawkfalcon

    Yeah but this is an alternative in the time being^
     
  9. It will set the block that was broken to air. You can't break water, so it will just change the physics when the item is dropped.
     
  10. Offline

    hawkfalcon

  11. Offline

    WarmakerT

    You set the block to air because you cancelled the block break event (99% sure)
    If you didn't, the block would just respawn.
     
  12. Offline

    JOPHESTUS

    This is awesome! I used it in JOPHSpawner. I've given you credits at the bottom :)
     
    hawkfalcon likes this.
  13. Offline

    WarmakerT

    Wrong quote :p
     
  14. Offline

    JOPHESTUS

    Sorry :p
     
  15. Offline

    hawkfalcon

    Thank you:)
    True.
     
  16. Offline

    WarmakerT

    And if you don't cancel the event, then the real drops will drop.
     
  17. Offline

    hawkfalcon

     
  18. Offline

    hawkfalcon

    updated.
     
  19. Offline

    Icyene

    Everyone is copying my TUTORIAL|LEVEL format now >.<

    Nice tutorial :) If the newbies see this it will decrease the amount of questions asking about this.
     
    hawkfalcon likes this.
  20. It is a good way of telling people what level that tutorial is, so i get why everyone is copying it:p
     
    hawkfalcon likes this.
  21. Offline

    hawkfalcon

    I made it extremely noob friendly:3
     
  22. Offline

    hawkfalcon

    derpNaturally();
     
  23. Offline

    aviator14

    If you are ignoring the cancelled state, doesn't that mean you can override any sort of protection on the block?

    Would something more like this work?
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)//must be high priority
     
    public void onBlockBreak(BlockBreakEvent event)//checks for any block break
    {
    if(!event.isCancelled() && event.getBlock().getType() == Material.~INSERT_BLOCK_TO_BREAK_HERE~)//checks for a specific block
    {
    Block block = event.getBlock();//creates variable block with the block broken
    Collection<ItemStack> drops = block.getDrops();//creates variable drops with the current drops of that block
    drops.clear();//remove those drops
    drops.add(new ItemStack(Material.~INSERT_MATERIAL_YOU_WANT_DROPPED_HERE~, 1); //do this for every itemstack you want to be dropped
    }
    }
     
  24. Offline

    hawkfalcon

    Possibly. Depends on what you want.
     
  25. Offline

    aviator14

    I retract my first question, having now learned that ignoreCancelled is the opposite of what it sounds like. (For those like me, if that attribute is true, your code will not run if the event is cancelled. So it actually IS what you'd want here.)
     
    hawkfalcon likes this.
  26. Offline

    KeybordPiano459

    Wonder what else you could do... :p
    Code:java
    1. Block block = event.getBlock();
    2. block.getDrops().clear();
    3. block.getDrops().add(new ItemStack(Material.EGG));
     
    hawkfalcon likes this.
  27. Offline

    DatCookiez

    What would you do for a mob?
     
  28. Offline

    KeybordPiano459

    On the event that the mob dies, clear the drops and add new ones, like I've done above.
     
  29. Offline

    hawkfalcon

  30. Offline

    KeybordPiano459

Thread Status:
Not open for further replies.

Share This Page