Event API

Feb 10, 2016
Event API
  • BungeeCord Event API

    How to use the BungeeCord API from a plugin



    BungeeCord has an rich event API that acts similarly to the system from Bukkit, with some differences. This page describes how to create an listener and create your own event.


    Creating your own listener(top)


    To make use of events in BungeeCord, create a new class in your project. You can call this class whatever fancies you, but for the purposes of this tutorial we're going to go ahead and call it "Events.java".

    Once you have created the new class that you wish to use as a listener, you must implement the Listener class in BungeeCord. You do this by making your class declaration look similar to: public class Events implements Listener

    Once you have implemented Listener into your class, you can listen for and react to any and all BungeeCord events (see a full list of events here).

    For a simple example, we will make use of the PostLoginEvent in BungeeCord. Let's add our first listener:
    Code (Java):
    public class Events implements Listener {

        @EventHandler
        public void onPostLogin(PostLoginEvent event) {
        }

    }
    The PostLoginEvent is called every time a ProxiedPlayer is successfully authenticated and is ready to be connected to a server. With our listener, we can send a message to all other players announcing that they have joined the Bungee network like so:
    Code (Java):
    public class Events implements Listener {

        @EventHandler
        public void onPostLogin(PostLoginEvent event) {
            for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
                player.sendMessage(new TextComponent(event.getPlayer().getName() + " has joined the network."));
            }
        }

    }
    Once you have completed your Events class, you must register the Listener in your main class. You can do so like this:
    Code (Java):
    public class Main extends Plugin {
        @Override
        public void onEnable() {
            getProxy().getPluginManager().registerListener(this, new Events());
        }
    }
    Be sure to make use of the full list of events so that you can interact appropriately with events in your plugins.

    Create your own event(top)


    To create a custom BungeeCord event, create an class. We will call it MyAwesomeEvent, but it can be anything you want. This class should extend Event. Go ahead and create the properties for your listener like normal. For the purpose of demostration, we will be using this event:

    Code (Java):
    public class MyAwesomeEvent extends Event {
      private final String message;

      public MyAwesomeEvent(String message) {
        this.message = message;
      }

      public String getMessage() {
        return message;
      }
    }
    The next step is actually dispatching the event. This is easy:

    Code (Java):
    getProxy().getPluginManager().callEvent(new MyAwesomeEvent("I'm awesome!"));
    You can then listen for MyAwesomeEvent using the steps described in the previous section.
  • Loading...
  • Loading...