How to save two values in one system?

Discussion in 'Plugin Development' started by Schwarzer Zylinder, Nov 26, 2011.

Thread Status:
Not open for further replies.
  1. Hi,
    I want to save a player and a location in one data system. But an array for example can only save one value. HashMap etc. doesn't work, because I don't know the players and so I don't have any key. Is there a system like an array, which can be read out completely without knowing something about the content? With index ids or something?

    Thanks.
     
  2. Why don't you know the players?
     
  3. Offline

    Nitnelave

    Well, you could create a holder class, with a player and a location, and do a list of this holder class:
    List<Holder> list;
    h = list.get(1);
    h.getPlayer();
    h.getLocation();
     
  4. Offline

    Darkman2412

    How can you add something if you don't know what to add?
     
  5. I get all Players standing in an area and save them to a HashMap for example. But I can't read it using for and index, I can only get values for specific keys. I could save the player names in a second system, but I want to have only one.
     
  6. Offline

    ItsHarry

    Why can't you read it using for and index? o-o
     
  7. Code:java
    1. private HashMap<String, Location> playerLocations = new HashMap<String, Location>();
    2.  
    3. public void putPlayer(Player player) {
    4. if (player == null)
    5. return;
    6.  
    7. playerLocations.put(player.getName(), player.getLocation());
    8. }
    9.  
    10. public Location[] giefPlayerLocations() {
    11. return playerLocations.values().toArray();
    12. }
    13.  
    14. public void iterateOverMap() {
    15. for (String playerName : playerLocations.keySet()) {
    16. //do stuff
    17. }
    18. }
     
    iPhysX likes this.
  8. Works now, thanks!
     
  9. Depending if you want to get Key and Values but not edit them you could also use this one:

    Code:java
    1. for (Map.Entry<K,V> entries : map.entrySet()) {
    2. entries.getKey(); // Gets the Key
    3. entries.getValue(); // Gets the Value bound to the Key
    4. }


    K,V are the Object classes used, eg String and Location, map is the Map (eg HashMap) you use
     
Thread Status:
Not open for further replies.

Share This Page