Search code examples
javapluginsbukkit

Bukkit config.yml resets when changing data


I have a problem with my config.yml file. When I start my
Plugin it automatically generates the default config.yml
file. This is the default preset:

#This Plugin was made by Yuki
#Version 0.0.1
Spawn:
  World: 
  X: 
  Y: 
  Z: 
  Yaw: 
  Pitch: 

I've created a comment(/setspawn), which enters the data in
the config.yml. This part works fine, with the little issue,
that every time the config.yml gets wiped completely.

config.yml after inserting data:

Spawn:
  World: world
  X: -155.45080613398784
  Y: 82.0
  Z: -57.60431500946787
  Yaw: -27.600231
  Pitch: 29.550203

I insert the data with the following lines:

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(sender instanceof Player) {
        Player player = (Player) sender;
        if(sender.hasPermission("cc.command.setSpawn")) {
            FileConfiguration config = Main.getPlugin().getConfig();
            config.set("Spawn.World", player.getWorld().getName());
            config.set("Spawn.X", player.getLocation().getX());
            config.set("Spawn.Y", player.getLocation().getY());
            config.set("Spawn.Z", player.getLocation().getZ());
            config.set("Spawn.Yaw", player.getLocation().getYaw());
            config.set("Spawn.Pitch", player.getLocation().getPitch());
            Main.getPlugin().saveConfig();
            player.sendMessage("Der Spawn wurde gesetzt!");
        }
    }
    return false;
}

Main class:

public class Main extends JavaPlugin {

    private static Main plugin;

    public void onEnable() {
        plugin = this;
        this.saveDefaultConfig();
        System.out.println("\033[1;36m"+"["+"\033[1;35m"+"CC"+"\033[1;36m"+"]"+"\033[0;35m"+"Crystal Cake is online"+"\033[0m");
        //commands
        getCommand("heal").setExecutor(new HealAndFood());
        getCommand("setspawn").setExecutor(new SetSpawnCommand());

        //Listeners
        Bukkit.getPluginManager().registerEvents(new JoinLeaveListener(), this);
        Bukkit.getPluginManager().registerEvents(new KillListener(), this);

    }

    public void onDisable() {
        System.out.println("\033[1;36m"+"["+"\033[1;35m"+"CC"+"\033[1;36m"+"]"+"\033[0;35m"+"Crystal Cake is offline"+"\033[0m");

    }

    public static Main getPlugin() {
        return plugin;
    }
}

Solution

  • Please include the onEnable() code.

    I am guessing that there is something in there which does the equivalent of saveDefaultConfig() when starting the plugin. :)

    EDIT: I have copied you code into another plugin of mine. I have no problems. After starting the server, the config stays as intended. Are there any messages in the logs?