From e6c101a3071943be0673c87a42919b14bf9fd770 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 16 Jan 2011 13:19:29 -0800 Subject: [PATCH] Added permissions and configuration support. --- .../ConfigurationPermissionsResolver.java | 9 ++ .../worldedit/bukkit/BukkitConfiguration.java | 103 ++++++++++++++++++ .../sk89q/worldedit/bukkit/BukkitPlayer.java | 10 +- .../sk89q/worldedit/bukkit/WorldEditAPI.java | 2 +- .../bukkit/WorldEditBlockListener.java | 2 +- .../bukkit/WorldEditPlayerListener.java | 25 ++++- .../worldedit/bukkit/WorldEditPlugin.java | 42 +++++-- 7 files changed, 171 insertions(+), 22 deletions(-) create mode 100644 src/com/sk89q/worldedit/bukkit/BukkitConfiguration.java diff --git a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java index 328448084..7029af233 100644 --- a/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java +++ b/src/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -104,4 +104,13 @@ public class ConfigurationPermissionsResolver { return groups.contains(group); } + + public String[] getGroups(String player) { + Set groups = userGroups.get(player.toLowerCase()); + if (groups == null) { + return new String[0]; + } + + return (String[])groups.toArray(); + } } diff --git a/src/com/sk89q/worldedit/bukkit/BukkitConfiguration.java b/src/com/sk89q/worldedit/bukkit/BukkitConfiguration.java new file mode 100644 index 000000000..c80272237 --- /dev/null +++ b/src/com/sk89q/worldedit/bukkit/BukkitConfiguration.java @@ -0,0 +1,103 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit.bukkit; + +import java.io.IOException; +import java.util.HashSet; +import java.util.logging.FileHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.util.config.Configuration; +import com.sk89q.util.StringUtil; +import com.sk89q.worldedit.LocalConfiguration; +import com.sk89q.worldedit.LogFormat; +import com.sk89q.worldedit.snapshots.SnapshotRepository; + +public class BukkitConfiguration extends LocalConfiguration { + private Configuration config; + private Logger logger; + + public BukkitConfiguration(Configuration config, Logger logger) { + this.config = config; + this.logger = logger; + } + + @Override + public void load() { + profile = config.getBoolean("debug", profile); + wandItem = config.getInt("wand-item", wandItem); + defaultChangeLimit = Math.max(-1, config.getInt( + "limits.max-blocks-changed.default", defaultChangeLimit)); + maxChangeLimit = Math.max(-1, + config.getInt("limits.max-blocks-changed.maximum", maxChangeLimit)); + maxRadius = Math.max(-1, config.getInt("limits.max-radius", maxRadius)); + maxSuperPickaxeSize = Math.max(1, config.getInt( + "limits.max-super-pickaxe-size", maxSuperPickaxeSize)); + registerHelp = true; + logComands = config.getBoolean("logging.log-commands", logComands); + superPickaxeDrop = config.getBoolean("super-pickaxe.drop-items", + superPickaxeDrop); + superPickaxeManyDrop = config.getBoolean( + "super-pickaxe.many-drop-items", superPickaxeManyDrop); + noDoubleSlash = config.getBoolean("no-double-slash", noDoubleSlash); + useInventory = config.getBoolean("use-inventory.enable", useInventory); + useInventoryOverride = config.getBoolean("use-inventory.allow-override", + useInventoryOverride); + maxBrushRadius = config.getInt("limits.max-brush-radius", maxBrushRadius); + + // Get disallowed blocks + disallowedBlocks = new HashSet(); + String defdisallowedBlocks = StringUtil.joinString(defaultDisallowedBlocks, ",", 0); + for (String b : config.getString("limits.disallowed-blocks", + defdisallowedBlocks).split(",")) { + try { + disallowedBlocks.add(Integer.parseInt(b)); + } catch (NumberFormatException e) { + } + } + + String snapshotsDir = config.getString("snapshots.directry", ""); + if (!snapshotsDir.trim().equals("")) { + snapshotRepo = new SnapshotRepository(snapshotsDir); + } else { + snapshotRepo = null; + } + + String type = config.getString("shell-save-type", "").trim(); + shellSaveType = type.equals("") ? null : type; + + String logFile = config.getString("logging.file", ""); + if (!logFile.equals("")) { + try { + FileHandler handler = new FileHandler(logFile, true); + handler.setFormatter(new LogFormat()); + logger.addHandler(handler); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not use log file " + logFile + ": " + + e.getMessage()); + } + } else { + for (Handler handler : logger.getHandlers()) { + logger.removeHandler(handler); + } + } + } +} diff --git a/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java index afb453cb8..a2bfb413d 100644 --- a/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/src/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -27,9 +27,11 @@ import com.sk89q.worldedit.bags.BlockBag; public class BukkitPlayer extends LocalPlayer { private Player player; + private WorldEditPlugin plugin; - public BukkitPlayer(ServerInterface server, Player player) { + public BukkitPlayer(WorldEditPlugin plugin, ServerInterface server, Player player) { super(server); + this.plugin = plugin; this.player = player; } @@ -109,8 +111,7 @@ public class BukkitPlayer extends LocalPlayer { @Override public String[] getGroups() { - // TODO Auto-generated method stub - return null; + return plugin.getGroups(player); } @Override @@ -121,8 +122,7 @@ public class BukkitPlayer extends LocalPlayer { @Override public boolean hasPermission(String perm) { - // TODO Auto-generated method stub - return true; + return plugin.hasPermission(player, "/" + perm); } @Override diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditAPI.java b/src/com/sk89q/worldedit/bukkit/WorldEditAPI.java index 4d7b24434..b0b9484f5 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditAPI.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditAPI.java @@ -36,6 +36,6 @@ public class WorldEditAPI { * @return */ public LocalSession getSession(Player player) { - return plugin.controller.getSession(new BukkitPlayer(plugin.server, player)); + return plugin.controller.getSession(new BukkitPlayer(plugin, plugin.server, player)); } } diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditBlockListener.java b/src/com/sk89q/worldedit/bukkit/WorldEditBlockListener.java index f2e287e7b..9aa947321 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditBlockListener.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditBlockListener.java @@ -72,6 +72,6 @@ public class WorldEditBlockListener extends BlockListener { } private BukkitPlayer wrapPlayer(Player player) { - return new BukkitPlayer(plugin.server, player); + return new BukkitPlayer(plugin, plugin.server, player); } } diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java index ab2f2608b..aa07df118 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlayerListener.java @@ -19,7 +19,6 @@ package com.sk89q.worldedit.bukkit; -import org.bukkit.*; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerEvent; @@ -58,13 +57,31 @@ public class WorldEditPlayerListener extends PlayerListener { * @param event Relevant event details */ public void onPlayerCommand(PlayerChatEvent event) { - if (plugin.controller.handleCommand(wrapPlayer(event.getPlayer()), - event.getMessage().split(" "))) { + String[] split = event.getMessage().split(" "); + + if (split[0].equalsIgnoreCase("/reload") + && plugin.hasPermission(event.getPlayer(), "/reload") + && split.length > 1) { + if (split[1].equalsIgnoreCase("WorldEdit")) { + try { + plugin.loadConfiguration(); + event.getPlayer().sendMessage("WorldEdit configuration reloaded."); + } catch (Throwable t) { + event.getPlayer().sendMessage("Error while reloading: " + + t.getMessage()); + } + + event.setCancelled(true); + return; + } + } + + if (plugin.controller.handleCommand(wrapPlayer(event.getPlayer()), split)) { event.setCancelled(true); } } private BukkitPlayer wrapPlayer(Player player) { - return new BukkitPlayer(plugin.server, player); + return new BukkitPlayer(plugin, plugin.server, player); } } diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 39d2b06d9..85a539d7b 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -20,13 +20,15 @@ package com.sk89q.worldedit.bukkit; import java.io.File; -import java.util.HashSet; +import java.util.logging.Logger; import org.bukkit.Server; +import org.bukkit.entity.Player; import org.bukkit.event.Event.Priority; import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.java.JavaPlugin; +import com.sk89q.bukkit.migration.ConfigurationPermissionsResolver; import com.sk89q.worldedit.*; /** @@ -35,37 +37,37 @@ import com.sk89q.worldedit.*; * @author sk89qs */ public class WorldEditPlugin extends JavaPlugin { + private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit"); + public final ServerInterface server; public final WorldEditController controller; public final WorldEditAPI api; + private final LocalConfiguration config; private final WorldEditPlayerListener playerListener = new WorldEditPlayerListener(this); private final WorldEditBlockListener blockListener = new WorldEditBlockListener(this); + private final ConfigurationPermissionsResolver perms; public WorldEditPlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) { super(pluginLoader, instance, desc, folder, plugin, cLoader); - - LocalConfiguration config = new LocalConfiguration() { - @Override - public void load() { - // TODO Auto-generated method stub - disallowedBlocks = new HashSet(); - } - }; + logger.info("WorldEdit " + desc.getVersion() + " loaded."); + + config = new BukkitConfiguration(getConfiguration(), logger); + perms = new ConfigurationPermissionsResolver(getConfiguration()); + loadConfiguration(); + server = new BukkitServerInterface(getServer()); controller = new WorldEditController(server, config); - api = new WorldEditAPI(this); registerEvents(); } public void onEnable() { - //loadConfiguration(); } public void onDisable() { @@ -83,6 +85,24 @@ public class WorldEditPlugin extends JavaPlugin { blockListener, Priority.Normal, this); } + public void loadConfiguration() { + getConfiguration().load(); + config.load(); + perms.load(); + } + + String[] getGroups(Player player) { + return perms.getGroups(player.getName()); + } + + boolean inGroup(Player player, String group) { + return perms.inGroup(player.getName(), group); + } + + boolean hasPermission(Player player, String perm) { + return perms.hasPermission(player.getName(), perm); + } + public WorldEditAPI getAPI() { return api; }