From 5d2a1958358cb86643ae16ad4f65c650678a1983 Mon Sep 17 00:00:00 2001 From: Dinnerbone Date: Sun, 17 Jul 2011 17:19:41 +0100 Subject: [PATCH] Implemented new permission system --- pom.xml | 7 +++ .../server/ServerConfigurationManager.java | 14 +++++ .../org/bukkit/craftbukkit/CraftServer.java | 63 +++++++++++++++++++ .../craftbukkit/entity/CraftHumanEntity.java | 61 ++++++++++++++++++ .../craftbukkit/entity/CraftPlayer.java | 7 ++- 5 files changed, 151 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 006c141515..be842a4b7f 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,13 @@ jar provided + + org.yaml + snakeyaml + 1.7 + jar + provided + com.google.guava guava-collections diff --git a/src/main/java/net/minecraft/server/ServerConfigurationManager.java b/src/main/java/net/minecraft/server/ServerConfigurationManager.java index 5b41812a0e..0d8d621990 100644 --- a/src/main/java/net/minecraft/server/ServerConfigurationManager.java +++ b/src/main/java/net/minecraft/server/ServerConfigurationManager.java @@ -431,11 +431,25 @@ public class ServerConfigurationManager { public void e(String s) { this.h.add(s.toLowerCase()); this.l(); + + // Craftbukkit start + Player player = server.server.getPlayer(s); + if (player != null) { + player.recalculatePermissions(); + } + // Craftbukkit end } public void f(String s) { this.h.remove(s.toLowerCase()); this.l(); + + // Craftbukkit start + Player player = server.server.getPlayer(s); + if (player != null) { + player.recalculatePermissions(); + } + // Craftbukkit end } private void k() { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 4b5e738d58..692fdd6d11 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit; +import java.io.FileNotFoundException; import org.bukkit.generator.ChunkGenerator; import com.avaje.ebean.config.DataSourceConfig; import com.avaje.ebean.config.ServerConfig; @@ -18,12 +19,14 @@ import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; import java.io.File; +import java.io.FileInputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import jline.ConsoleReader; @@ -58,9 +61,13 @@ import org.bukkit.craftbukkit.command.ServerCommandListener; import org.bukkit.scheduler.BukkitWorker; import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.event.world.WorldInitEvent; +import org.bukkit.permissions.Permission; import org.bukkit.plugin.PluginLoadOrder; import org.bukkit.util.config.Configuration; import org.bukkit.util.config.ConfigurationNode; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.SafeConstructor; +import org.yaml.snakeyaml.error.MarkedYAMLException; public final class CraftServer implements Server { private final String serverName = "Craftbukkit"; @@ -74,6 +81,7 @@ public final class CraftServer implements Server { protected final ServerConfigurationManager server; private final Map worlds = new LinkedHashMap(); private final Configuration configuration; + private final Yaml yaml = new Yaml(new SafeConstructor()); public CraftServer(MinecraftServer console, ServerConfigurationManager server) { this.console = console; @@ -101,6 +109,8 @@ public final class CraftServer implements Server { configuration.getString("settings.update-folder", "update"); configuration.getInt("settings.spawn-radius", 16); + configuration.getString("settings.permissions-file", "permissions.yml"); + if (configuration.getNode("aliases") == null) { List icanhasbukkit = new ArrayList(); icanhasbukkit.add("version"); @@ -139,6 +149,7 @@ public final class CraftServer implements Server { if (type == PluginLoadOrder.POSTWORLD) { commandMap.registerServerAliases(); + loadCustomPermissions(); } } @@ -149,6 +160,16 @@ public final class CraftServer implements Server { private void loadPlugin(Plugin plugin) { try { pluginManager.enablePlugin(plugin); + + List perms = plugin.getDescription().getPermissions(); + + for (Permission perm : perms) { + try { + pluginManager.addPermission(perm); + } catch (IllegalArgumentException ex) { + getLogger().log(Level.WARNING, "Plugin " + plugin.getDescription().getFullName() + " tried to register permission '" + perm.getName() + "' but it's already registered", ex); + } + } } catch (Throwable ex) { Logger.getLogger(CraftServer.class.getName()).log(Level.SEVERE, ex.getMessage() + " loading " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } @@ -359,6 +380,48 @@ public final class CraftServer implements Server { enablePlugins(PluginLoadOrder.POSTWORLD); } + private void loadCustomPermissions() { + File file = new File(configuration.getString("settings.permissions-file")); + FileInputStream stream; + + try { + stream = new FileInputStream(file); + } catch (FileNotFoundException ex) { + try { + file.createNewFile(); + } finally { + return; + } + } + + Map> perms; + + try { + perms = (Map>)yaml.load(stream); + } catch (MarkedYAMLException ex) { + getLogger().log(Level.WARNING, "Server permissions file " + file + " is not valid YAML: " + ex.toString()); + return; + } catch (Throwable ex) { + getLogger().log(Level.WARNING, "Server permissions file " + file + " is not valid YAML.", ex); + return; + } + + if (perms == null) { + getLogger().log(Level.INFO, "Server permissions file " + file + " is empty, ignoring it"); + return; + } + + Set keys = perms.keySet(); + + for (String name : keys) { + try { + pluginManager.addPermission(Permission.loadPermission(name, perms.get(name))); + } catch (Throwable ex) { + Bukkit.getServer().getLogger().log(Level.SEVERE, "Permission node '" + name + "' in server config is invalid", ex); + } + } + } + @Override public String toString() { return "CraftServer{" + "serverName=" + serverName + ",serverVersion=" + serverVersion + ",protocolVersion=" + protocolVersion + '}'; diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index 04723c3542..2e7ee5f56c 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -1,15 +1,23 @@ package org.bukkit.craftbukkit.entity; +import java.util.Set; import net.minecraft.server.EntityHuman; import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer; import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.permissions.PermissibleBase; +import org.bukkit.permissions.Permission; +import org.bukkit.permissions.PermissionAttachment; +import org.bukkit.permissions.PermissionAttachmentInfo; +import org.bukkit.plugin.Plugin; public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { private CraftInventoryPlayer inventory; + private final PermissibleBase perm = new PermissibleBase(this); + private boolean op; public CraftHumanEntity(final CraftServer server, final EntityHuman entity) { super(server, entity); @@ -55,4 +63,57 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { public int getSleepTicks() { return getHandle().sleepTicks; } + + public boolean isOp() { + return op; + } + + public boolean isPermissionSet(String name) { + return perm.isPermissionSet(name); + } + + public boolean isPermissionSet(Permission perm) { + return this.perm.isPermissionSet(perm); + } + + public boolean hasPermission(String name) { + return perm.hasPermission(name); + } + + public boolean hasPermission(Permission perm) { + return this.perm.hasPermission(perm); + } + + public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) { + return perm.addAttachment(plugin, name, value); + } + + public PermissionAttachment addAttachment(Plugin plugin) { + return perm.addAttachment(plugin); + } + + public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) { + return perm.addAttachment(plugin, name, value, ticks); + } + + public PermissionAttachment addAttachment(Plugin plugin, int ticks) { + return perm.addAttachment(plugin, ticks); + } + + public void removeAttachment(PermissionAttachment attachment) { + perm.removeAttachment(attachment); + } + + public void recalculatePermissions() { + perm.recalculatePermissions(); + } + + public void setOp(boolean value) { + this.op = value; + recalculatePermissions(); + } + + public Set getEffectivePermissions() { + return perm.getEffectivePermissions(); + } } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 7d1c2b711a..39240809ed 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -25,15 +25,20 @@ import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; public class CraftPlayer extends CraftHumanEntity implements Player { - public CraftPlayer(CraftServer server, EntityPlayer entity) { super(server, entity); } + @Override public boolean isOp() { return server.getHandle().isOp(getName()); } + @Override + public void setOp(boolean value) { + server.getHandle().e(getName()); + } + public boolean isPlayer() { return true; }