Some small fixes to permissions to register the correct Permissible in base and more accurate reverse lookups

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-09-02 23:39:01 +01:00
parent 2ba705febc
commit 1d6801b8e6
3 changed files with 43 additions and 27 deletions

View file

@ -1,27 +1,31 @@
package org.bukkit.command;
import org.bukkit.command.defaults.ReloadCommand;
import org.bukkit.command.defaults.PluginsCommand;
import org.bukkit.command.defaults.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import static org.bukkit.util.Java15Compat.Arrays_copyOfRange;
public final class SimpleCommandMap implements CommandMap {
private final Map<String, Command> knownCommands = new HashMap<String, Command>();
private final Set<String> aliases = new HashSet<String>();
private final Server server;
private static final Set<Command> fallbackCommands = new HashSet<Command>();
static {
fallbackCommands.add(new ListCommand());
fallbackCommands.add(new StopCommand());
fallbackCommands.add(new SaveCommand());
fallbackCommands.add(new SaveOnCommand());
fallbackCommands.add(new SaveOffCommand());
fallbackCommands.add(new OpCommand());
fallbackCommands.add(new DeopCommand());
}
public SimpleCommandMap(final Server server) {
this.server = server;
@ -110,6 +114,16 @@ public final class SimpleCommandMap implements CommandMap {
return registerdPassedLabel;
}
private Command getFallback(String label) {
for (Command cmd : fallbackCommands) {
if (label.startsWith(cmd.getName().toLowerCase())) {
return cmd;
}
}
return null;
}
/**
* {@inheritDoc}
*/
@ -122,6 +136,9 @@ public final class SimpleCommandMap implements CommandMap {
String sentCommandLabel = args[0].toLowerCase();
Command target = getCommand(sentCommandLabel);
if (target == null) {
target = getFallback(sentCommandLabel);
}
if (target == null) {
return false;
}

View file

@ -7,23 +7,15 @@ import org.bukkit.Instrument;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Note;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
import org.bukkit.map.MapView;
/**
* Represents a player, connected or not
*
*/
public interface Player extends HumanEntity, CommandSender {
/**
* Checks if this player is currently online
*
* @return true if they are online
*/
public boolean isOnline();
public interface Player extends HumanEntity, OfflinePlayer {
/**
* Gets the "friendly" name to display of this player. This may include color.
*

View file

@ -15,12 +15,19 @@ import org.bukkit.plugin.Plugin;
*/
public class PermissibleBase implements Permissible {
private ServerOperator opable = null;
private Permissible parent = this;
private final List<PermissionAttachment> attachments = new LinkedList<PermissionAttachment>();
private final Map<String, PermissionAttachmentInfo> permissions = new HashMap<String, PermissionAttachmentInfo>();
private boolean dirtyPermissions = true;
public PermissibleBase(ServerOperator opable) {
this.opable = opable;
if (opable instanceof Permissible) {
this.parent = (Permissible)opable;
}
calculatePermissions();
}
public boolean isOp() {
@ -121,7 +128,7 @@ public class PermissibleBase implements Permissible {
throw new IllegalArgumentException("Plugin " + plugin.getDescription().getFullName() + " is disabled");
}
PermissionAttachment result = new PermissionAttachment(plugin, this);
PermissionAttachment result = new PermissionAttachment(plugin, parent);
attachments.add(result);
recalculatePermissions();
@ -144,7 +151,7 @@ public class PermissibleBase implements Permissible {
recalculatePermissions();
} else {
throw new IllegalArgumentException("Given attachment is not part of Permissible object " + this);
throw new IllegalArgumentException("Given attachment is not part of Permissible object " + parent);
}
}
@ -156,12 +163,12 @@ public class PermissibleBase implements Permissible {
if (dirtyPermissions) {
clearPermissions();
Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(isOp());
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(isOp(), this);
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(isOp(), parent);
for (Permission perm : defaults) {
String name = perm.getName().toLowerCase();
permissions.put(name, new PermissionAttachmentInfo(this, name, null, true));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, this);
permissions.put(name, new PermissionAttachmentInfo(parent, name, null, true));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, parent);
calculateChildPermissions(perm.getChildren(), false, null);
}
@ -177,10 +184,10 @@ public class PermissibleBase implements Permissible {
Set<String> perms = permissions.keySet();
for (String name : perms) {
Bukkit.getServer().getPluginManager().unsubscribeFromPermission(name, this);
Bukkit.getServer().getPluginManager().unsubscribeFromPermission(name, parent);
}
Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(isOp(), this);
Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(isOp(), parent);
permissions.clear();
}
@ -193,8 +200,8 @@ public class PermissibleBase implements Permissible {
boolean value = children.get(name) ^ invert;
String lname = name.toLowerCase();
permissions.put(lname, new PermissionAttachmentInfo(this, lname, attachment, value));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, this);
permissions.put(lname, new PermissionAttachmentInfo(parent, lname, attachment, value));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, parent);
if (perm != null) {
calculateChildPermissions(perm.getChildren(), !value, attachment);
@ -230,7 +237,7 @@ public class PermissibleBase implements Permissible {
PermissionAttachment result = addAttachment(plugin);
if (Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveAttachmentRunnable(result), ticks) == -1) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add PermissionAttachment to " + this + " for plugin " + plugin.getDescription().getFullName() + ": Scheduler returned -1");
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add PermissionAttachment to " + parent + " for plugin " + plugin.getDescription().getFullName() + ": Scheduler returned -1");
result.remove();
return null;
} else {