Hashmap, String, List or what

Discussion in 'Spigot Plugin Development' started by Smyle, Feb 23, 2019.

  1. Sup.

    A couple days ago i started tryin to develop a kinda "warp teleport" style plugin with all these stuff like SQL Connection, storing data by using String and Integers (with UUID, etc.)... But i got an issue:

    Actually, i have 3 different Strings which will serve like "warp" names, such named as loja1, loja2 and loja3 (all of these Strings can be repleaced for other things such as "e-mail", "password", etc. it works the same way) and everything works perfectly fine until the momment i have to display all of these 3 strings (for other players) like you would use to display an List inside a loop.

    Here is what i use for display it for the player whom has the String (it works tho):

    [​IMG]

    This method will return/set the String loja1 as well as the other two methods i have to display/set all the other Strings. But the issue is... When i want to return these Strings in a command such as /displaystrings, for example, i would use getLoja1(), getLoja2(), etc. But what should i do if i want to return all of these strings for all players online? Like looping.

    No spon feed, pls, i just want to understand what i have to do, not copy paste a code and still being knowless.
    Thanks

    Ps: i hope all i said is readable, my english sucks (obviously i speak a different language)
     
  2. You might want to store them all in a List then instead of having a method for each one like getNomeLoja1() and setNomeLoja1(String loja1)
    you could create methods: getNomeLoja(int index) and setNomeLoja(int index, String loja)
    I hope that makes sense to you.
     
  3. Hey, thanks for replying.

    Okay, i will try to index them into a List, but what about if a player tries to see his own "warp"? How would i select the match number on List to return his "warp" name?

    Thanks again.
     
  4. You need to store a new hashmap or a class where you can store the player and the warp
     
  5. You can use a Map.

    Maps are collections of pairs.
    These pairs can be of the same type, or different types.
    For example:
    Code (Java):

    Map<String, Location> locations = new HashMap<>(); // new HashMap<String, Location>() for Java <7

    /* Put a pair */
    locations.put("spawn", new Location(world, 0, 0, 0));

    /* Get a warp */
    locations.get("spawn"); // Will return null if not found
     
    For more information, see the Javadoc for Map.
     
    • Agree Agree x 1