Cleaned up code.

This commit is contained in:
sk89q 2011-02-21 22:28:23 -08:00
parent dffd3682b2
commit 19deb404ee
3 changed files with 145 additions and 78 deletions

View File

@ -19,13 +19,7 @@
package com.sk89q.worldedit;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.*;
import java.util.logging.Logger;
import java.io.*;
import javax.script.ScriptException;
@ -37,13 +31,8 @@ import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.commands.*;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.scripting.*;
import com.sk89q.worldedit.tools.BlockTool;
import com.sk89q.worldedit.tools.Tool;
import com.sk89q.worldedit.tools.TraceTool;
import com.sk89q.worldedit.masks.BlockTypeMask;
import com.sk89q.worldedit.masks.ExistingBlockMask;
import com.sk89q.worldedit.masks.InvertedBlockTypeMask;
import com.sk89q.worldedit.masks.Mask;
import com.sk89q.worldedit.tools.*;
import com.sk89q.worldedit.masks.*;
import com.sk89q.worldedit.patterns.*;
/**
@ -55,7 +44,14 @@ import com.sk89q.worldedit.patterns.*;
* @author sk89q
*/
public class WorldEdit {
/**
* Logger for debugging.
*/
public static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Holds WorldEdit's version.
*/
private static String version;
/**
@ -881,7 +877,7 @@ public class WorldEdit {
try {
if (config.logCommands) {
logger.log(Level.INFO, "WorldEdit: " + player.getName() + ": "
logger.info("WorldEdit: " + player.getName() + ": "
+ StringUtil.joinString(split, " "));
}

View File

@ -96,7 +96,7 @@ public class BukkitPlayer extends LocalPlayer {
@Override
public String[] getGroups() {
return plugin.getGroups(player);
return plugin.getPermissionsResolver().getGroups(player.getName());
}
@Override
@ -106,7 +106,8 @@ public class BukkitPlayer extends LocalPlayer {
@Override
public boolean hasPermission(String perm) {
return plugin.hasPermission(player, perm);
return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp())
|| plugin.getPermissionsResolver().hasPermission(player.getName(), perm);
}
@Override

View File

@ -29,6 +29,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.bukkit.migration.PermissionsResolverManager;
import com.sk89q.bukkit.migration.PermissionsResolverServerListener;
@ -39,71 +42,134 @@ import com.sk89q.worldedit.regions.Region;
/**
* Plugin for Bukkit.
*
* @author sk89qs
* @author sk89q
*/
public class WorldEditPlugin extends JavaPlugin {
/**
* WorldEdit messages get sent here.
*/
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
ServerInterface server;
WorldEdit controller;
WorldEditAPI api;
/**
* The server interface that all server-related API goes through.
*/
protected ServerInterface server;
/**
* Main WorldEdit instance.
*/
protected WorldEdit controller;
/**
* Deprecated API.
*/
protected WorldEditAPI api;
/**
* Holds the configuration for WorldEdit.
*/
private BukkitConfiguration config;
/**
* The permissions resolver in use.
*/
private PermissionsResolverManager perms;
private WorldEditPlayerListener playerListener =
new WorldEditPlayerListener(this);
private WorldEditBlockListener blockListener =
new WorldEditBlockListener(this);
private PermissionsResolverServerListener permsListener;
/**
* Called on plugin enable.
*/
public void onEnable() {
logger.info("WorldEdit " + getDescription().getVersion() + " loaded.");
logger.info("WorldEdit " + getDescription().getVersion() + " enabled.");
// Make the data folders that WorldEdit uses
getDataFolder().mkdirs();
// Create the default configuration file
createDefaultConfiguration("config.yml");
// Set up configuration and such, including the permissions
// resolver
config = new BukkitConfiguration(getConfiguration(), logger);
perms = new PermissionsResolverManager(getConfiguration(), getServer(),
"WorldEdit", logger);
permsListener = new PermissionsResolverServerListener(perms);
perms = new PermissionsResolverManager(
getConfiguration(), getServer(), "WorldEdit", logger);
// Load the configuration
loadConfiguration();
// Setup interfaces
server = new BukkitServerInterface(this, getServer());
controller = new WorldEdit(server, config);
api = new WorldEditAPI(this);
// Now we can register events!
registerEvents();
}
/**
* Called on plugin disable.
*/
public void onDisable() {
controller.clearSessions();
}
/**
* Loads and reloads all configuration.
*/
protected void loadConfiguration() {
getConfiguration().load();
config.load();
perms.load();
}
private void registerEvents() {
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT,
playerListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_ANIMATION,
playerListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_ITEM,
playerListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS,
playerListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.BLOCK_DAMAGED,
blockListener, Priority.Normal, this);
getServer().getPluginManager().registerEvent(Event.Type.BLOCK_RIGHTCLICKED,
blockListener, Priority.Normal, this);
/**
* Register the events used by WorldEdit.
*/
protected void registerEvents() {
PlayerListener playerListener = new WorldEditPlayerListener(this);
BlockListener blockListener = new WorldEditBlockListener(this);
permsListener.register(this);
registerEvent(Event.Type.PLAYER_QUIT, playerListener);
registerEvent(Event.Type.PLAYER_ANIMATION, playerListener);
registerEvent(Event.Type.PLAYER_ITEM, playerListener);
registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener);
registerEvent(Event.Type.BLOCK_DAMAGED, blockListener);
registerEvent(Event.Type.BLOCK_RIGHTCLICKED, blockListener);
// The permissions resolver has some hooks of its own
(new PermissionsResolverServerListener(perms)).register(this);
}
private void createDefaultConfiguration(String name) {
/**
* Register an event.
*
* @param type
* @param listener
* @param priority
*/
protected void registerEvent(Event.Type type, Listener listener, Priority priority) {
getServer().getPluginManager()
.registerEvent(type, listener, priority, this);
}
/**
* Register an event at normal priority.
*
* @param type
* @param listener
*/
protected void registerEvent(Event.Type type, Listener listener) {
getServer().getPluginManager()
.registerEvent(type, listener, Priority.Normal, this);
}
/**
* Create a default configuration file from the .jar.
*
* @param name
*/
protected void createDefaultConfiguration(String name) {
File actual = new File(getDataFolder(), name);
if (!actual.exists()) {
InputStream input =
WorldEditPlugin.class.getResourceAsStream("/defaults/" + name);
this.getClass().getResourceAsStream("/defaults/" + name);
if (input != null) {
FileOutputStream output = null;
@ -115,8 +181,8 @@ public class WorldEditPlugin extends JavaPlugin {
output.write(buf, 0, length);
}
logger.info("WorldEdit: Default configuration file written: "
+ name);
logger.info(getDescription().getName()
+ ": Default configuration file written: " + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
@ -133,35 +199,23 @@ public class WorldEditPlugin extends JavaPlugin {
}
}
}
void loadConfiguration() {
getConfiguration().load();
config.load();
perms.load();
}
/**
* Called on WorldEdit command.
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd,
String commandLabel, String[] args) {
// Since WorldEdit is primarily made for use in-game, we're going
// to ignore the situation where the command sender is not aplayer.
if (!(sender instanceof Player)) {
return true;
}
Player player = (Player)sender;
if (cmd.getName().equalsIgnoreCase("reloadwe")
&& hasPermission(player, "worldedit.reload")) {
try {
loadConfiguration();
player.sendMessage("WorldEdit configuration reloaded.");
} catch (Throwable t) {
player.sendMessage("Error while reloading: "
+ t.getMessage());
}
return true;
}
// Add the command to the array because the underlying command handling
// code of WorldEdit expects it
String[] split = new String[args.length + 1];
System.arraycopy(args, 0, split, 1, args.length);
split[0] = "/" + cmd.getName();
@ -258,25 +312,41 @@ public class WorldEditPlugin extends JavaPlugin {
}
}
/**
* Get the API.
*
* @return
*/
@Deprecated
public WorldEditAPI getAPI() {
return api;
}
String[] getGroups(Player player) {
return perms.getGroups(player.getName());
/**
* Returns the configuration used by WorldEdit.
*
* @return
*/
public BukkitConfiguration getLocalConfiguration() {
return config;
}
boolean inGroup(Player player, String group) {
return perms.inGroup(player.getName(), group);
/**
* Get the permissions resolver in use.
*
* @return
*/
public PermissionsResolverManager getPermissionsResolver() {
return perms;
}
boolean hasPermission(Player player, String perm) {
return (!config.noOpPermissions && player.isOp())
|| perms.hasPermission(player.getName(), perm);
}
BukkitPlayer wrapPlayer(Player player) {
/**
* Used to wrap a Bukkit Player as a LocalPlayer.
*
* @param player
* @return
*/
public BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(this, this.server, player);
}
}