How to set custom block drops?

Discussion in 'Plugin Development' started by Milkywayz, Mar 18, 2012.

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

    Milkywayz

    Hey i want to set custom drops for blocks in config. I have the events fully registered.. This is a snippet of my drop listener class.
    PHP:
    public class MyDropListener implements Listener{
        private 
    ItemStack goldpick = new ItemStack(285);
        private 
    ItemStack goldshovel = new ItemStack(284);
        private 
    ItemStack goldaxe = new ItemStack(286);
        
    Goldtools plugin;
        public 
    MyDropListener(Goldtools instance) {
            
    plugin instance;
        }
        public 
    void onBlockBreak(BlockBreakEvent ev) {           
            
    Block block ev.getBlock();
            
    World world block.getWorld();
            
    Boolean allowed plugin.getConfig().getBoolean("Use.Customdrops");
            if (
    allowed) {
            if (
    ev.getPlayer().getItemInHand() == goldpick) {
                if (
    ev.getPlayer().hasPermission("goldtools.cdrop")) {
                    if (
    block.getType() == Material.STONE) {
                        
    ItemStack drop = new ItemStack(plugin.getConfig().getInt("Customdrops.pickaxe.stone"));
                        
    world.dropItem(block.getLocation(), drop);
                        return;
                    }
                    if (
    block.getType() == Material.BRICK) {
                        
    ItemStack drop = new ItemStack(plugin.getConfig().getInt("Customdrops.pickaxe.brick"));
                        
    drop.setAmount(1);
                        
    drop.setTypeId(plugin.getConfig().getInt("Customdrops.pickaxe.brick"));
                        
    world.dropItem(block.getLocation(), drop);
                        return;
                    }
    What am i doing wrong? This is so annoying... Also the "stone" is the one i test stuff on and try out things to find a solution. I usually learn from my errors and finally get it right but i don't know where to look now...
     
  2. Offline

    jamietech

    I believe there is an event.getDrops() which you can modify (it's a List, clear it and then add to it)
     
    com. BOY likes this.
  3. Offline

    Milkywayz

    I don't believe that is the easiest / most efficient way to do this.. I know there is a easier way to but I'm not quite sure since its like 5:33 am for me
     
  4. Offline

    fromgate

    onBlockBreak event have not a getDrops() method :(

    When I've need to change drops, I've did something like:

    event.setCancelled(true);
    event.getBlock().setType(Material.AIR);
    event.getBlock().getWorld().dropItemNaturally(new ItemStack (....), event.getBlock().getLocation());

    But I'm not sure that cancelling item is best way to set custom drop...

    I'll thankful if anyone can offer a better solution.
     
  5. Offline

    Milkywayz

    Thanks for the help but i don't think that will work.. i could try though
     
  6. Offline

    fromgate

    Same code is working in my plugin (may be errors in syntax -- I was typing from memory).

    But I think there will be better way to setup custom drop, than cancel current event. But I don't know how to prevent default dropping without canceling event.
     
  7. Offline

    Milkywayz

    Are you sure its dropItemNaturally? What does the naturally even change from dropItem ?
     
  8. Offline

    jamietech

    Naturally will allow the item to pop everywhere.
    Cancelling the event will also break block logging plugins ;)
     
  9. Offline

    fromgate

    And how prevent default drop without canceling event?
     
  10. Offline

    Milkywayz

    Yes indeed.. it'd make gold tools the tool of griefers lol.. Any ideas?
     
  11. Offline

    fromgate

    I've found solution how to prevent drop without cancel event. Thank to Grant McFerrin (he answered to my suggestion https://bukkit.atlassian.net/browse/BUKKIT-1217)

    event.getBlock().getDrops().clear();
    works fine.

    Milkywayz
    I think you can try this:
    event.getBlock().getDrops().add(drop);
     
    Milkywayz likes this.
  12. Offline

    Milkywayz

    You sir are a trooper you helped me throughly :) :) :)
     
  13. Offline

    Ewe Loon

    just something noone has mentioned , always put the amount in itemstacks
    private ItemStack goldpick = new ItemStack(285,1);
    private
    ItemStack goldshovel = new ItemStack(284,1);
    private
    ItemStack goldaxe = new ItemStack(286,1);

    my first plugin i didnt , and got itemstacks of 0 apples


    if you are not holding anhing
    ev.getPlayer().getItemInHand()
    will return null
    so use
    ItemStack itm=ev.getPlayer().getItemInHand();
    If( itm!=null){
    if (itm.getItemID()==285)

    and so on
    DONOT COMPAIR ITEMSTACKS, it wont work
    you could use
    if (itm.getItemID()==goldpick.getItemID())

    The loging plugin I wrote logs canceled events too,

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  14. Offline

    jamietech

    That's a bit silly.
     
    com. BOY likes this.
  15. It isn't possible though - if you grab the list and change it, you won't be able to put it onto the block again unless the block has a function called setDrops(ItemStack) or something
     
Thread Status:
Not open for further replies.

Share This Page