-1

I was trying to reload config.yml file on command with Bukkit Plugin, I don't know how to do it.

I searched on google and I found one answer, but when I used it, config.yml was not generating. Here's my code:

BlockChanger

Please help

QuickSilver
  • 3,915
  • 2
  • 13
  • 29
Maxisy
  • 50
  • 1
  • 6
  • 1
    Could you please add a short and relevant snippet here? I don't think many people will click on the link. – Leo Jun 17 '20 at 00:48

1 Answers1

0

First you need to remove the final modifier from your config variables, else this can't refresh from config file.

Then you need a method for reload the config and set the config variables again. An example based on your code:

@Override
public void onEnable() {

    loadConfig(this);
}

private final String prefix = ChatColor.AQUA + "[";

private String prefixTrue;
private String prefixFalse;

public void loadConfig(Plugin plugin) {
    File file = new File(plugin.getDataFolder().getAbsolutePath() + "/config.yml");
    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    prefixTrue = prefix + cfg.getString("prefix") + "]" + ChatColor.GREEN + " ";
    prefixFalse = prefix + cfg.getString("prefix") + "]" + ChatColor.RED + " ";
}

Make sure that you call the method loadConfig in onEnable and every time you want to reload the config

  • And should I leave `getConfig().options().copyDefaults(true);` and `saveConfig();` in `onEnable()`? – Maxisy Jun 19 '20 at 10:51
  • `copyDefaults(true)` can be helpful to create first time a config file with default values. The documentation can be found [here](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/file/FileConfigurationOptions.html#copyDefaults-boolean-). – Patrick Zamarian Jun 19 '20 at 11:18
  • OK, i got it. Thanks! – Maxisy Jun 19 '20 at 11:26