Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event system #130

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 registerListener(final Class<T> clazz, final Consumer<? super T> listener);
}
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 registerListener(final Class<T> clazz, final Consumer<? super T> listener) {
this.eventBus.register(clazz, listener::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