Creating new Path in Config File

Discussion in 'Spigot Plugin Development' started by Fuxion, Feb 14, 2018.

  1. Hello,
    So the problem is the plugin is not adding anything new to the config file when its supposed too but no errors show up. Im attempting to add a new path whenever a new player joins.

    Main Class :
    Code (Text):
    package me.fusion;


    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;

    import me.fusion.commands.ColorCode;
    import me.fusion.listeners.PlayerJoin;

    public class CC extends JavaPlugin {
        private File file = new File(getDataFolder(), "config.yml");

        private FileConfiguration config = getConfig();

        public void onEnable() {



        private void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new PlayerJoin(this), this);


        private void registerCommands() {
            getCommand("cc").setExecutor(new ColorCode(this));


        private void createConfig() {
            if (!getDataFolder().exists()) {
            if (!(file.exists())) {
                getLogger().info("config.yml not found, creating!");
            } else {



    PlayerJoin Class:
    Code (Text):
    package me.fusion.listeners;

    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import me.fusion.CC;
    import net.md_5.bungee.api.ChatColor;

    public class PlayerJoin implements Listener {
        private CC plugin;
        public PlayerJoin(CC pl){
                this.plugin = pl;
        public void onJoin(PlayerJoinEvent e) {
            String login = "Welcome Back!";
            ConfigurationSection section = plugin.getConfig().getConfigurationSection("players");
            Player player = e.getPlayer();
            String playerName =  player.getName();
            if(plugin.getConfig().get(playerName) != null){
            else {
                plugin.getConfig().createPath(section, playerName);
                plugin.getConfig().set(playerName, "&f");
                player.sendMessage(ChatColor.GOLD + "Your name has been added to the list! " + login);

    Config :
    Code (Text):
      defaultplayer: &f

  2. Instead of creating a new section for each user, you could save an ArrayList<String> to the config, you can then loop through it by using getConfig#getStringList("PLAYERS");, you can then determine whether or not the player has joined.
  3. You never saved the config when you added the player name.

    plugin.getConfig().createPath(section, playerName);
    plugin.getConfig().set(playerName, "&f");

    You need to get the config file, File save = new File(dataFolder, "config.yml");
  4. Code (Text):

      defaultplayer: &f
      some: &f
  5. And this is meant to help?
  6. More recommended to use the in-built saveConfig() method, since it's at your disposal - no need to reinvent the wheel :p
    • Agree Agree x 1
  7. Oh yeah, I'm using it because custom file extensions for my plugin.
    • Like Like x 1
  8. Code (Text):
  9. Code (Text):
  10. reloadConfig is unnecessary there.
  11. Okay
  12. maldahleh

    Wiki Team

    Please fix your spacing and all the blank lines, makes your code really messy to look at. You also don't need that createConfig method, just run saveDefaultConfig, it'll create the config, failing silently if it already exists.
    • Agree Agree x 1
  13. The best way do this is
    Code (Java):
    getConfig().set("players."+playerName, colorValue);
    Calling getConfig.set(playerName, "&f"); would result in something like this as your config.yml file:
    Code (YAML):
    Drkmaster83: '&f'
    instead of
    Code (YAML):
    : '&f'

Share This Page