Added a static WorldGuardPlugin instance and made the utility methods in RegionCommands static.

This commit is contained in:
wizjany 2013-06-01 18:14:59 -04:00
parent 17a466059e
commit b41be0c319
4 changed files with 42 additions and 34 deletions

View File

@ -18,7 +18,6 @@
package com.sk89q.worldguard.bukkit;
import org.bukkit.Bukkit;
import org.bukkit.World;
import com.sk89q.worldguard.protection.managers.RegionManager;
@ -27,7 +26,6 @@
* Helper class to get a reference to WorldGuard and its components.
*/
public class WGBukkit {
private static WorldGuardPlugin cachedPlugin = null;
private WGBukkit() {
}
@ -43,17 +41,16 @@ private WGBukkit() {
* @return the WorldGuard plugin or null
*/
public static WorldGuardPlugin getPlugin() {
if (cachedPlugin == null) {
cachedPlugin = (WorldGuardPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
}
return cachedPlugin;
return WorldGuardPlugin.inst();
}
/**
* Set cache to null for reload WorldGuardPlugin
* @deprecated instance is now stored directly in {@link WorldGuardPlugin}
*/
@Deprecated
public static void cleanCache() {
cachedPlugin = null;
// do nothing - plugin instance is stored in plugin class now
}
/**
@ -67,7 +64,7 @@ public static RegionManager getRegionManager(World world) {
if (getPlugin() == null) {
return null;
}
return cachedPlugin.getRegionManager(world);
return WorldGuardPlugin.inst().getRegionManager(world);
}
}

View File

@ -72,6 +72,11 @@
*/
public class WorldGuardPlugin extends JavaPlugin {
/**
* Current instance of this plugin.
*/
private static WorldGuardPlugin inst;
/**
* Manager for commands. This automatically handles nested commands,
* permissions checking, and a number of other fancy command things.
@ -102,7 +107,7 @@ public WorldGuardPlugin() {
configuration = new ConfigurationManager(this);
globalRegionManager = new GlobalRegionManager(this);
final WorldGuardPlugin plugin = this;
final WorldGuardPlugin plugin = inst = this;
commands = new CommandsManager<CommandSender>() {
@Override
public boolean hasPermission(CommandSender player, String perm) {
@ -111,6 +116,14 @@ public boolean hasPermission(CommandSender player, String perm) {
};
}
/**
* Get the current instance of WorldGuard
* @return WorldGuardPlugin instance
*/
public static WorldGuardPlugin inst() {
return inst;
}
/**
* Called on plugin enable.
*/

View File

@ -84,8 +84,8 @@ public RegionCommands(WorldGuardPlugin plugin) {
* @param sender the sender
* @return the permission model
*/
private RegionPermissionModel getPermissionModel(CommandSender sender) {
return new RegionPermissionModel(plugin, sender);
private static RegionPermissionModel getPermissionModel(CommandSender sender) {
return new RegionPermissionModel(WorldGuardPlugin.inst(), sender);
}
/**
@ -98,13 +98,13 @@ private RegionPermissionModel getPermissionModel(CommandSender sender) {
* @return a world
* @throws CommandException on error
*/
private World getWorld(CommandContext args, CommandSender sender, char flag)
private static World getWorld(CommandContext args, CommandSender sender, char flag)
throws CommandException {
if (args.hasFlag(flag)) {
return plugin.matchWorld(sender, args.getFlag('w'));
return WorldGuardPlugin.inst().matchWorld(sender, args.getFlag(flag));
} else {
if (sender instanceof Player) {
return plugin.checkPlayer(sender).getWorld();
return WorldGuardPlugin.inst().checkPlayer(sender).getWorld();
} else {
throw new CommandException("Please specify " +
"the world with -" + flag + " world_name.");
@ -120,18 +120,16 @@ private World getWorld(CommandContext args, CommandSender sender, char flag)
* @return the id given
* @throws CommandException thrown on an error
*/
private String validateRegionId(String id, boolean allowGlobal)
private static String validateRegionId(String id, boolean allowGlobal)
throws CommandException {
if (!ProtectedRegion.isValidId(id)) {
throw new CommandException(
"The region name of '" + id + "' contains characters that are not allowed.");
}
if (id.equalsIgnoreCase("__global__")) { // Sorry, no global
if (!allowGlobal) {
throw new CommandException(
"Sorry, you can't use __global__ here.");
}
if (!allowGlobal && id.equalsIgnoreCase("__global__")) { // Sorry, no global
throw new CommandException(
"Sorry, you can't use __global__ here.");
}
return id;
@ -148,7 +146,7 @@ private String validateRegionId(String id, boolean allowGlobal)
* @param allowGlobal true to allow selecting __global__
* @throws CommandException thrown if no region is found by the given name
*/
private ProtectedRegion findExistingRegion(
private static ProtectedRegion findExistingRegion(
RegionManager regionManager, String id, boolean allowGlobal)
throws CommandException {
// Validate the id
@ -184,7 +182,7 @@ private ProtectedRegion findExistingRegion(
* @return a region
* @throws CommandException thrown if no region was found
*/
private ProtectedRegion findRegionStandingIn(
private static ProtectedRegion findRegionStandingIn(
RegionManager regionManager, Player player) throws CommandException {
return findRegionStandingIn(regionManager, player, false);
}
@ -204,7 +202,7 @@ private ProtectedRegion findRegionStandingIn(
* @return a region
* @throws CommandException thrown if no region was found
*/
private ProtectedRegion findRegionStandingIn(
private static ProtectedRegion findRegionStandingIn(
RegionManager regionManager, Player player, boolean allowGlobal) throws CommandException {
ApplicableRegionSet set = regionManager.getApplicableRegions(
player.getLocation());
@ -248,8 +246,8 @@ private ProtectedRegion findRegionStandingIn(
* @return the selection
* @throws CommandException thrown on an error
*/
private Selection getSelection(Player player) throws CommandException {
WorldEditPlugin worldEdit = plugin.getWorldEdit();
private static Selection getSelection(Player player) throws CommandException {
WorldEditPlugin worldEdit = WorldGuardPlugin.inst().getWorldEdit();
Selection selection = worldEdit.getSelection(player);
if (selection == null) {
@ -270,7 +268,7 @@ private Selection getSelection(Player player) throws CommandException {
* @return a new region
* @throws CommandException thrown on an error
*/
private ProtectedRegion createRegionFromSelection(Player player, String id)
private static ProtectedRegion createRegionFromSelection(Player player, String id)
throws CommandException {
Selection selection = getSelection(player);
@ -298,7 +296,7 @@ private ProtectedRegion createRegionFromSelection(Player player, String id)
* @param regionManager the region manager
* @throws CommandException throw on an error
*/
private void commitChanges(CommandSender sender, RegionManager regionManager)
private static void commitChanges(CommandSender sender, RegionManager regionManager)
throws CommandException {
try {
if (regionManager.getRegions().size() >= 500) {
@ -318,7 +316,7 @@ private void commitChanges(CommandSender sender, RegionManager regionManager)
* @param regionManager the region manager
* @throws CommandException throw on an error
*/
private void reloadChanges(CommandSender sender, RegionManager regionManager)
private static void reloadChanges(CommandSender sender, RegionManager regionManager)
throws CommandException {
try {
if (regionManager.getRegions().size() >= 500) {
@ -338,9 +336,9 @@ private void reloadChanges(CommandSender sender, RegionManager regionManager)
* @param region the region
* @throws CommandException thrown on a command error
*/
private void setPlayerSelection(Player player, ProtectedRegion region)
private static void setPlayerSelection(Player player, ProtectedRegion region)
throws CommandException {
WorldEditPlugin worldEdit = plugin.getWorldEdit();
WorldEditPlugin worldEdit = WorldGuardPlugin.inst().getWorldEdit();
World world = player.getWorld();
@ -365,7 +363,7 @@ private void setPlayerSelection(Player player, ProtectedRegion region)
} else if (region instanceof GlobalProtectedRegion) {
throw new CommandException(
"Can't select global regions! " +
"That would cover the entire world.");
"That would cover the entire world.");
} else {
throw new CommandException("Unknown region type: " +
@ -382,10 +380,10 @@ private void setPlayerSelection(Player player, ProtectedRegion region)
* @param value the value
* @throws InvalidFlagFormat thrown if the value is invalid
*/
private <V> void setFlag(ProtectedRegion region,
private static <V> void setFlag(ProtectedRegion region,
Flag<V> flag, CommandSender sender, String value)
throws InvalidFlagFormat {
region.setFlag(flag, flag.parseInput(plugin, sender, value));
region.setFlag(flag, flag.parseInput(WorldGuardPlugin.inst(), sender, value));
}
/**

View File

@ -73,7 +73,7 @@ public void reload(CommandContext args, CommandSender sender) throws CommandExce
plugin.getGlobalRegionManager().unload();
plugin.getGlobalStateManager().load();
plugin.getGlobalRegionManager().preload();
WGBukkit.cleanCache();
// WGBukkit.cleanCache();
sender.sendMessage("WorldGuard configuration reloaded.");
} catch (Throwable t) {
sender.sendMessage("Error while reloading: "