This commit is contained in:
asofold 2014-11-20 12:28:50 +01:00
parent 99e4cdc1bc
commit db7775a2c7
3 changed files with 277 additions and 277 deletions

View File

@ -19,185 +19,185 @@ import fr.neatmonster.nocheatplus.utilities.ColorUtil;
public class PermissionUtil { public class PermissionUtil {
/** /**
* Entry for what the old state of a command was. * Entry for what the old state of a command was.
* @author mc_dev * @author mc_dev
* *
*/ */
public static class CommandProtectionEntry{ public static class CommandProtectionEntry{
public final Command command; public final Command command;
public final String label; public final String label;
public final String permission; public final String permission;
public final PermissionDefault permissionDefault; public final PermissionDefault permissionDefault;
public final String permissionMessage; public final String permissionMessage;
/** /**
* *
* @param command * @param command
* @param label trim + lower case. * @param label trim + lower case.
* @param permission * @param permission
* @param permissionDefault * @param permissionDefault
* @param permissionMessage * @param permissionMessage
*/ */
public CommandProtectionEntry(Command command, String label, String permission, PermissionDefault permissionDefault, String permissionMessage) { public CommandProtectionEntry(Command command, String label, String permission, PermissionDefault permissionDefault, String permissionMessage) {
this.command = command; this.command = command;
this.label = label; this.label = label;
this.permission = permission; this.permission = permission;
this.permissionDefault = permissionDefault; this.permissionDefault = permissionDefault;
this.permissionMessage = permissionMessage; this.permissionMessage = permissionMessage;
} }
public void restore() { public void restore() {
// (Don't skip resetting, as there could be fall-back aliases.) // (Don't skip resetting, as there could be fall-back aliases.)
// Command registered = CommandUtil.getCommand(label); // Command registered = CommandUtil.getCommand(label);
// if (registered == null || registered != command) return; // if (registered == null || registered != command) return;
if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) { if (!label.equalsIgnoreCase(command.getLabel().trim().toLowerCase())) {
command.setLabel(label); command.setLabel(label);
} }
command.setPermission(permission); command.setPermission(permission);
if (permission != null && permissionDefault != null) { if (permission != null && permissionDefault != null) {
Permission perm = Bukkit.getPluginManager().getPermission(permission); Permission perm = Bukkit.getPluginManager().getPermission(permission);
if (perm != null && perm.getDefault() != permissionDefault) { if (perm != null && perm.getDefault() != permissionDefault) {
perm.setDefault(permissionDefault); perm.setDefault(permissionDefault);
} }
} }
command.setPermissionMessage(permissionMessage); command.setPermissionMessage(permissionMessage);
} }
} }
/** /**
* *
* @param commands Command white-list. * @param commands Command white-list.
* @param permissionBase * @param permissionBase
* @param ops * @param ops
* @return * @return
*/ */
public static List<CommandProtectionEntry> protectCommands(Collection<String> commands, String permissionBase, boolean ops) { public static List<CommandProtectionEntry> protectCommands(Collection<String> commands, String permissionBase, boolean ops) {
return protectCommands(permissionBase, commands, true, ops); return protectCommands(permissionBase, commands, true, ops);
} }
/** /**
* Set up the command protection with the "unknown command" message for the permission message (attempt to hide the command). * Set up the command protection with the "unknown command" message for the permission message (attempt to hide the command).
* @param permissionBase * @param permissionBase
* @param ignoredCommands * @param ignoredCommands
* @param invertIgnored * @param invertIgnored
* @param ops * @param ops
* @return * @return
*/ */
public static List<CommandProtectionEntry> protectCommands(String permissionBase, Collection<String> ignoredCommands, boolean invertIgnored, boolean ops) { public static List<CommandProtectionEntry> protectCommands(String permissionBase, Collection<String> ignoredCommands, boolean invertIgnored, boolean ops) {
return protectCommands(permissionBase, ignoredCommands, invertIgnored, ops, ColorUtil.replaceColors(ConfigManager.getConfigFile().getString(ConfPaths.PROTECT_PLUGINS_HIDE_NOCOMMAND_MSG))); return protectCommands(permissionBase, ignoredCommands, invertIgnored, ops, ColorUtil.replaceColors(ConfigManager.getConfigFile().getString(ConfPaths.PROTECT_PLUGINS_HIDE_NOCOMMAND_MSG)));
} }
/** /**
* Set up the command protection with the given permission message. * Set up the command protection with the given permission message.
* @param permissionBase * @param permissionBase
* @param ignoredCommands * @param ignoredCommands
* @param invertIgnored * @param invertIgnored
* @param ops * @param ops
* @param permissionMessage * @param permissionMessage
* @return * @return
*/ */
public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) { public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
final Set<String> checked = new HashSet<String>(); final Set<String> checked = new HashSet<String>();
for (String label : ignoredCommands) { for (String label : ignoredCommands) {
checked.add(CommandUtil.getCommandLabel(label, false)); checked.add(CommandUtil.getCommandLabel(label, false));
} }
final PluginManager pm = Bukkit.getPluginManager(); final PluginManager pm = Bukkit.getPluginManager();
Permission rootPerm = pm.getPermission(permissionBase); Permission rootPerm = pm.getPermission(permissionBase);
if (rootPerm == null) { if (rootPerm == null) {
rootPerm = new Permission(permissionBase); rootPerm = new Permission(permissionBase);
pm.addPermission(rootPerm); pm.addPermission(rootPerm);
} }
final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>(); final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
// Apply protection based on white-list or black-list. // Apply protection based on white-list or black-list.
for (final Command command : CommandUtil.getCommands()) { for (final Command command : CommandUtil.getCommands()) {
final String lcLabel = command.getLabel().trim().toLowerCase(); final String lcLabel = command.getLabel().trim().toLowerCase();
if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) { if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
if (!invertIgnored) { if (!invertIgnored) {
continue; continue;
} }
} }
else if (invertIgnored) { else if (invertIgnored) {
continue; continue;
} }
// Set the permission for the command. // Set the permission for the command.
String cmdPermName = command.getPermission(); String cmdPermName = command.getPermission();
boolean cmdHadPerm; boolean cmdHadPerm;
if (cmdPermName == null) { if (cmdPermName == null) {
// Set a permission. // Set a permission.
cmdPermName = permissionBase + "." + lcLabel; cmdPermName = permissionBase + "." + lcLabel;
command.setPermission(cmdPermName); command.setPermission(cmdPermName);
cmdHadPerm = false; cmdHadPerm = false;
} }
else{ else{
cmdHadPerm = true; cmdHadPerm = true;
} }
// Set permission default behavior. // Set permission default behavior.
Permission cmdPerm = pm.getPermission(cmdPermName); Permission cmdPerm = pm.getPermission(cmdPermName);
if (cmdPerm == null) { if (cmdPerm == null) {
if (!cmdHadPerm) { if (!cmdHadPerm) {
cmdPerm = new Permission(cmdPermName); cmdPerm = new Permission(cmdPermName);
cmdPerm.addParent(rootPerm, true); cmdPerm.addParent(rootPerm, true);
pm.addPermission(cmdPerm); pm.addPermission(cmdPerm);
} }
} }
// Create change history entry. // Create change history entry.
if (cmdHadPerm) { if (cmdHadPerm) {
if (cmdPerm == null) { if (cmdPerm == null) {
changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, null, command.getPermissionMessage())); changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, null, command.getPermissionMessage()));
} }
else { else {
changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage())); changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
} }
} }
else { else {
changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage())); changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage()));
} }
// Change // Change
cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE); cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
command.setPermissionMessage(permissionMessage); command.setPermissionMessage(permissionMessage);
} }
return changed; return changed;
} }
/** /**
* Check if the checked set contains any trim+lower-case alias of the command. * Check if the checked set contains any trim+lower-case alias of the command.
* @param checked * @param checked
* @param command * @param command
* @return * @return
*/ */
private static final boolean containsAnyAliases(final Set<String> checked, final Command command) { private static final boolean containsAnyAliases(final Set<String> checked, final Command command) {
final Collection<String> aliases = command.getAliases(); final Collection<String> aliases = command.getAliases();
if (aliases != null) { if (aliases != null) {
for (final String alias : aliases) { for (final String alias : aliases) {
if (checked.contains(alias.trim().toLowerCase())) { if (checked.contains(alias.trim().toLowerCase())) {
return true; return true;
} }
} }
} }
return false; return false;
} }
/** /**
* Set a permission as child for all the other permissions given in a Collection. * Set a permission as child for all the other permissions given in a Collection.
* @param permissions Not expected to exist. * @param permissions Not expected to exist.
* @param childPermissionName * @param childPermissionName
*/ */
public static void addChildPermission(final Collection<String> permissions, final String childPermissionName, final PermissionDefault permissionDefault) { public static void addChildPermission(final Collection<String> permissions, final String childPermissionName, final PermissionDefault permissionDefault) {
final PluginManager pm = Bukkit.getPluginManager(); final PluginManager pm = Bukkit.getPluginManager();
Permission childPermission = pm.getPermission(childPermissionName); Permission childPermission = pm.getPermission(childPermissionName);
if (childPermission == null) { if (childPermission == null) {
childPermission = new Permission(childPermissionName, "auto-generated child permission (NoCheatPlus)", permissionDefault); childPermission = new Permission(childPermissionName, "auto-generated child permission (NoCheatPlus)", permissionDefault);
pm.addPermission(childPermission); pm.addPermission(childPermission);
} }
for (final String permissionName : permissions) { for (final String permissionName : permissions) {
Permission permission = pm.getPermission(permissionName); Permission permission = pm.getPermission(permissionName);
if (permission == null) { if (permission == null) {
permission = new Permission(permissionName, "auto-generated permission (NoCheatPlus)", permissionDefault); permission = new Permission(permissionName, "auto-generated permission (NoCheatPlus)", permissionDefault);
pm.addPermission(permission); pm.addPermission(permission);
} }
if (!permission.getChildren().containsKey(childPermissionName)) { if (!permission.getChildren().containsKey(childPermissionName)) {
childPermission.addParent(permission, true); childPermission.addParent(permission, true);
} }
} }
} }
} }

