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

View File

@ -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

View File

@ -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.");
}
}
}