From 137a951448d0f21250f9fc803cd8e596026128cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trysi=C5=84ski?= Date: Fri, 10 Dec 2021 21:00:34 +0100 Subject: [PATCH] Fix WorldGuard integration (#1630) --- .../GriefPrevention/DataStore.java | 3 +- .../GriefPrevention/WorldGuardWrapper.java | 60 +++++++++---------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java b/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java index 2104219..078ebc5 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/DataStore.java @@ -201,8 +201,7 @@ public abstract class DataStore GriefPrevention.AddLogEntry("Successfully hooked into WorldGuard."); } //if failed, world guard compat features will just be disabled. - catch (ClassNotFoundException exception) { } - catch (NoClassDefFoundError exception) { } + catch (IllegalStateException | IllegalArgumentException | ClassCastException | NoClassDefFoundError ignored) { } } private void loadSoftMutes() diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/WorldGuardWrapper.java b/src/main/java/me/ryanhamshire/GriefPrevention/WorldGuardWrapper.java index b6a9441..97672b1 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/WorldGuardWrapper.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/WorldGuardWrapper.java @@ -1,59 +1,59 @@ package me.ryanhamshire.GriefPrevention; +import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.World; +import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.WorldGuard; -import com.sk89q.worldguard.bukkit.BukkitPlayer; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.internal.platform.WorldGuardPlatform; -import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; class WorldGuardWrapper { - private WorldGuardPlugin worldGuard = null; + private final WorldGuardPlugin worldGuard; - public WorldGuardWrapper() throws ClassNotFoundException + public WorldGuardWrapper() throws IllegalArgumentException, IllegalStateException, ClassCastException { - this.worldGuard = (WorldGuardPlugin) GriefPrevention.instance.getServer().getPluginManager().getPlugin("WorldGuard"); + this.worldGuard = JavaPlugin.getPlugin(WorldGuardPlugin.class); } public boolean canBuild(Location lesserCorner, Location greaterCorner, Player creatingPlayer) { try { - BukkitPlayer localPlayer = new BukkitPlayer(this.worldGuard, creatingPlayer); - WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); - World world = platform.getMatcher().getWorldByName(lesserCorner.getWorld().getName()); - - if (platform.getSessionManager().hasBypass(localPlayer, world)) return true; - - RegionManager manager = platform.getRegionContainer().get(world); - - if (manager != null) + if (lesserCorner.getWorld() == null) { - ProtectedCuboidRegion tempRegion = new ProtectedCuboidRegion( - "GP_TEMP", - BlockVector3.at(lesserCorner.getX(), 0, lesserCorner.getZ()), - BlockVector3.at(greaterCorner.getX(), world.getMaxY(), greaterCorner.getZ())); - - ApplicableRegionSet overlaps = manager.getApplicableRegions(tempRegion); - for (ProtectedRegion r : overlaps.getRegions()) - { - if (!manager.getApplicableRegions(r).testState(localPlayer, Flags.BUILD)) - { - return false; - } - } return true; } - return true; + LocalPlayer localPlayer = this.worldGuard.wrapPlayer(creatingPlayer); + WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); + World world = BukkitAdapter.adapt(lesserCorner.getWorld()); + + if (platform.getSessionManager().hasBypass(localPlayer, world)) + { + return true; + } + + RegionManager manager = platform.getRegionContainer().get(world); + if (manager == null) + { + return true; + } + + ProtectedCuboidRegion tempRegion = new ProtectedCuboidRegion( + "GP_TEMP", + BlockVector3.at(lesserCorner.getX(), 0, lesserCorner.getZ()), + BlockVector3.at(greaterCorner.getX(), world.getMaxY(), greaterCorner.getZ())); + + return manager.getApplicableRegions(tempRegion).queryState(localPlayer, Flags.BUILD) == StateFlag.State.ALLOW; } catch (Throwable rock) { @@ -62,8 +62,8 @@ class WorldGuardWrapper "Consider updating/downgrading/removing WorldGuard or disable WorldGuard integration in GP's config " + "(CreationRequiresWorldGuardBuildPermission). If you're going to report this please be kind because " + "WorldEdit's API hasn't been :c", CustomLogEntryTypes.Debug, false); - return true; } + return true; } } \ No newline at end of file