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 java.io.File;

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

    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() {

            createConfig();
            registerEvents();
            registerCommands();

        }


        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()) {
                getDataFolder().mkdirs();
            }
            if (!(file.exists())) {
                getLogger().info("config.yml not found, creating!");
                saveDefaultConfig();
            } else {
                return;
            }

        }

    }
     

    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;
               
        }
        @EventHandler
        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){
                player.sendMessage(login);
                return;
            }
            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):
    players:
      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");
    plugin.getConfig().save(save);
     
  4. Code (Text):

    players:
      defaultplayer: &f
    something:
      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):
    getConfig().getConfigurationSection("players").getKeys(false).add(playername);
     
  9. Code (Text):
    saveConfig();
    reloadConfig();
     
  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):
    players:
      Drkmaster83
    : '&f'
     

Share This Page