player.getName() to string?

Discussion in 'Plugin Development' started by Razorcane, Oct 8, 2011.

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

    Razorcane

    Ok, so here's what I'm trying to do. I use a command to input a username into a .txt file. I then search all the files used by separate commands(such as guests.txt, admins.txt, etc.) for the username. I want to have an if statement(per file) to check whether a user(online player) is in that file or not, and run code based on which .txt file the user is in. I'm fairly new to Java so I haven't completely unwrapped all of it's constructors, so any help would be appreciated.'

    If you need any further explanation I will give it my best. Sorry about the thread name, that's the general idea of what I'm wanting to.
     
  2. Offline

    Razorcane

    Thanks but I'm not a fan of yml. I use config.txt for all of my configurations.
     
  3. Offline

    Razorcane

    I already use the File method. The problem is searching for a line that is the exact same as the username.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Really make it easy on yourself and use the Configuration class, all this done for you. You just need to reference the String you want by node it is on.
    If you really wanted to you can write out the yaml to a file that ends in txt.

    But if you insist.
    You have several tools, but you are probably best of using the Scanner Class, to read the file line by line. Then run a Matcher/Pattern classes on each line to see if a value exists.
     
  5. Offline

    Razorcane

    I think I'll be able to use a while loop to cycle through each line, something like:
    NOTE: br = BufferedReader.

    Code:
    while ((user = br.readLine()) != null) {
        //TODO
    }
    
    And then maybe run an if statement in the while loop like so:

    Code:
    if(user == player.getName()) {  //TODO }
    Of course, using this method would call the while loop until it found the right name, so that would use up resources. But I think it would work.
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    If you have some marker or delimiter, you can read the entire file into a string, and run regex over it to find the value you want.
     
  7. Offline

    Razorcane

    I do have a marker, but the only way I would know how to import the entire file into a string would be via an array or a List. As I said, I'm fairly new to Java.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Code:
    StringBuilder stringBuilder = new StringBuilder(); 
    Scanner scanner = new Scanner(new BufferedReader(new FileReader(new File(this.getDataFolder(), "configuration.txt"))));
    while(scanner.hasNextLine()){
    stringBuilder.append(scanner.nextLine());
    }
    This is one way of throwing a file into a string. You can then just come up with some fancy regular expression
    Code:
     Matcher m = Pattern.compile("RegularExpressionHere").matcher(stringBuilder);
    if (m.find()){
    String s = m.group();
    }
     
  9. Offline

    hatstand

    Have you looked at the Configuration class? It would save you so much pointless code:
    Code:java
    1. //Load plugins/<plugin name>/config.yml
    2. Configuration Config = getConfig();
    3. //Get the array of players from the node called 'players' in the config file
    4. List<String> Players = Config.getStringList("players");
    5. //If player's name is in the list, do stuff.
    6. if(Players.contains(player.getName()))
    7. {
    8. //Do stuff
    9. }

    This would check against a list in config.yml like this:
    Code:
    players: ['bob','guy','dude']
    If you need to associate extra data with each player, you can define them as child nodes of the player node, like so:
    Code:
    players:
        bob: junk
        dude: stuff
        guy: more stuff
    And load that list of players like so:
    Code:java
    1. //Get the list of the child nodes of the node called 'players' in the config file
    2. List<String> Players = Config.getKeys("players");
     
  10. Offline

    Razorcane

    I appreciate the offer, but I absolutely hate YML. And since I'm still learning Java, it's good practice using standard computer files. But it's fine, my method worked.
     
  11. Offline

    hatstand

    Well, the latest Recommended Build ensures that my example doesn't work, they changed the API.
     
Thread Status:
Not open for further replies.

Share This Page