add WG region list hooks

This commit is contained in:
jascotty2 2019-10-07 14:22:28 -05:00
parent 9c4b5ae948
commit 24948cf44b
2 changed files with 285 additions and 3 deletions

View File

@ -1,8 +1,14 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.worldguard.WorldGuardFlagHandler;
import com.songoda.core.hooks.worldguard.WorldGuardRegionHandler;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class WorldGuardHook {
@ -24,7 +30,7 @@ public class WorldGuardHook {
* @param flag name of the flag to set
* @param state default value of the flag
*/
public static void addHook(String flag, boolean state) {
public static void addHook(@NotNull String flag, boolean state) {
if(canHook) {
WorldGuardFlagHandler.addHook(flag, state);
}
@ -47,7 +53,8 @@ public class WorldGuardHook {
* @param flag ALLOW/DENY flag to check
* @return flag state, or null if undefined
*/
public static Boolean getBooleanFlag(Location l, String flag) {
@Nullable
public static Boolean getBooleanFlag(@NotNull Location l, @NotNull String flag) {
return canHook ? WorldGuardFlagHandler.getBooleanFlag(l, flag) : null;
}
@ -58,7 +65,34 @@ public class WorldGuardHook {
* @param flag ALLOW/DENY flag to check
* @return flag state, or null if undefined
*/
public static Boolean getBooleanFlag(Chunk c, String flag) {
@Nullable
public static Boolean getBooleanFlag(@NotNull Chunk c, @NotNull String flag) {
return canHook ? WorldGuardFlagHandler.getBooleanFlag(c, flag) : null;
}
public static boolean isPvpAllowed(@NotNull Location loc) {
return canHook ? Objects.equals(WorldGuardFlagHandler.getBooleanFlag(loc, "pvp"), Boolean.TRUE) : false;
}
public boolean isBreakAllowed(@NotNull Location loc) {
return canHook ? Objects.equals(WorldGuardFlagHandler.getBooleanFlag(loc, "block-break"), Boolean.TRUE) : false;
}
public boolean isExplosionsAllowed(@NotNull Location loc) {
return canHook ? Objects.equals(WorldGuardFlagHandler.getBooleanFlag(loc, "other-explosion"), Boolean.TRUE) : false;
}
public boolean isMobSpawningAllowed(@NotNull Location loc) {
return canHook ? Objects.equals(WorldGuardFlagHandler.getBooleanFlag(loc, "mob-spawning"), Boolean.TRUE) : false;
}
@NotNull
public static List<String> getRegionNames(@NotNull Location loc) {
return canHook ? WorldGuardRegionHandler.getRegionNames(loc) : Collections.EMPTY_LIST;
}
@NotNull
public static List<String> getRegionNames(@NotNull Chunk c) {
return canHook ? WorldGuardRegionHandler.getRegionNames(c) : Collections.EMPTY_LIST;
}
}

View File

@ -0,0 +1,248 @@
package com.songoda.core.hooks.worldguard;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
public class WorldGuardRegionHandler {
static boolean wgPlugin;
static Object worldGuardPlugin;
static boolean wg_v7 = false;
static boolean legacy_v60 = false;
static boolean legacy_v62 = false;
static boolean legacy_v5 = false;
static Method legacy_getRegionManager = null;
static Method legacy_getApplicableRegions_Region = null;
static Method legacy_getApplicableRegions_Location = null;
static Constructor legacy_newProtectedCuboidRegion;
static Class legacy_blockVectorClazz;
static Constructor legacy_newblockVector;
static Class legacy_VectorClazz;
static Constructor legacy_newVectorClazz;
static Method legacy_getApplicableRegions_Vector = null;
static void init() {
if ((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 7.x
Class.forName("com.sk89q.worldguard.protection.flags.WeatherTypeFlag");
wg_v7 = true;
} catch (ClassNotFoundException ex) {
try {
// if this class exists, we're on 6.2
Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry");
legacy_v62 = true;
} catch (ClassNotFoundException ex2) {
try {
// if this class exists, we're on 6.0
Class.forName("com.sk89q.worldguard.protection.flags.BuildFlag");
legacy_v60 = true;
} catch (ClassNotFoundException ex3) {
try {
// if this class exists, we're on 5.x
Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag");
legacy_v5 = true;
} catch (ClassNotFoundException ex4) {
// ¯\_()_/¯
wgPlugin = false;
}
}
}
}
}
if (wgPlugin && (legacy_v62 || legacy_v60 || legacy_v5)) {
try {
// cache reflection methods
if (legacy_getRegionManager == null) {
legacy_getRegionManager = worldGuardPlugin.getClass()
.getDeclaredMethod("getRegionManager", org.bukkit.World.class);
legacy_getApplicableRegions_Region = RegionManager.class.getDeclaredMethod("getApplicableRegions",
Class.forName("com.sk89q.worldguard.protection.regions.ProtectedRegion"));
legacy_getApplicableRegions_Location = RegionManager.class.getDeclaredMethod("getApplicableRegions",
Location.class);
legacy_blockVectorClazz = Class.forName("com.sk89q.worldedit.BlockVector");
legacy_newblockVector = legacy_blockVectorClazz.getConstructor(int.class, int.class, int.class);
legacy_newProtectedCuboidRegion = Class.forName("com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion")
.getConstructor(String.class, legacy_blockVectorClazz, legacy_blockVectorClazz);
legacy_VectorClazz = Class.forName("com.sk89q.worldedit.Vector");
legacy_newVectorClazz = legacy_VectorClazz.getConstructor(int.class, int.class, int.class);
legacy_getApplicableRegions_Vector = RegionManager.class.getDeclaredMethod("getApplicableRegions", legacy_VectorClazz);
}
} catch (Exception ex) {
//Bukkit.getServer().getLogger().log(Level.WARNING, "Failed to set legacy WorldGuard Flags", ex);
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not load WorldGuard methods for " + (legacy_v62 ? "6.2" : (legacy_v60 ? "6.0" : "5")));
wgPlugin = false;
}
}
}
public static List<String> getRegionNames(Chunk c) {
if (worldGuardPlugin == null) {
init();
}
if (!wgPlugin || c == null) {
return Collections.EMPTY_LIST;
} else if (legacy_v62 || legacy_v60 || legacy_v5) {
return getRegionNamesLegacy(c);
}
RegionManager worldManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(c.getWorld()));
if (worldManager == null) {
return Collections.EMPTY_LIST;
}
ProtectedCuboidRegion chunkRegion = new ProtectedCuboidRegion("__TEST__",
BlockVector3.at(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4),
BlockVector3.at((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15));
ApplicableRegionSet set = worldManager.getApplicableRegions(chunkRegion);
List<String> regions = new ArrayList<>();
List<String> parentNames = new ArrayList<>();
for (ProtectedRegion region : set) {
String id = region.getId();
regions.add(id);
ProtectedRegion parent = region.getParent();
while (parent != null) {
parentNames.add(parent.getId());
parent = parent.getParent();
}
}
regions.removeAll(parentNames);
return regions;
}
private static List<String> getRegionNamesLegacy(Chunk c) {
try {
// grab the applicable manager for this world
Object worldManager = (RegionManager) legacy_getRegionManager.invoke(worldGuardPlugin, c.getWorld());
if (worldManager == null) {
return null;
}
// Create a legacy ProtectedCuboidRegion
Object chunkRegion = legacy_newProtectedCuboidRegion.newInstance("__TEST__",
legacy_newblockVector.newInstance(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4),
legacy_newblockVector.newInstance((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15));
// now look for any intersecting regions
// ApplicableRegionSet's prototype is different from v5 to v6, but they're both Iterable
Iterable<ProtectedRegion> set = (Iterable<ProtectedRegion>) legacy_getApplicableRegions_Region.invoke(worldManager, chunkRegion);
List<String> regions = new ArrayList<>();
List<String> parentNames = new ArrayList<>();
for (ProtectedRegion region : set) {
String id = region.getId();
regions.add(id);
ProtectedRegion parent = region.getParent();
while (parent != null) {
parentNames.add(parent.getId());
parent = parent.getParent();
}
}
regions.removeAll(parentNames);
return regions;
} catch (Exception ex) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab regions from WorldGuard", ex);
}
return Collections.EMPTY_LIST;
}
public static List<String> getRegionNames(Location loc) {
if (worldGuardPlugin == null) {
init();
}
if (!wgPlugin || loc == null) {
return Collections.EMPTY_LIST;
} else if (legacy_v62 || legacy_v60 || legacy_v5) {
return getRegionNamesLegacy(loc);
}
RegionManager regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(loc.getWorld()));
if (regionManager == null) {
return Collections.EMPTY_LIST;
}
List<String> regions = new ArrayList<>();
List<String> parentNames = new ArrayList<>();
ApplicableRegionSet set = regionManager.getApplicableRegions(BukkitAdapter.asBlockVector(loc));
for (ProtectedRegion region : set) {
String id = region.getId();
regions.add(id);
ProtectedRegion parent = region.getParent();
while (parent != null) {
parentNames.add(parent.getId());
parent = parent.getParent();
}
}
regions.removeAll(parentNames);
return regions;
}
private static List<String> getRegionNamesLegacy(Location loc) {
try {
// grab the applicable manager for this world
Object worldManager = (RegionManager) legacy_getRegionManager.invoke(worldGuardPlugin, loc.getWorld());
if (worldManager == null) {
return Collections.EMPTY_LIST;
}
// create a vector object
Object vec = legacy_newVectorClazz.newInstance(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
// now look for any intersecting regions
// ApplicableRegionSet's prototype is different from v5 to v6, but they're both Iterable
Iterable<ProtectedRegion> set = (Iterable<ProtectedRegion>) legacy_getApplicableRegions_Vector.invoke(worldManager, legacy_VectorClazz.cast(vec));
List<String> regions = new ArrayList<>();
List<String> parentNames = new ArrayList<>();
for (ProtectedRegion region : set) {
String id = region.getId();
regions.add(id);
ProtectedRegion parent = region.getParent();
while (parent != null) {
parentNames.add(parent.getId());
parent = parent.getParent();
}
}
regions.removeAll(parentNames);
return regions;
} catch (Exception ex) {
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab regions from WorldGuard", ex);
}
return Collections.EMPTY_LIST;
}
}