[TUT] Bukkit's new FileConfiguration API! Create a YAML configuration!

Discussion in 'Resources' started by Windwaker, Oct 23, 2011.

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

    Pandemoneus

    You can't initilize static variables with an object that is created at runtime.
    In other words:
    Either make the variables not static or initilize them in onEnable() or later.

    Or have public static FileConfiguration config = getConfig(); before those two lines.
     
  2. Offline

    zipron

    Thank you so much!

    I do have a question though, I'm getting an illegal arg excption. First I got a nullpointer exception but I could fix that myself (it was because plugin variable wasn't correct) but I can't figure out what is goind wrong. I've test both the tutorial code and the code that fieldmaster posted on page 2 here.
    Code:
    @Override
    public void onEnable() {
    Main plugin = new Main();
    PluginDescriptionFile pdf = this.getDescription();
    this.log.info(this.name() + " version " + pdf.getVersion() + " is enabled");
    WorldCreator.name("mclipboard").generator(new ChunkOutlineGen()).createWorld();
     
            FileConfiguration config = plugin.getConfig();
     
            config.set("path", "teststring");
            config.set("test", "teststring");
            config.set("string", "teststring");
     
            plugin.saveConfig();
            plugin.reloadConfig();
     
            config.set("reloaded", "Does this save to plugin?");
    }
    
    Code:
    09:32:45 [SEVERE] Error occurred while enabling MClipboard v0.1 (Is it up to date?)
    java.lang.IllegalArgumentException: File cannot be null
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:171)
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
            at be.zipron.mclipboard.Main.onEnable(Main.java:26)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:215)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:336)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:363)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:250)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:232)
            at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:374)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:361)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:187)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    
    zip

    ps: how to make code blocks for java code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
    Windwaker likes this.
  3. Offline

    Wolvereness Bukkit Team Member

    zipron You should remove the line that reads "Main plugin = new Main();", and replace anywhere 'plugin' appears with 'this'
     
    zipron likes this.
  4. Offline

    zipron

    actually I did that before, but than I had an instance variable:
    Code:
    public static Main plugin = new Main();
    
    But that gave a nullpointer so tried to fix it like that, however I'm at school now so I won't be able to test it till later today =) thanks for the reply though =)
    zip
     
  5. Offline

    Windwaker

    You can have an instance variable like this
    Code:java
    1. private static Plugin instance;
    2.  
    3. public Plugin() {
    4. instance = this;
    5. }
    6.  
    7. public static Plugin getInstance() {
    8. return instance;
    9. }
     
  6. Offline

    zipron

    You solved the problem, thanks =)

    Windwaker
    I do have one more question which is not mentioned in the tutorial. I want to be able to create a per-player folder with a config in it:
    Code:
    plugins > mcliboard
                         config.yml
                 > player 1
                         playerconfig.yml
                 > player 2
                         playerconfig.yml
    
    Is that possible? and do you (or anyone else know how?)

    thanks =)
    zip
     
  7. Offline

    Wolvereness Bukkit Team Member

    Possible, but I'd recommend making a folder for all the players and putting a config file specific to that player. plugins/mcliboard/players/(playername).yml
     
  8. Offline

    zipron

    Yea I know that would be better, but I'm creating a plugin which allows players to save and load their own schematics, that's why I need it to be in a seperate folder (or mayby plugins/mcliboard/players/player1/playerconfig.yml)

    Also, could you tell me how to create other folders and config files and how to use them?
    thanks, zipron
     
  9. Offline

    Zicore

    Simple question: How do i write lists of objects with for example three values for each object ?
    Code:
    list:
        a:
            b: 1
            c: "hello"
            d: 1.3
        a:
            b: 4
            c: "world"
            d: 1.2
     
  10. Offline

    TopGear93

    is it possible to create multiple configs using this setup Windwaker
     
  11. Offline

    Windwaker

    I typically like to do something like this for multiple configs, it's a wrapper for YamlConfiguration to avoid all the ugly try-catch blocks.
     
  12. Offline

    Lolmewn Retired Staff

    What if I want to add comments to the config file? The Bukkit system tends to filter them out..
     
  13. Offline

    Windwaker

    The reason they are probably being filtered out of your config.yml is if you are doing something like this...

    Ok:
    Code:
    # Comment here
    Not ok:
    Code:
    #Comment here
     
  14. Offline

    Lolmewn Retired Staff

    Why's that? Also, is # Comment Here # correct?
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    the syntax for a comment in yaml is defined as
    Code:
    # <comment>
    where <comment> is your comment string. Note that there is a space before your comment
     
  16. Offline

    Lolmewn Retired Staff

    I see.
    I did some testing, It seems that every comment under the first value gets removed. All comments above it stay. Unfortunately, I have comments above every configuration value.. so they all get removed except for the first one.
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    you are allowed to have once contiguous block of comments at the top of your yaml file i.e.
    Code:
    # <comment>
    # <comment>
    # <comment>
    ...
    
     
  18. Offline

    nuclearcodes

    Thanks for this it helped me soooo much and I was getting stuck there!!!!
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    make sure you initialize it!
     
  20. Offline

    nuclearcodes

    got one problem with applying an config to a setRadius event:

    eclipse dosent like event.setRadius("NuclearTNT explosion radius"); in the listener class

    main class

    Code:java
    1. package me.nuclearcodes.NuclearTNT;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class NuclearTNT extends JavaPlugin {
    6.  
    7. //Sets the Listener on
    8. public void onEnable(){
    9. LoadConfiguration();
    10. System.out.print("[NuclearTNT] NuclearTNT Enabled!");
    11. getServer().getPluginManager().registerEvents(new NuclearTNTListener(),this);
    12.  
    13. }
    14.  
    15.  
    16. public void LoadConfiguration(){
    17. String path = "NuclearTNT explosion radius";
    18. getConfig().addDefault(path, "50");
    19. getConfig().options().copyDefaults(true);
    20. saveConfig();
    21.  
    22. }
    23. }
    24.  
    25.  


    listener class

    Code:java
    1. package me.nuclearcodes.NuclearTNT;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.entity.EntityType;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.ExplosionPrimeEvent;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.plugin.Plugin;
    9. public class NuclearTNTListener implements Listener {
    10.  
    11. public NuclearTNT plugin;
    12.  
    13. @EventHandler
    14. public void playerJoin(PlayerJoinEvent event) {
    15. event.getPlayer().sendMessage(ChatColor.RED+"NuclearTNT may crash your server, remember backup!");
    16. }
    17.  
    18. //Makes TNT explode like nuke
    19. @EventHandler
    20. public void TNTNuke(ExplosionPrimeEvent event){
    21. if(event.getEntityType() == EntityType.PRIMED_TNT){
    22. plugin.getConfig().getInt("NuclearTNT explosion radius");
    23. event.setRadius("NuclearTNT explosion radius");
    24.  
    25. }
    26. }
    27. }
     
  21. Offline

    Sagacious_Zed Bukkit Docs

    nuclearcodes
    I would assume that radius needs to be a number and not a string....
     
  22. Offline

    nuclearcodes

    so how would i make it relate to the config
     
  23. Offline

    Sagacious_Zed Bukkit Docs

    you should really learn java before building a plugin, generally it works out better then the other way around.

    Anyways, plugin.getConfig().getInt("NuclearTNT explosion radius") returns an int you can either inline that as the parameter to setRadius or store that in a local variable and pass that local variable in as the parameter to setRadius.
     
    Windwaker likes this.
  24. Offline

    nuclearcodes

    I know that it does this, my problem is everything Ive tried my own attempts and many other peoples advise has not worked and either gives a console error or eclipse error. Including what you have just said doesn't work. This is what i mean by nothing seems to work
     
  25. Offline

    dsmyth1915

    nuclearcodes just set your config line to a variable and call it in your setRadius.

    double radius = <config path here >
     
  26. Offline

    cat24max

    What does that mean?

    Code:
    20:33:01 [GRAVE] Could not load 'plugins\GroupJoinMessage.jar' in folder 'plugin
    s'
    org.bukkit.plugin.InvalidPluginException: java.lang.UnsupportedClassVersionError
    : me/cat24max/gjm/GJM : Unsupported major.minor version 51.0
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:150)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:53)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:425)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.UnsupportedClassVersionError: me/cat24max/gjm/GJM : Unsuppo
    rted major.minor version 51.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(Unknown Source)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:41)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:29)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:139)
            ... 8 more
     
  27. Offline

    dsmyth1915

    cat24max it means you compiled your pluginwithjava7 but ran bukkit with java6. Try changing your compiler to java6 or your bukkit to java7
     
  28. Offline

    wirher

    Thank you for this tutorial, Windwaker!
     
  29. Offline

    GeekPlaya

    I'm unable to add anything to a list. It claims it has no idea what type of elements are stored in it, so when I try to add a string (or anything), it won't let me.

    Could you please expand on the "adding to a list" concept? It doesn't seem to work for me at all when I follow your steps.
     
  30. Offline

    dakoslug

    How set a boolean value? It won't set the variable to true.
    Code:
    if(commandLabel.equalsIgnoreCase("downtime-reload") || commandLabel.equalsIgnoreCase("ed-check"))
    {
    if(sender.hasPermission("emergencydowntime.reload"))
    {
    if(EmergencyDowntimeplayerListener.downtimefull == true)
    {
    this.getConfig().set("reload", true);
    }
     
    public void loadConfiguration() {
    getConfig().addDefault("reload", false);
    getConfig().options().copyDefaults(true);
    saveConfig();
    }
     
Thread Status:
Not open for further replies.

Share This Page