View File

@ -14,57 +14,57 @@ import fr.neatmonster.nocheatplus.utilities.OnDemandTickListener;
*/ */
public class PlayerMessageSender extends OnDemandTickListener { public class PlayerMessageSender extends OnDemandTickListener {
private final class MessageEntry{ private final class MessageEntry{
public final String playerName; public final String playerName;
public final String message; public final String message;
public MessageEntry(final String playerName, final String message){ public MessageEntry(final String playerName, final String message){
this.playerName = playerName; this.playerName = playerName;
this.message = message; this.message = message;
} }
} }
/** Queued entries, also used as lock. */ /** Queued entries, also used as lock. */
private List<MessageEntry> messageEntries = new LinkedList<MessageEntry>(); private List<MessageEntry> messageEntries = new LinkedList<MessageEntry>();
@Override @Override
public boolean delegateTick(int tick, long timeLast) { public boolean delegateTick(int tick, long timeLast) {
// Copy entries. // Copy entries.
final List<MessageEntry> entries; final List<MessageEntry> entries;
synchronized (this) { synchronized (this) {
if (messageEntries.isEmpty()){ if (messageEntries.isEmpty()){
// Force unregister. // Force unregister.
unRegister(true); unRegister(true);
// Always continue here to never use external setRegistered. // Always continue here to never use external setRegistered.
return true; return true;
} }
entries = messageEntries; entries = messageEntries;
messageEntries = new LinkedList<PlayerMessageSender.MessageEntry>(); messageEntries = new LinkedList<PlayerMessageSender.MessageEntry>();
} }
// Do messaging. // Do messaging.
for (final MessageEntry entry : entries){ for (final MessageEntry entry : entries){
final Player player = DataManager.getPlayerExact(entry.playerName); final Player player = DataManager.getPlayerExact(entry.playerName);
if (player != null && player.isOnline()){ if (player != null && player.isOnline()){
player.sendMessage(entry.message); player.sendMessage(entry.message);
} }
} }
// Unregister if no further entries are there. // Unregister if no further entries are there.
synchronized (this) { synchronized (this) {
if (messageEntries.isEmpty()){ if (messageEntries.isEmpty()){
// Force unregister. // Force unregister.
unRegister(true); unRegister(true);
} }
} }
// Always continue here to never use external setRegistered. // Always continue here to never use external setRegistered.
return true; return true;
} }
public void sendMessageThreadSafe(final String playerName, final String message){ public void sendMessageThreadSafe(final String playerName, final String message){
final MessageEntry entry = new MessageEntry(playerName.toLowerCase(), message); final MessageEntry entry = new MessageEntry(playerName.toLowerCase(), message);
synchronized (this) { synchronized (this) {
messageEntries.add(entry); messageEntries.add(entry);
// Called register asynchronously, potentially. // Called register asynchronously, potentially.
register(); register();
} }
} }
} }

