fix worldguard flag injection for WG7

This commit is contained in:
jascotty2 2019-09-10 11:42:22 -05:00
parent 62a5dfbd90
commit 247b84c040
2 changed files with 19 additions and 20 deletions

View File

@ -6,19 +6,15 @@ import org.bukkit.Location;
public class WorldGuardHook { public class WorldGuardHook {
static boolean canHook, checkedCanHook = false; static boolean canHook = false;
private static void init() {
if(checkedCanHook) return;
static {
try { try {
// if this class exists, we're good to use WG classes // if this class exists, we're good to use WG classes
Class.forName("com.sk89q.worldguard.protection.flags.Flag"); Class.forName("com.sk89q.worldguard.protection.flags.Flag");
canHook = true; canHook = true;
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
} }
checkedCanHook = true;
} }
/** /**
@ -29,7 +25,6 @@ public class WorldGuardHook {
* @param state default value of the flag * @param state default value of the flag
*/ */
public static void addHook(String flag, boolean state) { public static void addHook(String flag, boolean state) {
init();
if(canHook) { if(canHook) {
WorldGuardFlagHandler.addHook(flag, state); WorldGuardFlagHandler.addHook(flag, state);
} }
@ -42,7 +37,6 @@ public class WorldGuardHook {
* called and added successfully * called and added successfully
*/ */
public static boolean isEnabled() { public static boolean isEnabled() {
init();
return canHook && WorldGuardFlagHandler.isEnabled(); return canHook && WorldGuardFlagHandler.isEnabled();
} }
@ -54,7 +48,6 @@ public class WorldGuardHook {
* @return flag state, or null if undefined * @return flag state, or null if undefined
*/ */
public static Boolean getBooleanFlag(Location l, String flag) { public static Boolean getBooleanFlag(Location l, String flag) {
init();
return canHook ? WorldGuardFlagHandler.getBooleanFlag(l, flag) : null; return canHook ? WorldGuardFlagHandler.getBooleanFlag(l, flag) : null;
} }
@ -66,7 +59,6 @@ public class WorldGuardHook {
* @return flag state, or null if undefined * @return flag state, or null if undefined
*/ */
public static Boolean getBooleanFlag(Chunk c, String flag) { public static Boolean getBooleanFlag(Chunk c, String flag) {
init();
return canHook ? WorldGuardFlagHandler.getBooleanFlag(c, flag) : null; return canHook ? WorldGuardFlagHandler.getBooleanFlag(c, flag) : null;
} }
} }

View File

@ -33,6 +33,7 @@ public class WorldGuardFlagHandler {
static Boolean wgPlugin = null; static Boolean wgPlugin = null;
static Object worldGuardPlugin; static Object worldGuardPlugin;
static boolean wg_v7 = false;
static boolean legacy_v6 = false; static boolean legacy_v6 = false;
static boolean legacy_v5 = false; static boolean legacy_v5 = false;
static boolean hooksInstalled = false; static boolean hooksInstalled = false;
@ -47,18 +48,24 @@ public class WorldGuardFlagHandler {
*/ */
public static void addHook(String flag, boolean state) { public static void addHook(String flag, boolean state) {
if (wgPlugin == null && (wgPlugin = (worldGuardPlugin = Bukkit.getPluginManager().getPlugin("WorldGuard")) != null)) { if (wgPlugin == null && (wgPlugin = (worldGuardPlugin = Bukkit.getPluginManager().getPlugin("WorldGuard")) != null)) {
// a number of flags were introduced in 7.x that aren't in 5 or 6
try { try {
// if this class exists, we're on 6.0 // if this class exists, we're on 7.x
Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry"); Class.forName("com.sk89q.worldguard.protection.flags.WeatherTypeFlag");
legacy_v6 = true; wg_v7 = true;
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
}
if(!legacy_v6) {
try { try {
// if this class exists, we're on 5.x // if this class exists, we're on 6.0
Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag"); Class.forName("com.sk89q.worldguard.protection.flags.FlagContextCreateEvent");
legacy_v5 = true; legacy_v6 = true;
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex2) {
try {
// if this class exists, we're on 5.x
Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag");
legacy_v5 = true;
} catch (ClassNotFoundException ex3) {
// ¯\_()_/¯
}
} }
} }
} }
@ -130,7 +137,7 @@ public class WorldGuardFlagHandler {
flags.put(flag, wgFlag); flags.put(flag, wgFlag);
} catch (Exception ex) { } catch (Exception ex) {
//Bukkit.getServer().getLogger().log(Level.WARNING, "Failed to set legacy WorldGuard Flags", ex); //Bukkit.getServer().getLogger().log(Level.WARNING, "Failed to set legacy WorldGuard Flags", ex);
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add flag {0} to WorldGuard", flag); Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add flag {0} to WorldGuard " + (legacy_v6 ? "6" : "5"), flag);
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard"); Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard");
wgPlugin = false; wgPlugin = false;
} }