Skip to content

Commit

Permalink
Added the VotifierEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
muriplz committed Jan 17, 2024
1 parent 569bd8d commit 3699e20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 49 deletions.
49 changes: 11 additions & 38 deletions src/main/java/com/kryeit/votifier/model/VotifierEvent.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<VotifierEvent> 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);
}
16 changes: 5 additions & 11 deletions src/main/java/com/kryeit/votifier/net/VoteReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3699e20

Please sign in to comment.