Solved How to get all items from a HashMap to a created inventory

Discussion in 'Spigot Plugin Development' started by Menfie, May 11, 2016.

  1. I have a HashMap like this: private HashMap<ItemStack, Integer> items = new HashMap<ItemStack, Integer>();

    What I want to do is I want to get all items from that HashMap to created inventory.

    Code (Text):
    public Inventory getAuctions(Player player) {
            Inventory Aucinv = Bukkit.createInventory(player, 54, "§c§lAuctions);
            Auctions ex = new Auctions();
            HashMap<ItemStack, Integer> items = ex.getHashmap();
            // ! HashMap.get !
            return Aucinv;
        }
     
  2. You can grab every key in a HashMap via HashMap.keySet(), then iterate over it with your prefered loop type.
     
    • Informative Informative x 1
  3. Thanks!
     
  4. If you also want to use the values set by these keys rather than iterating over the keys and grabbing the values through a get call you can grab both all at the same time via HashMap.entrySet().
     
    • Informative Informative x 1
  5. I did something like this but it's not sending every item but just last one. It has to tell me every item that i added before after adding items. Isn't it?

    Code (Text):
    if (subcommand.equalsIgnoreCase("add")) {

                        if (money != null) {
                            ItemStack i = player.getItemInHand();
                            commandSender.sendMessage("Working!");
                            Auctions ex = new Auctions();
                            HashMap<ItemStack, Integer> items = ex.getHashmap();
                            items.put(i, money);
                            for (Map.Entry <ItemStack, Integer> entry : items.entrySet()) {
                                ItemStack item = entry.getKey();
                                Integer price = entry.getValue();
                                String price2 = price.toString();
                                Integer itemAmount = item.getAmount();
                                player.sendMessage(price2 + itemAmount);
                            }

                        }

                    }
     
    #5 Menfie, May 11, 2016
    Last edited: May 11, 2016
  6. Can you show me the Auctions constructor. Also, you don't need to specifically store an Integer or int as a String before being able to use it in an inline string builder.

    Code (Text):
    String price2 = price.toString();
    Integer itemAmount = item.getAmount();
    player.sendMessage(price2 + itemAmount);
    Does the same as
    Code (Text):
    player.sendMessage(price + item.getAmount());
     
    • Useful Useful x 1
  7. Here is Auction.

    Code (Text):
    import org.bukkit.inventory.ItemStack;

    import java.util.HashMap;


    public class Auctions {

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

            public HashMap<ItemStack, Integer> getHashmap() {
                return items;
            }

            public void setHashmap (HashMap <ItemStack, Integer> items) {
                this.items = items;
            }

    }
     
     
  8. Ok i made some changes and here is my full code. When I type /aa there is nothing added.

    Code (Text):


    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import java.util.HashMap;
    import java.util.Map;

    public class Commands implements CommandExecutor {

        @Override

        public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {

            if (command.getLabel().equalsIgnoreCase("aa") && commandSender instanceof Player) {

                Player player = (Player) commandSender;

                if (args.length == 0) {

                    if (player.hasPermission("auctions.open")) {

                        player.openInventory(getAuctions(player));


                    } else {
                        commandSender.sendMessage("§e§l(!) §e§nYou have no permission!");
                    }

                }

                else if (args.length == 1) {
                    String subcommand = args[0];
                    if (subcommand.equalsIgnoreCase("help")) {
                        commandSender.sendMessage("§c§l/aa: §7List auctions");
                        commandSender.sendMessage("§c§l/aa add price: §7Add item on hand to auctions")
                    } else {
                        commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa help");
                    }
                }

                else if (args.length >= 2) {

                    String subcommand = args[0];
                    if (subcommand.equalsIgnoreCase("add")) {

                        try {
                            Integer.parseInt(args[1]);
                        } catch (NumberFormatException ex) {
                            commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa add price");
                            return true;
                        }
                        Integer money = Integer.parseInt(args[1]);

                        if (money != null) {
                            ItemStack i = player.getItemInHand();
                            if (i.getType() != Material.AIR) {
                                Auctions ex = new Auctions();
                                HashMap<ItemStack, Integer> items = ex.getHashmap();
                                items.put(i, money);
                                commandSender.sendMessage("§e§l(!) §eYour item is now on auction!");
                                player.getInventory().remove(i);

                            } else {
                                commandSender.sendMessage("§e§l(!) §eDon't you think you must have an item on your hand?");
                            }


                        }

                    } else {
                        commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa help");
                    }

                }

                else {

                    commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa help");

                }

            }

            return false;
        }

        public Inventory getAuctions(Player player) {
            Inventory Aucinv = Bukkit.createInventory(player, 54, "§c§lAuctions");
            Auctions ex = new Auctions();
            HashMap <ItemStack, Integer> items = ex.getHashmap();
            for (Map.Entry <ItemStack, Integer> entry : items.entrySet()) {
                ItemStack item = entry.getKey();
                Integer price = entry.getValue();
                Aucinv.addItem(item);
            }
            return Aucinv;
        }
    }
     
     
  9. Update: When I move my hashmap to same class file with commands it's working . . . Is there anything wrong with my Auctions class?
     
  10. I don't see any constructor to add or remove items from that HashMap? Also, this is a object that you'd like to crate a instance of at onEnable, then have a method to get that object. Then using that object to add and retrieve stuff from the map.

    Edit:

    As you can see,
    Auctions ex = new Auctions();
    You are creating a new Actions object every time someone types that command, you don't wanna do that. You wanna get the already existing object which you crated in the onEnable, then add and remove to that.
     
    • Informative Informative x 1
  11. Here, I'll give you a small example:

    Code (Java):

    public class MyPlugin extends JavaPlugin {

      private Auctions auctions = null; //we're not assigning it yet

      public void onEnable() {
        auctions = new Auctions(); //creating it ONE time
      }

      //getter for getting the auctions object
      public Auctions getAuctions() {
        return this.auctions;
      }
    }

    public class MyCommand implements CommandExecutor {

      //creating a instance of your main class so we can call the constructor
      MyPlugin plugin;
      public MyCommand(MyPlugin ins) {
        this.plugin = ins;
      }

      /*
      Your onCommand method...
      */

      {
        this.plugin.getAuctions().getHashmap().add(...);   //we're not creating a new one, put getting the already created one
      }
    }
     
    • Informative Informative x 1
  12. Thanks for your helps and great example.
     
    • Like Like x 1