View File

@ -11,55 +11,55 @@ import fr.neatmonster.nocheatplus.utilities.OnDemandTickListener;
*/ */
public class PlayerTask extends OnDemandTickListener { public class PlayerTask extends OnDemandTickListener {
// TODO: Consider overriding some logic, because it is used in the main thread only (context: isRegisterd + register). // TODO: Consider overriding some logic, because it is used in the main thread only (context: isRegisterd + register).
public final String lcName; public final String lcName;
protected boolean updateInventory = false; protected boolean updateInventory = false;
// protected boolean correctDirection = false; // protected boolean correctDirection = false;
/** /**
* *
* @param name Not demanded to be case sensitive. * @param name Not demanded to be case sensitive.
*/ */
public PlayerTask(final String name) { public PlayerTask(final String name) {
this.lcName = name.toLowerCase(); this.lcName = name.toLowerCase();
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public boolean delegateTick(final int tick, final long timeLast) { public boolean delegateTick(final int tick, final long timeLast) {
final Player player = DataManager.getPlayer(lcName); final Player player = DataManager.getPlayer(lcName);
if (player != null) { if (player != null) {
if (player.isOnline()) { if (player.isOnline()) {
// if (correctDirection) { // if (correctDirection) {
// final MCAccess access = NCPAPIProvider.getNoCheatPlusAPI().getMCAccess(); // final MCAccess access = NCPAPIProvider.getNoCheatPlusAPI().getMCAccess();
// access.correctDirection(player); // access.correctDirection(player);
// } // }
if (updateInventory) { if (updateInventory) {
player.updateInventory(); player.updateInventory();
} }
} }
} }
// Reset values (players logging back in should be fine or handled differently). // Reset values (players logging back in should be fine or handled differently).
updateInventory = false; updateInventory = false;
// correctDirection = false; // correctDirection = false;
return false; return false;
} }
public void updateInventory() { public void updateInventory() {
// TODO: Might not allow registering every tick. // TODO: Might not allow registering every tick.
updateInventory = true; updateInventory = true;
register(); register();
} }
// public void correctDirection() { // public void correctDirection() {
// correctDirection = true; // correctDirection = true;
// register(); // register();
// } // }
// TODO: updateHunger // TODO: updateHunger
} }