Config Comments

Discussion in 'Plugin Development' started by pookeythekid, Feb 9, 2014.

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

    pookeythekid

    Hey guys! I'm having trouble with commenting my config file for my login security plugin. I need to comment it since the settings might be a little confusing.
    In the original file, I have typed out the following text. Yes, it's really long, but the only point you need to get is that it has the correct comments.
    Code:
    LoginSpawn:
      x:
      y:
      z:
      Pitch:
      Yaw:
      World:
     
    # :: SETTINGS :: #
    # These are options for when a player is frozen.
    # They're set to full restriction by default, except for commands. The defaults are only SecureLogin commands.
     
    # Without adding arguments to the commands, and without using spaces,
    # list commands you want players to be able to chat while still frozen.
    # Separate these commands with commas. For example: /god,/gamemode,/kill
    # Remember, these don't grant permissions to commands. They allow players with permission
    # to use the commands while they're still frozen. These shouldn't matter if the setting
    # "freezeChat" is set to false.
    # NOTICE: Teleportation commands may not work! This is because the
    # player will still be frozen after teleporting, and will still need to login.
    allowedCommands: /login,/register,/sl,/securelogin,/setloginspawn,/loginspawn,/slreload,/setmessage,/setmessagehelp
     
    # Should players not be able to chat before they login?
    freezeChat: true
    # Should players be able to drop items before logging in?
    allowItemDrop: false
    # Should players be able to pick up items before logging in?
    allowItemPickup: false
    # Should players (if they can reach a block) be able to break blocks before logging in?
    allowBlockBreak: false
    # Should players (if they can reach a block to place on) be able to place blocks before logging in?
    allowBlockPlace: false
    # Should players be invincible before logging in?
    allowGetDamaged: false
    # Should players be able to damage other entities before logging in?
    allowDamaging: false
    # Should players be able to interact with buttons, levers, chests, etc. before logging in?
    allowInteract: false
     
    # :: MESSAGES :: #
    # Please do edit these. My ideas for color codes are terrible. xD
     
    # This message tells players that they must login after they join.
    # You may want to tell them to use /register if they don't have a password.
    Must-Login-Message: '&4&k|&4Login with /login <password>. If you have no password, use /register <password>.&4&k|'
     
    # This message tells people that they have been released from the chains of SecureLogin.
    Unfreeze-Message: '&aYou have been released! Be free!'
     
    # This message tells players that they have registered with /register. Telling them to use /login is recommended.
    You-Have-Registered-Message: '&aYou have registered with your new password. Use /login to be released.'
     
    # This message informs players that they will only be given one password. Ever.
    # It's HIGHLY recommended that you tell this to them!
    Remember-Your-Password-Message: '&1&l&kaaa&4&lRemember Your Password! You Will Not Be Given Another!&1&l&kaaa'
     
    Players:
      pookeythekid:
        Password: somepassword
        SecurityQuestion: Do you like pie?
        SecurityAnswer: Heck yeah!
      Calebboypowers10:
        Password: anotherpassword
        SecurityQuestion: Do you like cake?
        SecurityAnswer: No, not really.
    Now here's how it turns out whenever I load the server:
    Code:
    #
    # :: SETTINGS :: #
    # These are options for when a player is frozen.
    # They're set to full restriction by default, except for commands. The defaults are only SecureLogin commands.
    #
    # Without adding arguments to the commands, and without using spaces,
    # list commands you want players to be able to chat while still frozen.
    # Separate these commands with commas. For example: /god,/gamemode,/kill
    # Remember, these don't grant permissions to commands. They allow players with permission
    # to use the commands while they're still frozen. These shouldn't matter if the setting
    # "freezeChat" is set to false.
    # NOTICE: Teleportation commands may not work! This is because the
    # player will still be frozen after teleporting, and will still need to login.
    LoginSpawn: {}
    allowedCommands: /login,/register,/sl,/securelogin,/setloginspawn,/loginspawn,/slreload,/setmessage,/setmessagehelp
    freezeChat: true
    allowItemDrop: false
    allowItemPickup: false
    allowBlockBreak: false
    allowBlockPlace: false
    allowGetDamaged: false
    allowDamaging: false
    allowInteract: false
    Must-Login-Message: '&4&k|&4Login with /login <password>. If you have no password,
      use /register <password>.&4&k|'
    Unfreeze-Message: '&aYou have been released! Be free!'
    You-Have-Registered-Message: '&aYou have registered with your new password. Use /login
      to be released.'
    Remember-Your-Password-Message: '&1&l&kaaa&4&lRemember Your Password! You Will Not
      Be Given Another!&1&l&kaaa'
    Players:
      pookeythekid:
        Password: somepassword
        SecurityQuestion: Do you like pie?
        SecurityAnswer: Heck yeah!
      Calebboypowers10:
        Password: anotherpassword
        SecurityQuestion: Do you like cake?
        SecurityAnswer: No, not really.
    See the difference? I'd love to know how this can be solved. I'm sure it's possible because tons of other plugins I've seen manage to do it. Thanks!
     
  2. Offline

    Jake6177

    Extract a default file rather than creating a config via hard coding.
     
  3. Offline

    pookeythekid

    Jake6177 Sorry, I have next to no experience with file coding stuff. Could you please give me an example? :)
     
  4. Offline

    Jake6177

    You can use my FileManager class if you want. I'm not going to comment on it's code-style, as idc, but you can use it if you want: http://hastebin.com/datonileso.java

    Put that in a separate file in your project, then open it and change the first line "private static final Main plugin = Main.getInstance()" to "private static final <MainClassName> plugin = someStaticWayOfGettingMainHere".

    Then use:
    Code:java
    1. FileManager.copy(plugin.getResource("file.ext", new File(location/to/desired/extraction/point));


    enjoy
     
  5. Offline

    pookeythekid

    Jake6177 "location/to/desired/extraction/point" Wouldn't that vary according to the computer's user profile name? I want this to be for download, not really for personal use. Would you know how to declare a variable to use as the user profile name? Sorry for being ignorant, I'm a learning Bukkit plugin developer, not exactly a professional Java scripter.
     
  6. Offline

    Jake6177

    No. plugin.getResource() will get from the jar's root which is where things in the default package (for ant) or the resources folder (for maven) will go when the jar compiles. The second parameter will extract somewhere to the plugins folder, so you'd do something like this if the file was "conf.yml".

    FileManager.copy(plugin.getResource("conf.yml", new File(plugin.getDataFolder(), "conf.yml"));
     
  7. Offline

    pookeythekid

    Jake6177 Ah okay. A lot of Java still looks like gibberish to me... but this makes a bit of sense. Thanks!

    Edit: Oh wow! I just read it over like five times again... xD But it makes so much sense now! plugin.getResource() would be where the file is contained in the Jar file, and the "FileManager.copy" copies the first parameter in "plugin.getResource()" and puts it into the second parameter, which would be the plugin folder! I'm sure you already know this... but I felt like I should say this to officially get it straightened out myself.
    I also have a question with this new understanding: Does this line of code work for pretty much any kind of file? If it is, that would be very exciting to know how to generate more than one file; something I so desperately need to do now that my plugin memories are starting to grow larger.

    But if I'm wrong about this, please tell me. Thanks.

    Re-Edit: Hmm. The more I look at this, the more I think I'm wrong about it...
     
  8. Offline

    pookeythekid

    Jake6177 Umm.... Hey, I just got around to using the code that you gave me in this thread and applying it to my plugin, but now your FileManager class is not on HasteBin; it takes me to a new blank page. Did you delete it, or is this an error? I've seen it before, and it's a lot of code... Do I need that code badly, or can I do without?

    Hello, I'm back here with the same question: How do you generate a pre-written config file, while keeping the comments organized exactly how you plan them to be? I've just thought of a new theory, but I'm pretty sure I've seen a thread that proves it wrong. I've seen so many different methods of doing this, tried most of them, and none that I tried worked. The thread that I've seen that proves my theory wrong, also showed that there's no simple way to do this, which is why I'm here asking about it. I hope someone knows the answer to this, because in my new plugin, it's a little easy to get lost without comments in the config.

    Thanks!

    Here's the code of my new theory if it helps.
    Code:java
    1. public void onEnable() {
    2. if (/*Some way to check if config exists or not, I do not know how*/) {
    3. getConfig().set("#This setting will allow players to say hello");
    4. getConfig().set("allowSayHello: true");
    5. //And so on until you have all the comments and settings you want.
    6. }
    7. }
    8.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. Offline

    xTrollxDudex

    pookeythekid
    I don't know any API that allows this, you should need to use a BufferedWriter
     
  10. Offline

    pookeythekid

    xTrollxDudex Well... Here's the thing. I'm a noob. I'm extremely ignorant about Java script apart from Bukkit API, compared to most of the people who answer all these posts on this topic. It's very tough for me to collaborate with other Java scripters on the subject of Java, because I don't know what the heck they're talking about. Which leads me to the question: Could you please tell me how to use a BufferedWriter?

    Edit: It's been a month since I've posted this. Yay, I'm not too nooby to understand BufferedWriters anymore. :p
     
  11. Offline

    xTrollxDudex

    pookeythekid
    Well, you should learn Java. It will help you beyond measurable, I know that's what it did for me.

    PHP:
    BufferedWriter writer = new BufferedWriter(new FileWriter(new File(getDataFolder(), "config.yml")));
    writer.write("#Comment\n");
    writer.close();
     
  12. Offline

    pookeythekid

    xTrollxDudex I guess it's honest to claim that I am learning Java. Maybe slowly, as I'm only applying it to Bukkit, but I know a good piece of the language. Anyway, thanks for the code. :)

    Edit: Wait a second. Would you happen to know how to check if a file exists or not? Thanks.

    Re-Edit: xD That edit completely cancels my statement about "learning" Java... If you feel like a conversation, may I ask where and/or how you learned to apply Java to things other than Bukkit?
     
  13. Offline

    Goblom

    It would be best if you used saveDefaultConfig()

    Formatting is kept and its extremely simple. That method attempts to save a "config.yml" that is located inside your plugins jar, the config.yml should be located in the same place your plugin.yml is located.
     
  14. Offline

    Jake6177

    You can do it without and I do recommend bukkit's method but if you want my code then I'll put it back up
     
  15. Offline

    pookeythekid

    Goblom Jake6177 I've tried saveDefaultConfig(); and failed with it. It saves the same wrong stuff that just saveConfig(); does.

    Jake6177 Goblom WOW! It's that simple! Thank you very much! The thing that was messing me up was the line: "getConfig().options.copyDefaults(true);". I'm pretty sure that's what it is word for word.
    Also a question: Does this save the default config that I already have written before, every time I start the server? And if so, how do I check if the config.yml file already exists or not?

    Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  16. Offline

    Goblom

    pookeythekid saveDefaultConfig() will only save the config is a config.yml does not exist inside the plugins data folder. So, it will never overwrite a config.yml if one already exists.
     
  17. Offline

    pookeythekid

    Goblom Wow! This is so simple, too good to be true... yet is true, and works. Thank you very much. : D

    I will now start commenting away without a care in the world! ;D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  18. Offline

    Jake6177

    For reference by the way, the code I gave was more applicable for extracting files, such as locale files or others, not generally config but it can be used for it if wanted.
     
  19. Offline

    SquidFruit

    saveDefaultConfig();
    reloadConfig();
    NEVER SAVE ONDISABLE AND MAKE SURE TO HAVE DEFAULTCONIFG :D
     
  20. Offline

    Jade

    Merged threads as OP made a dupe that got response. Ignore doubleposts, as that was the fault of merging.
     
  21. Offline

    pookeythekid

    Jade Oh, what I did counts as a double post? I'm sorry, it's just that I really, really needed this answer, and I didn't get a solid answer for more than a week.

    SquidFruit Ha ha, I like the quote in your signature. ;P

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page