Storing values along with the player name

Discussion in 'Plugin Development' started by theninthworldix, Jan 18, 2011.

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

    theninthworldix

    When making a plugin where the command can be turned on and off, I will usually use an arraylist like so:

    private ArrayList<String> commandEnabled = new ArrayList<String>();
    //Not real code after this
    if (command == isOff){
    commandEnabled.add( player.name );
    }else if(command == isOn){
    commandEnabled.remove( player.name );}

    Now this is great if I just want an on/off command, but I want a command that can store values along with the name(only for on/off purposes). For example, if I was to make a cuboid plugin, I would want an array that stores the players name along with the x1,y1,z1,x2,y2,z2 and state.

    player[x1,y1,z1,x2,y2,z2,state]

    Is there a way to make the arraylist store multiple values along with the name as an id of sorts?

    EDIT: The code tags aren't working for me, so the color text will have to do.
     
  2. Offline

    Archelaus

    Look up 2-dimensional arrays.
     
  3. Offline

    theninthworldix

    How can 2d arrays be achieved with an ArrayList though? I know how do it with a regular array, but I don't think that gives the same functions (ex: .add(), .remove())
     
  4. Offline

    Mixcoatl

    You'll probably want to use a map and a class specialized to the type of settings you want to store.
    Here's a minimal example of a settings class:
    Code:
    class Settings {
        // Omitting getters and setters for brevity.
        double x1, y1, z1;
        double x2, y2, z2;
    }
    
    Then later on you could have a map like this:
    Code:
    Map<String, Settings> settingsMap = new HashMap<String, Settings>();
    
    // to get the settings for a player:
    Settings s = settingsMap.get(player.getName());
    
    // to add settings for a player:
    settingsMap.put(player.getName(), settings);
    
    // to remove a player's settings:
    settingsMap.remove(player.getName());
    I would like to point out that there are lots of different ways to do this. This is not the only way. But it's a very nice, clean way, especially when you need to keep complex information for each player (or mobile, or chunk, or...).
     
  5. Offline

    Raphfrk

    ArrayLists are a bad choice here btw.

    You would get better performance with a HashSet. With an ArrayList, to remove a player you have to scan the entire list.

    For your second issue, if I understand correctly, you should use a HashMap

    Code:
    HashMap<String,Object[]> map = new HashMap<String,Object[]>();
    
    You can then use

    To add:
    map.put( playerName , new Object { value, value, value, value } );

    To remove:
    map.remove( playerName );

    To retrieve:

    Object objArray = map.get( playerName );

    value1 = (String)objArray[2];
     
  6. Offline

    theninthworldix

    So if I wanted to change a variable in the Settings for a player, I would do settingsMap.put(player.getName(), Settings.x1); ?

    EDIT: That is simple enough Raphfrk =)
     
  7. Offline

    Archelaus

    Code:
               String data = "Name/X1/Y1/Z1/X2/Y2/Z2/STATE";
               String[] dataSplit = data.split("/");
               for(String dataI : dataSplit) {
                  System.out.println(dataI.toString());
               }
    Sorry, simpler solution found. I'm quite new to Java, I didn't know about HashMaps.
     
  8. Offline

    Raphfrk

    It would depend on how the Settings class works.

    It could be:

    Settings temp = settingsMap.get(playerName);
    temp.x1 = 4;

    Java is reference based, so once you have a reference to the element, you can just edit it.
     
  9. Offline

    Mixcoatl

    You need something like this:
    Code:
    final String playerName = player.getName();
    if (!settingsMap.containsKey(playerName)) {
        settingsMap.put(playerName, new Settings());
    }
    settingsMap.get(playerName).x1 = newValue;
    This can all be wrapped up into a single method call so you don't need to duplicate the code everywhere you need to access settings.
     
  10. Offline

    theninthworldix

    So, yea, I guess there are a few ways of going about doing this, now that I know a few ways of doing it, which is the best(most efficient, professional, commonly used)?
     
  11. Offline

    Mixcoatl

    There is no "right" way. The method I suggested would be more common in a professional environment, but not to the exclusion of all others. It depends upon precisely what you need and the context in which you need it. If you're storing only one value, for example, the method I suggested is overkill.
     
Thread Status:
Not open for further replies.

Share This Page