Solved Kit, How would I fix it?

Discussion in 'Spigot Plugin Development' started by RoboBoy99, Feb 19, 2020.

  1. For some reason when I use the method giveKit the ItemStacks of the selected kit being replaced by the current player inventory
    Code (Text):
        public Map<String, ItemStack[]> Kits = new HashMap<>();
       
        public void createKit(Player player, String kitName)
        {
            if(Kits.containsKey(kitName)) {
                player.sendMessage(ChatColor.RED + "The Kit " + kitName + " is already exits");
            } else {
                   
                Kits.put(kitName, player.getInventory().getContents());
               
            }
        }
       
        public  void giveKit(Player player, String kitName)
        {
            if(Kits.containsKey(kitName))
            {
                if(Kits.get(kitName) != null) {
                    for(ItemStack[] m : Kits.values())
                    {
                        player.getInventory().addItem(m);
                    }
                }
            }
        }
     
  2. Code (Java):
    if(Kits.containsKey(kitName)) {
      if(Kits.get(kitName) != null) {
        for(ItemStack[] m : Kits.values()) {
          player.getInventory().addItem(m);
        }
      }
    }
    What are you doing? look closly:
    for(ItemStack[] m : Kits.values())
    Why?
    why not just:
    Code (Java):
        for(ItemStack m : Kits.get(kitName)) {
          player.getInventory().addItem(m);
        }
     
  3. @PvPNiK
    Because I'm getting an error when I do that
    It's saying the error is in here:
    Code (Text):
    player.getInventory().addItem(m);    
     
    #3 RoboBoy99, Feb 20, 2020
    Last edited: Feb 20, 2020
  4. send the new code and the error
    edit: try:

    Code (Java):
        for(ItemStack m : Kits.get(kitName)) {
    if (m == null) continue;
          player.getInventory().addItem(m);
        }
     
  5. @PvPNiK
    Now i have no errors but it doesn't give me the items
     
  6. send you new code
    and add a debug message (after: if(Kits.containsKey(kitName)){ ):
    Bukkit.broadcastMessage(kitName + ", " + Kits.get(kitName));
     
  7. That's weird it's now working but the only thing that I changed was adding the broadcast msg but still thx