Skip to content

Commit

Permalink
Add LockEntityEvent (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymiel authored Feb 12, 2024
1 parent 206eedc commit 5bec292
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
22 changes: 22 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/event/LockEntityEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 5bec292

Please sign in to comment.