A more convenient way to store properties than HashMap?

Discussion in 'Plugin Development' started by TigerHix, Jul 16, 2013.

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

    TigerHix

    For example, I want to save player's properties (but not saving file to disk): points, coins & kills.
    This can achieve through a HashMap:

    HashMap <String, List <Integer> > playerDatabase = new HashMap <String, List <Integer> >;

    Or three HashMap:

    HashMap <String, Integer> pointDatabase = new HashMap <String, Integer>;
    HashMap <String, Integer> coinDatabase = new HashMap <String, Integer>;
    HashMap <String, Integer> killDatabase = new HashMap <String, Integer>;

    But beside of controlling HashMap, is there a way to just edit properties like this:

    Player p;
    p.points += 1000;
    p.coins = 100;
    p.kills ++;

    I am fresh to Java, so please forgive me for these noobish questions :(
     
  2. Offline

    seemethere

    You could create a separate class for a custom player object that includes that stuff.

    Code:java
    1. public class CustomPlayer {
    2. String name;
    3. int points;
    4. int coins;
    5. int kills;
    6.  
    7. public CustomPlayer(String name) {
    8. this.name = name;
    9. }
    10.  
    11. public String getName() {
    12. return name;
    13. }
    14.  
    15. public int getPoints() {
    16. return points;
    17. }
    18.  
    19. public int getCoins() {
    20. return coins;
    21. }
    22.  
    23. public int getKills() {
    24. return kills;
    25. }
    26.  
    27. public void setPoints(int newValue) {
    28. points = newValue;
    29. }
    30.  
    31. public void setCoins(int newValue) {
    32. coins = newValue;
    33. }
    34.  
    35. public void setKills(int newValue) {
    36. kills = newValue;
    37. }
    38. }


    Store those CustomPlayer objects in a List or Hashmap whatever you prefer and that'd do exactly what you want without creating unnecessary Hashmap complexity

    TigerHix ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    TigerHix likes this.
  3. Offline

    TigerHix

  4. Offline

    TigerHix

    seemethere
    I use this code to create a HashMap.
    public HashMap <String, QuakePlayer> = new HashMap <String, QuakePlayer>();

    But it returns a error: Syntax error on token ">", VariableDeclaratorId expected after this token :(

    Edit: What the hell to my IQ!? Problem solved.
     
  5. Offline

    BENCLABSTER

    name your hashmap :p
    Code:java
    1. public HashMap<String, QuakePlayer> quakePlayer = new HashMap<String, QuakePlayer>();
     
    TigerHix likes this.
  6. Offline

    bennie3211

    before the '=' you have to give the hashmap a name ;)

    EDIT; ninja'd
     
    TigerHix likes this.
  7. Offline

    TigerHix

Thread Status:
Not open for further replies.

Share This Page