Solved HashMap in HashMap

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

  1. I'm trying to create an auction plugin and right now I'm stuck. First I created a HashMap which holds ItemStack and Integer (price) then I realize I have to get the seller's name too. Since HashMaps can only have 2 objects I came up with this. I don't know if there is a better way to do this.

    The HashMaps:
    Code (Text):
    public class Auctions {
        public static LinkedHashMap<ItemStack, Integer> items = new LinkedHashMap<ItemStack, Integer>();
        public static LinkedHashMap<Player, LinkedHashMap<ItemStack, Integer>> auclist = new LinkedHashMap<Player, LinkedHashMap<ItemStack, Integer>>();
    }
     
    This is for adding items:
    Code (Text):
    Auctions.items.put(item, money);
    Auctions.auclist.put(player, Auctions.items);
    This is how I show the inventory:
    Code (Text):
    Inventory Aucinv = Bukkit.createInventory(player, 54, "§c§lAuctions");
            for (Map.Entry <Player, LinkedHashMap<ItemStack, Integer>> entry : Auctions.auclist.entrySet()) {
                Player player1 = entry.getKey();
                for (Map.Entry <ItemStack, Integer> entry1 : Auctions.items.entrySet()) {
                    ItemStack item = entry1.getKey();
                    Integer price = entry1.getValue();
                    Aucinv.addItem(item);
                }
            }
    The thing is how can I get names and (itemname, price) when player buy it?
     
  2. What I did with my Auction plugin was set the Price and Seller's name into the Lore of the item, then I looped through the lore and split "Price: 500" at ":" and split "Seller: Squallz" at ":", then assigned them to strings/integers.
     
    • Useful Useful x 1
  3. Thats great but I wonder can I do this in my way? (Yes I love challange)
     
  4. Guys, you're using an object-oriented programming language. Create a class, that stores the ItemStacks, Price and the seller, then simply move over to a List and remove the first entry whenever an auction is over.
     
    • Winner Winner x 2
    • Agree Agree x 1
    • Informative Informative x 1
  5. Instead of creating HashMaps inside of HashMaps, you could try creating a class with a constructor that holds the ItemStack + Integer, example:

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

    public class Example {
        public ItemStack item = null;
        public int price = 0;
     
        public Example(ItemStack item, int price) {
            this.item = item;
            this.price = price;
        }
    }
    Now, instead of doing... that, you can do this:
    Code (Text):
    public static LinkedHashMap<Player, Example> auclist = new LinkedHashMap<Player, Example>();
    EDIT: @Puharesource replied faster than me... :(

    EDIT 2: You can also use a list and hold the player who is selling the item in the Example class, also, to initialize the class, you need to use this:

    Code (Text):
    Example example = new Example(item, price);

    hashmap.put(player, example);
     
    • Useful Useful x 1
  6. Thats a better idea! I will try that and see if I can manage to get it working ;)
     
  7. Before I learned about using Constructors, I done the way you were doing before (or even worse, saving the data on a String so I could store more info...)

    Well, DON'T DO THAT AND USE CONSTRUCTORS.

    It will way better using constructors than doing some kind of black magic just to store the values.
     
    • Informative Informative x 1