mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-03-13 06:59:30 +01:00
Fix LogAction, let it extend a more generic one.
This commit is contained in:
parent
1c608ecda1
commit
98ae3fb192
@ -0,0 +1,146 @@
|
|||||||
|
package fr.neatmonster.nocheatplus.actions.types;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import fr.neatmonster.nocheatplus.NCPAPIProvider;
|
||||||
|
import fr.neatmonster.nocheatplus.actions.Action;
|
||||||
|
import fr.neatmonster.nocheatplus.actions.ActionList;
|
||||||
|
import fr.neatmonster.nocheatplus.checks.ViolationData;
|
||||||
|
import fr.neatmonster.nocheatplus.config.ConfPaths;
|
||||||
|
import fr.neatmonster.nocheatplus.config.ConfigFile;
|
||||||
|
import fr.neatmonster.nocheatplus.config.ConfigFileWithActions;
|
||||||
|
import fr.neatmonster.nocheatplus.config.ConfigManager;
|
||||||
|
import fr.neatmonster.nocheatplus.logging.LogManager;
|
||||||
|
import fr.neatmonster.nocheatplus.logging.StreamID;
|
||||||
|
import fr.neatmonster.nocheatplus.utilities.ColorUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic log action, capable of logging to any stream of LogManager with any
|
||||||
|
* log level. Custom actions might need an altered action factory or override
|
||||||
|
* toString, due to action prefixes/suffixes.
|
||||||
|
*
|
||||||
|
* @author asofold
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class GenericLogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||||
|
|
||||||
|
public static class GenericLogActionConfig {
|
||||||
|
/**
|
||||||
|
* Config path to check for optimized actions or during runtime. Set to
|
||||||
|
* null, to always log
|
||||||
|
*/
|
||||||
|
public final String configPathActive;
|
||||||
|
public final boolean chatColor;
|
||||||
|
public final StreamID streamID;
|
||||||
|
public final Level level;
|
||||||
|
/**
|
||||||
|
* Suffix for log actions, such as i for ingame, f for file and c for
|
||||||
|
* console. Band-aid for writing log actions back to config.
|
||||||
|
*/
|
||||||
|
public final String actionConfigSuffix;
|
||||||
|
|
||||||
|
public GenericLogActionConfig(String configPathActive, StreamID streamID, boolean chatColor, Level level, String actionSuffix) {
|
||||||
|
this.configPathActive = configPathActive;
|
||||||
|
this.streamID = streamID;
|
||||||
|
this.chatColor = chatColor;
|
||||||
|
this.level = level;
|
||||||
|
this.actionConfigSuffix = actionSuffix;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final GenericLogActionConfig[] configs;
|
||||||
|
|
||||||
|
/** Set if to check the configuration flag on execute being called. */
|
||||||
|
private final boolean checkActive;
|
||||||
|
/** Set if any config demands replacing color. */
|
||||||
|
private final boolean replaceColor;
|
||||||
|
/** Set if any config demands stripping color. */
|
||||||
|
private final boolean stripColor;
|
||||||
|
|
||||||
|
public GenericLogAction(final String name, final int delay, final int repeat, final String message,
|
||||||
|
final boolean checkActive, final GenericLogActionConfig... configs) {
|
||||||
|
super(name, delay, repeat, message);
|
||||||
|
final List<GenericLogActionConfig> temp = new ArrayList<GenericLogAction.GenericLogActionConfig>(configs.length);
|
||||||
|
boolean replaceColor = false;
|
||||||
|
boolean stripColor = false;
|
||||||
|
boolean checkActiveUseful = false;
|
||||||
|
for (int i = 0; i < configs.length; i++) {
|
||||||
|
GenericLogActionConfig config = configs[i];
|
||||||
|
if (config == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
temp.add(config);
|
||||||
|
if (config.chatColor) {
|
||||||
|
replaceColor = true;
|
||||||
|
} else {
|
||||||
|
stripColor = true;
|
||||||
|
}
|
||||||
|
if (config.configPathActive != null) {
|
||||||
|
checkActiveUseful = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.configs = temp.toArray(new GenericLogActionConfig[temp.size()]);
|
||||||
|
this.checkActive = checkActive ? checkActiveUseful : false;
|
||||||
|
this.replaceColor = replaceColor;
|
||||||
|
this.stripColor = stripColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
|
||||||
|
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE) || configs.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final List<GenericLogActionConfig> temp = new ArrayList<GenericLogAction.GenericLogActionConfig>(configs.length);
|
||||||
|
for (int i = 0; i < configs.length; i ++) {
|
||||||
|
final GenericLogActionConfig logConfig = configs[i];
|
||||||
|
if (checkActive && logConfig.configPathActive != null && !config.getBoolean(logConfig.configPathActive)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
temp.add(logConfig);
|
||||||
|
}
|
||||||
|
if (temp.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final GenericLogActionConfig[] logConfigs = temp.toArray(new GenericLogActionConfig[temp.size()]);
|
||||||
|
return new GenericLogAction(name, delay, repeat, message, false, logConfigs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(final ViolationData violationData) {
|
||||||
|
// TODO: Consider permission caching or removing the feature? [Besides, check earlier?]
|
||||||
|
if (violationData.player.hasPermission(violationData.getPermissionSilent())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final LogManager logManager = NCPAPIProvider.getNoCheatPlusAPI().getLogManager();
|
||||||
|
final String message = super.getMessage(violationData);
|
||||||
|
final String messageNoColor = stripColor ? ColorUtil.removeColors(message) : null;
|
||||||
|
final String messageWithColor = replaceColor ? ColorUtil.replaceColors(message) : null;
|
||||||
|
final ConfigFile configFile = checkActive ? ConfigManager.getConfigFile() : null;
|
||||||
|
for (int i = 0; i < configs.length; i ++) {
|
||||||
|
final GenericLogActionConfig config = configs[i];
|
||||||
|
if (checkActive && config.configPathActive != null && !configFile.getBoolean(config.configPathActive)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
logManager.log(config.streamID, config.level, config.chatColor ? messageWithColor : messageNoColor);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the string that's used to define the action in the configuration file.
|
||||||
|
*
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder builder = new StringBuilder(32 + 2 * configs.length);
|
||||||
|
builder.append("log:" + name + ":" + delay + ":" + repeat + ":");
|
||||||
|
for (int i = 0; i < configs.length; i ++) {
|
||||||
|
builder.append(configs[i].actionConfigSuffix);
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,32 +1,24 @@
|
|||||||
package fr.neatmonster.nocheatplus.actions.types;
|
package fr.neatmonster.nocheatplus.actions.types;
|
||||||
|
|
||||||
import fr.neatmonster.nocheatplus.NCPAPIProvider;
|
import java.util.logging.Level;
|
||||||
import fr.neatmonster.nocheatplus.actions.Action;
|
|
||||||
import fr.neatmonster.nocheatplus.actions.ActionList;
|
|
||||||
import fr.neatmonster.nocheatplus.checks.ViolationData;
|
|
||||||
import fr.neatmonster.nocheatplus.config.ConfPaths;
|
import fr.neatmonster.nocheatplus.config.ConfPaths;
|
||||||
import fr.neatmonster.nocheatplus.config.ConfigFileWithActions;
|
|
||||||
import fr.neatmonster.nocheatplus.logging.LogManager;
|
|
||||||
import fr.neatmonster.nocheatplus.logging.Streams;
|
import fr.neatmonster.nocheatplus.logging.Streams;
|
||||||
import fr.neatmonster.nocheatplus.utilities.ColorUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print a log message to various locations.
|
* Default log action for standard targets.
|
||||||
*/
|
*/
|
||||||
public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
public class LogAction extends GenericLogAction {
|
||||||
|
|
||||||
// Some flags to decide where the log message should show up, based on the configuration file.
|
protected static final GenericLogActionConfig configIngame = new GenericLogActionConfig(
|
||||||
/** Log to chat? */
|
ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE, Streams.NOTIFY_INGAME, true, Level.INFO, "i");
|
||||||
public final boolean toChat;
|
protected static final GenericLogActionConfig configConsole = new GenericLogActionConfig(
|
||||||
|
ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE, Streams.SERVER_LOGGER, false, Level.INFO, "c");
|
||||||
/** Log to console? */
|
protected static final GenericLogActionConfig configFile = new GenericLogActionConfig(
|
||||||
public final boolean toConsole;
|
ConfPaths.LOGGING_BACKEND_FILE_ACTIVE, Streams.DEFAULT_FILE, false, Level.INFO, "f");
|
||||||
|
|
||||||
/** Log to file? */
|
|
||||||
public final boolean toFile;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new log action.
|
* Instantiates a new log action (not optimized).
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* the name
|
* the name
|
||||||
@ -45,55 +37,7 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
|||||||
*/
|
*/
|
||||||
public LogAction(final String name, final int delay, final int repeat, final boolean toChat,
|
public LogAction(final String name, final int delay, final int repeat, final boolean toChat,
|
||||||
final boolean toConsole, final boolean toFile, final String message) {
|
final boolean toConsole, final boolean toFile, final String message) {
|
||||||
super(name, delay, repeat, message);
|
super(name, delay, repeat, message, true, toChat ? configIngame : null, toConsole ? configConsole : null, toFile ? configFile : null);
|
||||||
// Might switch to only store the prefixes (null = deactivated).
|
|
||||||
this.toChat = toChat;
|
|
||||||
this.toConsole = toConsole;
|
|
||||||
this.toFile = toFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
|
|
||||||
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see
|
|
||||||
* fr.neatmonster.nocheatplus.actions.Action#execute(fr.neatmonster.nocheatplus
|
|
||||||
* .checks.ViolationData)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean execute(final ViolationData violationData) {
|
|
||||||
if (!violationData.player.hasPermission(violationData.getPermissionSilent())) {
|
|
||||||
final String message = super.getMessage(violationData);
|
|
||||||
final LogManager logManager = NCPAPIProvider.getNoCheatPlusAPI().getLogManager();
|
|
||||||
if (toChat) {
|
|
||||||
logManager.info(Streams.NOTIFY_INGAME, ColorUtil.replaceColors(message));
|
|
||||||
}
|
|
||||||
if (toConsole) {
|
|
||||||
logManager.info(Streams.SERVER_LOGGER, ColorUtil.removeColors(message));
|
|
||||||
}
|
|
||||||
if (toFile) {
|
|
||||||
logManager.info(Streams.DEFAULT_FILE, ColorUtil.removeColors(message));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the string that's used to define the action in the logfile.
|
|
||||||
*
|
|
||||||
* @return the string
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "log:" + name + ":" + delay + ":" + repeat + ":" + (toConsole ? "c" : "") + (toChat ? "i" : "")
|
|
||||||
+ (toFile ? "f" : "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user