Hashmap or string array?

Discussion in 'Plugin Development' started by johnny boy, Jan 21, 2017.

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

    johnny boy

    I have a plugin and I need to score everyone's pitch and yaw. Would I use a hash map? So I can store their name and their pitch + yaw or just their name :p

    (I think I remember how to use hashmaps.. Wasn't it
    Code:Java
    1. Hashmap<Stuff in here> = new Hashmap();
    )
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    johnny boy

    How the hell do I do this... I need to get all the online players, store their pitch and yaw AND Their UUID THEN once the player dies I need to loop through the hashmap and set the pitch and yaw to their name. Jesus Christ..
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Can you take a step back and tell why you need this?
    Might be a better solution
     
  5. Offline

    Irantwomiles

    But you might want to use the config, and based on what you're doing get all of the online players and store their uuid, pitch, and yaw in the config (note this will not be very efficient if you have a lot of players). This is my best guess since I don't actually know what you're doing.
     
  6. Offline

    johnny boy

    Worn
    Unfortunately you are wrong. I have a plugin and when a player dies to lightning the player looks up, so I store the players name, pitch, and yaw in a hashmap then when the player dies, I make a for loop then I check if the player is in the hashmap, if they are, then I set their pitch and yaw to what it was 1 tick before the player died, if no player is found, the loop will go on to the next person. So please don't say that I don't know what I'm doing.
     
  7. Offline

    Irantwomiles

    I don't know how you got that from what I said... I just proposed another solution. and I tagged you because I agreed with the fact that we need to know what he wants to do before proposing other solutions.
     
  8. Offline

    johnny boy

    Can I store more than 1 thing in a hashmap.. Can't figure out how :/ It should just be <String, h, String, g> or something like that.
     
  9. Offline

    Irantwomiles

    You can't do th at with a map, which is why I proposed a config.
     
  10. Offline

    johnny boy

    Hm.. but a config is a bit more complex, a hashmap will probably save space and time. I'll start the hashmaps now.

    EDIT: Am I in the right direction?
    Code:Java
    1.  
    2. HashMap<UUID, String> UUID = new HashMap<UUID, String>();
    3. HashMap<Float, Float> Pitch = new HashMap<Float, Float>();
    4. HashMap<Float, Float> Yaw = new HashMap<Float, Float>();
     
    Last edited by a moderator: Jan 21, 2017
  11. Offline

    timtower Administrator Administrator Moderator

    @MemeplexOwner Make a custom class that stores the Yaw and Pitch.
    Put that in a map next to the UUID
     
    Irantwomiles likes this.
  12. Offline

    ShaneCraftDev

    @MemeplexOwner
    There is no relation to the player's uuid in your pitch and yaw map collections. If you want an easy answer, I'd suggest using a map with the uuid of the player as String as key and either a wrapper or tuple/pair class for the value.
    Code:java
    1.  
    2. Map<String, Tuple<Float, Float> playerAngles = new HashMap<>();
    3.  
    4. // example store
    5. playerAngles.put("some uuid", new Tuple<Float, Float>(0.5F, 3.14159F));
    6.  
     
  13. Offline

    kameronn

    Why not just use an array in the hashmap instead of creating an object
     
  14. Offline

    johnny boy

    I have used HashMaps, not Maps. Google time!
     
  15. @MemeplexOwner
    Well, if you have used HashMaps, then you have also used Maps. Maps is just referring to the general type of collection which stores objects in pairs (ie HashMap, TreeMap, ConcurrentMap, etc).

    @kameronn
    While using an array would certainly work, I think it'd make the code a bit messy. If you really for whatever reason don't want to create an object, then use @ShaneCraftDev's solution of an already existing key-value pair object (ie Tuple, Pair, etc)
     
  16. Offline

    johnny boy

  17. @MemeplexOwner
    It's just a way of storing two object in one object, it's basically a HashMap which can only contain one value. I'd recommend using the org.apache.commons.lang3.tuple.Pair class. So you'd just have the HashMap take a UUID for identifying the players, and as value have a Pair object with two floats (or whatever other objects you wanted to store). Something like this:
    Code:java
    1. Map<UUID, Pair<Float, Float>> myMap = new HashMap<>();
     
  18. Offline

    DoggyCode™

    Well, you can. Just like in the config, which is built on the system of having keys and values. However, instead of doing something like this in the config:

    HTML:
    key1:
      myobject:
        value1: 2
        value2: 'this is a value'
        value3: false
    Your hashmap would look like
    PHP:
    HashMap<KeyMyObjectmap = new HashMap<KeyMyObject>();
    MyObject being:

    PHP:
    class MyObject {

      private 
    int value1;
      private 
    String value2;
      private 
    boolean value3;

    }
     
  19. Offline

    johnny boy

    I'm abandoning this project. So thanks for all the help but I'm gonna come back to it another time.. Doesn't mean I'm not gonna do it"
     
Thread Status:
Not open for further replies.

Share This Page