Properly storing information

Discussion in 'Plugin Development' started by Mrwinkles, Jul 22, 2014.

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

    Mrwinkles

    I was hoping someone could enlighten me on the topic.

    Essentially my question is what is the most efficient way of storing player data?

    Arraylists/Hashmaps etc. (This just doesn't seem like a clean way of doing it for some reason.)

    Or should I be implementing getters/setters to hold the information?
     
  2. Offline

    Gater12

    Mrwinkles
    Make a class that stores player data and attributes. Make a HashMap that stores the player data object to the player (Like a "manager" class)
     
  3. Offline

    jthort

    Gater12 Mrwinkles Well I think it would depend on how much data you are planning on storing. I mean if it's only a few then there really is no reason to create another class unless it's for organizational purposes.
     
  4. Offline

    Mrwinkles

    So I created a class to store specific variables about each player such as admin/vip/muted etc but I don't think I'm doing it correctly as the variables are overwritten every time a new player connects.

    Gater12 jthort Any idea?
     
  5. Offline

    Kassestral

    Mrwinkles
    Can we see the code for it?
     
  6. Offline

    Mrwinkles

    At the top of the class:
    Code:
    nPlayer nPlayer;

    On PlayerJoinEvent I have:
    Code:
    nPlayer = new nPlayer(player.getName());

    The nPlayer class iteself:
    Code:
    public class nPlayer {
        private String name;
        private boolean muted;
        private boolean admin;
        private boolean mod;
        private boolean pvp;
     
        public nPlayer(String name) {
            name = name;
            muted = false;
            admin = false;
        }
    }
    
     
  7. Offline

    jthort

    Mrwinkles Your going to want to do
    Code:
    this.name = name
    in your constructor, along with muted and admin.

    Also you have a variable named the same as your class, make it something like
    Code:
    NewPlayer nPlayer = NewPlayer(...);
    Another thing, you have a variable in your PlayerJoinEvent, and everytime you are setting that variable to the NEW nPlayer object. To hold all the new player objects your going to want to use a List.
     
  8. Offline

    Mrwinkles


    Ah yes, thanks for the tips.

    As for the list.. something a little along the lines of..
    Code:
    public ArrayList<NewPlayer> nPlayerList = new ArrayList<NewPlayer>();
    on PlayerJoinEvent:
    Code:
    nPlayerList.add(nPlayer);
    And then when I need a specific player I just iterate through the list and set the variable using one the methods written to get the variables in the NewPlayer class:
    Code:
    for (NewPlayer nPlayer : nPlayerList) {
        if (nPlayer.getName() == player.getName()) {
            nPlayer.setAdmin(true);
        }
    }
    
    ??
     
  9. Offline

    ResultStatic

    Mrwinkles use a hashmap. HashMap<String,NewPlayer>
     
  10. Offline

    Mrwinkles


    I managed to get it to work but I agree that I should be using a HashMap thanks.
     
  11. Offline

    jthort

    Th erected is re
    Ally no need for hash maps when you can just created a function that iterates through the list and checks the name but whatever works
     
  12. Offline

    Mrwinkles


    Code:
    public NewPlayer getNewPlayer(String playername) {
            for (NewPlayer nPlayer : nPlayerList) {
                if (nPlayer.getName() == playername) {
                  return nPlayer;
                }
            }
            return null;
        }
    Wouldn't a hasmap be more efficient as you could just refer to the key which would be the players name and get the NewPlayer instance from it?
     
  13. Offline

    Traks

    Please don't use the == operator for object comparisons, it compares references to objects and not the actual logical implementation of what makes two objects equal. Use the equals(Object) method instead. And yes, HashMaps in particular are much more efficient than iterating over a Collection, since HashMap lookups are much closer to O(1) than O(n)
     
    AoH_Ruthless likes this.
  14. Offline

    DannyDog

    Mrwinkles
    Couldn't you just iterate through the list of instances then check each one if name equals the player's name?
     
  15. Offline

    Dragonphase

  16. Offline

    Mrwinkles


    You're right. I have gone ahead and made the appropriate changes thanks!

    Dragonphase I will definitely keep that in mind though maybe I should use something like mysql for the NewPlayer class data?
     
Thread Status:
Not open for further replies.

Share This Page