Skip to content

Commit

Permalink
Event system
Browse files Browse the repository at this point in the history
Preliminary work for #90
  • Loading branch information
rymiel committed Feb 11, 2024
1 parent e99b44c commit 0681e27
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ dependencies {
implementation(group = "net.kyori", name = "adventure-api", version = "4.15.0")
implementation(group = "net.kyori", name = "adventure-text-minimessage", version = "4.15.0")
implementation(group = "net.kyori", name = "adventure-platform-bukkit", version = "4.3.2")
implementation(group = "net.kyori", name = "event-api", version = "3.0.0") {
exclude(module = "guava")
exclude(module = "checker-qual")
}
implementation(group = "org.bstats", name = "bstats-bukkit", version = "3.0.2")
implementation(group = "org.popcraft", name = "chunky-nbt", version = "1.3.127")
implementation(project(":bolt-common"))
Expand Down
4 changes: 4 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/BoltAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox;
import org.popcraft.bolt.event.Event;
import org.popcraft.bolt.protection.BlockProtection;
import org.popcraft.bolt.protection.EntityProtection;
import org.popcraft.bolt.protection.Protection;
Expand All @@ -13,6 +14,7 @@

import java.util.Collection;
import java.util.UUID;
import java.util.function.Consumer;

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public interface BoltAPI {
Expand Down Expand Up @@ -59,4 +61,6 @@ public interface BoltAPI {
boolean canAccess(final Protection protection, final SourceResolver sourceResolver, final String... permissions);

void registerPlayerSourceResolver(final PlayerSourceResolver playerSourceResolver);

<T extends Event> void registerEvent(final Class<T> clazz, final Consumer<? super T> subscriber);
}
15 changes: 15 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.popcraft.bolt;

import net.kyori.event.EventBus;
import net.kyori.event.SimpleEventBus;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.AdvancedPie;
import org.bstats.charts.DrilldownPie;
Expand Down Expand Up @@ -48,6 +50,7 @@
import org.popcraft.bolt.data.SimpleProtectionCache;
import org.popcraft.bolt.data.migration.lwc.ConfigMigration;
import org.popcraft.bolt.data.migration.lwc.TrustMigration;
import org.popcraft.bolt.event.Event;
import org.popcraft.bolt.lang.Translation;
import org.popcraft.bolt.lang.Translator;
import org.popcraft.bolt.listeners.BlockListener;
Expand Down Expand Up @@ -150,6 +153,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class BoltPlugin extends JavaPlugin implements BoltAPI {
Expand Down Expand Up @@ -194,6 +198,7 @@ public class BoltPlugin extends JavaPlugin implements BoltAPI {
private boolean doorsFixPlugins;
private Bolt bolt;
private CallbackManager callbackManager;
private EventBus<Event> eventBus;

@Override
public void onEnable() {
Expand All @@ -217,6 +222,7 @@ public void onEnable() {
registerEvents();
registerCommands();
this.callbackManager = new CallbackManager(this);
this.eventBus = new SimpleEventBus<>(Event.class);
profileCache.load();
final Metrics metrics = new Metrics(this, 17711);
registerCustomCharts(metrics, databaseConfiguration);
Expand Down Expand Up @@ -555,6 +561,15 @@ public CallbackManager getCallbackManager() {
return this.callbackManager;
}

public EventBus<Event> getEventBus() {
return this.eventBus;
}

@Override
public <T extends Event> void registerEvent(final Class<T> clazz, final Consumer<? super T> subscriber) {
this.eventBus.register(clazz, subscriber::accept);
}

public BoltPlayer player(final Player player) {
return player(player.getUniqueId());
}
Expand Down
13 changes: 13 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/event/Cancellable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.popcraft.bolt.event;

public abstract class Cancellable implements Event {
private boolean cancelled;

public boolean isCancelled() {
return cancelled;
}

public void setCancelled(final boolean cancelled) {
this.cancelled = cancelled;
}
}
4 changes: 4 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.popcraft.bolt.event;

public interface Event {
}
22 changes: 22 additions & 0 deletions bukkit/src/main/java/org/popcraft/bolt/event/LockBlockEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.popcraft.bolt.event;

import org.bukkit.block.Block;
import org.bukkit.entity.Player;

public class LockBlockEvent extends Cancellable implements Event {
private final Player player;
private final Block block;

public LockBlockEvent(Player player, Block block) {
this.player = player;
this.block = block;
}

public Player getPlayer() {
return this.player;
}

public Block getBlock() {
return this.block;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.bukkit.util.Vector;
import org.popcraft.bolt.BoltPlugin;
import org.popcraft.bolt.access.Access;
import org.popcraft.bolt.event.LockBlockEvent;
import org.popcraft.bolt.lang.Translation;
import org.popcraft.bolt.matcher.Match;
import org.popcraft.bolt.protection.BlockProtection;
Expand Down Expand Up @@ -219,6 +220,18 @@ private boolean triggerActions(final Player player, final Protection protection,
final Action.Type actionType = action.getType();
switch (actionType) {
case LOCK -> {
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;
}

final String protectionType = Optional.ofNullable(action.getData())
.flatMap(type -> plugin.getBolt().getAccessRegistry().getProtectionByType(type))
.map(Access::type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Translation {
public static final String CLICK_LOCKED = "click_locked";
public static final String CLICK_LOCKED_NO_EXIST = "click_locked_no_exist";
public static final String CLICK_LOCKED_NO_PERMISSION = "click_locked_no_permission";
public static final String CLICK_LOCKED_CANCELLED = "click_locked_cancelled";
public static final String CLICK_LOCKED_ALREADY = "click_locked_already";
public static final String CLICK_LOCKED_CHANGED = "click_locked_changed";
public static final String CLICK_NOT_LOCKED = "click_not_locked";
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/lang/en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ click_info=Click on a protection to view info.
click_locked=Locked <yellow><protection_type></yellow> <yellow><protection></yellow>.
click_locked_no_exist=Protection type <yellow><protection_type></yellow> does not exist.
click_locked_no_permission=You don't have permission to use that protection type!
click_locked_cancelled=You're not allowed to lock this <yellow><protection></yellow>.
click_locked_already=This <yellow><protection></yellow> is already locked.
click_locked_changed=Changed the protection type to <yellow><protection_type></yellow>.
click_not_locked=This <yellow><protection></yellow> isn't locked.
Expand Down

0 comments on commit 0681e27

Please sign in to comment.