Add option to use permission subscriptions for in-game-chat logging.

See: https://github.com/NoCheatPlus/NoCheatPlus/pull/14 (@Cobi)

This allows will still check the players with stored permissions
but re-check their permissions.
This commit is contained in:
asofold 2013-02-26 01:58:21 +01:00
parent ee400b7525
commit a5cc3f819f
3 changed files with 56 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -14,6 +15,7 @@ import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -25,6 +27,7 @@ import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.permissions.Permissible;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -194,15 +197,28 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
} }
/** /**
* Send all players with the nocheatplus.admin.notify permission a message. * Send all players with the nocheatplus.admin.notify permission a message.<br>
* This is likely to be more efficient than iterating over all players and * This will act according to configuration (stored permissions or permission subscriptions).
* checking their permissions. However this only updates permissions with
* login and world changes currently.
* *
* @param message * @param message
* @return Number of players messaged. * @return Number of players messaged.
*/ */
public static int sendAdminNotifyMessage(final String message){ public static int sendAdminNotifyMessage(final String message){
if (ConfigManager.getConfigFile().getBoolean(ConfPaths.LOGGING_USESUBSCRIPTIONS)){
// TODO: Might respect console settings, or add extra config section (e.g. notifications).
return sendAdminNotifyMessageSubscriptions(message);
}
else{
return sendAdminNotifyMessageStored(message);
}
}
/**
* Send notification to players with stored notify-permission (world changes, login, permissions are not re-checked here).
* @param message
* @return
*/
public static int sendAdminNotifyMessageStored(final String message){
final Set<String> names = nameSetPerms.getPlayers(Permissions.ADMINISTRATION_NOTIFY); final Set<String> names = nameSetPerms.getPlayers(Permissions.ADMINISTRATION_NOTIFY);
if (names == null) return 0; if (names == null) return 0;
int done = 0; int done = 0;
@ -216,6 +232,37 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
return done; return done;
} }
/**
* Send notification to all CommandSenders found in permission subscriptions for the notify-permission as well as players that have stored permissions (those get re-checked here).
* @param message
* @return
*/
public static int sendAdminNotifyMessageSubscriptions(final String message){
final Set<Permissible> permissibles = Bukkit.getPluginManager().getPermissionSubscriptions(Permissions.ADMINISTRATION_NOTIFY);
final Set<String> names = nameSetPerms.getPlayers(Permissions.ADMINISTRATION_NOTIFY);
final Set<String> done = new HashSet<String>(permissibles.size() + (names == null ? 0 : names.size()));
for (final Permissible permissible : permissibles){
if (permissible instanceof CommandSender && permissible.hasPermission(Permissions.ADMINISTRATION_NOTIFY)){
final CommandSender sender = (CommandSender) permissible;
sender.sendMessage(message);
done.add(sender.getName());
}
}
// Fall-back checking for players.
if (names != null){
for (final String name : names){
if (!done.contains(name)){
final Player player = DataManager.getPlayerExact(name);
if (player != null && player.hasPermission(Permissions.ADMINISTRATION_NOTIFY)){
player.sendMessage(message);
done.add(name);
}
}
}
}
return done.size();
}
/** The event listeners. */ /** The event listeners. */
private final List<Listener> listeners = new ArrayList<Listener>(); private final List<Listener> listeners = new ArrayList<Listener>();

View File

@ -34,6 +34,7 @@ public abstract class ConfPaths {
public static final String LOGGING_FILE = LOGGING + "file"; public static final String LOGGING_FILE = LOGGING + "file";
public static final String LOGGING_FILENAME = LOGGING + "filename"; public static final String LOGGING_FILENAME = LOGGING + "filename";
public static final String LOGGING_INGAMECHAT = LOGGING + "ingamechat"; public static final String LOGGING_INGAMECHAT = LOGGING + "ingamechat";
public static final String LOGGING_USESUBSCRIPTIONS = LOGGING + "usesubscriptions";
/* /*
* e e ,e, 888 888 * e e ,e, 888 888
@ -51,9 +52,6 @@ public abstract class ConfPaths {
public static final String MISCELLANEOUS_UPDATETIMEOUT = MISCELLANEOUS + "updatetimeout"; public static final String MISCELLANEOUS_UPDATETIMEOUT = MISCELLANEOUS + "updatetimeout";
public static final String MISCELLANEOUS_REPORTTOMETRICS = MISCELLANEOUS + "reporttometrics"; public static final String MISCELLANEOUS_REPORTTOMETRICS = MISCELLANEOUS + "reporttometrics";
public static final String MISCELLANEOUS_MANAGELISTENERS = MISCELLANEOUS + "managelisteners"; public static final String MISCELLANEOUS_MANAGELISTENERS = MISCELLANEOUS + "managelisteners";
private static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY = MISCELLANEOUS + "nomovedtooquickly.";
public static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY_ENABLED = MISCELLANEOUS_NOMOVEDTOOQUICKLY + "enabled";
public static final String MISCELLANEOUS_NOMOVEDTOOQUICKLY_USEPROXY = MISCELLANEOUS_NOMOVEDTOOQUICKLY + "useproxy";
/** TEMP: hidden flag to disable all lag adaption with one flag. */ /** TEMP: hidden flag to disable all lag adaption with one flag. */
public static final String MISCELLANEOUS_LAG = MISCELLANEOUS + "lag"; public static final String MISCELLANEOUS_LAG = MISCELLANEOUS + "lag";

View File

@ -56,6 +56,9 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.LOGGING_FILENAME, "nocheatplus.log"); set(ConfPaths.LOGGING_FILENAME, "nocheatplus.log");
set(ConfPaths.LOGGING_INGAMECHAT, true); set(ConfPaths.LOGGING_INGAMECHAT, true);
// Notifications settings.
set(ConfPaths.LOGGING_USESUBSCRIPTIONS, false);
/* /*
* e e ,e, 888 888 * e e ,e, 888 888
* d8b d8b " dP"Y e88'888 ,e e, 888 888 ,"Y88b 888 8e ,e e, e88 88e 8888 8888 dP"Y * d8b d8b " dP"Y e88'888 ,e e, 888 888 ,"Y88b 888 8e ,e e, e88 88e 8888 8888 dP"Y