From 709e78c3bc146b2e8fe084ac27915a635992b4d5 Mon Sep 17 00:00:00 2001 From: TWME <65117253+TWME-TW@users.noreply.github.com> Date: Fri, 28 Jun 2024 01:02:57 +0800 Subject: [PATCH] Add support for GriefDefender - Added `/integration/griefdefender/GriefDefenderIntegration.java` - Added `/integration/griefdefender/GriefDefenderIntegrationImpl.java` - Modified logic in `/integration/Integration.java` and `/integration/SectionPermissionChecker.java` This commit enables AxiomPaper to support GriefDefender claims. When a world has GriefDefender enabled, players will only be able to use Axiom within territories where they have building permissions. These changes expand AxiomPaper's ability to check for more region protection plugins. --- build.gradle.kts | 4 + gradle/libs.versions.toml | 2 + .../axiom/commands/AxiomDebugCommand.java | 7 +- .../axiom/integration/Integration.java | 11 ++- .../integration/SectionPermissionChecker.java | 10 +++ .../GriefDefenderIntegration.java | 27 +++++++ .../GriefDefenderIntegrationImpl.java | 80 +++++++++++++++++++ 7 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegration.java create mode 100644 src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegrationImpl.java diff --git a/build.gradle.kts b/build.gradle.kts index 66a4f9c..574b3e3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,6 +24,7 @@ repositories { maven("https://repo.papermc.io/repository/maven-public/") maven("https://maven.enginehub.org/repo/") maven("https://maven.playpro.com") + maven("https://repo.glaremasters.me/repository/bloodshot") } dependencies { @@ -47,6 +48,9 @@ dependencies { // CoreProtect support compileOnly(libs.coreprotect) + + // GriefDefender support + compileOnly(libs.griefdefender) } tasks { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 41e9a0b..8ba853d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ reflection-remapper = "0.1.2-20240315.033304-2" viaversion-api = "5.0.1" worldguard-bukkit = "7.1.0-20240503.180049-12" zstd-jni = "1.5.5-4" +griefdefender = "2.1.0-20240112.231634-8" # Plugins paperweight-userdev = "1.7.1" @@ -25,6 +26,7 @@ reflection-remapper = { group = "xyz.jpenilla", name = "reflection-remapper", ve viaversion-api = { group = "com.viaversion", name = "viaversion-api", version.ref = "viaversion-api" } worldguard-bukkit = { group = "com.sk89q.worldguard", name = "worldguard-bukkit", version.ref = "worldguard-bukkit" } zstd-jni = { group = "com.github.luben", name = "zstd-jni", version = "1.5.5-4" } +griefdefender = { group = "com.griefdefender", name = "api", version.ref = "griefdefender" } [plugins] paperweight-userdev = { id = "io.papermc.paperweight.userdev", version.ref = "paperweight-userdev" } diff --git a/src/main/java/com/moulberry/axiom/commands/AxiomDebugCommand.java b/src/main/java/com/moulberry/axiom/commands/AxiomDebugCommand.java index fe6b2c5..e076ba1 100644 --- a/src/main/java/com/moulberry/axiom/commands/AxiomDebugCommand.java +++ b/src/main/java/com/moulberry/axiom/commands/AxiomDebugCommand.java @@ -85,7 +85,8 @@ public class AxiomDebugCommand { ); enum IntegrationType { PLOT_SQUARED, - WORLD_GUARD + WORLD_GUARD, + GRIEF_DEFENDER } manager.command( base(manager, "canBreakBlockAtCurrentPosition").optional("type", EnumParser.enumParser(IntegrationType.class)).handler(context -> { @@ -99,6 +100,8 @@ public class AxiomDebugCommand { canBreakBlock = PlotSquaredIntegration.canBreakBlock(player, block); } else if (integrationType == IntegrationType.WORLD_GUARD) { canBreakBlock = WorldGuardIntegration.canBreakBlock(player, block.getLocation()); + } else if (integrationType == IntegrationType.GRIEF_DEFENDER) { + canBreakBlock = Integration.canBreakBlock(player, block); } else { canBreakBlock = Integration.canBreakBlock(player, block); } @@ -115,6 +118,8 @@ public class AxiomDebugCommand { canPlaceBlock = PlotSquaredIntegration.canPlaceBlock(player, player.getLocation()); } else if (integrationType == IntegrationType.WORLD_GUARD) { canPlaceBlock = WorldGuardIntegration.canPlaceBlock(player, player.getLocation()); + } else if (integrationType == IntegrationType.GRIEF_DEFENDER) { + canPlaceBlock = Integration.canPlaceBlock(player, player.getLocation()); } else { canPlaceBlock = Integration.canPlaceBlock(player, player.getLocation()); } diff --git a/src/main/java/com/moulberry/axiom/integration/Integration.java b/src/main/java/com/moulberry/axiom/integration/Integration.java index 039134a..7c92420 100644 --- a/src/main/java/com/moulberry/axiom/integration/Integration.java +++ b/src/main/java/com/moulberry/axiom/integration/Integration.java @@ -1,28 +1,33 @@ package com.moulberry.axiom.integration; +import com.moulberry.axiom.integration.griefdefender.GriefDefenderIntegration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import com.moulberry.axiom.integration.worldguard.WorldGuardIntegration; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; +import java.util.List; + public class Integration { // todo: test if all this is working for both plotsqured, worldguard, plotsquared+worldguard public static boolean canBreakBlock(Player player, Block block) { - return PlotSquaredIntegration.canBreakBlock(player, block) && WorldGuardIntegration.canBreakBlock(player, block.getLocation()); + return PlotSquaredIntegration.canBreakBlock(player, block) && WorldGuardIntegration.canBreakBlock(player, block.getLocation()) && GriefDefenderIntegration.canBreakBlock(player, block.getLocation()); } public static boolean canPlaceBlock(Player player, org.bukkit.Location loc) { - return PlotSquaredIntegration.canPlaceBlock(player, loc) && WorldGuardIntegration.canPlaceBlock(player, loc); + return PlotSquaredIntegration.canPlaceBlock(player, loc) && WorldGuardIntegration.canPlaceBlock(player, loc) && GriefDefenderIntegration.canPlaceBlock(player, loc); } public static SectionPermissionChecker checkSection(Player player, World world, int cx, int cy, int cz) { SectionPermissionChecker plotSquared = PlotSquaredIntegration.checkSection(player, world, cx, cy, cz); SectionPermissionChecker worldGuard = WorldGuardIntegration.checkSection(player, world, cx, cy, cz); + SectionPermissionChecker griefDefender = GriefDefenderIntegration.checkSection(player, world, cx, cy, cz); - return SectionPermissionChecker.combine(plotSquared, worldGuard); + List checkers = List.of(plotSquared, worldGuard, griefDefender); + return SectionPermissionChecker.combine(checkers); } } diff --git a/src/main/java/com/moulberry/axiom/integration/SectionPermissionChecker.java b/src/main/java/com/moulberry/axiom/integration/SectionPermissionChecker.java index 50f1a5d..27df572 100644 --- a/src/main/java/com/moulberry/axiom/integration/SectionPermissionChecker.java +++ b/src/main/java/com/moulberry/axiom/integration/SectionPermissionChecker.java @@ -9,6 +9,16 @@ public interface SectionPermissionChecker { boolean allowed(int x, int y, int z); Box bounds(); + static SectionPermissionChecker combine(List checkers) { + if (checkers.isEmpty()) return NONE_ALLOWED; + + SectionPermissionChecker currentCombinedChecker = checkers.getFirst(); + for (int i = 1; i < checkers.size(); i++) { + currentCombinedChecker = combine(currentCombinedChecker , checkers.get(i)); + } + return currentCombinedChecker ; + } + static SectionPermissionChecker combine(SectionPermissionChecker first, SectionPermissionChecker second) { if (first.noneAllowed() || second.noneAllowed()) { return NONE_ALLOWED; diff --git a/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegration.java b/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegration.java new file mode 100644 index 0000000..a640d8f --- /dev/null +++ b/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegration.java @@ -0,0 +1,27 @@ +package com.moulberry.axiom.integration.griefdefender; + +import com.moulberry.axiom.integration.SectionPermissionChecker; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +public class GriefDefenderIntegration { + public static boolean canBreakBlock(Player player, Location loc) { + if (!Bukkit.getPluginManager().isPluginEnabled("GriefDefender")) { + return true; + } + return GriefDefenderIntegrationImpl.isBuilder(player, loc); + } + + public static boolean canPlaceBlock(Player player, Location loc) { + return canBreakBlock(player, loc); + } + + public static SectionPermissionChecker checkSection(Player player, World world, int cx, int cy, int cz) { + if (!Bukkit.getPluginManager().isPluginEnabled("GriefDefender")) { + return SectionPermissionChecker.ALL_ALLOWED; + } + return GriefDefenderIntegrationImpl.checkSection(player, world, cx, cy, cz); + } +} \ No newline at end of file diff --git a/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegrationImpl.java b/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegrationImpl.java new file mode 100644 index 0000000..b7b48e1 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/integration/griefdefender/GriefDefenderIntegrationImpl.java @@ -0,0 +1,80 @@ +package com.moulberry.axiom.integration.griefdefender; + +import com.griefdefender.api.GriefDefender; +import com.griefdefender.api.claim.Claim; +import com.griefdefender.api.claim.TrustTypes; +import com.griefdefender.lib.flowpowered.math.vector.Vector3i; +import com.moulberry.axiom.integration.SectionPermissionChecker; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class GriefDefenderIntegrationImpl { + + public static boolean isBuilder(Player player, Location loc) { + if (!GriefDefender.getCore().isEnabled(loc.getWorld().getUID())) { + return true; + } + + Claim claim = GriefDefender.getCore().getClaimAt(loc); + + if (claim == null) { + return true; + } + return claim.isUserTrusted(player.getUniqueId(), TrustTypes.BUILDER); + } + + public static SectionPermissionChecker checkSection(Player player, World world, int cx, int cy, int cz) { + if (!GriefDefender.getCore().isEnabled(world.getUID())) { + return SectionPermissionChecker.ALL_ALLOWED; + } + + int minX = Math.min(cx * 16, cx * 16 + 15); + int minY = Math.min(cy * 16, cy * 16 + 15); + int minZ = Math.min(cz * 16, cz * 16 + 15); + int maxX = Math.max(cx * 16, cx * 16 + 15); + int maxY = Math.max(cy * 16, cy * 16 + 15); + int maxZ = Math.max(cz * 16, cz * 16 + 15); + + + Vector3i lesserBoundaryCorner = new Vector3i(minX, minY, minZ); + Vector3i greaterBoundaryCorner = new Vector3i(maxX, maxY, maxZ); + + List claims = new ArrayList<>(GriefDefender.getCore().getAllClaims()); + if (!GriefDefender.getCore().isEnabled(world.getUID())) { + return SectionPermissionChecker.ALL_ALLOWED; + } + + claims.removeIf(claim -> !claim.getWorldUniqueId().equals(world.getUID())); + + claims.removeIf(claim -> !claim.isUserTrusted(player.getUniqueId(), TrustTypes.BUILDER)); + + List claimList = getClaimInArea(claims, lesserBoundaryCorner, greaterBoundaryCorner); + + if (claimList.isEmpty()) { + + return SectionPermissionChecker.NONE_ALLOWED; + } + + return SectionPermissionChecker.ALL_ALLOWED; + } + + private static @NotNull List getClaimInArea(List claims, Vector3i lesserBoundaryCorner, Vector3i greaterBoundaryCorner) { + + List claimList = new ArrayList<>(); + for (Claim claim : claims) { + Vector3i lesser = claim.getLesserBoundaryCorner(); + Vector3i greater = claim.getGreaterBoundaryCorner(); + if (lesser.getX() <= lesserBoundaryCorner.getX() && lesser.getZ() <= lesserBoundaryCorner.getZ() && greater.getX() >= greaterBoundaryCorner.getX() && greater.getZ() >= greaterBoundaryCorner.getZ()) { + + claimList.add(claim); + } + } + + return claimList; + } +} \ No newline at end of file