config.yml help

Discussion in 'Plugin Development' started by RebzO1, Feb 4, 2014.

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

    RebzO1

    ok so i made a simple plugin which has some of the commands that are available in essentials but i have a question about it

    here is a snippit of one section

    Code:java
    1. if (CommandLabel.equalsIgnoreCase("gm0")){
    2. if(args.length == 0){
    3. p.setGameMode(GameMode.SURVIVAL);
    4. p.sendMessage(ChatColor.AQUA + "Gamemode Updated.");
    5. }else if(args.length == 1){
    6. if(p.getServer().getPlayer(args[0])!=null){
    7. Player targetPlayer = p.getServer().getPlayer(args[0]);
    8. targetPlayer.setGameMode(GameMode.ADVENTURE);
    9. targetPlayer.sendMessage(ChatColor.AQUA + "Gamemode Updated");
    10. }else{
    11. p.sendMessage(ChatColor.RED + "Player Not Online");
    12. }
    13. }
    14. return true;
    15. }


    How would i make this so i have a config file & for each command i have there would be a allow command true/false and the default for everything would be true?

    Or would this mean i have to rewrite everything?

    I have never used configs before so i just need a nudge in the right direction.
     
  2. Offline

    Windy Day

  3. Offline

    RebzO1

    Windy Day

    browsed over that and while it may seem like a simple guide i still am unsure how to do this
     
  4. Offline

    Caprei

    RebzO1 I'm pretty tired so I will not test the following code. (I strongly disapprove of just copy and pasting code, so I'll add comments explaining it, please read them). Note: I skipped all the imports and just got to the bit you need.
    So make a config.yml file in your project, not in your package. It should look something like this:
    Code:
    command1: true
    command2: true
    You can replace command1 and command2 with any name you want.
    I'll explain more about that later.

    Now in your onEnable() write:
    Code:java
    1. onEnable(){
    2. getServer().getConfig().options().copyDefaults(true);
    3. saveConfig();
    4. //We're getting the configuration file, accessing the options and specifying that the server should copy the default values we set in the config file(both true)
    5. //Then we're saving the config.
    6. }


    Now in onCommand():
    Code:java
    1. if (cmd.getName("gm0")){
    2. if(getConfig().getBoolean("command1")){
    3. //getting the Config, accessing the path called "command1" which returns a boolean value, since it returns a boolean value we don't need to put a condition.
    4. if(args.length == 0){
    5. p.setGameMode(GameMode.SURVIVAL);
    6. p.sendMessage(ChatColor.AQUA + "Gamemode Updated.");
    7. }
    8. }


    Warning: Untested. I'm really tired and I didn't try it in eclipse or anything so there's a medium chance that it will return an error, just tag me back if you receive an error. Not to mention, it's been a while since I've worked with configuration files.
     
  5. Offline

    RebzO1

    Caprei
    ok now that i understand..
    i will test right now and post again with results

    Caprei
    seems good apart from

    on this line:
    getServer().getConfig().options().copyDefaults(true);

    i get this error:
    The method getConfig() is undefined for the type Server
    What could this be?

    ill have to change it back to the way it was for now players just lost the ability to used everythin

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

    ItsLeandro

    RebzO1 Can you show where you have putten that line? Otherwise we don't know what you did wrong.
     
  7. Offline

    RebzO1

    changed

    getServer().getConfig().options().copyDefaults(true);
    to
    ((Plugin) getServer()).getConfig().options().copyDefaults(true);

    the error went away and all commands work (haven't tested changing something to false yet.)
    but now i get

    Code:
    [22:12:45 ERROR]: Error occurred while enabling CC-Tools v1.1.3 (Is it up to date?)
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.CraftServer cannot be cast to org.bukkit.plugin.Plugin
            at me.rebz.cctools.Main.onEnable(Main.java:30) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:385) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:303) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:285) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:347) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:324) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:280) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:186) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:436) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [spigot-1.7.2-R0.3.1303-SNAPSHOT.jar:git-Spigot-1303]
    [22:12:45 INFO]: [BungeeSuite] Enabling BungeeSuitePortals v0.5b-6
    
    now i really am lost

    ItsLeandro

    Code:java
    1. @Override
    2. public void onEnable() {
    3. ((Plugin) getServer()).getConfig().options().copyDefaults(true);
    4. saveConfig();
    5. logger.info("CC-Tools Enabled!");
    6. }
    7. public void onDisable() {
    8. logger.info("CC-Tools Disabled!");
    9. }



    line 3

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

    L33m4n123

    Why would you want to cast Plugin to getServer(). Makes 0 sense. Either you do Bukkit.getServer() or plugin.getServer() where plugin is the instance of your main class.
     
  9. Offline

    RebzO1

    L33m4n123
    because im still learning and that was eclipses quickfix

    changed too:
    Bukkit.getServer().getConfig().options().copyDefaults(true);

    Error now reverts to previous:
    The method getConfig() is undefined for the type Server
     
  10. Offline

    L33m4n123

  11. Offline

    RebzO1

    L33m4n123
    i read through that and ill be honest, understood some things but not it all

    how would i get the instance from my Main.class

    == edit ==
    im sorry about the noob question if it comes across that way
    but i do appreciate your help
     
  12. Offline

    Caprei

    RebzO1 Woops, you aren't supposed to do getServer()... Sorry, I was tired.

    In your onEnable(), You shouldn't have Bukkit.getServer().getConfig()....
    It should just be getConfig().options().copydefaults(true);
     
  13. Offline

    mazentheamazin

    RebzO1
    Or create a static method in your Main class:
    Code:java
    1. Main plugin;
    2.  
    3. public static Main getPlugin() {
    4. return plugin;
    5. }
    6.  

    Then in your onEnable()
    Code:java
    1. //Put this line of code in your onEnable
    2. plugin = this


    Then call this method by doing <Class that extends JavaPlugin>.getPlugin().getConfig() etc etc.
     
  14. Offline

    RebzO1

    Caprei
    lol don't worry about it,
    done what you said and its fixed

    so once again thank you for your time
     
  15. Offline

    Caprei

    RebzO1 No problem, I hope you read my comments explaining it? :)
     
  16. Offline

    RebzO1

    Caprei
    ya i understand now so atleast thats one question i won't have to ask again
     
Thread Status:
Not open for further replies.

Share This Page