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

View File

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

View File

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