From c83e5f640b923e7711d9ac467d0970b61f5737ba Mon Sep 17 00:00:00 2001 From: 56738 Date: Sun, 6 Oct 2024 10:28:44 +0200 Subject: [PATCH] Show message when trying to select an entity in a disallowed region --- .../player/PlayerDiscoverElementEvent.java | 48 +++++++++++++++++++ .../command/SessionCommands.java | 2 + .../command/sender/EasPlayer.java | 11 +++++ .../context/ChangeContext.java | 2 + .../editor/node/ElementSelectionNodeImpl.java | 2 +- .../easyarmorstands/messages.properties | 4 ++ .../griefdefender/GriefDefenderAddon.java | 1 + .../plotsquared/v6/PlotSquaredAddon.java | 1 + .../residence/ResidenceAddon.java | 1 + .../worldguard/v6/WorldGuardAddon.java | 1 + .../worldguard/v7/WorldGuardAddon.java | 1 + .../region/RegionListener.java | 17 ++++++- 12 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 easyarmorstands-api/src/main/java/me/m56738/easyarmorstands/api/event/player/PlayerDiscoverElementEvent.java diff --git a/easyarmorstands-api/src/main/java/me/m56738/easyarmorstands/api/event/player/PlayerDiscoverElementEvent.java b/easyarmorstands-api/src/main/java/me/m56738/easyarmorstands/api/event/player/PlayerDiscoverElementEvent.java new file mode 100644 index 00000000..e6906ab7 --- /dev/null +++ b/easyarmorstands-api/src/main/java/me/m56738/easyarmorstands/api/event/player/PlayerDiscoverElementEvent.java @@ -0,0 +1,48 @@ +package me.m56738.easyarmorstands.api.event.player; + +import me.m56738.easyarmorstands.api.element.EditableElement; +import me.m56738.easyarmorstands.api.element.Element; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +/** + * Called to check whether an element can be discovered by a player. + *

+ * Can be used to prevent displaying buttons for certain elements. + */ +public class PlayerDiscoverElementEvent extends PlayerEvent implements Cancellable { + private static final HandlerList handlerList = new HandlerList(); + private final EditableElement element; + private boolean cancelled; + + public PlayerDiscoverElementEvent(@NotNull Player player, @NotNull EditableElement element) { + super(player); + this.element = element; + } + + public static @NotNull HandlerList getHandlerList() { + return handlerList; + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } + + public @NotNull Element getElement() { + return element; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } +} diff --git a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/SessionCommands.java b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/SessionCommands.java index 1eb0be35..b858bd26 100644 --- a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/SessionCommands.java +++ b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/SessionCommands.java @@ -141,6 +141,8 @@ public void group(EasPlayer sender, Session session, @Argument("entities") Multi return; } group.addMember(editableElement); + } else { + return; } } } diff --git a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/sender/EasPlayer.java b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/sender/EasPlayer.java index 22e40b02..fcf724fa 100644 --- a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/sender/EasPlayer.java +++ b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/command/sender/EasPlayer.java @@ -7,6 +7,7 @@ import me.m56738.easyarmorstands.api.element.ElementType; import me.m56738.easyarmorstands.api.event.player.PlayerCreateElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerDestroyElementEvent; +import me.m56738.easyarmorstands.api.event.player.PlayerDiscoverElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerEditElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerEditPropertyEvent; import me.m56738.easyarmorstands.api.property.Property; @@ -104,6 +105,16 @@ public boolean canEditElement(EditableElement element) { return !event.isCancelled(); } + @Override + public boolean canDiscoverElement(EditableElement element) { + if (!element.canEdit(player)) { + return false; + } + PlayerDiscoverElementEvent event = new PlayerDiscoverElementEvent(player, element); + Bukkit.getPluginManager().callEvent(event); + return !event.isCancelled(); + } + @Override public boolean canChangeProperty(Element element, Property property, T value) { PlayerEditPropertyEvent event = new PlayerEditPropertyEvent<>(player, element, property, property.getValue(), value); diff --git a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/context/ChangeContext.java b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/context/ChangeContext.java index 095254f7..bf0c0473 100644 --- a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/context/ChangeContext.java +++ b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/context/ChangeContext.java @@ -37,5 +37,7 @@ public interface ChangeContext { boolean canEditElement(EditableElement element); + boolean canDiscoverElement(EditableElement element); + boolean canChangeProperty(Element element, Property property, T value); } diff --git a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/editor/node/ElementSelectionNodeImpl.java b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/editor/node/ElementSelectionNodeImpl.java index 9a4f471b..bd9a2f79 100644 --- a/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/editor/node/ElementSelectionNodeImpl.java +++ b/easyarmorstands-plugin/src/main/java/me/m56738/easyarmorstands/editor/node/ElementSelectionNodeImpl.java @@ -235,7 +235,7 @@ private SelectableElement getElement(ElementDiscoveryEntry entry) { private ElementEntry addEntry(ElementDiscoveryEntry entry) { SelectableElement element = entry.getElement(); ChangeContext context = new EasPlayer(session.player()); - if (element == null || !context.canEditElement(element)) { + if (element == null || !context.canDiscoverElement(element)) { return ElementEntry.EMPTY; } ElementButton button = new ElementButton(entry, session, element); diff --git a/easyarmorstands-plugin/src/main/resources/me/m56738/easyarmorstands/messages.properties b/easyarmorstands-plugin/src/main/resources/me/m56738/easyarmorstands/messages.properties index 1837e591..96f4f6e5 100644 --- a/easyarmorstands-plugin/src/main/resources/me/m56738/easyarmorstands/messages.properties +++ b/easyarmorstands-plugin/src/main/resources/me/m56738/easyarmorstands/messages.properties @@ -104,6 +104,7 @@ easyarmorstands.error.destroy-unsupported=This entity cannot be destroyed. easyarmorstands.error.entity-not-found=Entity not found. easyarmorstands.error.glow-color-unsupported=This entity doesn''t support a custom glow color. easyarmorstands.error.griefdefender.deny-create=Can''t spawn this entity on this claim. +easyarmorstands.error.griefdefender.deny-select=Can''t select this entity on this claim. easyarmorstands.error.griefdefender.deny-destroy=Can''t delete this entity on this claim. easyarmorstands.error.group-selected=This command doesn''t support editing a group of entities. easyarmorstands.error.group-too-big=Too many entities. @@ -115,9 +116,11 @@ easyarmorstands.error.nothing-selected=You are not editing an entity. easyarmorstands.error.nothing-to-redo=No changes left to redo. easyarmorstands.error.nothing-to-undo=No changes left to undo. easyarmorstands.error.plotsquared.deny-create=Can''t spawn this entity on this plot. +easyarmorstands.error.plotsquared.deny-select=Can''t select this entity on this plot. easyarmorstands.error.plotsquared.deny-destroy=Can''t delete this entity on this plot. easyarmorstands.error.reset-unsupported=The selected property cannot be reset. easyarmorstands.error.residence.deny-create=Can''t spawn this entity on this claim. +easyarmorstands.error.residence.deny-select=Can''t select this entity on this claim. easyarmorstands.error.residence.deny-destroy=Can''t delete this entity on this claim. easyarmorstands.error.scale-unsupported=This entity cannot be rescaled. easyarmorstands.error.set-unsupported=The selected tool cannot accept custom values. @@ -127,6 +130,7 @@ easyarmorstands.error.text-line-width-unsupported=This entity doesn''t support a easyarmorstands.error.text-unsupported=This entity doesn''t support custom text. easyarmorstands.error.unsupported-entity=This entity cannot be edited. easyarmorstands.error.worldguard.deny-create=Can''t spawn this entity here. +easyarmorstands.error.worldguard.deny-select=Can''t select this entity here. easyarmorstands.error.worldguard.deny-destroy=Can''t destroy that entity here. easyarmorstands.hint.clear-clipboard=Use {0} to clear your clipboard. easyarmorstands.hint.copy-all-properties=Use {0} to copy all properties of an entity. diff --git a/easyarmorstands-region/easyarmorstands-griefdefender/src/main/java/me/m56738/easyarmorstands/griefdefender/GriefDefenderAddon.java b/easyarmorstands-region/easyarmorstands-griefdefender/src/main/java/me/m56738/easyarmorstands/griefdefender/GriefDefenderAddon.java index 1854bf85..8a193a03 100644 --- a/easyarmorstands-region/easyarmorstands-griefdefender/src/main/java/me/m56738/easyarmorstands/griefdefender/GriefDefenderAddon.java +++ b/easyarmorstands-region/easyarmorstands-griefdefender/src/main/java/me/m56738/easyarmorstands/griefdefender/GriefDefenderAddon.java @@ -13,6 +13,7 @@ public GriefDefenderAddon() { Permissions.GRIEFDEFENDER_BYPASS, new GriefDefenderPrivilegeChecker(GriefDefender.getCore()), Message.error("easyarmorstands.error.griefdefender.deny-create"), + Message.error("easyarmorstands.error.griefdefender.deny-select"), Message.error("easyarmorstands.error.griefdefender.deny-destroy") ), plugin); } diff --git a/easyarmorstands-region/easyarmorstands-plotsquared/src/main/java/me/m56738/easyarmorstands/plotsquared/v6/PlotSquaredAddon.java b/easyarmorstands-region/easyarmorstands-plotsquared/src/main/java/me/m56738/easyarmorstands/plotsquared/v6/PlotSquaredAddon.java index 778332e4..b86c3f3f 100644 --- a/easyarmorstands-region/easyarmorstands-plotsquared/src/main/java/me/m56738/easyarmorstands/plotsquared/v6/PlotSquaredAddon.java +++ b/easyarmorstands-region/easyarmorstands-plotsquared/src/main/java/me/m56738/easyarmorstands/plotsquared/v6/PlotSquaredAddon.java @@ -13,6 +13,7 @@ public PlotSquaredAddon() { Permissions.PLOTSQUARED_BYPASS, new PlotSquaredPrivilegeChecker(new PlotAPI()), Message.error("easyarmorstands.error.plotsquared.deny-create"), + Message.error("easyarmorstands.error.plotsquared.deny-select"), Message.error("easyarmorstands.error.plotsquared.deny-destroy") ), plugin); } diff --git a/easyarmorstands-region/easyarmorstands-residence/src/main/java/me/m56738/easyarmorstands/residence/ResidenceAddon.java b/easyarmorstands-region/easyarmorstands-residence/src/main/java/me/m56738/easyarmorstands/residence/ResidenceAddon.java index cae9c8b7..55cd5f62 100644 --- a/easyarmorstands-region/easyarmorstands-residence/src/main/java/me/m56738/easyarmorstands/residence/ResidenceAddon.java +++ b/easyarmorstands-region/easyarmorstands-residence/src/main/java/me/m56738/easyarmorstands/residence/ResidenceAddon.java @@ -13,6 +13,7 @@ public ResidenceAddon() { Permissions.RESIDENCE_BYPASS, new ResidencePrivilegeChecker(Residence.getInstance()), Message.error("easyarmorstands.error.residence.deny-create"), + Message.error("easyarmorstands.error.residence.deny-select"), Message.error("easyarmorstands.error.residence.deny-destroy") ), plugin); } diff --git a/easyarmorstands-region/easyarmorstands-worldguard-v6/src/main/java/me/m56738/easyarmorstands/worldguard/v6/WorldGuardAddon.java b/easyarmorstands-region/easyarmorstands-worldguard-v6/src/main/java/me/m56738/easyarmorstands/worldguard/v6/WorldGuardAddon.java index 3ed757a1..12309966 100644 --- a/easyarmorstands-region/easyarmorstands-worldguard-v6/src/main/java/me/m56738/easyarmorstands/worldguard/v6/WorldGuardAddon.java +++ b/easyarmorstands-region/easyarmorstands-worldguard-v6/src/main/java/me/m56738/easyarmorstands/worldguard/v6/WorldGuardAddon.java @@ -12,6 +12,7 @@ public WorldGuardAddon() { Permissions.WORLDGUARD_BYPASS, new WorldGuardPrivilegeChecker(), Message.error("easyarmorstands.error.worldguard.deny-create"), + Message.error("easyarmorstands.error.worldguard.deny-select"), Message.error("easyarmorstands.error.worldguard.deny-destroy") ), plugin); } diff --git a/easyarmorstands-region/easyarmorstands-worldguard-v7/src/main/java/me/m56738/easyarmorstands/worldguard/v7/WorldGuardAddon.java b/easyarmorstands-region/easyarmorstands-worldguard-v7/src/main/java/me/m56738/easyarmorstands/worldguard/v7/WorldGuardAddon.java index 0c778042..faef7e38 100644 --- a/easyarmorstands-region/easyarmorstands-worldguard-v7/src/main/java/me/m56738/easyarmorstands/worldguard/v7/WorldGuardAddon.java +++ b/easyarmorstands-region/easyarmorstands-worldguard-v7/src/main/java/me/m56738/easyarmorstands/worldguard/v7/WorldGuardAddon.java @@ -12,6 +12,7 @@ public WorldGuardAddon() { Permissions.WORLDGUARD_BYPASS, new WorldGuardPrivilegeChecker(), Message.error("easyarmorstands.error.worldguard.deny-create"), + Message.error("easyarmorstands.error.worldguard.deny-select"), Message.error("easyarmorstands.error.worldguard.deny-destroy") ), plugin); } diff --git a/easyarmorstands-region/src/main/java/me/m56738/easyarmorstands/region/RegionListener.java b/easyarmorstands-region/src/main/java/me/m56738/easyarmorstands/region/RegionListener.java index 24a6a618..a9975cbc 100644 --- a/easyarmorstands-region/src/main/java/me/m56738/easyarmorstands/region/RegionListener.java +++ b/easyarmorstands-region/src/main/java/me/m56738/easyarmorstands/region/RegionListener.java @@ -5,6 +5,7 @@ import me.m56738.easyarmorstands.api.element.EntityElement; import me.m56738.easyarmorstands.api.event.player.PlayerCreateElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerDestroyElementEvent; +import me.m56738.easyarmorstands.api.event.player.PlayerDiscoverElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerEditElementEvent; import me.m56738.easyarmorstands.api.event.player.PlayerEditPropertyEvent; import me.m56738.easyarmorstands.api.event.session.SessionStartEvent; @@ -26,12 +27,14 @@ public class RegionListener implements Listener { private final String bypassPermission; private final RegionPrivilegeChecker privilegeChecker; private final Component createError; + private final Component selectError; private final Component destroyError; - public RegionListener(String bypassPermission, RegionPrivilegeChecker privilegeChecker, Component createError, Component destroyError) { + public RegionListener(String bypassPermission, RegionPrivilegeChecker privilegeChecker, Component createError, Component selectError, Component destroyError) { this.bypassPermission = bypassPermission; this.privilegeChecker = privilegeChecker; this.createError = createError; + this.selectError = selectError; this.destroyError = destroyError; } @@ -48,6 +51,17 @@ public void onInitialize(SessionStartEvent event) { bypassCache.remove(event.getPlayer()); } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onDiscover(PlayerDiscoverElementEvent event) { + if (isAllowed(event.getPlayer(), event.getElement().getProperties().get(EntityPropertyTypes.LOCATION).getValue())) { + return; + } + if (event.getPlayer().hasPermission(bypassPermission)) { + return; + } + event.setCancelled(true); + } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEdit(PlayerEditElementEvent event) { if (isAllowed(event.getPlayer(), event.getElement().getProperties().get(EntityPropertyTypes.LOCATION).getValue())) { @@ -57,6 +71,7 @@ public void onEdit(PlayerEditElementEvent event) { return; } event.setCancelled(true); + audience(event.getPlayer()).sendMessage(selectError); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)