From 247b84c040befce60cadad4c148d44efe97a6df9 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Tue, 10 Sep 2019 11:42:22 -0500 Subject: [PATCH] fix worldguard flag injection for WG7 --- .../songoda/core/hooks/WorldGuardHook.java | 12 ++------- .../worldguard/WorldGuardFlagHandler.java | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/songoda/core/hooks/WorldGuardHook.java b/src/main/java/com/songoda/core/hooks/WorldGuardHook.java index b0c71eb8..05bd5f06 100644 --- a/src/main/java/com/songoda/core/hooks/WorldGuardHook.java +++ b/src/main/java/com/songoda/core/hooks/WorldGuardHook.java @@ -6,19 +6,15 @@ import org.bukkit.Location; public class WorldGuardHook { - static boolean canHook, checkedCanHook = false; - - private static void init() { - if(checkedCanHook) return; + static boolean canHook = false; + static { try { // if this class exists, we're good to use WG classes Class.forName("com.sk89q.worldguard.protection.flags.Flag"); canHook = true; } catch (ClassNotFoundException ex) { } - - checkedCanHook = true; } /** @@ -29,7 +25,6 @@ public class WorldGuardHook { * @param state default value of the flag */ public static void addHook(String flag, boolean state) { - init(); if(canHook) { WorldGuardFlagHandler.addHook(flag, state); } @@ -42,7 +37,6 @@ public class WorldGuardHook { * called and added successfully */ public static boolean isEnabled() { - init(); return canHook && WorldGuardFlagHandler.isEnabled(); } @@ -54,7 +48,6 @@ public class WorldGuardHook { * @return flag state, or null if undefined */ public static Boolean getBooleanFlag(Location l, String flag) { - init(); return canHook ? WorldGuardFlagHandler.getBooleanFlag(l, flag) : null; } @@ -66,7 +59,6 @@ public class WorldGuardHook { * @return flag state, or null if undefined */ public static Boolean getBooleanFlag(Chunk c, String flag) { - init(); return canHook ? WorldGuardFlagHandler.getBooleanFlag(c, flag) : null; } } diff --git a/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java b/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java index 5f6d1b30..05103431 100644 --- a/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java +++ b/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java @@ -33,6 +33,7 @@ public class WorldGuardFlagHandler { static Boolean wgPlugin = null; static Object worldGuardPlugin; + static boolean wg_v7 = false; static boolean legacy_v6 = false; static boolean legacy_v5 = false; static boolean hooksInstalled = false; @@ -47,18 +48,24 @@ public class WorldGuardFlagHandler { */ public static void addHook(String flag, boolean state) { 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 { - // if this class exists, we're on 6.0 - Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry"); - legacy_v6 = true; + // if this class exists, we're on 7.x + Class.forName("com.sk89q.worldguard.protection.flags.WeatherTypeFlag"); + wg_v7 = true; } catch (ClassNotFoundException ex) { - } - if(!legacy_v6) { try { - // if this class exists, we're on 5.x - Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag"); - legacy_v5 = true; - } catch (ClassNotFoundException ex) { + // if this class exists, we're on 6.0 + Class.forName("com.sk89q.worldguard.protection.flags.FlagContextCreateEvent"); + legacy_v6 = true; + } 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); } catch (Exception 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"); wgPlugin = false; }