Add ability to log all violations. Do cleanup NCPHookManager on disable.

Similar to TestNCP but reduced/different features:
* Config: trace for the log file and notify to send to notify channel.
* It's not possible to confine whose messages you receive (yet).

Meant for better local/quick testing in the first place.
This commit is contained in:
asofold 2015-06-07 20:09:00 +02:00
parent dce27260e4
commit f9a2172340
6 changed files with 178 additions and 1 deletions

View File

@ -147,6 +147,8 @@ public class ViolationData implements IViolationInfo, ActionData {
return player.getUniqueId().toString();
case VIOLATIONS:
return String.valueOf((long) Math.round(vL));
case WORLD:
return player.getWorld().getName();
default:
break;
}

View File

@ -51,6 +51,9 @@ public abstract class ConfPaths {
private static final String LOGGING_EXTENDED = LOGGING + "extended.";
public static final String LOGGING_EXTENDED_STATUS = LOGGING_EXTENDED + "status";
private static final String LOGGING_EXTENDED_ALLVIOLATIONS = LOGGING_EXTENDED + "allviolations.";
public static final String LOGGING_EXTENDED_ALLVIOLATIONS_TRACE = LOGGING_EXTENDED_ALLVIOLATIONS + "trace";
public static final String LOGGING_EXTENDED_ALLVIOLATIONS_NOTIFY = LOGGING_EXTENDED_ALLVIOLATIONS + "notify";
@GlobalConfig
private static final String MISCELLANEOUS = "miscellaneous.";

View File

@ -40,6 +40,8 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.LOGGING_ACTIVE, true);
set(ConfPaths.LOGGING_MAXQUEUESIZE, 5000);
set(ConfPaths.LOGGING_EXTENDED_STATUS, false);
set(ConfPaths.LOGGING_EXTENDED_ALLVIOLATIONS_TRACE, false);
set(ConfPaths.LOGGING_EXTENDED_ALLVIOLATIONS_NOTIFY, false);
set(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE, true);
set(ConfPaths.LOGGING_BACKEND_CONSOLE_PREFIX, "[NoCheatPlus] ");
set(ConfPaths.LOGGING_BACKEND_CONSOLE_ASYNCHRONOUS, true);

View File

@ -74,6 +74,9 @@ import fr.neatmonster.nocheatplus.config.ConfigManager;
import fr.neatmonster.nocheatplus.event.IHaveMethodOrder;
import fr.neatmonster.nocheatplus.event.ListenerManager;
import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager;
import fr.neatmonster.nocheatplus.hooks.NCPHookManager;
import fr.neatmonster.nocheatplus.hooks.allviolations.AllViolationsConfig;
import fr.neatmonster.nocheatplus.hooks.allviolations.AllViolationsHook;
import fr.neatmonster.nocheatplus.logging.BukkitLogManager;
import fr.neatmonster.nocheatplus.logging.LogManager;
import fr.neatmonster.nocheatplus.logging.StaticLog;
@ -188,6 +191,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
/** Feature tags by keys, for features that might not be available. */
private final LinkedHashMap<String, LinkedHashSet<String>> featureTags = new LinkedHashMap<String, LinkedHashSet<String>>();
/** Hook for logging all violations. */
protected final AllViolationsHook allViolationsHook = new AllViolationsHook();
/** Tick listener that is only needed sometimes (component registration). */
protected final OnDemandTickListener onDemandTickListener = new OnDemandTickListener() {
@Override
@ -603,6 +609,10 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
}
sched.cancelTasks(this);
// Remove hooks.
allViolationsHook.unregister();
NCPHookManager.removeAllHooks();
// Exemptions cleanup.
if (verbose) {
logManager.info(Streams.INIT, "[NoCheatPlus] Reset ExemptionManager...");
@ -893,6 +903,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
// Set up consistency checking.
scheduleConsistencyCheckers();
// Setup allViolationsHook
allViolationsHook.setConfig(new AllViolationsConfig(config));
// if (config.getBoolean(ConfPaths.MISCELLANEOUS_CHECKFORUPDATES)){
// // Is a new update available?
// final int timeout = config.getInt(ConfPaths.MISCELLANEOUS_UPDATETIMEOUT, 4) * 1000;
@ -987,7 +1000,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
}
// (Re-) schedule consistency checking.
scheduleConsistencyCheckers();
// Cache some things.
// Cache some things. TODO: Where is this comment from !?
// Re-setup allViolationsHook.
allViolationsHook.setConfig(new AllViolationsConfig(config));
}
/**

View File

@ -0,0 +1,31 @@
package fr.neatmonster.nocheatplus.hooks.allviolations;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile;
/**
* Configuration for the AllViolationsHook.
* @author asofold
*
*/
public class AllViolationsConfig {
/** Log all players to console. */
public final boolean allToTrace;
/** Log all violations to the in-game notification channel. */
public final boolean allToNotify;
// TODO: More config on what to print (uuid, etc., default tags).
// TODO: More filtering like in TestNCP.
public AllViolationsConfig(ConfigFile config) {
allToTrace = config.getBoolean(ConfPaths.LOGGING_EXTENDED_ALLVIOLATIONS_TRACE);
allToNotify = config.getBoolean(ConfPaths.LOGGING_EXTENDED_ALLVIOLATIONS_NOTIFY);
}
public boolean doesLogAnything() {
return allToTrace || allToNotify;
}
}

