Solved SignRankup Config File Problem

Discussion in 'Spigot Plugin Development' started by Lutheron, Aug 2, 2018.

  1. Hello.
    Im new to plugin coding, and i would like some help with config file reload.
    Everything loads fine when i start the server, but when im editing something in the config and type "rankup-reload" nothing really happens. 1.8.9 plugin if important. This is my messy code:
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("rankup-reload")) {
                       ReloadConfig();
                     player.sendMessage("Reloaded Config");
                 }
    Code (Text):
    private void ReloadConfig() {
             RankName1 = getConfig().getString("RankName1");
             RankName2 = getConfig().getString("RankName2");
             RankName3 = getConfig().getString("RankName3");
             RankName4 = getConfig().getString("RankName4");  
             SignPrice1 = getConfig().getInt("SignPrice1");
             SignPrice2 = getConfig().getInt("SignPrice2");
             SignPrice3 = getConfig().getInt("SignPrice3");
             xPosition = getConfig().getInt("xPosition");
             yPosition = getConfig().getInt("yPosition");
             zPosition = getConfig().getInt("zPosition");
             xPosition1 = getConfig().getInt("xPosition1");
             yPosition1 = getConfig().getInt("yPosition1");
             zPosition1 = getConfig().getInt("zPosition1");
             xPosition2 = getConfig().getInt("xPosition2");
             yPosition2 = getConfig().getInt("yPosition2");
             zPosition2 = getConfig().getInt("zPosition2");
             RankupPosition = getConfig().getBoolean("Teleportation");
             SignListener.SignListenerConstructor(SignPrice1, SignPrice2, SignPrice3, RankName1, RankName2, RankName3, RankName4, perm);
         }
    Code (Text):
    public void onEnable() {
             Bukkit.getServer().getPluginManager().registerEvents(new SignListener(), this);
             if (!setupEconomy()) {
                 this.getLogger().severe("Disabled due to no Vault dependency found!");
                 Bukkit.getPluginManager().disablePlugin(this);
                 return;            
             };  
             setupPermissions();
             getConfig().options().copyDefaults(true);
             saveConfig();
             ReloadConfig();
         }
    Code (Text):
    public static void SignListenerConstructor(int APrice, int BPrice, int CPrice, String AName, String BName, String CName, String DName, Permission perm1) {
                ASignPrice = APrice;
                BSignPrice = BPrice;
                CSignPrice = CPrice;
                ARankName = AName;
                BRankName = BName;
                CRankName = CName;
                DRankName = DName;
                perm = perm1;          
            }
     
  2. It doesn't look like you're calling the reloadConfig() method (lowecase r, not your method).

    Also remember that if you store the value from config into a variable, then you need to set it again after reloadConfig() is called (which looks like what you're doing in ReloadConfig() ).

    Not related to your problem, but a tip:
    It's not necessary to store them in variables either. You can if it helps, but all of it is stored in memory when the plugin loads the config, so what I generally do is instead of storing them in variables, I'll use getter methods and return the value directly from the config. This will make it so that you don't need to set the variables after each reload.
     
    #2 OffLuffy, Aug 2, 2018
    Last edited: Aug 2, 2018
    • Informative Informative x 1
  3. i found a solution, just use /reload, that works. And thanks for the tip, i think im gonna stick to variables, because i have no idea what getter methods is.
     
  4. A getter method is simply a method that gets a value. You may have the method in any class. I'll usually make a ConfigHelper class. For example:

    Code (Java):
    public String getRankName1() { return getConfig().getString("RankName1"); }
    Notice here how it's simply returning the value from the config. By doing this, you don't need the variable. You can set the access level (public, private, protected) to the same thing as your variable and it'll have the same access in the same places as the variables.
     
    • Informative Informative x 1
  5. That's actually alot better than what i have made, i will try out that method another day, but now i have another problem... This doesn't work anymore. what's up with that.
    Code (Text):
    if (cmd.getName().equalsIgnoreCase("rankupteleport")) {
                     if(RankupPosition == true) {
                         RankupPosition = false;
                         This ---> this.getConfig().set("Teleportation", false);
                         player.sendMessage(ChatColor.RED + "Teleportation for rankup is disabled");
                     } else if(RankupPosition == false) {
                         RankupPosition = true;
                         And This ---> this.getConfig().set("Teleportation", true);
                         player.sendMessage(ChatColor.GREEN + "Teleportation for rankup is enabled");
                     }
                 }
     
  6. After you set a config value, be sure to call saveConfig(). Until you call the save method, then the changes are only reflected in memory and not written to file.

    If you're saying that they aren't even changing it in memory, then you have a logic error somewhere more than likely. To track that down, I'd usually print out messages to console for each if() block to make sure that it's calling that part of the code.
     
    • Informative Informative x 1
  7. just found out. and yes it was saveConfig();
    Thanks for the help, and im pretty sure im ready to release my first plugin.
     
    • Like Like x 1