From 0681e27f5807d11ab25a655bdefdc51e696515b6 Mon Sep 17 00:00:00 2001 From: Emilia Kond Date: Sun, 11 Feb 2024 09:17:04 +0200 Subject: [PATCH] Event system Preliminary work for #90 --- bukkit/build.gradle.kts | 4 ++++ .../main/java/org/popcraft/bolt/BoltAPI.java | 4 ++++ .../java/org/popcraft/bolt/BoltPlugin.java | 15 +++++++++++++ .../org/popcraft/bolt/event/Cancellable.java | 13 +++++++++++ .../java/org/popcraft/bolt/event/Event.java | 4 ++++ .../popcraft/bolt/event/LockBlockEvent.java | 22 +++++++++++++++++++ .../bolt/listeners/BlockListener.java | 13 +++++++++++ .../org/popcraft/bolt/lang/Translation.java | 1 + common/src/main/resources/lang/en.properties | 1 + 9 files changed, 77 insertions(+) create mode 100644 bukkit/src/main/java/org/popcraft/bolt/event/Cancellable.java create mode 100644 bukkit/src/main/java/org/popcraft/bolt/event/Event.java create mode 100644 bukkit/src/main/java/org/popcraft/bolt/event/LockBlockEvent.java diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index c853b413..53998145 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -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")) diff --git a/bukkit/src/main/java/org/popcraft/bolt/BoltAPI.java b/bukkit/src/main/java/org/popcraft/bolt/BoltAPI.java index ea8ad0f2..99f9cfa0 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/BoltAPI.java +++ b/bukkit/src/main/java/org/popcraft/bolt/BoltAPI.java @@ -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; @@ -13,6 +14,7 @@ import java.util.Collection; import java.util.UUID; +import java.util.function.Consumer; @SuppressWarnings("BooleanMethodIsAlwaysInverted") public interface BoltAPI { @@ -59,4 +61,6 @@ public interface BoltAPI { boolean canAccess(final Protection protection, final SourceResolver sourceResolver, final String... permissions); void registerPlayerSourceResolver(final PlayerSourceResolver playerSourceResolver); + + void registerEvent(final Class clazz, final Consumer subscriber); } diff --git a/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java b/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java index cbf324fd..a0864d1e 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java +++ b/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java @@ -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; @@ -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; @@ -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 { @@ -194,6 +198,7 @@ public class BoltPlugin extends JavaPlugin implements BoltAPI { private boolean doorsFixPlugins; private Bolt bolt; private CallbackManager callbackManager; + private EventBus eventBus; @Override public void onEnable() { @@ -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); @@ -555,6 +561,15 @@ public CallbackManager getCallbackManager() { return this.callbackManager; } + public EventBus getEventBus() { + return this.eventBus; + } + + @Override + public void registerEvent(final Class clazz, final Consumer subscriber) { + this.eventBus.register(clazz, subscriber::accept); + } + public BoltPlayer player(final Player player) { return player(player.getUniqueId()); } diff --git a/bukkit/src/main/java/org/popcraft/bolt/event/Cancellable.java b/bukkit/src/main/java/org/popcraft/bolt/event/Cancellable.java new file mode 100644 index 00000000..64a697a0 --- /dev/null +++ b/bukkit/src/main/java/org/popcraft/bolt/event/Cancellable.java @@ -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; + } +} diff --git a/bukkit/src/main/java/org/popcraft/bolt/event/Event.java b/bukkit/src/main/java/org/popcraft/bolt/event/Event.java new file mode 100644 index 00000000..d28a4390 --- /dev/null +++ b/bukkit/src/main/java/org/popcraft/bolt/event/Event.java @@ -0,0 +1,4 @@ +package org.popcraft.bolt.event; + +public interface Event { +} diff --git a/bukkit/src/main/java/org/popcraft/bolt/event/LockBlockEvent.java b/bukkit/src/main/java/org/popcraft/bolt/event/LockBlockEvent.java new file mode 100644 index 00000000..9769dce7 --- /dev/null +++ b/bukkit/src/main/java/org/popcraft/bolt/event/LockBlockEvent.java @@ -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; + } +} diff --git a/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java b/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java index dc388e2b..5efbd918 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java +++ b/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java @@ -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; @@ -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) diff --git a/common/src/main/java/org/popcraft/bolt/lang/Translation.java b/common/src/main/java/org/popcraft/bolt/lang/Translation.java index b6edb75a..f2edbbce 100644 --- a/common/src/main/java/org/popcraft/bolt/lang/Translation.java +++ b/common/src/main/java/org/popcraft/bolt/lang/Translation.java @@ -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"; diff --git a/common/src/main/resources/lang/en.properties b/common/src/main/resources/lang/en.properties index dfb1adb2..288ff0d6 100644 --- a/common/src/main/resources/lang/en.properties +++ b/common/src/main/resources/lang/en.properties @@ -9,6 +9,7 @@ click_info=Click on a protection to view info. click_locked=Locked . click_locked_no_exist=Protection type 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 . click_locked_already=This is already locked. click_locked_changed=Changed the protection type to . click_not_locked=This isn't locked.