problems with config

Discussion in 'Spigot Plugin Development' started by M7MDMCGamer, May 2, 2017.

  1. i have problems with config i want when player join it make section in config.yml and add player UUID and 0 points but it dont work here is the code
    Code (Java):

        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            if(getConfig().contains("players." + e.getPlayer().getUniqueId()) == false){
                getConfig().set("players."+e.getPlayer().getUniqueId() + ".points" , 0);
            }
            if(getConfig().getBoolean("settings.maintenance") == true){
            e.getPlayer().kickPlayer(ChatColor.DARK_RED + "Maintenance Mode Is Enabled Please Join Later.");
            }
        }
     
    NOTE: the event work but the config part not working
     
  2. Is the config being created? Can you post the whole class? Make sure you call saveDefaultConfig() in your onEnable, also make sure you've actually created the config.yml in your project.
     
  3. the full class
    Code (Java):

    package gd.rf.m7md.m7mdcraft;

    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import gd.rf.m7md.m7mdcraft.fly.Fly;

    public class M7MDCraft extends JavaPlugin implements Listener {
       
        public void onEnable(){
       
            getConfig().addDefault("settings.maintenance", false);
            getConfig().options().copyDefaults(true);
           saveConfig();
            getCommand("fly").setExecutor(new Fly());
            getServer().getPluginManager().registerEvents(this, this);
         
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            if(getConfig().contains("players." + e.getPlayer().getUniqueId()) == false){
                getConfig().set("players."+e.getPlayer().getUniqueId() + ".points" , 0);
            }
            if(getConfig().getBoolean("settings.maintenance") == true){
            e.getPlayer().kickPlayer(ChatColor.DARK_RED + "Maintenance Mode Is Enabled Please Join Later.");
            }
        }

    }
     
     
  4.  
  5. Spoonfeed:

    Code (Text):
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player p = event.getPlayer();
            UUID uuid = p.getUniqueId();
            if (!this.getConfig().contains("Players." + uuid))
                this.getConfig().set("Players." + uuid + ".points", 0);

            if (this.getConfig().getBoolean("settings.maintenance"))
                p.kickPlayer(ChatColor.RED + "Maintenance mode is enabled, please join later.");
        }
     
  6. it doesn't work
     
  7. BananaPuncher714

    Supporter

    That's such a common mistake many people have when setting a value in the config.:p You just forgot to call saveConfig():D after you set a value. BTW, you should call saveDefaultConfig() in your onEnable, not saveConfig().
     
  8. The saveDefaultConfig() method also saves comments when saving, and yes I forgot about saveConfig() after I set a value hihi :p
     
  9. BananaPuncher714

    Supporter

    The saveDefaultConfig() may save comments, but saveConfig() will remove them.