Using a YamlConfiguration

Discussion in 'Plugin Development' started by JellyRekt, Aug 29, 2017.

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

    JellyRekt

    I'm working on a plugin that depends on Factions, for which I need to store data from players in a YamlConfiguration outpostsConfig. Every time I start the server, I get an error that I don't know how to fix.

    Here is the code in my main class, which initializes the YamlConfiguration:
    Code:
    package org.zgnetwork.factionsoutpost;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.zgnetwork.factionsoutpost.commands.CmdFactionsCustom;
    import org.zgnetwork.factionsoutpost.commands.CommandListener;
    
    public class FactionsOutpost extends JavaPlugin {
        private File outpostFile;
        private YamlConfiguration outpostConfig;
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new CommandListener(this), this);
            outpostFile = new File("outposts.yml");
            outpostConfig = YamlConfiguration.loadConfiguration(outpostFile);
        }
       
        public FileConfiguration getOutpostConfig() {
            return outpostConfig;
        }
        public void saveOutpostConfig() {
            try {
                outpostConfig.save(outpostFile);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    And here is the error I'm getting when I start the server:
    Show Spoiler

    Code:
    [18:00:07] [Server thread/ERROR]: Cannot load outposts.yml
    org.bukkit.configuration.InvalidConfigurationException: could not determine a constructor for the tag tag:yaml.org,2002:org.bukkit.craftbukkit.v1_12_R1.CraftWorld
     in 'string', line 4, column 14:
              world: !!org.bukkit.craftbukkit.v1_12_R ... 
                     ^
    
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:162) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:130) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:179) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.zgnetwork.factionsoutpost.FactionsOutpost.onEnable(FactionsOutpost.java:20) [FactionsOutpost-0.0.1-SNAPSHOT.jar:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:374) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:421) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:382) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:337) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Caused by: org.yaml.snakeyaml.constructor.ConstructorException: could not determine a constructor for the tag tag:yaml.org,2002:org.bukkit.craftbukkit.v1_12_R1.CraftWorld
     in 'string', line 4, column 14:
              world: !!org.bukkit.craftbukkit.v1_12_R ... 
                     ^
    
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructUndefined.construct(SafeConstructor.java:548) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:376) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:185) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:357) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:532) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:376) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:185) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:357) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:532) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:376) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:185) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:357) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:532) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:376) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:185) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:357) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:532) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:143) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:129) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:505) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.yaml.snakeyaml.Yaml.load(Yaml.java:424) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[spigot-1.12.jar:git-Spigot-596221b-9a1fc1e]
        ... 15 more
    
     
  2. Online

    timtower Moderator Moderator

    @JellyRekt Save the name of the world not the world itself.
     
  3. Offline

    JellyRekt

    Where are you talking about?
     
  4. Online

    timtower Moderator Moderator

    @JellyRekt Something saved a world in that config, change that to only save the name.
     
  5. Offline

    FabeGabeMC

    @JellyRekt What @timtower is trying to say is that you somehow tried to save the World object, which of course has a lot of data in it. Instead of doing that, you should instead save the UUID or name of the world.
     
  6. Offline

    JellyRekt

    I see what you mean. I changed that function to save the name of the world instead of the world itself and reinstalled the plugin, but I'm still getting the same exact error.
    EDIT:
    This function exists in an object called Outpost, and is where I was accidentally trying to store the world:
    Code:
        public void setLocation(Location location) {
            String path = "" + faction.getId() + "." + name + ".location.";
            plugin.getOutpostConfig().set(path + "world", location.getWorld().getName());
            plugin.getOutpostConfig().set(path + "x", location.getX());
            plugin.getOutpostConfig().set(path + "y", location.getY());
            plugin.getOutpostConfig().set(path + "z", location.getZ());
            plugin.saveOutpostConfig();
        }
    
     
  7. Online

    timtower Moderator Moderator

    @JellyRekt Did you update the jar on the server? Is this the only place that saves stuff? Could you post the generated config?
     
  8. Offline

    JellyRekt

    1- Yes
    2- This is the only place that saves a location, and this class is the only one that interacts directly with the config. Here is that full class.
    3- The config is currently empty and will be until a player successfully adds information to it.
     
  9. Offline

    Horsey

    Why doesn't anyone just save the Location object...
     
  10. Offline

    JellyRekt

    I'm storing it to a YamlConfiguration. Hence the need to save the Location's properties.
     
  11. Offline

    Horsey

    You can just use Config#set(Location,"path") and (Location) Config#get("path");
     
  12. Offline

    AdamDev

    @Horsey
    He's using a completely different setup for the config. As you can see later on he will be adding more configs.

    @JellyRekt
    For further reference on more configs use this thread (not posted long ago):
    https://www.spigotmc.org/threads/multiple-config-files.263774/

    Other than that in your first post, you have a import that is not by bukkit:
    Your importing a different import of world.
    Where ever your saving the config you have to change that world import to a bukkit world. That import is getting it from the server files of the code.

    Hope this helps!
     
  13. Offline

    Horsey

    @AdamDev I don't see why that would be a problem if he's just saving the location object?
     
  14. Offline

    AdamDev

    @Horsey
    Again, even though that he is asking to save the location object the config he's using doesn't always work, but the reason why he was getting a world error was because of the import was incorrect.
     
Thread Status:
Not open for further replies.

Share This Page