Add tags to PlayerData + implement a notify-off tag.

Not sure this is that much final, but PlayerData can now carry tags.
NoCheatPlus.sendAdminNotify... will now check PlayerData for the
"notify_off" tag, as a preparation for a command to turn off
notifications.
This commit is contained in:
asofold 2013-06-11 20:06:57 +02:00
parent 1d53115abe
commit 888c7d937a
2 changed files with 104 additions and 5 deletions

View File

@ -1,5 +1,8 @@
package fr.neatmonster.nocheatplus.players;
import java.util.HashSet;
import java.util.Set;
import fr.neatmonster.nocheatplus.components.IData;
/**
@ -26,8 +29,13 @@ import fr.neatmonster.nocheatplus.components.IData;
*/
public class PlayerData implements IData {
public static final String TAG_NOTIFY_OFF = "notify_off";
public final PlayerTask task;
/** Not sure this is the future of extra properties. */
protected Set<String> tags = null;
/** Lower case name of the player. */
final String lcName;
@ -40,5 +48,67 @@ public class PlayerData implements IData {
this.task = new PlayerTask(this.lcName);
}
/**
* Test if present.
* @param tag
* @return
*/
public boolean hasTag(final String tag){
return tags != null && tags.contains(tag);
}
/**
* Add the tag.
* @param tag
*/
public void addTag(final String tag){
if (tags == null){
tags = new HashSet<String>();
}
tags.add(tag);
}
/**
* Remove the tag.
* @param tag
*/
public void removeTag(final String tag) {
if (tags != null){
tags.remove(tag);
if (tags.isEmpty()){
tags = null;
}
}
}
/**
* Add tag or remove tag, based on arguments.
* @param tag
* @param add The tag will be added, if set to true. If set to false, the tag will be removed.
*/
public void setTag(final String tag, final boolean add){
if (add){
addTag(tag);
}
else{
removeTag(tag);
}
}
/**
* Check if notifications are turned off, this does not bypass permission checks.
* @return
*/
public boolean getNotifyOff(){
return hasTag(TAG_NOTIFY_OFF);
}
/**
* Allow or turn off notifications. A player must have the admin.notify permission to receive notifications.
* @param notifyOff set to true to turn off notifications.
*/
public void setNotifyOff(final boolean notifyOff){
setTag(TAG_NOTIFY_OFF, notifyOff);
}
}

View File

@ -78,6 +78,7 @@ import fr.neatmonster.nocheatplus.permissions.PermissionUtil;
import fr.neatmonster.nocheatplus.permissions.PermissionUtil.CommandProtectionEntry;
import fr.neatmonster.nocheatplus.permissions.Permissions;
import fr.neatmonster.nocheatplus.players.DataManager;
import fr.neatmonster.nocheatplus.players.PlayerData;
import fr.neatmonster.nocheatplus.updates.Updates;
import fr.neatmonster.nocheatplus.utilities.BlockProperties;
import fr.neatmonster.nocheatplus.utilities.OnDemandTickListener;
@ -98,6 +99,9 @@ import fr.neatmonster.nocheatplus.utilities.TickTask;
*/
public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
private static final String MSG_CONFIG_OUTDATED = ChatColor.RED + "NCP: " + ChatColor.WHITE + "Your configuration might be outdated.\n" + "Some settings could have changed, you should regenerate it!";
private static final String MSG_NOTIFY_OFF = ChatColor.RED + "NCP: " + ChatColor.WHITE + "Notifications are turned " + ChatColor.RED + "OFF" + ChatColor.WHITE + ".";
//////////////////
// Static API
//////////////////
@ -154,6 +158,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
/** Components that need notification on reloading.
* (Kept here, for if during runtime some might get added.)*/
private final List<INotifyReload> notifyReload = new LinkedList<INotifyReload>();
/** If to use subscriptions or not. */
protected boolean useSubscriptions = false;
/** Permission states stored on a per-world basis, updated with join/quit/kick. */
protected final List<PermStateReceiver> permStateReceivers = new ArrayList<PermStateReceiver>();
@ -259,7 +266,7 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
@Override
public int sendAdminNotifyMessage(final String message){
if (ConfigManager.getConfigFile().getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS)){
if (useSubscriptions){
// TODO: Might respect console settings, or add extra config section (e.g. notifications).
return sendAdminNotifyMessageSubscriptions(message);
}
@ -267,6 +274,11 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
return sendAdminNotifyMessageStored(message);
}
}
private final boolean hasTurnedOffNotifications(final String playerName){
final PlayerData data = DataManager.getPlayerData(playerName, false);
return data != null && data.getNotifyOff();
}
/**
* Send notification to players with stored notify-permission (world changes, login, permissions are not re-checked here).
@ -278,6 +290,10 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
if (names == null) return 0;
int done = 0;
for (final String name : names){
if (hasTurnedOffNotifications(name)){
// Has turned off notifications.
continue;
}
final Player player = DataManager.getPlayerExact(name);
if (player != null){
player.sendMessage(message);
@ -299,6 +315,10 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
for (final Permissible permissible : permissibles){
if (permissible instanceof CommandSender && permissible.hasPermission(Permissions.ADMINISTRATION_NOTIFY)){
final CommandSender sender = (CommandSender) permissible;
if ((sender instanceof Player) && hasTurnedOffNotifications(((Player) sender).getName())){
continue;
}
sender.sendMessage(message);
done.add(sender.getName());
}
@ -309,6 +329,9 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
if (!done.contains(name)){
final Player player = DataManager.getPlayerExact(name);
if (player != null && player.hasPermission(Permissions.ADMINISTRATION_NOTIFY)){
if (hasTurnedOffNotifications(player.getName())){
continue;
}
player.sendMessage(message);
done.add(name);
}
@ -660,6 +683,8 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
final ConfigFile config = ConfigManager.getConfigFile();
useSubscriptions = config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS);
// Initialize MCAccess.
initMCAccess(config);
@ -858,6 +883,8 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
if (config.getBoolean(ConfPaths.MISCELLANEOUS_PROTECTPLUGINS)) setupCommandProtection();
// (Re-) schedule consistency checking.
scheduleConsistencyCheckers();
// Cache some things.
useSubscriptions = config.getBoolean(ConfPaths.LOGGING_BACKEND_INGAMECHAT_SUBSCRIPTIONS);
}
@Override
@ -990,16 +1017,18 @@ public class NoCheatPlus extends JavaPlugin implements NoCheatPlusAPI {
protected void onJoin(final Player player){
updatePermStateReceivers(player);
if (nameSetPerms.hasPermission(player.getName(), Permissions.ADMINISTRATION_NOTIFY)){
final String playerName = player.getName();
if (nameSetPerms.hasPermission(playerName, Permissions.ADMINISTRATION_NOTIFY)){
// Login notifications...
// // Update available.
// if (updateAvailable) player.sendMessage(ChatColor.RED + "NCP: " + ChatColor.WHITE + "A new update of NoCheatPlus is available.\n" + "Download it at http://nocheatplus.org/update");
// Outdated config.
if (configOutdated) player.sendMessage(ChatColor.RED + "NCP: " + ChatColor.WHITE + "Your configuration might be outdated.\n" + "Some settings could have changed, you should regenerate it!");
if (configOutdated) player.sendMessage(MSG_CONFIG_OUTDATED);
if (hasTurnedOffNotifications(playerName)){
player.sendMessage(MSG_NOTIFY_OFF);
}
}
ModUtil.motdOnJoin(player);
for (final JoinLeaveListener jlListener : joinLeaveListeners){