setDrops

Discussion in 'Spigot Plugin Development' started by HashOIRE, May 24, 2015.

  1. Hello !
    I have search on the javadocs/bukkit for change a drop of a bloc but I don't know.
    I want change the drop of Iron_Ore etc.
    Do you know ?

    Thank you ^^
     
  2. Hi maxouland1,

    You'll need to get the drops list with the getDrops() method, and alter it this way.

    Alternatively, clear the list from getDrops() and then drop an item manually.
     
  3. Do you have an example ? :/
     
  4. event.getDrops().clear();
    event.getDrops().add(ItemStack);
     
  5. I'll test
     
  6. I have make this:
    Code (Text):
        public void onBlockBreak(BlockBreakEvent e){
            ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
            if(e.getBlock().getType() == Material.OBSIDIAN)
                e.getBlock().getDrops().clear();
            e.getBlock().getDrops().add(iron);
    But it doesn't work. Why ?
     
  7. There are a few mistakes here:

    - Did you add an EventHandler annotation, and register events onEnable()?

    - use .equals() for Materials, not ==

    - Use curly braces on your if statement, as in if statement without them only works on one line.
     
  8. Code (Text):
    public class QuickMine extends JavaPlugin implements Listener{
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);}
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e){
            if(e.getBlock().getType().equals(Material.OBSIDIAN));{
                ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
                e.getBlock().getDrops().clear();
            e.getBlock().getDrops().add(iron);

    }}}
    It doesn't work :/
     
  9. Do you have a solution ?
     
  10. need an @Override annotation on your onEnable?
     
  11. No, when you are comparing two enums you have to use == not .equals(). I also think you can't change the drops, you just have to manually drop the item in the world at the block's location.
     
  12. Code (Text):
    public class QuickMine extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);}
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e){
            if(e.getBlock().getType().equals(Material.OBSIDIAN));{
                ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
                e.getBlock().getDrops().clear();
            e.getBlock().getDrops().add(iron);
    It doesn't work :c
     
  13. This should work.

    Code (Text):

    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
        Block block = e.getBlock()
        ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
        if(block.getType() == Material.OBSIDIAN) {
            ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
            drops.add(iron);
            e.getBlock().getDrops().clear();
            for(int i = 0; i < drops.size(); i++) {
                ItemStack drop = drops.get(i);
                block.getWorld().spawn(block.getLocation(), drop);
            }
        }
    }
     
     
  14. It doesn't work :(
     
  15. For change drops best way is remove drops and spawn items on that location.
     
    • Like Like x 1
  16. You could try clearing the drops and just doing e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
     
    • Like Like x 1
    • Agree Agree x 1
  17. I find ! Thanks you for your help :)
     
  18. You can simply set event cancel and set that block to air. Then drop items.
     
    • Agree Agree x 1
  19. That's not even necessary.
    Just use:
     
  20. What i am saying is for clear drops. I said that too before ;)