From d4eb0a37211489ce0644b1aac5d424edc28d8d47 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Wed, 6 Sep 2023 13:31:10 +0800 Subject: [PATCH] Preliminary WorldGuard support --- build.gradle.kts | 11 ++++ .../axiom/integration/RegionProtection.java | 27 +++++++++ .../RegionProtectionWorldGuard.java | 57 +++++++++++++++++++ .../packet/SetBlockBufferPacketListener.java | 22 ++++++- 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/moulberry/axiom/integration/RegionProtection.java create mode 100644 src/main/java/com/moulberry/axiom/integration/RegionProtectionWorldGuard.java diff --git a/build.gradle.kts b/build.gradle.kts index 0121061..a98ff0c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,8 +17,11 @@ java { } repositories { + mavenCentral() maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") maven("https://jitpack.io") + maven("https://maven.enginehub.org/repo/") + maven("https://repo.papermc.io/repository/maven-public/") } dependencies { @@ -27,6 +30,14 @@ dependencies { // Zstd Compression Library implementation("com.github.luben:zstd-jni:1.5.5-4") + + // WorldGuard support + compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.1.0-SNAPSHOT") + + // PlotSquared support + implementation(platform("com.intellectualsites.bom:bom-newest:1.37")) + compileOnly("com.intellectualsites.plotsquared:plotsquared-core") + compileOnly("com.intellectualsites.plotsquared:plotsquared-bukkit") { isTransitive = false } } tasks { diff --git a/src/main/java/com/moulberry/axiom/integration/RegionProtection.java b/src/main/java/com/moulberry/axiom/integration/RegionProtection.java new file mode 100644 index 0000000..9b95e46 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/integration/RegionProtection.java @@ -0,0 +1,27 @@ +package com.moulberry.axiom.integration; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.entity.Player; + +public class RegionProtection { + + private final RegionProtectionWorldGuard worldGuard; + + public RegionProtection(Player player, World world) { + if (Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) { + this.worldGuard = RegionProtectionWorldGuard.tryCreate(player, world); + } else { + this.worldGuard = null; + } + } + + public boolean canBuildInSection(int cx, int cy, int cz) { + if (this.worldGuard != null && !this.worldGuard.canBuildInSection(cx, cy, cz)) return false; + // todo: PlotSquared + return true; + } + + + +} diff --git a/src/main/java/com/moulberry/axiom/integration/RegionProtectionWorldGuard.java b/src/main/java/com/moulberry/axiom/integration/RegionProtectionWorldGuard.java new file mode 100644 index 0000000..784fb3e --- /dev/null +++ b/src/main/java/com/moulberry/axiom/integration/RegionProtectionWorldGuard.java @@ -0,0 +1,57 @@ +package com.moulberry.axiom.integration; + +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.WorldGuard; +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.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionContainer; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; + +public class RegionProtectionWorldGuard { + + private final LocalPlayer player; + private final RegionManager regionManager; + + public RegionProtectionWorldGuard(LocalPlayer player, RegionManager regionManager) { + this.player = player; + this.regionManager = regionManager; + } + + @Nullable + public static RegionProtectionWorldGuard tryCreate(Player player, World world) { + WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform(); + + RegionContainer regionContainer = platform.getRegionContainer(); + com.sk89q.worldedit.world.World worldEditWorld = BukkitAdapter.adapt(world); + LocalPlayer worldGuardPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + + // Don't do any protection if player has bypass + if (platform.getSessionManager().hasBypass(worldGuardPlayer, worldEditWorld)) { + return null; + } + + RegionManager regionManager = regionContainer.get(worldEditWorld); + if (regionManager == null) return null; + + return new RegionProtectionWorldGuard(worldGuardPlayer, regionManager); + } + + public boolean canBuildInSection(int cx, int cy, int cz) { + BlockVector3 min = BlockVector3.at(cx*16, cy*16, cz*16); + BlockVector3 max = BlockVector3.at(cx*16+15, cy*16+15, cz*16+15); + ProtectedRegion test = new ProtectedCuboidRegion("dummy", min, max); + ApplicableRegionSet regions = this.regionManager.getApplicableRegions(test, RegionQuery.QueryOption.COMPUTE_PARENTS); + return regions.testState(this.player, Flags.BUILD); + } + +} diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java index 5a0d2be..44b7479 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java @@ -5,6 +5,21 @@ import com.moulberry.axiom.buffer.BiomeBuffer; import com.moulberry.axiom.buffer.BlockBuffer; import com.moulberry.axiom.buffer.CompressedBlockEntity; import com.moulberry.axiom.event.AxiomModifyWorldEvent; +import com.moulberry.axiom.integration.RegionProtection; +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.WorldGuardPlugin; +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 com.sk89q.worldguard.protection.regions.RegionContainer; +import com.sk89q.worldguard.protection.regions.RegionQuery; import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.shorts.Short2ObjectMap; @@ -34,7 +49,6 @@ import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.lighting.LightEngine; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; -import org.bukkit.World; import org.bukkit.craftbukkit.v1_20_R1.CraftWorld; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -102,6 +116,8 @@ public class SetBlockBufferPacketListener { Bukkit.getPluginManager().callEvent(modifyWorldEvent); if (modifyWorldEvent.isCancelled()) return; + RegionProtection regionProtection = new RegionProtection(player.getBukkitEntity(), world.getWorld()); + // Allowed, apply buffer BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(); @@ -119,6 +135,10 @@ public class SetBlockBufferPacketListener { continue; } + if (!regionProtection.canBuildInSection(cx, cy, cz)) { + continue; + } + LevelChunk chunk = world.getChunk(cx, cz); chunk.setUnsaved(true);