1.15.2 per player variable

Discussion in 'Spigot Plugin Development' started by catssss, Mar 27, 2020.

  1. how would I make a per player variable so that each player has a variable because I want to make something like hypixel skyblock mana but don't know how I would save the integer for the player.
  2. Not that I am not willing to help, but it sounds like you have little or no knowledge in what you would need to accomplish this in Java.

    You would use a very basic data structure in Java that is called Map for this.
    Since you obviously don't know what this is (or you would not ask the question), I would recommend you to read up on how some basic data structures work in Java. This will improve the quality of your future coding and will save us time answering questions related to fundamental Java knowledge :)
  3. I know what a map is IDK how to use them super well but I have used them before but IDK how I would use it for what I want I could probably figure it out and get it to work but it would be pretty bad compared to someone else's code
  4. Code (Java):
    // Create a map
    Map<UUID, YourType> map = new HashMap<UUID, YourType>();

    // Put values into it, using the player UUID as key and a value
    map.put(player.getUniqueId(), value);

    // Check if a value was put for the players UUID before

    // Get a value with the players UUID (returns null if not set)
    YourType value = map.get(player.getUniqueId());
    You just need to integrate it in your code properly.

    Edit: Changed small typo mentioned by @drives_a_ford - now follows LSP
    #4 Noxyro, Mar 27, 2020
    Last edited: Apr 15, 2020
    • Like Like x 1
  5. Thank you that actually clears up how to use a hashmap some as well
  6. This doesn't really need to be persistent because it is gonna have a max value and if somebody reloads and makes it go to max that's fine
  7. hi I just started using this and I wanted to know how I would edit a value would it just be map.put(player.getunigueid, value)
  8. Putting into a map adds a new entry or replaces an entry if the key already exists
  9. drives_a_ford


    Remember the Liskov substitution principle.
    The variable should be of type Map, not HashMap.
  10. Was a typo, as I started writing it in Kotlin syntax first ;)
    Changed now.