MessagingLib 1.0

MessagingLib provides a better handling of plugin messages between players and minecraft servers.

  1. tmxx
    Tested Minecraft Versions:
    • 1.7
    • 1.8
    • 1.9
    • 1.10
    Contributors:
    tmxx
    What is MessagingLib?

    MessagingLib is a library for Bukkit and BungeeCord developers to easily send and receive plugin messages. If you are a server owner and don't know anything about developing software for Bukkit or BungeeCord and only need this library because a plugin you use depends on it only download this library and put the jars in the plugins folders of Bukkit/ BungeeCord. If you are a developer and want to know how to use this library you should continue reading this article.

    How do I use it as developer?

    First you have to include MessagingLib in your build path. If you are using maven you have to add a repository the following way for bukkit:

    Code (Text):
    <repositories>
      <repository>
                <id>messaging-lib-repo</id>
                <url>https://raw.github.com/devtmxx/MessagingLib/mvn-repo-bukkit/</url>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
    </repositories>
    and for bungeecord:

    Code (Text):
    <repositories>
      <repository>
                <id>messaging-lib-repo</id>
                <url>https://raw.github.com/devtmxx/MessagingLib/mvn-repo-bungeecord/</url>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
    </repositories>
    After doing that you can add the library to your dependencies the following way for bukkit:

    Code (Text):
    <dependencies>
      <dependency>
        <groupId>net.tmxx</groupId>
        <artifactId>messaging-lib-bukkit</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
    and to bungeecord:

    Code (Text):
    <dependencies>
      <dependency>
        <groupId>net.tmxx</groupId>
        <artifactId>messaging-lib-bungeecord</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
    Now you are ready to use the library in your code!
    With a quick example I will show you how to use it correctly. Our Mission:
    When a player joins we will query all server names and the amount of online players on them and display the information to the player.

    We should start making the plugin by writing the plugin.yml

    Code (Text):
    name: MessagingTest
    version: 1.0
    main: net.tmxx.messagetest.MessageTest
    author: tmxx
    description: Testing the MessagingLib
    depend: [MessagingLib]
    website: http://dev-tmxx.net/
    I will call my plugin MessagingTest. You should fill out the information you need. But make sure to add MessagingLib to "depend". Otherwise you may get some errors when starting your plugin.

    I've wrote all the code needed to complete our mission in one class. To have a good formatted code you may create an own class for every listener/ message listener.

    Code (Text):
    package net.tmxx.messagingtest;

    import net.tmxx.messaginglib.MessagingLib;
    import net.tmxx.messaginglib.core.listener.MessageHandler;
    import net.tmxx.messaginglib.core.listener.MessageListener;
    import net.tmxx.messaginglib.core.message.bungeecord.GetServersMessage;
    import net.tmxx.messaginglib.core.message.bungeecord.PlayerCountMessage;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    /**
    * Created by tmxx on 17.01.2016
    */
    public class MessagingTest extends JavaPlugin implements Listener, MessageListener {
        @Override
        public void onEnable() {
            MessagingLib.getMessageRegistry().registerAllBungeeCordMessages();
            MessagingLib.getMessageManager().registerListener( this, this );

            getServer().getPluginManager().registerEvents( this, this );
        }

        @EventHandler
        public void onPlayerJoin( PlayerJoinEvent event ) {
            Bukkit.getScheduler().runTaskLater( this, new Runnable() {
                @Override
                public void run() {
                    MessagingLib.getMessageManager().sendPluginMessage( new GetServersMessage(), event.getPlayer() );
                }
            }, 20L );
        }

        @MessageHandler
        public void onGetServers( GetServersMessage getServersMessage, Player player ) {
            for ( String server : getServersMessage.getServers() ) {
                PlayerCountMessage message = new PlayerCountMessage();
                message.setServer( server );
                MessagingLib.getMessageManager().sendPluginMessage( message, player );
            }
        }

        @MessageHandler
        public void onPlayerCount( PlayerCountMessage playerCountMessage, Player player ) {
            player.sendMessage( "Server " + playerCountMessage.getServer() + " has " + playerCountMessage.getPlayerCount() + " players online!" );
        }
    }
     
    As this is a Bukkit plugin we need to extend JavaPlugin, as well as we need to implement Listener and MessageListener because we want to catch the PlayerJoinEvent and plugin messages. In #onEnable() we first need to register all packets we need. In the example I directly registered all by bungeecord pre-defined messages. Then I register the MessageListener and the standard Bukkit Listener. The next step is creating the PlayerJoinEvent. If a player joins I send the GetServersMessage to the player. When sending the message I use a delay of 20 ticks or 1 second to prevent MessagingLib from cancelling the message because the player may not be fully on the server. That's all we need in the #onPlayerJoin(). Now we have to deal with the plugin messages. We can easily create a method annotated with the MessageHandler and with a plugin message and a player as argument. Whenever a plugin message of the given type arrives this message will get called. First we listen for the GetServersMessage. If it arrives we will loop through the servers and send a PlayerCountMessage specifying the server to the player. Now we know we will get a PlayerCountMessage back as answer. This message we need to listen to and send the information to the player. Done! The Mission succeeded!

    I hope I could help some of you messing around with plugin messages. If you want some more information you can read the JavaDoc for this library or you go to its GitHub page.