1.12 YamlConfiguration.loadConfiguration(inputstream)

Discussion in 'Spigot Plugin Development' started by gronnmann, Jun 4, 2017.

  1. So I have been using this method to update the config if there's a new file. The problem is, it doesn't work with 1.12. My method:
    Code (Java):
    public void configUpdate(){
           
           
           
            String ver = pl.getDescription().getVersion();
            double pluginVer = Double.parseDouble(ver);
            double configVer = config.getDouble("config_version");
           
            Debug.print("Current plugin version: " + pluginVer + ", Current config version: " + configVer);
           
            if (pluginVer > configVer){
                try{
                    InputStream newConfigStream = pl.getClass().getResourceAsStream("/config.yml");
                   
                    if (newConfigStream == null)return;
                   
                    FileConfiguration newConfig = YamlConfiguration.loadConfiguration(newConfigStream);
                   
                    System.out.println("[CoinFlipper] Old config found. Updating...");
                   
                    for (String field : newConfig.getConfigurationSection("").getKeys(false)){
                        if (config.get(field) == null){
                            config.set(field, newConfig.get(field));
                            System.out.println("[CoinFlipper] Adding field '" + field + "' with value '" + newConfig.get(field) + "'");
                        }
                    }
                   
                    config.set("config_version", Double.parseDouble(pl.getDescription().getVersion()));
                   
                    this.saveConfig();
                   
                   
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
           
    }
    Full class here
    Stack trace:
    Code (Text):
    [11:40:54 ERROR]: Error occurred while enabling CoinFlipper v1.86 (Is it up to date?)
    java.lang.NoSuchMethodError: org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Ljava/io/InputStream;)Lorg/bukkit/configuration/file/YamlConfiguration;
            at io.github.gronnmann.coinflipper.ConfigManager.configUpdate(ConfigManager.java:112) ~[?:?]
            at io.github.gronnmann.coinflipper.Main.onEnable(Main.java:45) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:401) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:361) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.reload(CraftServer.java:723) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.Bukkit.reload(Bukkit.java:534) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:622) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1326) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1186) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:37) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(SourceFile:9) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:695) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:366) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:650) [spigot12.6.jar:git-Bukkit-fe3ca95]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot12.6.jar:git-Bukkit-fe3ca95]
    Are there any ways of doing this in the new update?
     
  2. Use an inputstreamreader or directly a file. Inputstream was deprecated a long time and got removed now.
     
    • Like Like x 1
    • Like Like x 1
  3. As FileStream has been deprecated you will need to use something along the lines of below. I'm assuming your config.yml is in the root of the plugin folder.

    Code (Text):
    File newConfigStream = new File(YourPlugin.instance.getDataFolder(), "config.yml");
    if (!(newConfigStream).exists()) return;
     
    • Like Like x 1
  4. Thanks, I will try that.
    Gonna try this