From 20c39bc84f539e3e749f018973eb8b4769c2ec32 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 17 Dec 2020 16:43:56 +0000 Subject: [PATCH] Added basis for registering/checking WorldGuard flags --- .../ecoenchants/enchantments/EcoEnchant.java | 11 +++- .../enchantments/util/WatcherTriggers.java | 12 +++- .../worldguard/WorldguardManager.java | 59 +++++++++++++++++++ .../worldguard/WorldguardWrapper.java | 10 ++++ .../plugins/WorldguardIntegrationImpl.java | 29 +++++++++ .../ecoenchants/util/internal/Loader.java | 7 ++- 6 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardManager.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardWrapper.java create mode 100644 Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/plugins/WorldguardIntegrationImpl.java diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java index 6d7ef8ae..d6e1a2d7 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/EcoEnchant.java @@ -27,7 +27,14 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; @SuppressWarnings({"unchecked", "deprecation"}) @@ -78,6 +85,8 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist Bukkit.getPluginManager().addPermission(permission); } + //WorldguardManager.registerFlag(this.getPermissionName() + "-enabled", true); + if(type.getRequiredToExtend() != null && !type.getRequiredToExtend().isInstance(this)) { Logger.error("Enchantment " + key + " has type " + this.getType().getName() + " but doesn't extend " + type.getRequiredToExtend().getName()); return; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java index b289d875..d11c1035 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WatcherTriggers.java @@ -10,12 +10,20 @@ import com.willfp.ecoenchants.nms.TridentStack; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.entity.*; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Trident; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockDamageEvent; -import org.bukkit.event.entity.*; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityShootBowEvent; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffectType; diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardManager.java b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardManager.java new file mode 100644 index 00000000..f86ae04d --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardManager.java @@ -0,0 +1,59 @@ +package com.willfp.ecoenchants.integrations.worldguard; + +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import org.bukkit.Location; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; + +import java.util.HashSet; +import java.util.Set; + +public class WorldguardManager { + private static final Set worldguardWrappers = new HashSet<>(); + + /** + * Register a new WorldGuard integration + * + * @param worldguard The integration to register + */ + public static void register(WorldguardWrapper worldguard) { + worldguardWrappers.add(worldguard); + } + + /** + * Register a new StateFlag with worldguard + * + * @param flagName The name of the flag + * @param defaultValue The default value for the flag to have + */ + public static void registerFlag(String flagName, boolean defaultValue) { + worldguardWrappers.forEach(worldguardWrapper -> worldguardWrapper.registerFlag(flagName, defaultValue)); + } + + /** + * Get if an enchant is enabled at a player's location + * + * @param enchant The enchantment to check + * @param player The player to query + * @return If the enchantment is enabled at a player's location + */ + public static boolean enabledForPlayer(EcoEnchant enchant, LivingEntity player) { + if (!(player instanceof Player)) return true; + if(worldguardWrappers.isEmpty()) return true; + return worldguardWrappers.stream().anyMatch(worldguardWrapper -> worldguardWrapper.enabledForPlayer(enchant, (Player) player, player.getLocation())); + } + + /** + * Get if an enchant is enabled at a specific location + * + * @param enchant The enchantment to check + * @param player The player to query + * @param location The location to query + * @return If the enchantment is enabled at a player's location + */ + public static boolean enabledForPlayer(EcoEnchant enchant, LivingEntity player, Location location) { + if (!(player instanceof Player)) return true; + if(worldguardWrappers.isEmpty()) return true; + return worldguardWrappers.stream().anyMatch(worldguardWrapper -> worldguardWrapper.enabledForPlayer(enchant, (Player) player, location)); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardWrapper.java b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardWrapper.java new file mode 100644 index 00000000..1206e2d5 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/WorldguardWrapper.java @@ -0,0 +1,10 @@ +package com.willfp.ecoenchants.integrations.worldguard; + +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public interface WorldguardWrapper { + void registerFlag(String name, boolean def); + boolean enabledForPlayer(EcoEnchant enchant, Player player, Location location); +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/plugins/WorldguardIntegrationImpl.java b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/plugins/WorldguardIntegrationImpl.java new file mode 100644 index 00000000..9ebf1fb5 --- /dev/null +++ b/Plugin/src/main/java/com/willfp/ecoenchants/integrations/worldguard/plugins/WorldguardIntegrationImpl.java @@ -0,0 +1,29 @@ +package com.willfp.ecoenchants.integrations.worldguard.plugins; + +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.flags.StateFlag; +import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; +import com.willfp.ecoenchants.enchantments.EcoEnchant; +import com.willfp.ecoenchants.integrations.worldguard.WorldguardWrapper; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public class WorldguardIntegrationImpl implements WorldguardWrapper { + private static final FlagRegistry REGISTRY = WorldGuard.getInstance().getFlagRegistry(); + + @Override + public void registerFlag(String name, boolean def) { + StateFlag flag = new StateFlag(name, def); + if(REGISTRY.get(name) == null) { + REGISTRY.register(flag); + } + } + + @Override + public boolean enabledForPlayer(EcoEnchant enchant, Player player, Location location) { + if(WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(WorldGuardPlugin.inst().wrapPlayer(player), BukkitAdapter.adapt(location.getWorld()))) return true; + return WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().testState(BukkitAdapter.adapt(location), WorldGuardPlugin.inst().wrapPlayer(player), (StateFlag) REGISTRY.get(enchant.getKey().getKey() + "-enabled")); + } +} diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/util/internal/Loader.java b/Plugin/src/main/java/com/willfp/ecoenchants/util/internal/Loader.java index 6db911c0..0f73e1da 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/util/internal/Loader.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/util/internal/Loader.java @@ -36,6 +36,8 @@ import com.willfp.ecoenchants.integrations.mcmmo.McmmoManager; import com.willfp.ecoenchants.integrations.mcmmo.plugins.McmmoIntegrationImpl; import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager; import com.willfp.ecoenchants.integrations.placeholder.plugins.PlaceholderIntegrationPAPI; +import com.willfp.ecoenchants.integrations.worldguard.WorldguardManager; +import com.willfp.ecoenchants.integrations.worldguard.plugins.WorldguardIntegrationImpl; import com.willfp.ecoenchants.util.interfaces.Callable; import com.willfp.ecoenchants.util.interfaces.EcoRunnable; import com.willfp.ecoenchants.util.internal.updater.PlayerJoinListener; @@ -115,7 +117,10 @@ public class Loader { final HashMap integrations = new HashMap() {{ // AntiGrief - put("WorldGuard", () -> AntigriefManager.register(new AntigriefWorldGuard())); + put("WorldGuard", () -> { + AntigriefManager.register(new AntigriefWorldGuard()); + WorldguardManager.register(new WorldguardIntegrationImpl()); + }); put("GriefPrevention", () -> AntigriefManager.register(new AntigriefGriefPrevention())); put("FactionsUUID", () -> AntigriefManager.register(new AntigriefFactionsUUID())); put("Towny", () -> AntigriefManager.register(new AntigriefTowny()));