diff --git a/bukkit/src/main/java/org/popcraft/bolt/event/LockEntityEvent.java b/bukkit/src/main/java/org/popcraft/bolt/event/LockEntityEvent.java new file mode 100644 index 00000000..8bcc83f6 --- /dev/null +++ b/bukkit/src/main/java/org/popcraft/bolt/event/LockEntityEvent.java @@ -0,0 +1,22 @@ +package org.popcraft.bolt.event; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +public class LockEntityEvent extends Cancellable implements Event { + private final Player player; + private final Entity entity; + + public LockEntityEvent(Player player, Entity entity) { + this.player = player; + this.entity = entity; + } + + public Player getPlayer() { + return this.player; + } + + public Entity getEntity() { + return this.entity; + } +} diff --git a/bukkit/src/main/java/org/popcraft/bolt/listeners/InteractionListener.java b/bukkit/src/main/java/org/popcraft/bolt/listeners/InteractionListener.java index 536d6bb8..8dab06bc 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/listeners/InteractionListener.java +++ b/bukkit/src/main/java/org/popcraft/bolt/listeners/InteractionListener.java @@ -7,7 +7,9 @@ import org.bukkit.entity.Player; import org.popcraft.bolt.BoltPlugin; import org.popcraft.bolt.access.Access; +import org.popcraft.bolt.event.Cancellable; import org.popcraft.bolt.event.LockBlockEvent; +import org.popcraft.bolt.event.LockEntityEvent; import org.popcraft.bolt.lang.Translation; import org.popcraft.bolt.protection.Protection; import org.popcraft.bolt.util.Action; @@ -62,18 +64,23 @@ private boolean triggerAction(final Player player, final Protection protection, final Action.Type actionType = action.getType(); switch (actionType) { case LOCK -> { + final Cancellable event; if (object instanceof final Block block) { - final LockBlockEvent event = new LockBlockEvent(player, block); - plugin.getEventBus().post(event); - if (event.isCancelled()) { - BoltComponents.sendMessage( - player, - Translation.CLICK_LOCKED_CANCELLED, - plugin.isUseActionBar(), - Placeholder.component(Translation.Placeholder.PROTECTION, Protections.displayType(block, player)) - ); - break; - } + event = new LockBlockEvent(player, block); + } else if (object instanceof final Entity entity) { + event = new LockEntityEvent(player, entity); + } else { + throw new IllegalStateException("Protection is not a block or entity"); + } + plugin.getEventBus().post(event); + if (event.isCancelled()) { + BoltComponents.sendMessage( + player, + Translation.CLICK_LOCKED_CANCELLED, + plugin.isUseActionBar(), + Placeholder.component(Translation.Placeholder.PROTECTION, displayName) + ); + break; } final String protectionType = Optional.ofNullable(action.getData()) .flatMap(type -> plugin.getBolt().getAccessRegistry().getProtectionByType(type))