Fixing some errors related to WG

This commit is contained in:
nossr50 2019-01-22 17:59:45 -08:00
parent cbb726e020
commit cf0b60d165
3 changed files with 41 additions and 55 deletions

View File

@ -14,79 +14,54 @@ import org.bukkit.plugin.Plugin;
import static org.bukkit.Bukkit.getServer; import static org.bukkit.Bukkit.getServer;
public class WorldGuardManager { public class WorldGuardManager {
private static WorldGuardManager instance; private static WorldGuardManager instance;
private Plugin worldGuardPluginRef; private WorldGuardPlugin worldGuardPluginRef;
private boolean flagsRegistered = false;
public static WorldGuardManager getInstance() {
if(instance == null)
instance = new WorldGuardManager();
return instance;
}
public WorldGuardManager() public WorldGuardManager()
{ {
instance = this;
init();
}
public void init()
{
if(getWorldGuard() != null)
{
worldGuardPluginRef = getWorldGuard();
registerFlags();
}
} }
public boolean hasMainFlag(Player player) public boolean hasMainFlag(Player player)
{ {
//Not sure when we're supposed to know when WorldGuard is loaded if(player == null)
if(!flagsRegistered) return false;
registerFlags();
BukkitPlayer localPlayer = BukkitAdapter.adapt(player); BukkitPlayer localPlayer = BukkitAdapter.adapt(player);
com.sk89q.worldedit.util.Location loc = localPlayer.getLocation(); 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); //ApplicableRegionSet set = query.getApplicableRegions(loc);
}
} 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;
} }
public boolean hasXPFlag(Player player) public boolean hasXPFlag(Player player)
{ {
//Not sure when we're supposed to know when WorldGuard is loaded if(player == null)
if(!flagsRegistered) return false;
registerFlags();
BukkitPlayer localPlayer = BukkitAdapter.adapt(player); BukkitPlayer localPlayer = BukkitAdapter.adapt(player);
com.sk89q.worldedit.util.Location loc = localPlayer.getLocation(); com.sk89q.worldedit.util.Location loc = localPlayer.getLocation();
if(flagsRegistered) //WorldGuardPlugin worldGuard = getWorldGuard();
{ RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
if(getWorldGuard() != null) 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 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;
} }
private WorldGuardPlugin getWorldGuard() { private WorldGuardPlugin getWorldGuard() {
@ -97,10 +72,11 @@ public class WorldGuardManager {
return null; // Maybe you want throw an exception instead 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) if(getWorldGuard() == null)
return; return;
@ -108,11 +84,14 @@ public class WorldGuardManager {
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry(); FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
try { try {
// register our flag with the registry // 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_ENABLE_WG_FLAG);
registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG); registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG);
flagsRegistered = true; System.out.println("mcMMO has registered WG flags successfully!");
} catch (FlagConflictException e) { } catch (FlagConflictException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("mcMMO has failed to register WG flags!");
// some other plugin registered a flag by the same name already. // some other plugin registered a flag by the same name already.
// you may want to re-register with a different name, but this // you may want to re-register with a different name, but this
// could cause issues with saved flags in region files. it's better // 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;
}
} }

View File

@ -16,8 +16,10 @@ public class WorldGuardUtils {
return false; // Maybe you want throw an exception instead return false; // Maybe you want throw an exception instead
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
//Silently Fail //Silently Fail
mcMMO.p.getLogger().severe("Failed to detect worldguard."); //mcMMO.p.getLogger().severe("Failed to detect worldguard.");
return false;
} }
return true; return true;

View File

@ -219,6 +219,13 @@ public class mcMMO extends JavaPlugin {
worldBlacklist = new WorldBlacklist(this); 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. * Things to be run when the plugin is disabled.
*/ */