How To Read & Write To Config File

Discussion in 'Plugin Development' started by CompleteClient, Aug 9, 2014.

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

    CompleteClient

    I need to be able to write to a config file and read from it. I am pretty new to Bukkit, and could not easily find a way to do this, so I felt it would be best to ask here. This is what I need more in depth.

    1. Write the following if the user is new to the server:
    USERNAME:
    LINK1:
    LINK2:
    2. Update the links with commands like: /link1 LINK or /link2 LINK
    3. Read the lines if some other player goes: /links USERNAME

    Thank you guys for the help, and you all rock!
     
  2. Offline

    SmooshCakez

  3. Offline

    pookeythekid

    CompleteClient Firstly, what does your plugin do? Secondly, are you new to Bukkit, or new to Java? It's a lot easier to learn Bukkit if you already know Java, although my primary source of learning any Java was from Bukkit plugins (and YouTube).

    Anyway, the JavaPlugin class, the class that your main class extends while you're making a Bukkit plugin, and it has a method called getConfig(). In your JavaProject, in the same location as your plugin.yml, put a file called config.yml. Then, in your onEnable() method, put "saveDeafaultConfig();" and that'll create the config file and whatever you already have in it from your creating it in your JavaProject. If you want to put anything into the config, use "getConfig().set(<stuff>);" and "saveConfig();" after that. If you want to read the config, put "getConfig().getString/Int/Long/etc.("string path.separated with.dots as.config sections");".

    That may be confusing... but just go to YouTube for more help.
     
  4. Offline

    xYourFreindx

    I believe this link will be helpful to you, if you have not already been there.
    Your config will have to look like this...
    Code:
    Username:
        Link1:
        Link2:
    Username:
        Link1:
        Link2:
    Cuz flat don't work bro.

    Anyways, assuming you're using the player join event....
    First iterate through the config, and see if the player is already on there. If not, use:
    getConfig().set(e.getPlayer().getName() + ".LINK1", "LINK");
    (That, is if you already have a link assigned for the player. If you want the player to be able to set their own link, you'll need to replace "LINK" with something like, "Enter this command to set a link", and then when they use the command, use that same line, only replace "LINK" with args[0].

    EDIT: Oh and don't forget to saveConfig(); :p
     
  5. Offline

    SmooshCakez

    Just saying, he should probably use unique IDs (for 1.8) for storage. player.getUniqueId();
     
  6. Offline

    xYourFreindx

    SmooshCakez
    Storage for uuids only matters when storing within the plugin, for example, a array list, or hash map. Storing player objects is bad. Storing their names is not bad, unless you're worried about name changes)
    But he's storing the info on a config. And as everyone knows, configs can't leak memory. At least not in the way you're concerned about. (Not that you could store a player object into a config any who)

    Anyways, the whole point in his plugin is so people can look up links for other players, and I doubt anyone wants to get and type out the whole UUID for a player into a command. That kinda defeats the purpose, wouldn't you agree?
     
  7. Offline

    SmooshCakez

    Just saying that it would be better for, example, if a user changes their username. They could still type the player's name. In the command, he can get the player from the name typed in, then get the unique ID from that. But, of course, it was just a suggestion, not a requirement.
     
  8. Offline

    xYourFreindx

    SmooshCakez
    So you mean.... Player types in name... Plugin get's UUID for that player, and searches the config for that UUID... Then returns the value?
    That would be nice if he is, indeed worried about name changes.
    What do you say CompleteClient ? You up for it? :p
     
Thread Status:
Not open for further replies.

Share This Page