Fix support for WorldGuard v6.1.3 and later

This commit is contained in:
Eric 2016-11-23 17:08:20 +01:00
parent 103662c532
commit eda52af179
3 changed files with 83 additions and 25 deletions

View File

@ -42,10 +42,6 @@
<id>vault-repo</id> <id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases/</url> <url>http://nexus.hc.to/content/repositories/pub_releases/</url>
</repository> </repository>
<repository>
<id>sk89q-repo</id>
<url>http://maven.sk89q.com/artifactory/repo/</url>
</repository>
<repository> <repository>
<id>epiceric-repo</id> <id>epiceric-repo</id>
<url>http://epicericee.github.io/ShopChest/maven/</url> <url>http://epicericee.github.io/ShopChest/maven/</url>
@ -68,7 +64,7 @@
<dependency> <dependency>
<groupId>com.sk89q</groupId> <groupId>com.sk89q</groupId>
<artifactId>worldguard</artifactId> <artifactId>worldguard</artifactId>
<version>6.1.1-SNAPSHOT</version> <version>6.1.3-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -68,9 +68,7 @@ public class ShopChest extends JavaPlugin {
} }
@Override @Override
public void onEnable() { public void onLoad() {
instance = this;
config = new Config(this); config = new Config(this);
if (config.enable_debug_log) { 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()); debug("Enabling ShopChest version " + getDescription().getVersion());
if (!getServer().getPluginManager().isPluginEnabled("Vault")) { 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!"); getLogger().warning("Plugin may still work, but more errors are expected!");
} }
Plugin worldGuardPlugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"); if (worldGuard != null && !ShopFlag.isLoaded()) {
if (worldGuardPlugin instanceof WorldGuardPlugin) { ShopFlag.init(this, false);
worldGuard = (WorldGuardPlugin) worldGuardPlugin;
ShopFlag.init();
try { try {
// Reload WorldGuard regions, so that custom flags are applied // Reload WorldGuard regions, so that custom flags are applied

View File

@ -1,9 +1,10 @@
package de.epiceric.shopchest.worldguard; package de.epiceric.shopchest.worldguard;
import com.google.common.collect.Lists;
import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag; 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.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -11,6 +12,7 @@ import java.lang.reflect.Modifier;
public class ShopFlag { public class ShopFlag {
private static Flag<?>[] customFlagList; private static Flag<?>[] customFlagList;
private static boolean loaded = false;
public static final StateFlag CREATE_SHOP; public static final StateFlag CREATE_SHOP;
public static final StateFlag USE_SHOP; public static final StateFlag USE_SHOP;
@ -24,8 +26,55 @@ public class ShopFlag {
customFlagList = new Flag[] {CREATE_SHOP, USE_SHOP, USE_ADMIN_SHOP}; customFlagList = new Flag[] {CREATE_SHOP, USE_SHOP, USE_ADMIN_SHOP};
} }
public static void init() { public static boolean isLoaded() {
// Add custom flags to WorldGuard's flag list 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 {
String[] spl = worldGuardVersion.split("\\.");
if (spl.length > 0) {
majorVersion = Integer.parseInt(spl[0]);
if (spl.length > 1) {
minorVersion = Integer.parseInt(spl[1]);
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;
}
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 { try {
Field flagListField = DefaultFlag.class.getField("flagsList"); Field flagListField = DefaultFlag.class.getField("flagsList");
@ -38,8 +87,12 @@ public class ShopFlag {
modifiersField.setInt(flagListField, flagListField.getModifiers() & ~Modifier.FINAL); modifiersField.setInt(flagListField, flagListField.getModifiers() & ~Modifier.FINAL);
flagListField.set(null, flags); flagListField.set(null, flags);
loaded = true;
} catch (NoSuchFieldException | IllegalAccessException e) { } catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace(); plugin.debug("Failed to initialize WorldGuard flags");
plugin.debug(e);
plugin.getLogger().severe("Failed to initialize custom WorldGuard flags.");
}
} }
} }