Dropping multiple items at location

Discussion in 'Plugin Development' started by someguyonthestreet, May 12, 2015.

Thread Status:
Not open for further replies.
  1. I'm trying to drop multiple saved items at a location but it just throws me an error that i don't know how to fix
    Error: http://gyazo.com/9d568fb0ec3f2ab1ce7df7c2742a2b20
    Code:
    Code:
    Location spawn = new Location(player.getWorld(), getConfig().getInt("spawn.x"), getConfig().getInt("spawn.y"), getConfig().getInt("spawn.z"));
                               
                           
                                    for (ItemStack i : armor.get(player.getName())) {
                                        if (!(i == new ItemStack(Material.AIR))) {
                                player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                    }
                                   
                                    for (ItemStack i : items.get(player.getName())) {
                                        if (!(i == new ItemStack(Material.AIR))) {
                                        player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                            }
                                   
                                        if (!(hand.get(player.getName()) == new ItemStack(Material.AIR))) {
                                        player.getWorld().dropItemNaturally(spawn, hand.get(player.getName()));
                                        }
     
  2. Offline

    Gater12

    @someguyonthestreet
    You can compare getType which returns a Material which you may compare with.
     
  3. Offline

    Zombie_Striker

    Check if ItemStack#getType() == Material.AIR and if so, continue; through the loop.
     
  4. @Zombie_Striker @Gater12

    Now its giving me a nullpointerexception, should I cover it in a try and catch?
    My Code:
    Code:
    Location spawn = new Location(player.getWorld(), getConfig().getInt("spawn.x"), getConfig().getInt("spawn.y"), getConfig().getInt("spawn.z"));
                               
                           
                                    for (ItemStack i : armor.get(player.getName())) {
                                        if (!(i.getType() == Material.AIR || i.getType() != null)) {
                                player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                    }
                                   
                                    for (ItemStack i : items.get(player.getName())) {
                                        if (!(i.getType() == Material.AIR || i.getType() != null)) {
                                        player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                            }
                                   
                                        if (!(hand.get(player.getName()).getType() == Material.AIR || hand.get(player.getName()).getType() != null)) {
                                        player.getWorld().dropItemNaturally(spawn, hand.get(player.getName()));
                                        }
     
  5. Offline

    caderape

  6. Offline

    Zombie_Striker

    Make sure armor is != null
    i != null before getting the type.
    You should add null checks around everything you have so far, see what is null, and try to find a way of making it == something.
     
  7. @caderape @Zombie_Striker
    So instead of "if(!(i.getType()== Material.AIR||i.getType()!=null)){"
    I should do "if (i != null && i.getType() != Material.AIR){"
    ??
     
  8. Offline

    Zombie_Striker

    @someguyonthestreet Here, this is slightly broken code (so it is protected against noobs who cant code), Once you fix the Syntax errors, this will work.
    Code:
    Location spawn = new Location(player.getWorld(), getConfig().getInt("spawn.x"), getConfig().getInt("spawn.y"), getConfig().getInt("spawn.z"));
    if(spawn = null)
    return/continue;
                              
                           if(armor = null);
    return/break;
                                    for (ItemStack i : armor.get(player.getName())) {
                                        if (i =! null (&&) i.getType() != null)) {
                                player.getWorld().dropItemNaturally(spawn, i)
                                        }
                                    }
                                   if(items = null) return/continue;
                                    for (ItemStack i : items.get(player.getName())) {
                                        if (i =! null & i.getType() =! Material.AIR)) {
                                        player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                            }
                                   if(hands === null) return/continue;
                                   if(hands.get(PlayerName) === null) return/continue;
                                        if (!(hand.get(player.getName()) =! null && hand.get(player.getName()).getType() != Material.AIR)) {
                                        player.getWorld().dropItemNaturally(spawn, hand.get(player.getName()));
                                        }}
     
  9. Still doesn't work and I have my null checks so I need help rather then somebody giving me bad code just to fix which might not work.
    Code:
    Location spawn = new Location(player.getWorld(), getConfig().getInt("spawn.x"), getConfig().getInt("spawn.y"), getConfig().getInt("spawn.z"));
                               
                           
                                    for (ItemStack i : armor.get(player.getName())) {
                                        if (i.getType() != Material.AIR && i != null) {
                                player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                    }
                                   
                                    for (ItemStack i : items.get(player.getName())) {
                                        if (i.getType() != Material.AIR && i != null) {
                                        player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                            }
                                   
                                        if (hand.get(player.getName()).getType() != Material.AIR && hand.get(player.getName()).getType() != null) {
                                        player.getWorld().dropItemNaturally(spawn, hand.get(player.getName()));
                                        }
    Ok, i think i fixed the above code, but now its giving me the same error where i started...
    Error: http://gyazo.com/764ddbe9016ef053c9dbe3a3840d9268
    Code:
    Code:
    Location spawn = new Location(player.getWorld(), getConfig().getInt("spawn.x"), getConfig().getInt("spawn.y"), getConfig().getInt("spawn.z"));
                               
                           
                                    for (ItemStack i : armor.get(player.getName())) {
                                        if (!(i.getType() == Material.AIR && i == null)) {
                                player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                    }
                                   
                                    for (ItemStack i : items.get(player.getName())) {
                                        if (!(i.getType() == Material.AIR && i == null)) {
                                        player.getWorld().dropItemNaturally(spawn, i);
                                        }
                                            }
                                   
                                        if (!(hand.get(player.getName()).getType() == Material.AIR && hand.get(player.getName()) == null)) {
                                        player.getWorld().dropItemNaturally(spawn, hand.get(player.getName()));
                                        }
                               
                                   
                                armor.remove(player.getName());
                                items.remove(player.getName());
                                hand.remove(player.getName());
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page