From cf0b60d165a9944cdbdc7808101e6ff1381fbba9 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 22 Jan 2019 17:59:45 -0800 Subject: [PATCH] Fixing some errors related to WG --- .../com/gmail/nossr50/WorldGuardManager.java | 85 +++++++------------ .../com/gmail/nossr50/WorldGuardUtils.java | 4 +- src/main/java/com/gmail/nossr50/mcMMO.java | 7 ++ 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/WorldGuardManager.java b/src/main/java/com/gmail/nossr50/WorldGuardManager.java index 27c0c98b2..45b5258ab 100644 --- a/src/main/java/com/gmail/nossr50/WorldGuardManager.java +++ b/src/main/java/com/gmail/nossr50/WorldGuardManager.java @@ -14,79 +14,54 @@ import org.bukkit.plugin.Plugin; import static org.bukkit.Bukkit.getServer; public class WorldGuardManager { - private static WorldGuardManager instance; - private Plugin worldGuardPluginRef; - private boolean flagsRegistered = false; + private WorldGuardPlugin worldGuardPluginRef; + + public static WorldGuardManager getInstance() { + if(instance == null) + instance = new WorldGuardManager(); + + return instance; + } public WorldGuardManager() { - instance = this; - init(); - } - - public void init() - { - if(getWorldGuard() != null) - { - worldGuardPluginRef = getWorldGuard(); - registerFlags(); - } } public boolean hasMainFlag(Player player) { - //Not sure when we're supposed to know when WorldGuard is loaded - if(!flagsRegistered) - registerFlags(); + if(player == null) + return false; BukkitPlayer localPlayer = BukkitAdapter.adapt(player); com.sk89q.worldedit.util.Location loc = localPlayer.getLocation(); - if(flagsRegistered) - { - if(getWorldGuard() != null) - { - //WorldGuardPlugin worldGuard = getWorldGuard(); - RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); - RegionQuery query = container.createQuery(); - //ApplicableRegionSet set = query.getApplicableRegions(loc); + //WorldGuardPlugin worldGuard = getWorldGuard(); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); - return query.testState(loc, WorldGuardPlugin.inst().wrapPlayer(player), WorldGuardFlags.MCMMO_ENABLE_WG_FLAG); - } - } - //Return False if these fail - mcMMO.p.getLogger().severe("Failed to detect WG flags, is WG running properly?"); - return false; + //ApplicableRegionSet set = query.getApplicableRegions(loc); + + return query.testState(loc, WorldGuardPlugin.inst().wrapPlayer(player), WorldGuardFlags.MCMMO_ENABLE_WG_FLAG); } public boolean hasXPFlag(Player player) { - //Not sure when we're supposed to know when WorldGuard is loaded - if(!flagsRegistered) - registerFlags(); + if(player == null) + return false; BukkitPlayer localPlayer = BukkitAdapter.adapt(player); com.sk89q.worldedit.util.Location loc = localPlayer.getLocation(); - if(flagsRegistered) - { - if(getWorldGuard() != null) - { - //WorldGuardPlugin worldGuard = getWorldGuard(); - RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); - RegionQuery query = container.createQuery(); + //WorldGuardPlugin worldGuard = getWorldGuard(); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); - //ApplicableRegionSet set = query.getApplicableRegions(loc); + //ApplicableRegionSet set = query.getApplicableRegions(loc); - return query.testState(loc, WorldGuardPlugin.inst().wrapPlayer(player), WorldGuardFlags.MCMMO_XP_WG_FLAG); - } - } - //Return False if these fail - mcMMO.p.getLogger().severe("Failed to detect WG flags, is WG running properly?"); - return false; + return query.testState(loc, WorldGuardPlugin.inst().wrapPlayer(player), WorldGuardFlags.MCMMO_XP_WG_FLAG); } private WorldGuardPlugin getWorldGuard() { @@ -97,10 +72,11 @@ public class WorldGuardManager { return null; // Maybe you want throw an exception instead } - return (WorldGuardPlugin) plugin; + worldGuardPluginRef = (WorldGuardPlugin) plugin; + return worldGuardPluginRef; } - private void registerFlags() + public void registerFlags() { if(getWorldGuard() == null) return; @@ -108,11 +84,14 @@ public class WorldGuardManager { FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry(); try { // register our flag with the registry + /*registry.register(WorldGuardFlags.MCMMO_ENABLE_WG_FLAG); + registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG);*/ registry.register(WorldGuardFlags.MCMMO_ENABLE_WG_FLAG); registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG); - flagsRegistered = true; + System.out.println("mcMMO has registered WG flags successfully!"); } catch (FlagConflictException e) { e.printStackTrace(); + System.out.println("mcMMO has failed to register WG flags!"); // some other plugin registered a flag by the same name already. // you may want to re-register with a different name, but this // could cause issues with saved flags in region files. it's better @@ -120,7 +99,5 @@ public class WorldGuardManager { } } - public static WorldGuardManager getInstance() { - return instance; - } + } diff --git a/src/main/java/com/gmail/nossr50/WorldGuardUtils.java b/src/main/java/com/gmail/nossr50/WorldGuardUtils.java index c9bc9b2bd..a28b270c4 100644 --- a/src/main/java/com/gmail/nossr50/WorldGuardUtils.java +++ b/src/main/java/com/gmail/nossr50/WorldGuardUtils.java @@ -16,8 +16,10 @@ public class WorldGuardUtils { return false; // Maybe you want throw an exception instead } } catch (Exception e) { + e.printStackTrace(); //Silently Fail - mcMMO.p.getLogger().severe("Failed to detect worldguard."); + //mcMMO.p.getLogger().severe("Failed to detect worldguard."); + return false; } return true; diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index c0386285d..744281439 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -219,6 +219,13 @@ public class mcMMO extends JavaPlugin { worldBlacklist = new WorldBlacklist(this); } + @Override + public void onLoad() + { + if(getServer().getPluginManager().getPlugin("WorldGuard") != null) + WorldGuardManager.getInstance().registerFlags(); + } + /** * Things to be run when the plugin is disabled. */