diff --git a/pom.xml b/pom.xml index 2763b5b..6bdea54 100644 --- a/pom.xml +++ b/pom.xml @@ -42,10 +42,6 @@ vault-repo http://nexus.hc.to/content/repositories/pub_releases/ - - sk89q-repo - http://maven.sk89q.com/artifactory/repo/ - epiceric-repo http://epicericee.github.io/ShopChest/maven/ @@ -68,7 +64,7 @@ com.sk89q worldguard - 6.1.1-SNAPSHOT + 6.1.3-SNAPSHOT provided diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 549b1b0..a1b55ec 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -68,9 +68,7 @@ public class ShopChest extends JavaPlugin { } @Override - public void onEnable() { - instance = this; - + public void onLoad() { config = new Config(this); if (config.enable_debug_log) { @@ -90,6 +88,19 @@ public class ShopChest extends JavaPlugin { } } + debug("Loading ShopChest version " + getDescription().getVersion()); + + Plugin worldGuardPlugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"); + if (worldGuardPlugin instanceof WorldGuardPlugin) { + worldGuard = (WorldGuardPlugin) worldGuardPlugin; + ShopFlag.init(this, true); + } + } + + @Override + public void onEnable() { + instance = this; + debug("Enabling ShopChest version " + getDescription().getVersion()); if (!getServer().getPluginManager().isPluginEnabled("Vault")) { @@ -122,10 +133,8 @@ public class ShopChest extends JavaPlugin { getLogger().warning("Plugin may still work, but more errors are expected!"); } - Plugin worldGuardPlugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"); - if (worldGuardPlugin instanceof WorldGuardPlugin) { - worldGuard = (WorldGuardPlugin) worldGuardPlugin; - ShopFlag.init(); + if (worldGuard != null && !ShopFlag.isLoaded()) { + ShopFlag.init(this, false); try { // Reload WorldGuard regions, so that custom flags are applied diff --git a/src/main/java/de/epiceric/shopchest/worldguard/ShopFlag.java b/src/main/java/de/epiceric/shopchest/worldguard/ShopFlag.java index f306771..3bbfdd5 100644 --- a/src/main/java/de/epiceric/shopchest/worldguard/ShopFlag.java +++ b/src/main/java/de/epiceric/shopchest/worldguard/ShopFlag.java @@ -1,9 +1,10 @@ package de.epiceric.shopchest.worldguard; +import com.google.common.collect.Lists; import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.StateFlag; -import org.bukkit.Bukkit; +import de.epiceric.shopchest.ShopChest; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -11,6 +12,7 @@ import java.lang.reflect.Modifier; public class ShopFlag { private static Flag[] customFlagList; + private static boolean loaded = false; public static final StateFlag CREATE_SHOP; public static final StateFlag USE_SHOP; @@ -24,22 +26,73 @@ public class ShopFlag { customFlagList = new Flag[] {CREATE_SHOP, USE_SHOP, USE_ADMIN_SHOP}; } - public static void init() { - // Add custom flags to WorldGuard's flag list + public static boolean isLoaded() { + return loaded; + } + + public static void init(final ShopChest plugin, boolean onLoad) { + String worldGuardVersion = plugin.getWorldGuard().getDescription().getVersion(); + + int majorVersion = 0; + int minorVersion = 0; + int fixVersion = 0; + try { - Field flagListField = DefaultFlag.class.getField("flagsList"); + String[] spl = worldGuardVersion.split("\\."); + if (spl.length > 0) { + majorVersion = Integer.parseInt(spl[0]); - Flag[] flags = new Flag[DefaultFlag.flagsList.length + customFlagList.length]; - System.arraycopy(DefaultFlag.flagsList, 0, flags, 0, DefaultFlag.flagsList.length); - System.arraycopy(customFlagList, 0, flags, DefaultFlag.flagsList.length, customFlagList.length); + if (spl.length > 1) { + minorVersion = Integer.parseInt(spl[1]); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(flagListField, flagListField.getModifiers() & ~Modifier.FINAL); + if (spl.length > 2) { + int length = 0; + for (int i = 0; i < spl[2].toCharArray().length; i++) { + char c = spl[2].toCharArray()[i]; + if (c >= '0' && c <= '9') { + length++; + } else break; + } - flagListField.set(null, flags); - } catch (NoSuchFieldException | IllegalAccessException e) { - e.printStackTrace(); + fixVersion = Integer.parseInt(spl[2].substring(0, length)); + } + } + } else { + plugin.getLogger().severe("Failed to initialize custom WorldGuard flags."); + plugin.debug("Failed to initialize WorldGuard flags: Unknown/Invalid version: " + worldGuardVersion); + return; + } + } catch (NumberFormatException e) { + plugin.debug("Failed to initialize WorldGuard flags"); + plugin.debug(e); + plugin.getLogger().severe("Failed to initialize custom WorldGuard flags."); + return; + } + + if (((majorVersion == 6 && minorVersion == 1 && fixVersion >= 3) || (majorVersion == 6 && minorVersion > 1) || majorVersion > 6)) { + if (onLoad) { + plugin.getWorldGuard().getFlagRegistry().registerAll(Lists.newArrayList(customFlagList)); + loaded = true; + } + } else { + try { + Field flagListField = DefaultFlag.class.getField("flagsList"); + + Flag[] flags = new Flag[DefaultFlag.flagsList.length + customFlagList.length]; + System.arraycopy(DefaultFlag.flagsList, 0, flags, 0, DefaultFlag.flagsList.length); + System.arraycopy(customFlagList, 0, flags, DefaultFlag.flagsList.length, customFlagList.length); + + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(flagListField, flagListField.getModifiers() & ~Modifier.FINAL); + + flagListField.set(null, flags); + loaded = true; + } catch (NoSuchFieldException | IllegalAccessException e) { + plugin.debug("Failed to initialize WorldGuard flags"); + plugin.debug(e); + plugin.getLogger().severe("Failed to initialize custom WorldGuard flags."); + } } }