mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-02-20 13:41:26 +01:00
Spaces.
This commit is contained in:
parent
881bd44330
commit
4dcebd11c7
@ -10,14 +10,14 @@ import java.util.Map.Entry;
|
||||
import fr.neatmonster.nocheatplus.config.ConfigFileWithActions;
|
||||
|
||||
public abstract class AbstractActionList<D extends ActionData, L extends AbstractActionList<D, L>>{
|
||||
|
||||
|
||||
public static interface ActionListFactory<D extends ActionData, L extends AbstractActionList<D, L>>{
|
||||
public L getNewActionList(String permissionSilent);
|
||||
}
|
||||
|
||||
/** Something to return if nothing is set. */
|
||||
protected static final Action<?, ?>[] emptyArray = new Action[0];
|
||||
|
||||
|
||||
public static interface ActionListFactory<D extends ActionData, L extends AbstractActionList<D, L>>{
|
||||
public L getNewActionList(String permissionSilent);
|
||||
}
|
||||
|
||||
/** Something to return if nothing is set. */
|
||||
protected static final Action<?, ?>[] emptyArray = new Action[0];
|
||||
|
||||
/** This is a very bad design decision, but it's also really convenient to define this here. */
|
||||
public final String permissionSilent;
|
||||
@ -28,7 +28,7 @@ public abstract class AbstractActionList<D extends ActionData, L extends Abstra
|
||||
/** The thresholds of this list. **/
|
||||
protected final List<Integer> thresholds = new ArrayList<Integer>();
|
||||
|
||||
protected final ActionListFactory<D, L> listFactory;
|
||||
protected final ActionListFactory<D, L> listFactory;
|
||||
|
||||
/**
|
||||
* Instantiates a new action list.
|
||||
@ -37,7 +37,7 @@ public abstract class AbstractActionList<D extends ActionData, L extends Abstra
|
||||
* the permission
|
||||
*/
|
||||
public AbstractActionList(final String permissionSilent, final ActionListFactory<D, L> listFactory) {
|
||||
this.listFactory = listFactory;
|
||||
this.listFactory = listFactory;
|
||||
this.permissionSilent = permissionSilent + ".silent";
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ public abstract class AbstractActionList<D extends ActionData, L extends Abstra
|
||||
* @return the array of actions whose threshold was closest to the violation level but not bigger
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Action<D, L>[] getActions(final double violationLevel) {
|
||||
public Action<D, L>[] getActions(final double violationLevel) {
|
||||
Integer result = null;
|
||||
|
||||
for (final Integer threshold : thresholds)
|
||||
@ -87,47 +87,56 @@ public abstract class AbstractActionList<D extends ActionData, L extends Abstra
|
||||
|
||||
this.actions.put(threshold, actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a copy of this list, but optimize it, i.e. remove entries that are
|
||||
* never called, possibly do other optimizations which are possible given
|
||||
* the specific configuration.
|
||||
*
|
||||
* @param config Configuration to adapt to.
|
||||
* @return Optimized AbstractActionList, individual Actions can be identical instances, altered Action instances must always be new instances, arrays are always new arrays.
|
||||
*/
|
||||
public L getOptimizedCopy(final ConfigFileWithActions<D, L> config) {
|
||||
final L newList = listFactory.getNewActionList(permissionSilent);
|
||||
for (final Entry<Integer, Action<D, L>[]> entry : actions.entrySet()){
|
||||
final Integer t = entry.getKey();
|
||||
final Action<D, L>[] a = getOptimizedCopy(config, t, entry.getValue());
|
||||
if (a != null && a.length > 0){
|
||||
newList.setActions(t, a);
|
||||
}
|
||||
}
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optimized copy of the Actions array, given the config in use.
|
||||
* @param config
|
||||
* @param threshold
|
||||
* @param actions
|
||||
* @return Copy with optimized entries, null or empty arrays are possible. Contained Actions might be identical to the given ones, just changed actions must be new instances to preserve consistency, Action instances are not to be altered.
|
||||
*/
|
||||
public Action<D, L>[] getOptimizedCopy(final ConfigFileWithActions<D, L> config, final Integer threshold, final Action<D, L>[] actions)
|
||||
{
|
||||
if (actions == null || actions.length == 0) return null;
|
||||
final ArrayList<Action<D, L>> optimized = new ArrayList<Action<D, L>>();
|
||||
for (final Action<D, L> action : actions){
|
||||
final Action<D, L> optAction = action.getOptimizedCopy(config, threshold);
|
||||
if (optAction != null) optimized.add(optAction);
|
||||
}
|
||||
if (optimized.isEmpty()) return null;
|
||||
@SuppressWarnings("unchecked")
|
||||
final Action<D, L>[] optActions = (Action<D, L>[]) new Action<?, ?>[optimized.size()];
|
||||
optimized.toArray(optActions);
|
||||
return optActions;
|
||||
}
|
||||
/**
|
||||
* Return a copy of this list, but optimize it, i.e. remove entries that are
|
||||
* never called, possibly do other optimizations which based on the specific
|
||||
* configuration.
|
||||
*
|
||||
* @param config
|
||||
* Configuration to adapt to.
|
||||
* @return Optimized AbstractActionList, individual Actions can be identical
|
||||
* instances, altered Action instances must always be new instances,
|
||||
* arrays are always new arrays.
|
||||
*/
|
||||
public L getOptimizedCopy(final ConfigFileWithActions<D, L> config) {
|
||||
final L newList = listFactory.getNewActionList(permissionSilent);
|
||||
for (final Entry<Integer, Action<D, L>[]> entry : actions.entrySet()){
|
||||
final Integer t = entry.getKey();
|
||||
final Action<D, L>[] a = getOptimizedCopy(config, t, entry.getValue());
|
||||
if (a != null && a.length > 0){
|
||||
newList.setActions(t, a);
|
||||
}
|
||||
}
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optimized copy of the Actions array, given the config in use.
|
||||
* @param config
|
||||
* @param threshold
|
||||
* @param actions
|
||||
* @return Copy with optimized entries, null or empty arrays are possible. Contained Actions might be identical to the given ones, just changed actions must be new instances to preserve consistency, Action instances are not to be altered.
|
||||
*/
|
||||
public Action<D, L>[] getOptimizedCopy(final ConfigFileWithActions<D, L> config, final Integer threshold, final Action<D, L>[] actions)
|
||||
{
|
||||
if (actions == null || actions.length == 0) {
|
||||
return null;
|
||||
}
|
||||
final ArrayList<Action<D, L>> optimized = new ArrayList<Action<D, L>>();
|
||||
for (final Action<D, L> action : actions){
|
||||
final Action<D, L> optAction = action.getOptimizedCopy(config, threshold);
|
||||
if (optAction != null) {
|
||||
optimized.add(optAction);
|
||||
}
|
||||
}
|
||||
if (optimized.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
final Action<D, L>[] optActions = (Action<D, L>[]) new Action<?, ?>[optimized.size()];
|
||||
optimized.toArray(optActions);
|
||||
return optActions;
|
||||
}
|
||||
}
|
||||
|
@ -16,13 +16,13 @@ import fr.neatmonster.nocheatplus.utilities.ColorUtil;
|
||||
* Print a log message to various locations.
|
||||
*/
|
||||
public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||
|
||||
private static final String PREFIX_CHAT = ChatColor.RED + "NCP: "+ ChatColor.WHITE ;
|
||||
private static final String PREFIX_CONSOLE= "[NoCheatPlus] ";
|
||||
private static final String PREFIX_FILE = "";
|
||||
|
||||
// TODO: pull down to providers for (console), !chat!, (file) - then move to NCPCompat.
|
||||
|
||||
|
||||
private static final String PREFIX_CHAT = ChatColor.RED + "NCP: "+ ChatColor.WHITE ;
|
||||
private static final String PREFIX_CONSOLE= "[NoCheatPlus] ";
|
||||
private static final String PREFIX_FILE = "";
|
||||
|
||||
// TODO: pull down to providers for (console), !chat!, (file) - then move to NCPCompat.
|
||||
|
||||
// Some flags to decide where the log message should show up, based on the configuration file.
|
||||
/** Log to chat? */
|
||||
public final boolean toChat;
|
||||
@ -32,7 +32,7 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||
|
||||
/** Log to file? */
|
||||
public final boolean toFile;
|
||||
|
||||
|
||||
/** Message prefixes. */
|
||||
public final String prefixChat, prefixConsole, prefixFile;
|
||||
|
||||
@ -65,7 +65,7 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||
prefixConsole = PREFIX_CONSOLE;
|
||||
prefixFile = PREFIX_FILE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for optimized actions.
|
||||
* @param name
|
||||
@ -86,31 +86,31 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||
toConsole = prefixConsole != null;
|
||||
toFile = prefixFile != null;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (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);
|
||||
if (toChat) {
|
||||
NCPAPIProvider.getNoCheatPlusAPI().sendAdminNotifyMessage(ColorUtil.replaceColors(prefixChat + message));
|
||||
}
|
||||
if (toConsole) {
|
||||
LogUtil.logInfo(ColorUtil.removeColors(prefixConsole + message));
|
||||
}
|
||||
if (toFile) {
|
||||
StaticLogFile.fileLogger.info(ColorUtil.removeColors(prefixFile + message));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (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);
|
||||
if (toChat) {
|
||||
NCPAPIProvider.getNoCheatPlusAPI().sendAdminNotifyMessage(ColorUtil.replaceColors(prefixChat + message));
|
||||
}
|
||||
if (toConsole) {
|
||||
LogUtil.logInfo(ColorUtil.removeColors(prefixConsole + message));
|
||||
}
|
||||
if (toFile) {
|
||||
StaticLogFile.fileLogger.info(ColorUtil.removeColors(prefixFile + message));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the string that's used to define the action in the logfile.
|
||||
@ -123,35 +123,35 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
|
||||
+ (toFile ? "f" : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
|
||||
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE)) {
|
||||
return null;
|
||||
}
|
||||
final String prefixChat = filterPrefix(config, ConfPaths.LOGGING_BACKEND_INGAMECHAT_PREFIX, PREFIX_CHAT, this.toChat && config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE));
|
||||
final String prefixConsole = filterPrefix(config, ConfPaths.LOGGING_BACKEND_CONSOLE_PREFIX, PREFIX_CONSOLE, this.toConsole && config.getBoolean(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE));
|
||||
final String prefixFile = filterPrefix(config, ConfPaths.LOGGING_BACKEND_FILE_PREFIX, PREFIX_FILE, this.toFile && config.getBoolean(ConfPaths.LOGGING_BACKEND_FILE_ACTIVE));
|
||||
if (allNull(prefixChat, prefixConsole, prefixFile)) {
|
||||
return null;
|
||||
}
|
||||
return new LogAction(name, delay, repeat, prefixChat, prefixConsole, prefixFile, message);
|
||||
}
|
||||
|
||||
private static boolean allNull(Object... objects) {
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
if (objects[i] != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final String filterPrefix(final ConfigFileWithActions<ViolationData, ActionList> config, final String path, final String defaultValue, final boolean use) {
|
||||
if (!use) {
|
||||
return null;
|
||||
}
|
||||
final String prefix = config.getString(path);
|
||||
return prefix == null ? defaultValue : prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
|
||||
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE)) {
|
||||
return null;
|
||||
}
|
||||
final String prefixChat = filterPrefix(config, ConfPaths.LOGGING_BACKEND_INGAMECHAT_PREFIX, PREFIX_CHAT, this.toChat && config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE));
|
||||
final String prefixConsole = filterPrefix(config, ConfPaths.LOGGING_BACKEND_CONSOLE_PREFIX, PREFIX_CONSOLE, this.toConsole && config.getBoolean(ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE));
|
||||
final String prefixFile = filterPrefix(config, ConfPaths.LOGGING_BACKEND_FILE_PREFIX, PREFIX_FILE, this.toFile && config.getBoolean(ConfPaths.LOGGING_BACKEND_FILE_ACTIVE));
|
||||
if (allNull(prefixChat, prefixConsole, prefixFile)) {
|
||||
return null;
|
||||
}
|
||||
return new LogAction(name, delay, repeat, prefixChat, prefixConsole, prefixFile, message);
|
||||
}
|
||||
|
||||
private static boolean allNull(Object... objects) {
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
if (objects[i] != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final String filterPrefix(final ConfigFileWithActions<ViolationData, ActionList> config, final String path, final String defaultValue, final boolean use) {
|
||||
if (!use) {
|
||||
return null;
|
||||
}
|
||||
final String prefix = config.getString(path);
|
||||
return prefix == null ? defaultValue : prefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,69 +6,69 @@ import fr.neatmonster.nocheatplus.actions.Action;
|
||||
import fr.neatmonster.nocheatplus.actions.ActionData;
|
||||
|
||||
public abstract class ConfigFileWithActions<D extends ActionData, L extends AbstractActionList<D, L>> extends RawConfigFile {
|
||||
|
||||
|
||||
/** The factory. */
|
||||
protected AbstractActionFactory<D, L> factory = null;
|
||||
|
||||
// /**
|
||||
// * @deprecated Use resetActionFactory.
|
||||
// */
|
||||
// public void regenerateActionLists(){
|
||||
// resetActionFactory();
|
||||
// }
|
||||
|
||||
/**
|
||||
* This should set (override if necessary) a default ActionFactory. NCP will use ConfigManager.getActionsFactoryFactory. <br>
|
||||
* Do this after reading new data or changing the AbstractActionFactory instance.<br>
|
||||
* This must set or override the internal factory field to enable/update ActionList getting.<br>
|
||||
* If factory is null on getting an ActionList, this will be called internally.
|
||||
*/
|
||||
public abstract void setActionFactory();
|
||||
|
||||
/**
|
||||
* Explicitly set the ActionFactory, also allow setting to null for lazy reset/get.
|
||||
* @param factory
|
||||
*/
|
||||
public void setActionFactory(final AbstractActionFactory<D, L> factory){
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method to get an optimized action list from the configuration.
|
||||
*
|
||||
* @param path
|
||||
* the path
|
||||
* @param permission
|
||||
* the permission
|
||||
* @return the action list
|
||||
*/
|
||||
public L getOptimizedActionList(final String path, final String permission)
|
||||
{
|
||||
if (factory == null){
|
||||
setActionFactory();
|
||||
}
|
||||
final String value = this.getString(path);
|
||||
return factory.createActionList(value, permission).getOptimizedCopy(this);
|
||||
}
|
||||
// /**
|
||||
// * @deprecated Use resetActionFactory.
|
||||
// */
|
||||
// public void regenerateActionLists(){
|
||||
// resetActionFactory();
|
||||
// }
|
||||
|
||||
/**
|
||||
* A convenience method to get default action lists from the configuration, without
|
||||
* applying any optimization.
|
||||
*
|
||||
* @param path
|
||||
* the path
|
||||
* @param permission
|
||||
* the permission
|
||||
* @return the action list
|
||||
*/
|
||||
public L getDefaultActionList(final String path, final String permission)
|
||||
{
|
||||
if (factory == null){
|
||||
setActionFactory();
|
||||
}
|
||||
final String value = this.getString(path);
|
||||
return factory.createActionList(value, permission);
|
||||
}
|
||||
/**
|
||||
* This should set (override if necessary) a default ActionFactory. NCP will use ConfigManager.getActionsFactoryFactory. <br>
|
||||
* Do this after reading new data or changing the AbstractActionFactory instance.<br>
|
||||
* This must set or override the internal factory field to enable/update ActionList getting.<br>
|
||||
* If factory is null on getting an ActionList, this will be called internally.
|
||||
*/
|
||||
public abstract void setActionFactory();
|
||||
|
||||
/**
|
||||
* Explicitly set the ActionFactory, also allow setting to null for lazy reset/get.
|
||||
* @param factory
|
||||
*/
|
||||
public void setActionFactory(final AbstractActionFactory<D, L> factory){
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method to get an optimized action list from the configuration.
|
||||
*
|
||||
* @param path
|
||||
* the path
|
||||
* @param permission
|
||||
* the permission
|
||||
* @return the action list
|
||||
*/
|
||||
public L getOptimizedActionList(final String path, final String permission)
|
||||
{
|
||||
if (factory == null){
|
||||
setActionFactory();
|
||||
}
|
||||
final String value = this.getString(path);
|
||||
return factory.createActionList(value, permission).getOptimizedCopy(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method to get default action lists from the configuration, without
|
||||
* applying any optimization.
|
||||
*
|
||||
* @param path
|
||||
* the path
|
||||
* @param permission
|
||||
* the permission
|
||||
* @return the action list
|
||||
*/
|
||||
public L getDefaultActionList(final String path, final String permission)
|
||||
{
|
||||
if (factory == null){
|
||||
setActionFactory();
|
||||
}
|
||||
final String value = this.getString(path);
|
||||
return factory.createActionList(value, permission);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@ -91,5 +91,5 @@ public abstract class ConfigFileWithActions<D extends ActionData, L extends Abst
|
||||
|
||||
set(path, string.toString().trim());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user