Adding to an array list from a different class file

Discussion in 'Plugin Development' started by AdvsNoob, Mar 1, 2012.

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

    AdvsNoob

    I am making a plugin that will allow people to join and if its there first time joining they will recieve certain items. I plan on doing this by creating an ArrayList for the string and adding the players name in there and then later comparing them when they rejoin. The problem is i am not really sure on how to add their name in the list.. The list is inside of a config.yml file i made but i just cant seem to figure out how to add their names into it.

    It always tells me i need an int which then another player joins and it will replace the first person who joined. The second question i have is actually giving the players the items described in the list.

    i need to be able to store two int's into the config file on one line (This will declare what items to give and how many) the owner of the server can add and change the items by changing the items id and the number.

    And of course i want to make it so when the player joins it will add everything on the list. I tried doing a for loop but over all i just cant seem to get it working. I do know java so can someone just tell me what i should try doing? Maybe a HashMap, if so write an example?
     
  2. Offline

    JayEffKay

    What I did to check if the player has never been on before, was to see if the playername.dat file exsists in the main world folder, so no need for a player list.

    In an onplayerjoin listener:
    Code:
            File file = new File( p.getWorld().getName() + "/players/" + name
                    + ".dat" );
            if ( !file.exists() ) {
                //player is new
            }
    
    What seems to be another option is the player.hasPlayedBefore() function. However, for some reason, that will 'reset' each session. That means if the server restarts, all players will be treated as if they're new, so don't use this.

    edit: I'm sorry, you said when a player 'joins' but do you actually mean join the server? Else I would suggest one yml or txt file to store the names offline (not the config.yml use that for configurations), and an array or Set to store that list in your plugin.
    Here I posted something about lists in YML.
     
  3. Offline

    Zaros

    Why not just user a setter method?

    Code:java
    1. arrayList<String> myArr = new ArrayList<String>();
    2.  
    3. public void addPlayer(String player){
    4. myArr.add(player);
    5. }
    6.  


    As for adding them to the file, your going to want to make a file reader and add each line (username) to the arraylist one at a time (or something like this):

    Code:java
    1. arrayList<String> players = new ArrayList<String>();
    2. BufferedReader in = new BufferedReader(new FileReader("players.txt"););
    3.  
    4. String player;
    5.  
    6. while ((player = in.nextLine()) != null){
    7. players.add(player);
    8. }
    9. in.close();


    Character stream might be simpler too...
     
  4. Offline

    AdvsNoob

    WOW! I forgot all about the .dat files! And as for the config items I'm still a bit confused on that but I'm sure I can figure something out :] Thank you guys!
     
  5. what if you have an server whit around the 225 unique players?
     
  6. Offline

    JayEffKay

    It all depends on how often you'll need to load/save information to the hard-disk. It's shouldn't be too bad to have information about 200 different players in one file, as long as you'd only have to access the file whenever a player joins/leaves.
    Additionally you could keep players that leave in memory so you don't have to load them a second time when the player joins in the same session. And you could add an extra check if anything has changed to prevent unnecessary saves.
    I'm definitely not saying this is the best method, but I do not believe this will cause any noticeable lag on your server.
     
Thread Status:
Not open for further replies.

Share This Page