From f1da6105c0141d75da483e866cca2107676ed8ba Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Wed, 19 Jul 2023 10:07:08 -0500 Subject: [PATCH] feat: disable playing emotes in WG region --- .../hmccosmetics/hooks/worldguard/WGHook.java | 17 +++++++-- .../hooks/worldguard/WGListener.java | 36 ++++++++++++++----- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGHook.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGHook.java index 027bd5d5..10316ef9 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGHook.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGHook.java @@ -17,21 +17,26 @@ public class WGHook { /** * @implNote Please use {@link #getCosmeticEnableFlag()} instead */ - public static StateFlag COSMETIC_ENABLE_FLAG; + private static StateFlag COSMETIC_ENABLE_FLAG; + + private static StateFlag EMOTES_ENABLE_FLAG; /** * @implNote Please use {@link #getCosmeticWardrobeFlag()} instead */ - public static StringFlag COSMETIC_WARDROBE_FLAG; + private static StringFlag COSMETIC_WARDROBE_FLAG; public WGHook() { FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry(); try { StateFlag cosmeticFlag = new StateFlag("cosmetic-enable", false); + StateFlag emoteFlag = new StateFlag("emotes-enable", false); StringFlag wardrobeFlag = new StringFlag("cosmetic-wardrobe"); registry.register(cosmeticFlag); + registry.register(emoteFlag); registry.register(wardrobeFlag); COSMETIC_ENABLE_FLAG = cosmeticFlag; + EMOTES_ENABLE_FLAG = emoteFlag; COSMETIC_WARDROBE_FLAG = wardrobeFlag; } catch (FlagConflictException e) { Flag existing = registry.get("cosmetic-enable"); @@ -53,6 +58,14 @@ public class WGHook { return COSMETIC_ENABLE_FLAG; } + /** + * Gets the emotes enable {@link StateFlag} + * @return The emotes enable {@link StateFlag} + */ + public static StateFlag getEmotesEnableFlag() { + return EMOTES_ENABLE_FLAG; + } + /** * Gets the cosmetic wardrobe {@link StateFlag} * @return The cosmetic wardrobe {@link StateFlag} diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGListener.java index 451ea49b..51f9f8e4 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/hooks/worldguard/WGListener.java @@ -1,5 +1,6 @@ package com.hibiscusmc.hmccosmetics.hooks.worldguard; +import com.hibiscusmc.hmccosmetics.api.events.PlayerEmoteStartEvent; import com.hibiscusmc.hmccosmetics.config.Wardrobe; import com.hibiscusmc.hmccosmetics.config.WardrobeSettings; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; @@ -11,6 +12,7 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.RegionContainer; import com.sk89q.worldguard.protection.regions.RegionQuery; import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; @@ -26,10 +28,7 @@ public class WGListener implements Listener { CosmeticUser user = CosmeticUsers.getUser(event.getPlayer()); if (user == null) return; Location location = event.getPlayer().getLocation(); - com.sk89q.worldedit.util.Location loc = BukkitAdapter.adapt(location); - RegionContainer region = WorldGuard.getInstance().getPlatform().getRegionContainer(); - RegionQuery query = region.createQuery(); - ApplicableRegionSet set = query.getApplicableRegions(loc); + ApplicableRegionSet set = getRegions(location); if (user.getHidden()) { if (user.getHiddenReason() == CosmeticUser.HiddenReason.WORLDGUARD && set.getRegions().size() == 0) { user.showCosmetics(); @@ -57,10 +56,7 @@ public class WGListener implements Listener { CosmeticUser user = CosmeticUsers.getUser(event.getPlayer()); if (user == null) return; Location location = event.getTo(); - com.sk89q.worldedit.util.Location loc = BukkitAdapter.adapt(location); - RegionContainer region = WorldGuard.getInstance().getPlatform().getRegionContainer(); - RegionQuery query = region.createQuery(); - ApplicableRegionSet set = query.getApplicableRegions(loc); + ApplicableRegionSet set = getRegions(location); if (user.getHidden()) { if (user.getHiddenReason() == CosmeticUser.HiddenReason.WORLDGUARD && set.getRegions().size() == 0) { user.showCosmetics(); @@ -82,4 +78,28 @@ public class WGListener implements Listener { } } } + + @EventHandler + public void onPlayerEmote(PlayerEmoteStartEvent event) { + Player player = event.getUser().getPlayer(); + if (player == null) return; + Location location = player.getLocation(); + ApplicableRegionSet set = getRegions(location); + for (ProtectedRegion protectedRegion : set.getRegions()) { + if (protectedRegion.getFlags().containsKey(WGHook.getEmotesEnableFlag())) { + if (protectedRegion.getFlags().get(WGHook.getEmotesEnableFlag()).toString().equalsIgnoreCase("DENY")) { + event.setCancelled(true); + return; + } + return; + } + } + } + + private ApplicableRegionSet getRegions(Location location) { + com.sk89q.worldedit.util.Location loc = BukkitAdapter.adapt(location); + RegionContainer region = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = region.createQuery(); + return query.getApplicableRegions(loc); + } }