Solved Plugin saves uncompleted config file.

Discussion in 'Plugin Development' started by cd-con, Nov 22, 2022.

  1. Offline

    cd-con

    I have a config file:
    Code:
    # Plugin disabled by default - for configuration
    enabled: false
    # Experimental feature. Kick bots for most popular stress-testers with no whitelist check
    bot-attack-protection: false
    bypass-bot-check-for-whitelisted: false
    # 'Ban' all players which includes this pattern
    bot-name-pattern:
      - MCSTORM_IO
      - BOT
    locale:
      prefix: "&l[&r&6MultiWhitelist&r§l]&r"
      universal-whitelist-message: "You don't belongs to any group in the whitelist."
      shield-state-activated: "&2Bot shield activated!"
      shield-state-deactivated: "&cBot shield deactivated!"
      group-whitelist-enabled: "&2Group was enabled! Now players from that group can join server."
      group-whitelist-disabled: "&cGroup was disabled! Players from that group no longer can join server."
      mw-reloaded: "&2Config reloaded!"
      mw-updated: "&2Value updated!"
    # Some statistic. Just for fun.
    stats:
      players-joined-ever: 0
      players-blocked-by-whitelist: 0
      players-blocked-by-bot-filter: 0
    groups:
      myGroup:
        enabled: true
        whitelist-file: "/groups/myGroup.txt"
        whitelist-message: "Your group is disabled right now. Come back later."
    and my code to store config:
    Code:
    if (!getDataFolder().exists()) {
                saveDefaultConfig();
                getConfig().options().copyDefaults(true);
            }
    But when I running this plugin on my test server, this file is saved (but jar file isn't corrupted):

    Code:
    # Plugin disabled by default - for configuration
    stats:
      players-joined-ever: 3
      players-blocked-by-whitelist: 0
      players-blocked-by-bot-filter: 0
    locale: {}
    groups: {}
    
    Also code for saving values on shutdown:
    Code:
            if (config != null) {
                config.set("stats.players-joined-ever", PlayersJoinedCounter);
                config.set("stats.players-blocked-by-whitelist", PlayersWhitelistBlockedCounter);
                config.set("stats.players-blocked-by-bot-filter", BotsBlockedCounter);
    
                // Idk why but sometimes plugin also erase this values
                for (Map.Entry<String, GroupStruct> entry:presentGroups.entrySet()) {
                    config.set("groups."+entry.getKey()+".enabled", entry.getValue().Enabled);
                    config.set("groups."+entry.getKey()+".whitelist-message", entry.getValue().WhitelistMessage);
                }
                saveConfig();
            } else {
                getLogger().warning("Error occurred while attempt to save config file. Not critical, but stats from this session will be removed");
            }
     
  2. Online

    timtower Administrator Administrator Moderator

    @cd-con Where do you get the config before you shutdown?
    Do you get it AFTER saving the default config or before?
     
  3. Offline

    cd-con

    @timtower my plugin logic for config works like this:

    1. Plugin checks config existance, and saves a new if folder with plugin data doesn't exists
    2. Plugin execute reload function, which loads config from drive (and store it in the global variable) and reads values. (also this function bind for the /reload plugin command)
    *doing some work*
    3. Plugin checks if variable value is present and then executing save function.

    So, I executing all my main actions with config after saving default file

    Also, if I copy-paste config from IDE, it also doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 22, 2022
  4. Online

    timtower Administrator Administrator Moderator

    @cd-con Checked the logs for errors?
     
  5. Offline

    cd-con

    No error messages, all clear.

    But with broken config works all my custom messages (they are loads from config).
    upload_2022-11-23_10-50-17.png

    Calling some presentGroups (variable that stores all groups) related commands cause an error (Because presentGroups is empty - config wasn't loaded)

    Updated: Tried to remove save function from onDisable and all started working fine.

    2nd update: Also works fine if I remove all files from plugin folder (it restore defaults as meant)
     
    Last edited: Nov 22, 2022
  6. Online

    timtower Administrator Administrator Moderator

    @cd-con What if you disable the piece from the onDisable?
     
  7. Offline

    cd-con

    Okay, all started working fine (including saves) after I used this thing
    Code:
    if (config == null) {
         config = getConfig();
    }else{
         reloadConfig();
         config = getConfig();
    }
    in reload function.

    Update: Made it correct.
     
    Last edited: Nov 23, 2022

Share This Page