Skip to content

Vote Listeners and VotifierEvent

kramerc edited this page Sep 27, 2012 · 2 revisions

Vote listeners are what expand onto Votifier and allow the server to perform actions when a notification is received. With Votifier 1.8 and later, Bukkit plugins can handle the VotifierEvent which essentially acts in the same manner as a traditional vote listener.

Vote Listeners

Vote listeners have been around since the beginning of Votifier. They are meant to extend onto Votifier providing customizability that allow players to get rewarded for instance in a specific manner tailored to the server's needs.

Writing Vote Listeners

A vote listener implements the VoteListener interface which contains an implementation of the voteMade method.

A basic vote listener looks something like this:

import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.model.VoteListener;
 
public class BasicVoteListener implements VoteListener {

    public void voteMade(Vote vote) {
        System.out.println("Received: " + vote);
    }

}

Compiling Vote Listeners

Vote listeners can be compiled by including Votifier in the class path. For example:

javac -cp Votifier.jar FlatfileVoteListener.java

If you are referencing anything from Bukkit or any other plugin, you will have to include that in your class path as well.

VotifierEvent Handlers

Whenever a vote notification is received, Votifier will invoke each vote listener installed in its listeners directory. In addition, it also issues a custom VotifierEvent via the main server thread that can be processed by other plugins.

Which approach should I use?

Most vote listeners handle simple functions: rewarding a player with currency, logging votes to a file or database, etc. There are many listener developers that are very creative in what they do with a vote notification.

However, a traditional vote listener is limited by the fact that it is a single Java class file, which imparts some restrictions on what can be done. For example, all functionality has to be coded in a single class. There are no options for using other classes or inner classes without some ClassLoader trickery and installing additional support files. In addition, vote listeners run in a separate thread, which can lead to concurrency issues when accessing certain Bukkit/CraftBukkit resources.

If your vote listener has grown into a complex beast that could use a good dose of refactoring, needs to access certain concurrency-sensitive resources, or you want an existing plugin to receive vote notifications, you might want to consider writing/updating a plugin to handle VotifierEvent events.

How do I handle a VotifierEvent?

VotifierEvent events are handled in the same manner as other Bukkit events. Either create a class that implements the Listener interface or use an existing listener class. Add a method that takes VotifierEvent as an argument and annotate it as an event handler. For example, the snippets below define a new listener class for handling VotifierEvent.

import org.bukkit.event.Listener;
import com.vexsoftware.votifier.model.VotifierEvent;
import com.vexsoftware.votifier.model.Vote;
    
public class CustomEventListener implements Listener {
    
    @EventHandler(priority=EventPriority.NORMAL)
    public void onVotifierEvent(VotifierEvent event) {
        Vote vote = event.getVote();
    
        /*
         * Process Vote record as you see fit
         */
    }
}

Register the listener in your plugin's onEnable() method.

public void onEnable() {
    [...]
    getServer().getPluginManager()
        .registerEvents(new CustomEventListener(), this);
    [...]
}

The VotifierEvent class is provided by Votifier and requires that Votifier is loaded before your plugin can use it. To ensure that this happens, add Votifier as a dependency in your plugin.yml. For example:

depends: [Votifier]