From 7405f1551093cddbda0235e68e1fc92c7d3ed322 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Thu, 21 Mar 2024 23:25:48 +0100 Subject: [PATCH] Add support for GriefPrevention Protection Refactored the YardWatch.java file to check if the GriefPrevention plugin is enabled. If enabled, GriefPrevention Protection is registered. Additionally, a new class GriefPreventionProtection.java was added which checks if a location is protected, if a player can break/place a block, interact with a block or entity, or damage an entity based on GriefPrevention's claims. Moreover, added a new dependency for GriefPrevention in pom.xml. --- pom.xml | 7 +- .../youhavetrouble/yardwatch/YardWatch.java | 18 +++-- .../hooks/GriefPreventionProtection.java | 65 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/yardwatch/hooks/GriefPreventionProtection.java diff --git a/pom.xml b/pom.xml index 373d0a5..54a66f8 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,11 @@ 2.0.0 compile - + + com.github.GriefPrevention + GriefPrevention + 16.18.2 + provided + diff --git a/src/main/java/me/youhavetrouble/yardwatch/YardWatch.java b/src/main/java/me/youhavetrouble/yardwatch/YardWatch.java index f2d0991..533de57 100644 --- a/src/main/java/me/youhavetrouble/yardwatch/YardWatch.java +++ b/src/main/java/me/youhavetrouble/yardwatch/YardWatch.java @@ -1,6 +1,8 @@ package me.youhavetrouble.yardwatch; +import me.youhavetrouble.yardwatch.hooks.GriefPreventionProtection; import me.youhavetrouble.yardwatch.hooks.WorldGuardProtection; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; @@ -8,13 +10,19 @@ public final class YardWatch extends JavaPlugin { @Override public void onEnable() { + PluginManager pluginManager = getServer().getPluginManager(); - if (getServer().getPluginManager().isPluginEnabled("WorldGuard")) { - getServer().getServicesManager().register( - Protection.class, new WorldGuardProtection(this), this, ServicePriority.Normal - ); - } + if (pluginManager.isPluginEnabled("WorldGuard")) { + getServer().getServicesManager().register( + Protection.class, new WorldGuardProtection(this), this, ServicePriority.Normal + ); + } + if (pluginManager.isPluginEnabled("GriefPrevention")) { + getServer().getServicesManager().register( + Protection.class, new GriefPreventionProtection(this), this, ServicePriority.Normal + ); + } } } diff --git a/src/main/java/me/youhavetrouble/yardwatch/hooks/GriefPreventionProtection.java b/src/main/java/me/youhavetrouble/yardwatch/hooks/GriefPreventionProtection.java new file mode 100644 index 0000000..c66ed36 --- /dev/null +++ b/src/main/java/me/youhavetrouble/yardwatch/hooks/GriefPreventionProtection.java @@ -0,0 +1,65 @@ +package me.youhavetrouble.yardwatch.hooks; + +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import me.youhavetrouble.yardwatch.Protection; +import me.youhavetrouble.yardwatch.YardWatch; +import org.bukkit.Location; +import org.bukkit.block.BlockState; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +import java.util.UUID; + +public class GriefPreventionProtection implements Protection { + + private final YardWatch plugin; + + public GriefPreventionProtection(YardWatch plugin) { + this.plugin = plugin; + } + + @Override + public boolean isEnabled() { + return plugin.getServer().getPluginManager().isPluginEnabled("GriefPrevention"); + } + + @Override + public boolean isProtected(Location location) { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(location, true, null); + if (claim == null) return false; + return claim.checkPermission(UUID.fromString("00000000-0000-0000-0000-000000000000"), ClaimPermission.Build, null) != null; + } + + @Override + public boolean canBreakBlock(Player player, BlockState blockState) { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(blockState.getLocation(), true, null); + return claim == null || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Build) || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Edit); + } + + @Override + public boolean canPlaceBlock(Player player, Location location) { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(location, true, null); + return claim == null || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Build) || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Edit); + } + + @Override + public boolean canInteract(Player player, BlockState blockState) { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(blockState.getLocation(), true, null); + return claim == null || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Access); + } + + @Override + public boolean canInteract(Player player, Entity target) { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(target.getLocation(), true, null); + return claim == null || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Access); + } + + @Override + public boolean canDamage(Entity damager, Entity target) { + if (!(damager instanceof Player player)) return true; + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(target.getLocation(), true, null); + return claim == null || claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Access); + } +}