Inventory From HashMap

Discussion in 'Plugin Development' started by 1Camer0471, Feb 11, 2015.

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


    So I am using this code to try and load an inventory from a hashmap:
    But for some reason, I is returning the "debug:3" on line 12

    2. if (thiefinv.containsKey(p.getUniqueId())) {
    3. ArrayList<ItemStack[]> tinv = new ArrayList<>();
    4. tinv.add(thiefinv.get(p.getUniqueId()).getContents());
    5. for (ItemStack[] tinvloop : tinv) {
    6. for (ItemStack tinvtest : tinvloop) {
    7. if (tinvloop != null && tinvtest != null
    8. && tinvtest.getType() != Material.AIR) {
    10. p.getInventory().addItem(tinvtest);
    11. } else {
    12. p.sendMessage("debug:3");
    13. }
    14. break;
    15. }
    16. }
    18. }
    19. p.closeInventory();
    20. p.setExp(thiefexp.get(p.getUniqueId()));
    21. }

    To make sure that there inventory is not null, I add in a glass onJoin
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e) {
    4. final Player p = e.getPlayer();
    5. p.getInventory().addItem(new ItemStack(Material.GLASS));

    Also, In my onQuit, I save thier inventory and then clear it so that items dont get duplicated:
    3. Location savedthiefloc = new Location(p.getWorld(), x, y, z);
    4. lastthiefloc.put(p.getUniqueId(), savedthiefloc);
    5. System.out.println("Saved " + p.getName()
    6. + "'s location to map: lastthiefloc");
    7. thiefinv.put(p.getUniqueId(), inv);
    8. inv.clear();
  2. Offline


    Why are you using a break statement?
  3. Offline


    You have a lot of '&&' in your if statement. You should break each of them down and see which one is causing the problem.
  4. Offline


    1. You're using "break" without even checking anything, which will cause the for-loop to break after the first entry like @1Rogue stated
    2. How is this supposed to work? Are you REALLY using an ItemStack[] as key in your map?

    3. If there'a a stacktrace -> show it please
  5. Offline


    No, its <UUID, Inventory> but you gotta loop through the stacks in the inventory and then through the items in the statcks, there is no stack trace cause I have a null check, its returning "debug:3" on line 12
  6. Offline


    I'll reiterate my question:
  7. Offline


    I didn't know what they did xD I have always used them but i guess thats a bad idea :/
  8. Offline


    It's a general principle that if you don't know what something does, using it randomly is probably a very bad idea.
  9. Offline


    Okay, thanks for lettimg me know what the break does, but do you have any idea why I'm getting debug:3 ?
  10. Offline


    Clearly one of the itemstacks is null
  11. Offline


    But I use a for loop to load only not null itemstacks and add in a glass so taht they have atleast one item
  12. Offline


    You copy the contents of an inventory:

    1. tinv.add(thiefinv.get(p.getUniqueId()).getContents());

    That in itself will have null values if any of the inventory spaces is empty. After that you run a for loop, and "debug:3" is printed if any are null. Fairly self-explanatory.

    Additionally, your theifinv#get call will likely return null if you aren't using an ItemStack[] for the map keys (which, by the way, is an awful idea. Use player UUIDs).
  13. Offline


    Doesn't map.get() not return the key?

    I'm having a new problem, its giving me only the last item from my saved inv

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page