Solved Method getDrops() doesnt working?

Discussion in 'Plugin Development' started by Gonmarte, Oct 9, 2015.

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

    Gonmarte

    Hi
    I dont know why when i mine a iron block it dont drop a iron ingot... instead drop the iron ore :/
    CODE
    Code:
    ItemStack smelts = new ItemStack(Material.DIAMOND_PICKAXE);
            ItemMeta metasmelts= smelts.getItemMeta();
            metasmelts.setDisplayName("Smelts Pick");
            smelts.setItemMeta(metasmelts);
        if(player.getItemInHand().equals(smelts)) {
               
                if(event.getBlock().getType().equals(Material.IRON_ORE)) {
               Block iron_ore = event.getBlock();
               iron_ore.getType().equals(Material.IRON_ORE);
                iron_ore.getDrops().clear();
                iron_ore.getDrops().add(new ItemStack(Material.IRON_INGOT));
                }
               
               
            }
    
     
  2. Offline

    Kakarot798

    Could you post you full code?
     
  3. Offline

    Scimiguy

    Instead of modifying the getDrops() list, you have to handle the event yourself unfortunately

    Cancel the event, set the block mined as air, and drop the item naturally using event.getBlock().getWorld().dropItemNaturally();

    Probably not the most efficient way to handle checking for special pickaxes either, but meh
     
    Gonmarte likes this.
  4. Offline

    SuperSniper

    @Gonmarte
    What Event are you using to do this? BlockBreakEvent?

    If you are, there are a couple things wrong with your code:

    Code:
    ItemStack smelts = new ItemStack(Material.DIAMOND_PICKAXE);
            ItemMeta metasmelts= smelts.getItemMeta();
            metasmelts.setDisplayName("Smelts Pick");
            smelts.setItemMeta(metasmelts);
        if(player.getItemInHand().equals(smelts)) {
               
                if(event.getBlock().getType().equals(Material.IRON_ORE)) { // use "==" to compare objects. 
    // if(event.getBlock().getType == Material.IRON_ORE) {
               Block iron_ore = event.getBlock();
               iron_ore.getType().equals(Material.IRON_ORE); // Completely useless, remove this line.
                iron_ore.getDrops().clear();
                iron_ore.getDrops().add(new ItemStack(Material.IRON_INGOT));
                }
               
               
            }
    
     
  5. Offline

    Gonmarte

    @SuperSniper Im going to try with that!

    @Scimiguy Thank you it work xd :D

    @SuperSniper It wont work but i could do it with the way of the schimiguy

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  6. Offline

    Scimiguy

    @Gonmarte Please mark the thread as solved ;)
     
  7. Offline

    Gonmarte

    @Scimiguy @SuperSniper BAD NEWS. I found out an error... if i put the block the the same location(same place) and then if i break it it will drop a iroon ingot, but then if i change the place it will drop an iron ore..
    Code:
    if(player.getItemInHand().equals(smelts)) {
             
                if(event.getBlock().getType()==Material.IRON_ORE) {
                 
                    event.setCancelled(true);
                    event.getBlock().setType(Material.AIR);
                    event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
                }
             
             
            }
    
    
     
    Last edited: Oct 9, 2015
  8. Offline

    caderape

    @Gonmarte
    Post the full method.
    You compare two itemstack with equals, so the itemstack must be the same, the durability also.

    If it's #blockbreakevent, you dont have to cancel the event, you can manage with the dropslist.
     
  9. Offline

    Scimiguy

    As far as I was aware the dropslist approach doesnt work for blockbreak
    @caderape

    I don't undertsand what you mean

    @Gonmarte
     
  10. Offline

    Gonmarte

    @Scimiguy what i mean is,when i put a block on the ground and i break it it will drop an iron ingot, if i put the block at the same place it will drop an iron ingot BUT if i put the block in another place ( another block) it will drop an iron ore..

    @caderape
    Code:
    @EventHandler
        public void onBlockBreak2(BlockBreakEvent event) {
           
            final Player player = event.getPlayer();
           
            ItemStack smelts = new ItemStack(Material.DIAMOND_PICKAXE);
            ItemMeta metasmelts= smelts.getItemMeta();
            metasmelts.setDisplayName("Smelts Pick");
            smelts.setItemMeta(metasmelts);
           
            if(player.getItemInHand()==smelts) {
               
                if(event.getBlock().getType()==Material.IRON_ORE) {
                   
                    event.setCancelled(true);
                    event.getBlock().setType(Material.AIR);
                    event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
    
                }
               }
               
            }
    
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  11. Offline

    Scimiguy

    Put a console out inside your if statement and debug it
     
  12. Offline

    Zombie_Striker

    @Gonmarte
    Have "2" in your method name raises some flags. You should not have more than one of the same events in the same class.

    instead of creating a new instance EVERY SINGE TIME a player breaks a block, move the smelts instance out of the method.
    Same for item meta.

    Are you sure the item in the hand is ever the same as smelts?
     
  13. Offline

    Gonmarte

    @Zombie_Striker I just have 1 blockbreakevent, i called blockbreakevent2 because the class is called blockbreakevent.
    I didnt understand what you mean :/
     
  14. Offline

    Zombie_Striker

    @Gonmarte
    Alright.
    You should never have a number as a name of a field or method. If you have to have "2" or "%" or any otehr charactor beside A-Z, then you're doing something wrong. Also, you should not have things (classes/fields/parameters/methods) with the same name (preferably, do not have semi-simular names between objects.)

    The way you currently have it is that whenever a blocks break create a new itemstack EVEN IF IT IS NEVER USED. A NEW ONCE EACH AND EVERY TIME. Instead of doing the above, move those two methods outside the method, that way there is only one instance of each.
    By this I mean is smelts ever added to the players inventory? Or atleast an itemstack with all the same values? If not, then smelts will never be equal to the item in the hand, and as such will never drop the iron ingot.
     
  15. Offline

    Gonmarte

    @Zombie_Striker It only create a new item stack if a okayer breaks an iron ore, so that shouldnt be a proble m right?
    And yes i made a command to add it to the players inventory. I change a few things because the problem might be an issue with the durability of the item. So i did this:
    Code:
            if(player.getItemInHand().hasItemMeta()) {
               
                if(player.getItemInHand().getItemMeta().hasDisplayName()) {
                   
                    if(player.getItemInHand().getItemMeta().getDisplayName().equals("Smelter Pick")) {
               
                if(event.getBlock().getType()==Material.IRON_ORE) {
                    event.setCancelled(true);
                    event.getBlock().setType(Material.AIR);
                    event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
                    player.sendMessage("smelters");
                }
                }
                }
            }
    
    
    PS: i didnt test it yet, ill test it in 2 hours now im too busy :/
     
  16. Offline

    Zombie_Striker

    @Gonmarte
    But there must be atleast 1000 iron ore blocks within a 16x16 chunk region, (That are naturally spawning, I'm not even going to include the possibility that someone might place an iron ore down and mine it up again), meaning there is a possibility that there could be 1000 unused instances of the Iron ore.

    Only create those instances once by simply moving them out of the method (it isn't that hard)

    And yes, the durability is most likely your problem. Try checking if the type and displayname are the same instead of the whole itemstack itself.
     
    Gonmarte likes this.
  17. Offline

    Scimiguy

    Could just give it some lore, and check the lore
    @Gonmarte
     
  18. Offline

    Gonmarte

    @Zombie_Striker Thank you for your help I solved it :D The problem was the durability!
    Once more ty :D
     
Thread Status:
Not open for further replies.

Share This Page