Monchao
Monchao

Reputation: 15

Creating custom YML file in minecraft plugin

A little lost and confused here.

I'm currently trying to figure out how to create custom .yml files within my Minecraft plugin. I play on storing player's UUIDs along with custom strings in it. However, I don't know what to do at this point.

Here's what I have right now, in my Main class:

package me.spoonme;

import java.io.File;
import java.io.IOException;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import me.spoonme.Commands.Welcome;


public class Main extends JavaPlugin {

    public FileConfiguration myFileConfig = null;
    public File myFile;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents((Listener)new Welcome(this), (Plugin)(this));

        myFile = new File(getDataFolder(), "playerstring.yml");
        if(!myFile.exists()) {
            try {
                myFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileConfiguration myFileConfig = YamlConfiguration.loadConfiguration(myFile);
        try {
              myFileConfig.save(myFile);
            } catch(IOException e) {
              e.printStackTrace();
            }
    }


}

I'm trying to follow an old post from 2016 (which isn't my smartest idea, but was having troubles finding other guides on it)

When I try to run the plugin on my test server to see if it creates the file, it shows this error:

[23:07:49 INFO]: [Welcome] Enabling Welcome v1.0.0
[23:07:49 WARN]: java.io.IOException: The system cannot find the path specified
[23:07:49 WARN]:        at java.io.WinNTFileSystem.createFileExclusively(Native Method)
[23:07:49 WARN]:        at java.io.File.createNewFile(Unknown Source)
[23:07:49 WARN]:        at me.spoonme.Main.onEnable(Main.java:27)
[23:07:49 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
[23:07:49 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335)
[23:07:49 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
[23:07:49 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:356)
[23:07:49 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:316)
[23:07:49 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.q(MinecraftServer.java:402)
[23:07:49 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.k(MinecraftServer.java:370)
[23:07:49 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.a(MinecraftServer.java:325)
[23:07:49 WARN]:        at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:211)
[23:07:49 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:505)
[23:07:49 WARN]:        at java.lang.Thread.run(Unknown Source)

It's showing an error in my Main class at line 27 which is:

                myFile.createNewFile();

Any help?

Upvotes: 0

Views: 2320

Answers (1)

Jerome Wolff
Jerome Wolff

Reputation: 377

To fix this exception, you'll have to create the Datafolder first.

Creating Datafolder

if (!this.getDatafolder().exists()) {
    this.getDatafolder().mkdir();
}

After that you can set/read values in/from the YML File

Set/Read YML File

myFileConfig.set("user.monchao", 1);
int value = myFileConfig.getInt("user.monchao");

This example also works with every other primitive data type.

Upvotes: 1

Related Questions