Quick config notify overhaul.

* Reduce details and only send a configurable amount of lines in
notify/chat, default to 5, just state to check logs in the end,
instead of lengthy hints.
* Log detailed output to STATUS (file/s).
* Fix wrong build number printed to set configversion.created to.
This commit is contained in:
asofold 2017-04-19 11:15:19 +02:00
parent 45210e1a25
commit bb96f63f7d
5 changed files with 55 additions and 21 deletions

View File

@ -69,6 +69,7 @@ public abstract class ConfPaths {
@GlobalConfig // TODO: Per file versions should also be supported.
public static final String CONFIGVERSION = "configversion.";
public static final String CONFIGVERSION_NOTIFY = CONFIGVERSION + "notify";
public static final String CONFIGVERSION_NOTIFYMAXPATHS = CONFIGVERSION + "notifymaxpaths";
/** Build number of the build for which the default config was first created (DefaultConfig.buildNumber), updated with first save. */
public static final String CONFIGVERSION_CREATED = CONFIGVERSION + "created";
/** Build number of the build for which the default config was first created (DefaultConfig.buildNumber), updated with each save. */

View File

@ -15,7 +15,9 @@
package fr.neatmonster.nocheatplus.config;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@ -228,7 +230,7 @@ public class ConfigManager {
overrideCreated = true;
}
if (!overrideCreated && globalConfig.getInt(ConfPaths.CONFIGVERSION_CREATED, 0) >= 0
&& ConfigManager.isConfigUpToDate(globalConfig) == null) {
&& ConfigManager.isConfigUpToDate(globalConfig, 0) == null) {
// Workaround: Update the created build number, to not warn on further changes.
overrideCreated = true;
}
@ -307,12 +309,25 @@ public class ConfigManager {
isInitialized = true;
}
@Deprecated
public static String isConfigUpToDate(final ConfigFile globalConfig) {
return isConfigUpToDate(globalConfig, -1);
}
/**
* Retrieve a warning string containing information about changed default
* configuration values.
*
* @param globalConfig
* @return null if everything is fine, a string with a message stating problems otherwise.
* @param maxLines
* Maximum number of configuration paths to include in the output
* - note that extra lines will always be included, for
* introduction and in the end some hints. A negative value means
* no limit.
* @return null if everything is fine, a string with a message stating
* problems otherwise.
*/
public static String isConfigUpToDate(ConfigFile globalConfig) {
public static String isConfigUpToDate(final ConfigFile globalConfig, final int maxPaths) {
Object created_o = globalConfig.get(ConfPaths.CONFIGVERSION_CREATED);
int buildCreated = -1;
if (created_o != null && created_o instanceof Integer) {
@ -322,7 +337,8 @@ public class ConfigManager {
if (buildCreated < 0) {
return null;
}
final int maxBuildContained = globalConfig.getMaxLastChangedBuildNumber();
final ConfigFile defaultConfig = new DefaultConfig();
final int maxBuildContained = defaultConfig.getMaxLastChangedBuildNumber();
// Legacy build number comparison.
final int currentBuild = BuildParameters.buildNumber;
if (currentBuild != Integer.MIN_VALUE && buildCreated > Math.max(maxBuildContained, currentBuild)) {
@ -331,7 +347,6 @@ public class ConfigManager {
}
// So far so good... test individual paths.
final List<String> problems = new LinkedList<String>();
final ConfigFile defaultConfig = new DefaultConfig();
final Map<String, Integer> lastChangedBuildNumbers = defaultConfig.getLastChangedBuildNumbers();
// TODO: Consider some behavior for entire nodes ?
for (final Entry<String, Integer> entry : lastChangedBuildNumbers.entrySet()) {
@ -343,7 +358,7 @@ public class ConfigManager {
final String path = entry.getKey();
final Object defaultValue = defaultConfig.get(path);
if (defaultValue instanceof ConfigurationSection) {
problems.add("Changed with build " + defaultBuild + ", can not handle entire configuration sections yet: " + path);
problems.add(path + (maxPaths >= 0 ? "" : (" - Changed with build " + defaultBuild + ", can not handle entire configuration sections yet. ")));
continue;
}
final Object currentValue = globalConfig.get(path);
@ -352,14 +367,27 @@ public class ConfigManager {
continue;
}
if (defaultBuild > buildCreated && !defaultValue.equals(currentValue)) {
problems.add("Changed with build " + defaultBuild + ": " + path);
problems.add(path + (maxPaths >= 0 ? "" : (" - Changed with build " + defaultBuild + ".")));
continue;
}
}
if (!problems.isEmpty()) {
problems.add(0, "The following configuration default values have changed:");
problems.add("(Remove/update individual values or set configversion.created to " + maxBuildContained + " to ignore all, then reload the configuration with the 'ncp reload' command.)");
return StringUtil.join(problems, "\n");
Collections.sort(problems); // Sort by path.
final List<String> outList;
if (maxPaths >= 0 && problems.size() > maxPaths) {
outList = new ArrayList<String>(problems.subList(0, maxPaths));
}
else {
outList = problems;
}
outList.add(0, "The following configuration default values have changed:");
if (maxPaths >= 0) {
outList.add("-> " + problems.size() + " entries in total, check the log file(s) for a complete list.");
}
else {
outList.add("(Remove/update individual values or set configversion.created to " + maxBuildContained + " to ignore all, then reload the configuration with the 'ncp reload' command.)");
}
return StringUtil.join(outList, "\n");
}
// No errors could be determined (or versions coudl not be determined): ignore.
return null;

View File

@ -43,6 +43,7 @@ public class DefaultConfig extends ConfigFile {
// Config version.
set(ConfPaths.CONFIGVERSION_NOTIFY, true, 785);
set(ConfPaths.CONFIGVERSION_NOTIFYMAXPATHS, 5, 1085);
// not set(ConfPaths.CONFIGVERSION_CREATED, -1);
// not set(ConfPaths.CONFIGVERSION_SAVED, -1);
set(ConfPaths.LOGGING_ACTIVE, true, 785);

View File

@ -16,6 +16,7 @@ package fr.neatmonster.nocheatplus.config;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -358,15 +359,15 @@ public class RawConfigFile extends YamlConfiguration {
}
/**
* Get the entire map of path -> last changed build number.
* Get the entire map of path -> last changed build number, unmodifiable.
* <hr>
* Note that querying individual paths is not yet supported, as there may be
* different ways of handling parent/child node relations with this.
*
* @return
* @return An unmodifiable map is returned.
*/
public Map<String, Integer> getLastChangedBuildNumbers() {
return lastChangedBuildNumbers;
return Collections.unmodifiableMap(lastChangedBuildNumbers);
}
/**
@ -379,7 +380,7 @@ public class RawConfigFile extends YamlConfiguration {
int max = 0;
for (Integer v : lastChangedBuildNumbers.values()) {
if (v != null) {
max = Math.max(max, v);
max = Math.max(max, v.intValue());
}
}
return max;

View File

@ -159,8 +159,10 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
/** Lower case player name to milliseconds point of time of release */
private final Map<String, Long> denyLoginNames = Collections.synchronizedMap(new HashMap<String, Long>());
/** Configuration problems (likely put to ConfigManager later). */
protected String configProblems = null;
/** Configuration problems (send to chat). */
protected String configProblemsChat = null;
/** Configuration problems (send to log files). */
protected String configProblemsFile = null;
// /** Is a new update available? */
// private boolean updateAvailable = false;
@ -1057,9 +1059,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
logOtherNotes(config);
// Is the version the configuration was created with consistent with the current one?
if (configProblems != null && config.getBoolean(ConfPaths.CONFIGVERSION_NOTIFY)) {
if (configProblemsFile != null && config.getBoolean(ConfPaths.CONFIGVERSION_NOTIFY)) {
// Could use custom prefix from logging, however ncp should be mentioned then.
logManager.warning(Streams.INIT, "" + configProblems);
logManager.warning(Streams.INIT, "" + configProblemsFile);
}
// Care for already online players.
@ -1193,7 +1195,8 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
* @param config
*/
private void setInstanceMembers(final ConfigFile config) {
configProblems = ConfigManager.isConfigUpToDate(config);
configProblemsChat = ConfigManager.isConfigUpToDate(config, config.getInt(ConfPaths.CONFIGVERSION_NOTIFYMAXPATHS));
configProblemsFile = configProblemsChat == null ? null : ConfigManager.isConfigUpToDate(config, -1);
useSubscriptions = config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS);
clearExemptionsOnJoin = config.getBoolean(ConfPaths.COMPATIBILITY_EXEMPTIONS_REMOVE_JOIN);
clearExemptionsOnLeave = config.getBoolean(ConfPaths.COMPATIBILITY_EXEMPTIONS_REMOVE_LEAVE);
@ -1366,9 +1369,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
// if (updateAvailable) player.sendMessage(ChatColor.RED + "NCP: " + ChatColor.WHITE + "A new update of NoCheatPlus is available.\n" + "Download it at http://nocheatplus.org/update");
// Inconsistent config version.
if (configProblems != null && ConfigManager.getConfigFile().getBoolean(ConfPaths.CONFIGVERSION_NOTIFY)) {
if (configProblemsChat != null && ConfigManager.getConfigFile().getBoolean(ConfPaths.CONFIGVERSION_NOTIFY)) {
// Could use custom prefix from logging, however ncp should be mentioned then.
sendMessageOnTick(playerName, ChatColor.RED + "NCP: " + ChatColor.WHITE + configProblems);
sendMessageOnTick(playerName, ChatColor.RED + "NCP: " + ChatColor.WHITE + configProblemsChat);
}
// Message if notify is turned off.
if (data.getNotifyOff()) {