Solved HashMap Getting Values

Discussion in 'Spigot Plugin Development' started by Nutowen, May 6, 2017.

  1. I have a hashmap and set each itemstack to have an integer (percentage). I want to make it so when I set the config each item has a percentage.

    Code (Text):

    ItemStack[] armorArray = invC.getContents();
                           
                            int id = 0;
                            for(ItemStack item : armorArray){
                            Main.inst().getConfig().set("Common.Items." + id + ".item", item);
                            Main.inst().getConfig().set("Common.Items." + id + ".percentage", AddItem.getInstance().items);
                           
                            id++;
                            }
                           
                            Main.inst().saveConfig();

     
    How would I get all the ints from the hashmap
     
  2. Sorry, I'm having trouble understanding your issue. Are you trying to make a HashMap? Or get values from a HashMap that isn't in the code?
     
  3. I think he's wanting to grab the items from a config and a hashmap with the pre-determined percentages in the config or something of the genre.
    I do not understand very well either.
     
    • Agree Agree x 1
  4. Could you please explain,
    All im getting is that you want to iterate over the players inventory and set each item to be a percentage?

    Would you want them to be a random chance?
     
  5. I wanna get values from a hashmap set in different code yea
     
  6. Aaaand can you provide the code that your question literally revolves around? If your value of the hashmap is an integer, you can just do HashMap#values()
     
  7. You do /additem <percentage> and an inventory shows up. you click a slot and its replaced with the item in your hand. Then I want the item and number to be saved to the config. I just need all the numbers from the hashmap which when you do /additem the item and number in the args is saved in the hashmap
     
  8. So can I see the HashMap, and other code?
     
  9. Code (Text):

    public HashMap<ItemStack,Integer> items = new HashMap<ItemStack,Integer>();

     public int percentage;
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("additem")){
                if(args.length != 1){
                    sender.sendMessage("Need percentage");
                }
                 setupInvAdmin();

                  try {
                       percentage = Integer.parseInt(args[0]);
                   } catch (Exception arg11) {
                       sender.sendMessage(ChatColor.RED + "Percentage should be a number!");
                       return true;
                   }
                  items.put(p.getItemInHand(), percentage);
                 
                  p.openInventory(inv);