load flags even when not registering custom flags

This commit is contained in:
jascotty2 2019-10-07 16:38:20 -05:00
parent 24948cf44b
commit acdbd82e32
2 changed files with 67 additions and 34 deletions

View File

@ -87,9 +87,8 @@ public class Config extends ConfigSection {
boolean autoremove = false; boolean autoremove = false;
/** /**
* load comments when loading the file * load comments when loading the file
* TODO
*/ */
boolean loadComments = false; boolean loadComments = true;
/** /**
* Default comment applied to config nodes * Default comment applied to config nodes
*/ */
@ -440,7 +439,6 @@ public class Config extends ConfigSection {
} }
protected void parseComments(@NotNull String contents, @NotNull Map<?, ?> input) { protected void parseComments(@NotNull String contents, @NotNull Map<?, ?> input) {
// TODO?
// if starts with a comment, load all nonbreaking comments as a header // if starts with a comment, load all nonbreaking comments as a header
// then load all comments and assign to the next valid node loaded // then load all comments and assign to the next valid node loaded
// (Only load comments that are on their own line) // (Only load comments that are on their own line)

View File

@ -19,6 +19,7 @@ import java.lang.reflect.Modifier;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk; import org.bukkit.Chunk;
@ -31,7 +32,7 @@ import org.bukkit.Location;
*/ */
public class WorldGuardFlagHandler { public class WorldGuardFlagHandler {
static Boolean wgPlugin = null; static boolean wgPlugin;
static Object worldGuardPlugin; static Object worldGuardPlugin;
static boolean wg_v7 = false; static boolean wg_v7 = false;
static boolean legacy_v60 = false; static boolean legacy_v60 = false;
@ -40,15 +41,8 @@ public class WorldGuardFlagHandler {
static boolean hooksInstalled = false; static boolean hooksInstalled = false;
static Map<String, Object> flags = new HashMap(); static Map<String, Object> flags = new HashMap();
/** static {
* Attempt to register a worldGuard flag (ALLOW/DENY) <br /> if ((wgPlugin = (worldGuardPlugin = Bukkit.getPluginManager().getPlugin("WorldGuard")) != null)) {
* Note: This must be called before WorldGuard loads, or it will fail.
*
* @param flag name of the flag to set
* @param state default value of the flag
*/
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 // 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 7.x // if this class exists, we're on 7.x
@ -77,6 +71,16 @@ public class WorldGuardFlagHandler {
} }
} }
} }
}
/**
* Attempt to register a worldGuard flag (ALLOW/DENY) <br />
* Note: This must be called before WorldGuard loads, or it will fail.
*
* @param flag name of the flag to set
* @param state default value of the flag
*/
public static void addHook(String flag, boolean state) {
if (!wgPlugin) { if (!wgPlugin) {
return; return;
} }
@ -135,12 +139,14 @@ public class WorldGuardFlagHandler {
// and put the new list into place // and put the new list into place
setStaticField(flagField, flagsNew); setStaticField(flagField, flagsNew);
if (legacy_v62) { // SimpleFlagRegistry is NOT in 6.0 if (legacy_v62) { // SimpleFlagRegistry is NOT in 6.0 or 6.1
// register this flag in the registry // register this flag in the registry
Object flagRegistry = getPrivateField(worldGuardPlugin.getClass(), worldGuardPlugin, "flagRegistry"); if(legacy_simpleFlagRegistryClazz == null) {
Class simpleFlagRegistryClazz = Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry"); legacy_worldGuardPlugin_flagRegistry = getPrivateField(worldGuardPlugin.getClass(), worldGuardPlugin, "flagRegistry");
Method registerSimpleFlagRegistry = simpleFlagRegistryClazz.getDeclaredMethod("register", Flag.class); legacy_simpleFlagRegistryClazz = Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry");
registerSimpleFlagRegistry.invoke(flagRegistry, wgFlag); legacy_simpleFlagRegistry_get = legacy_simpleFlagRegistryClazz.getDeclaredMethod("get", String.class);
}
legacy_simpleFlagRegistryClazz.getDeclaredMethod("register", Flag.class).invoke(legacy_worldGuardPlugin_flagRegistry, wgFlag);
} }
// all good! // all good!
@ -168,7 +174,41 @@ public class WorldGuardFlagHandler {
} }
public static boolean isEnabled() { public static boolean isEnabled() {
return wgPlugin != null && wgPlugin; return wgPlugin;
}
public static Object getFlag(String flag) {
Object flagObj = flags.get(flag);
// load a flag if we don't know it
if (flagObj == null) {
if (wg_v7) {
flags.put(flag, flagObj = WorldGuard.getInstance().getFlagRegistry().get(flag));
} else if (legacy_v62) {
try {
if (legacy_simpleFlagRegistryClazz == null) {
legacy_worldGuardPlugin_flagRegistry = getPrivateField(worldGuardPlugin.getClass(), worldGuardPlugin, "flagRegistry");
legacy_simpleFlagRegistryClazz = Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry");
legacy_simpleFlagRegistry_get = legacy_simpleFlagRegistryClazz.getDeclaredMethod("get", String.class);
}
flags.put(flag, flagObj = legacy_simpleFlagRegistry_get.invoke(legacy_worldGuardPlugin_flagRegistry, flag));
} catch (Exception ex) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex);
}
} else if (!legacy_loadedFlags && (legacy_v60 || legacy_v5)) {
try {
Class defaultFlagClazz = Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag");
Field flagField = defaultFlagClazz.getField("flagsList");
Flag<?>[] flagsOld = (Flag<?>[]) flagField.get(null);
Stream.of(flagsOld).forEach(f -> flags.put(f.getName(), f));
flagObj = flags.get(flag);
} catch (Exception ex) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex);
}
legacy_loadedFlags = true;
}
}
return flagObj;
} }
/** /**
@ -179,20 +219,16 @@ public class WorldGuardFlagHandler {
* @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) {
if (wgPlugin == null || !wgPlugin) { if (!wgPlugin) {
return null; return null;
} }
Object flagObj = flags.get(flag); Object flagObj = getFlag(flag);
// There's a different way to get this in the old version // There's a different way to get this in the old version
if (legacy_v62 || legacy_v60 || legacy_v5) { if (legacy_v62 || legacy_v60 || legacy_v5) {
return flagObj == null ? null : getBooleanFlagLegacy(l, flagObj); return flagObj == null ? null : getBooleanFlagLegacy(l, flagObj);
} }
// for convinience, we can load a flag if we don't know it
if (flagObj == null) {
flags.put(flag, flagObj = WorldGuard.getInstance().getFlagRegistry().get(flag));
}
// so, what's up? // so, what's up?
if (flagObj instanceof StateFlag) { if (flagObj instanceof StateFlag) {
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
@ -211,20 +247,15 @@ public class WorldGuardFlagHandler {
* @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) {
if (wgPlugin == null || !wgPlugin) { if (!wgPlugin) {
return null; return null;
} }
Object flagObj = flags.get(flag); Object flagObj = getFlag(flag);
// There's a different way to get this in the old version // There's a different way to get this in the old version
if (legacy_v62 || legacy_v60 || legacy_v5) { if (legacy_v62 || legacy_v60 || legacy_v5) {
return flagObj == null ? null : getBooleanFlagLegacy(c, flagObj); return flagObj == null ? null : getBooleanFlagLegacy(c, flagObj);
} }
// for convinience, we can load a flag if we don't know it
if (flagObj == null) {
flags.put(flag, flagObj = WorldGuard.getInstance().getFlagRegistry().get(flag));
}
// so, what's up? // so, what's up?
if (flagObj instanceof StateFlag) { if (flagObj instanceof StateFlag) {
RegionManager worldManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(c.getWorld())); RegionManager worldManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(c.getWorld()));
@ -254,6 +285,10 @@ public class WorldGuardFlagHandler {
static Class legacy_VectorClazz; static Class legacy_VectorClazz;
static Constructor legacy_newVectorClazz; static Constructor legacy_newVectorClazz;
static Method legacy_getApplicableRegions_Vector = null; static Method legacy_getApplicableRegions_Vector = null;
static Class legacy_simpleFlagRegistryClazz = null; // only used for 6.2
static Method legacy_simpleFlagRegistry_get = null; // only used for 6.2
static Object legacy_worldGuardPlugin_flagRegistry = null; // only used for 6.2
static boolean legacy_loadedFlags = false;
private static Boolean getBooleanFlagLegacy(Location l, Object flag) { private static Boolean getBooleanFlagLegacy(Location l, Object flag) {
try { try {
@ -294,7 +329,7 @@ public class WorldGuardFlagHandler {
// also doesn't have a "queryState" function // also doesn't have a "queryState" function
//getFlag(T flag) //getFlag(T flag)
if (legacy5_applicableRegionSet_getFlag == null) { if (legacy5_applicableRegionSet_getFlag == null) {
legacy5_applicableRegionSet_getFlag = Class.forName("com.sk89q.worldguard.protection.ApplicableRegionSet").getMethod("getFlag", Object.class); legacy5_applicableRegionSet_getFlag = Class.forName("com.sk89q.worldguard.protection.ApplicableRegionSet").getMethod("getFlag", Flag.class);
} }
result = (State) legacy5_applicableRegionSet_getFlag.invoke(set, flag); result = (State) legacy5_applicableRegionSet_getFlag.invoke(set, flag);
} }