Selected kit Hashmap

Discussion in 'Plugin Development' started by TheeLuke, Jul 8, 2019.

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

    TheeLuke

    Hello!

    As you can read from the title, I am doing a hashmap that knows what kit the player has, and gives it to them at the start of the game. I know a good bit of Java but not the most.

    One section I am unfamiliar with is Hashmaps. My goal with the kit selection is that the plugin should know the player, the kit they have selected, and then give it to them at the start of the game. Currently, all I have is a basic HashMap

    Hashmap:
    private HashMap<Player, String> kit = new Hashmap<Player, String>();

    I don't want to be stealing code and leaving with questions, if you could teach me so for next time I have a better understanding, that would be helpful!

    Thanks!
     
  2. So you just created the hashmap. To put data into it do
    kit.put(player, "string");
    Note that hashmaps have unique keys meaning that you override the kit when using .put() with the same player again.
    To read data just type
    kit.get(player);

    Hope this helps, otherwise explain what else you want to know ^^
     
  3. Offline

    CraftCreeper6

    @TheeLuke

    HashMaps (as you may know) contain a key and a value.

    For your example the key is the player ** and the value is the kit they have selected.

    ** You should really not be storing the player, you should be storing their UUID, and parsing that UUID to a player again afterwards, this saves many problems that can be caused by storing the Player class.

    When the player selects a kit you need to add that player and their kit to the HashMap.

    Code:
    map#put(key, value)
    That snippet of code should help with that.

    When you want to retrieve each player and their kit you can run a while loop, for loop, whatever you choose.

    Code:
    for (key k in map#keys)
    {
           player p = k#player // not a real method, substitute with UUID parse
           value v = map[k]
           // Do whatever you want with  the key and value
    }
     
    Kars likes this.
  4. @TheeLuke
    So just to be precise:
    All keys of a hashmap you get with
    Code:
    hashMap.keySet();
    (In the for loop) And also you can't use [key] on a hashmap, instead do
    Code:
    hashMap.get(key);
    Currently I dont have an IDE, so correct me if I say that there is no for(key k in something) loop. In my opinion the 'in' should be replaced with ':'
     
  5. Offline

    CraftCreeper6

    It was pseudo code, and I've been doing a bunch of python lately. I wasn't intending to give OP exact code as it encourages them to figure it out themselves, I've just given a stepping stone.

    But yes, you're right.
     
    DerDonut likes this.
Thread Status:
Not open for further replies.

Share This Page