View File

@ -0,0 +1,124 @@
package fr.neatmonster.nocheatplus.hooks.allviolations;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.NCPAPIProvider;
import fr.neatmonster.nocheatplus.actions.ParameterName;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.access.IViolationInfo;
import fr.neatmonster.nocheatplus.hooks.ILast;
import fr.neatmonster.nocheatplus.hooks.IStats;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
import fr.neatmonster.nocheatplus.hooks.NCPHookManager;
import fr.neatmonster.nocheatplus.logging.LogManager;
import fr.neatmonster.nocheatplus.logging.Streams;
import fr.neatmonster.nocheatplus.utilities.StringUtil;
/**
* Default hook for logging all violations in a generic way. This will not log
* if violation processing got cancelled by compatibility hooks.
*
* @author asofold
*
*/
public class AllViolationsHook implements NCPHook, ILast, IStats {
private AllViolationsConfig config;
private Integer hookId = null;
/** White list. */
private final ParameterName[] parameters;
private final String[] noParameterTexts;
public AllViolationsHook() {
Collection<ParameterName> parameters = new LinkedHashSet<ParameterName>();
for (ParameterName name : ParameterName.values()) {
parameters.add(name);
}
for (ParameterName name : Arrays.asList(ParameterName.PLAYER, ParameterName.PLAYER_NAME, ParameterName.PLAYER_DISPLAY_NAME,
ParameterName.IP, ParameterName.CHECK, ParameterName.UUID, ParameterName.VIOLATIONS, ParameterName.WORLD)) {
parameters.remove(name);
}
this.parameters = parameters.toArray(new ParameterName[parameters.size()]);
noParameterTexts = new String[parameters.size()];
for (int i = 0; i < this.parameters.length; i++) {
this.noParameterTexts[i] = "[" + this.parameters[i].getText() + "]";
}
}
public void setConfig(AllViolationsConfig config) {
this.config = config;
if (config == null || !config.doesLogAnything()) {
unregister();
} else {
register();
}
}
public void unregister() {
if (hookId != null) {
NCPHookManager.removeHook(this);
this.hookId = null;
}
}
public void register() {
unregister();
this.hookId = NCPHookManager.addHook(CheckType.ALL, this);
}
@Override
public String getHookName() {
return "AllViolations(NCP)";
}
@Override
public String getHookVersion() {
return "1.0";
}
@Override
public boolean onCheckFailure(final CheckType checkType, final Player player, final IViolationInfo info) {
final AllViolationsConfig config = this.config;
if (config == null) {
return false;
}
// Generate message.
// TODO: More colors?
final StringBuilder builder = new StringBuilder(300);
final String playerName = player.getName();
builder.append("[VL] " + ChatColor.YELLOW + playerName);
final String displayName = ChatColor.stripColor(player.getDisplayName()).trim();
if (!playerName.equals(displayName)) {
builder.append(" -> " + displayName);
}
builder.append(ChatColor.WHITE + " ");
builder.append(checkType.toString());
builder.append(" VL=" + StringUtil.fdec1.format(info.getTotalVl()));
builder.append("(+" + StringUtil.fdec1.format(info.getAddedVl()) + ")");
builder.append(ChatColor.GRAY);
for (int i = 0; i < parameters.length; i++) {
final ParameterName name = parameters[i];
final String value = info.getParameter(name);
if (value != null && !value.isEmpty() && !value.equals(this.noParameterTexts[i])) {
builder.append(" " + name.getText() + "=" + value);
}
}
final String message = builder.toString();
// Send to where it is appropriate.
final LogManager logManager = NCPAPIProvider.getNoCheatPlusAPI().getLogManager();
if (config.allToNotify) {
logManager.info(Streams.NOTIFY_INGAME, message);
}
if (config.allToTrace) {
logManager.info(Streams.TRACE_FILE, ChatColor.stripColor(message));
}
return false;
}
}