From 3699e20c14d943070556d40474987c34808f673a Mon Sep 17 00:00:00 2001 From: muriplz <46825658+muriplz@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:37:25 +0100 Subject: [PATCH] Added the VotifierEvent --- .../kryeit/votifier/model/VotifierEvent.java | 49 +++++-------------- .../com/kryeit/votifier/net/VoteReceiver.java | 16 ++---- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/kryeit/votifier/model/VotifierEvent.java b/src/main/java/com/kryeit/votifier/model/VotifierEvent.java index 526bf3d..6af00e7 100644 --- a/src/main/java/com/kryeit/votifier/model/VotifierEvent.java +++ b/src/main/java/com/kryeit/votifier/model/VotifierEvent.java @@ -1,6 +1,7 @@ package com.kryeit.votifier.model; -import org.bukkit.event.*; +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; /** * {@code VotifierEvent} is a custom Bukkit event class that is sent @@ -10,42 +11,14 @@ * @author frelling * */ -public class VotifierEvent extends Event { - /** - * Event listener handler list. - */ - private static final HandlerList handlers = new HandlerList(); +public interface VotifierEvent { + // Event instance + Event EVENT = EventFactory.createArrayBacked(VotifierEvent.class, (listeners) -> (vote) -> { + for (VotifierEvent listener : listeners) { + listener.onVoteReceived(vote); + } + }); - /** - * Encapsulated vote record. - */ - private Vote vote; - - /** - * Constructs a vote event that encapsulated the given vote record. - * - * @param vote - * vote record - */ - public VotifierEvent(final Vote vote) { - this.vote = vote; - } - - /** - * Return the encapsulated vote record. - * - * @return vote record - */ - public Vote getVote() { - return vote; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } + // Method called when a vote is received + void onVoteReceived(Vote vote); } diff --git a/src/main/java/com/kryeit/votifier/net/VoteReceiver.java b/src/main/java/com/kryeit/votifier/net/VoteReceiver.java index 249bfad..1b98b5c 100644 --- a/src/main/java/com/kryeit/votifier/net/VoteReceiver.java +++ b/src/main/java/com/kryeit/votifier/net/VoteReceiver.java @@ -27,6 +27,8 @@ import java.net.SocketException; import java.util.logging.*; import javax.crypto.BadPaddingException; + +import com.kryeit.votifier.MinecraftServerSupplier; import org.bukkit.Bukkit; import com.kryeit.votifier.Votifier; @@ -172,17 +174,9 @@ public void run() { } } - // Call event in a synchronized fashion to ensure that the - // custom event runs in the - // the main server thread, not this one. - plugin.getServer().getScheduler() - .scheduleSyncDelayedTask(plugin, new Runnable() { - public void run() { - Bukkit.getServer().getPluginManager() - .callEvent(new VotifierEvent(vote)); - } - }); - + // Call event + VotifierEvent.EVENT.invoker().onVoteReceived(vote); + // Clean up. writer.close(); in.close();