Added permissions and configuration support.

This commit is contained in:
sk89q 2011-01-16 13:19:29 -08:00
parent 94b2b4713e
commit e6c101a307
7 changed files with 171 additions and 22 deletions

View File

@ -104,4 +104,13 @@ public class ConfigurationPermissionsResolver {
return groups.contains(group); return groups.contains(group);
} }
public String[] getGroups(String player) {
Set<String> groups = userGroups.get(player.toLowerCase());
if (groups == null) {
return new String[0];
}
return (String[])groups.toArray();
}
} }

View File

@ -0,0 +1,103 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Integer>();
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);
}
}
}
}

View File

@ -27,9 +27,11 @@ import com.sk89q.worldedit.bags.BlockBag;
public class BukkitPlayer extends LocalPlayer { public class BukkitPlayer extends LocalPlayer {
private Player player; private Player player;
private WorldEditPlugin plugin;
public BukkitPlayer(ServerInterface server, Player player) { public BukkitPlayer(WorldEditPlugin plugin, ServerInterface server, Player player) {
super(server); super(server);
this.plugin = plugin;
this.player = player; this.player = player;
} }
@ -109,8 +111,7 @@ public class BukkitPlayer extends LocalPlayer {
@Override @Override
public String[] getGroups() { public String[] getGroups() {
// TODO Auto-generated method stub return plugin.getGroups(player);
return null;
} }
@Override @Override
@ -121,8 +122,7 @@ public class BukkitPlayer extends LocalPlayer {
@Override @Override
public boolean hasPermission(String perm) { public boolean hasPermission(String perm) {
// TODO Auto-generated method stub return plugin.hasPermission(player, "/" + perm);
return true;
} }
@Override @Override

View File

@ -36,6 +36,6 @@ public class WorldEditAPI {
* @return * @return
*/ */
public LocalSession getSession(Player player) { public LocalSession getSession(Player player) {
return plugin.controller.getSession(new BukkitPlayer(plugin.server, player)); return plugin.controller.getSession(new BukkitPlayer(plugin, plugin.server, player));
} }
} }

View File

@ -72,6 +72,6 @@ public class WorldEditBlockListener extends BlockListener {
} }
private BukkitPlayer wrapPlayer(Player player) { private BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(plugin.server, player); return new BukkitPlayer(plugin, plugin.server, player);
} }
} }

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import org.bukkit.*;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
@ -58,13 +57,31 @@ public class WorldEditPlayerListener extends PlayerListener {
* @param event Relevant event details * @param event Relevant event details
*/ */
public void onPlayerCommand(PlayerChatEvent event) { public void onPlayerCommand(PlayerChatEvent event) {
if (plugin.controller.handleCommand(wrapPlayer(event.getPlayer()), String[] split = event.getMessage().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); event.setCancelled(true);
} }
} }
private BukkitPlayer wrapPlayer(Player player) { private BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(plugin.server, player); return new BukkitPlayer(plugin, plugin.server, player);
} }
} }

View File

@ -20,13 +20,15 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.logging.Logger;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader; import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.bukkit.migration.ConfigurationPermissionsResolver;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
/** /**
@ -35,37 +37,37 @@ import com.sk89q.worldedit.*;
* @author sk89qs * @author sk89qs
*/ */
public class WorldEditPlugin extends JavaPlugin { public class WorldEditPlugin extends JavaPlugin {
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
public final ServerInterface server; public final ServerInterface server;
public final WorldEditController controller; public final WorldEditController controller;
public final WorldEditAPI api; public final WorldEditAPI api;
private final LocalConfiguration config;
private final WorldEditPlayerListener playerListener = private final WorldEditPlayerListener playerListener =
new WorldEditPlayerListener(this); new WorldEditPlayerListener(this);
private final WorldEditBlockListener blockListener = private final WorldEditBlockListener blockListener =
new WorldEditBlockListener(this); new WorldEditBlockListener(this);
private final ConfigurationPermissionsResolver perms;
public WorldEditPlugin(PluginLoader pluginLoader, Server instance, public WorldEditPlugin(PluginLoader pluginLoader, Server instance,
PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) { PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
super(pluginLoader, instance, desc, folder, plugin, cLoader); super(pluginLoader, instance, desc, folder, plugin, cLoader);
LocalConfiguration config = new LocalConfiguration() { logger.info("WorldEdit " + desc.getVersion() + " loaded.");
@Override
public void load() { config = new BukkitConfiguration(getConfiguration(), logger);
// TODO Auto-generated method stub perms = new ConfigurationPermissionsResolver(getConfiguration());
disallowedBlocks = new HashSet<Integer>(); loadConfiguration();
}
};
server = new BukkitServerInterface(getServer()); server = new BukkitServerInterface(getServer());
controller = new WorldEditController(server, config); controller = new WorldEditController(server, config);
api = new WorldEditAPI(this); api = new WorldEditAPI(this);
registerEvents(); registerEvents();
} }
public void onEnable() { public void onEnable() {
//loadConfiguration();
} }
public void onDisable() { public void onDisable() {
@ -83,6 +85,24 @@ public class WorldEditPlugin extends JavaPlugin {
blockListener, Priority.Normal, this); 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() { public WorldEditAPI getAPI() {
return api; return api;
} }