Skip to content

Commit

Permalink
Show message when trying to select an entity in a disallowed region
Browse files Browse the repository at this point in the history
  • Loading branch information
56738 committed Oct 6, 2024
1 parent 75fcf39 commit c83e5f6
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public void group(EasPlayer sender, Session session, @Argument("entities") Multi
return;
}
group.addMember(editableElement);
} else {
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <T> boolean canChangeProperty(Element element, Property<T> property, T value) {
PlayerEditPropertyEvent<T> event = new PlayerEditPropertyEvent<>(player, element, property, property.getValue(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public interface ChangeContext {

boolean canEditElement(EditableElement element);

boolean canDiscoverElement(EditableElement element);

<T> boolean canChangeProperty(Element element, Property<T> property, T value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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())) {
Expand All @@ -57,6 +71,7 @@ public void onEdit(PlayerEditElementEvent event) {
return;
}
event.setCancelled(true);
audience(event.getPlayer()).sendMessage(selectError);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down

0 comments on commit c83e5f6

Please sign in to comment.