1. Welcome to LilyPad. Download the project, explore the forums, and create your own LilyPad network.


    If you use the software and enjoy it or have a question, or would like to contribute to the future of the software directly or through resources, please sign up and join our little community.

Tutorial: MessageRequests and talking between servers

Discussion in 'Tutorials' started by hawkfalcon, Jun 13, 2013.

  1. hawkfalcon

    hawkfalcon New Member

    Hello everybody! This tutorial will be talking about how to communicate between two servers with lilypad. I will be giving a brief explanation of what it does and then giving an example of how to use it.

    Requests:
    Requests allow you to request something to be sent to another server. There are many types of requests that can be made. They can be found here. I am going to talk about MessageRequest specifically.

    MessageRequests:
    MessageRequests request lilypad to send a message to another server. For example, say I wanted to be able to so /say on one server and have the message appear on another server, I would use a message request. You would use a message request to send the message you wish to say on the other server to the other server.

    MessageEvent:
    A messageevent would be on the other server in order to listen for any requests being sent. To ensure it is the correct request, both the request and the messageevent listen on a particular channel.


    Enough writing, now I will show some sample code. The MessageRequest would be on one server, and the MessageEvent would be on the other.

    MessageRequest:
    Code (java):
    public Connect getBukkitConnect() {
            return (Connect) plugin.getServer().getServicesManager().getRegistration(Connect.class).getProvider();
        }

        public void request(String message) {
            Connect c = getBukkitConnect();
            MessageRequest request = null;
            try {
                request = new MessageRequest("name", "ch", message); //servername, channelname (short), message
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            FutureResult<MessageResult> futureResult = null;
            try {
                futureResult = c.request(request);
            } catch (RequestException e) {
                e.printStackTrace();
            }
        }
    MessageEvent:

    Code (java):

    public class Plugin {
        private MessageListener listener = new MessageListener();
        public void onEnable() {
            Connect connect = plugin.getServer().getServicesManager().getRegistration(Connect.class).getProvider();
            connect.registerEvents(listener);
        }
        public void onDisable() {
            Connect connect = plugin.getServer().getServicesManager().getRegistration(Connect.class).getProvider();
            connect.unregisterEvents(listener);
        }
    }
     
    Code (JAVA):

    public class MessageListener {
                @EventListener
                public void onMessage(MessageEvent me) {
                    if (!me.getChannel().equals("ch")) {  //name of channel
                        return;
                    }
                    String sender = me.getSender(); //gets the server it was sent from
                    String message = "Error";
                    try {
                        message = me.getMessageAsString(); //set string message to the message sent
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
               }
         }
    }
    And that is pretty much it!
    Let me know if you have any questions and enjoy!
    Last edited by a moderator: Jan 8, 2014
    • Like Like x 4
    • Disagree Disagree x 1
    • Informative Informative x 1
  2. CMonster95

    CMonster95 New Member

    Is there a way to tie this into a command? An example of this would be a server that favors local chat, but allows a global chat using the /shout command. The global chat would need to be cross-server. Thanks, I'll paste the code that I'm working on at the moment, and then I'll tie it in all together at the end.

    Code (text):

    package com.oreomc;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LilyShout extends JavaPlugin {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("shout")) {
                Player p = (Player)sender;
                if(args.length > 0) {
                    StringBuilder buffer = new StringBuilder();
                    for(int i = 0; i < args.length; i++) {
                        buffer.append(' ').append(args[i]);
                    }
                    Bukkit.broadcastMessage("<" + p.getName() + "> " + buffer.toString());
                }
            }
            return false;
        }
    }
     
    So far, the above code only works per-server. The goal would be for one server to "listen" for the shout command and then relay the shout arguments. Thank you!
  3. CMonster95

    CMonster95 New Member

    After working on it, I believe that I am going to use a two-file method (which may have been what I had to do in the first place). There are two files LilyShout (responsible for broadcasting message) and LilyListen (responsible for listening to LilyShout). I believe that LilyShout is working, but I won't be able to tell completely until LilyListen is working properly, which it isn't. Here is the source code for LilyShout:
    Code (text):
    package com.oreomc;
     
    import java.io.UnsupportedEncodingException;
     
    import lilypad.client.connect.api.Connect;
    import lilypad.client.connect.api.request.RequestException;
    import lilypad.client.connect.api.request.impl.MessageRequest;
    import lilypad.client.connect.api.result.FutureResult;
    import lilypad.client.connect.api.result.impl.MessageResult;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LilyShout extends JavaPlugin {
       
        public Connect getBukkitConnect() {
            return (Connect) this.getServer().getServicesManager().getRegistration(Connect.class).getProvider();
        }
     
        public void request(String message) {
            Connect c = getBukkitConnect();
            MessageRequest request = null;
            try {
                request = new MessageRequest("name", "ch", message); //servername, channelname (short), message
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            FutureResult<MessageResult> futureResult = null;
            try {
                futureResult = c.request(request);
            } catch (RequestException e) {
                e.printStackTrace();
            }
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("shout")) {
                Player p = (Player)sender;
                if(args.length > 0) {
                    StringBuilder buffer = new StringBuilder();
                    for(int i = 0; i < args.length; i++) {
                        buffer.append(' ').append(args[i]);
                    }
                    String sendmessage = new String(" <" + p.getName() + "> " + buffer.toString());
                    Bukkit.broadcastMessage(ChatColor.RED + "[SHOUT]" + ChatColor.WHITE + sendmessage);
                    request(sendmessage);
                }
            }
            return false;
        }
    }
     
    I am looking for something simple for LilyListen. I need it to simply broadcast everything that comes over the channel. Can anyone help me out on this? The above MessageEvent did not compile when put into a package. Thank you.
  4. CMonster95

    CMonster95 New Member

  5. hawkfalcon

    hawkfalcon New Member

  6. CMonster95

    CMonster95 New Member


    Yes, you're right. However, I was just pointing people over there who, like me, might have been at a loss of answers over this tutorial. With a complete plugin, the viewer can see the finished product. It's hard to put snippets of code into a program if you are fumbling in the dark.
  7. hawkfalcon

    hawkfalcon New Member

    Ah, okay, did you get it working? :)
  8. CMonster95

    CMonster95 New Member


    Yep! Thank you for your help!
    • Friendly Friendly x 1
  9. boboman13

    boboman13 Member Contributor

    Thank you very much for the tutorial! It helped me create my very first open source Lilypad plugin :)
    • Friendly Friendly x 1
  10. hawkfalcon

    hawkfalcon New Member

    Glad you liked it!
  11. Codisimus

    Codisimus New Member

    This helped but I am still confused about FutureResults. In the Listener, how do I set the StatusCode to be returned? Or am I going about this completely wrong and I just have to send another message? Any input should help. Thanks.
  12. Coelho

    Coelho Software Engineer Staff Member Administrator Maintainer

    The statuscode refers to whether the message was sent or not. It has nothing to do with the other side.
  13. Codisimus

    Codisimus New Member

    That is what I thought but then wut is the point of
    Code (text):
    INVALID_ROLE  - Failure due to the fact of the player not having the required role to make the request.
  14. Coelho

    Coelho Software Engineer Staff Member Administrator Maintainer

    Oops, I messed that up on the Javadoc. INVALID_ROLE means you are not authenticated, or you don't have the correct role to complete an action or etc.
  15. Codisimus

    Codisimus New Member

    Oh ok, that makes sense. Thanks for clearing that up! I simply sent a response message back in my case.
  16. Stilldabomb

    Stilldabomb New Member

    You should probably update this tutorial, but other than that, thanks for the help :p
  17. hawkfalcon

    hawkfalcon New Member

    Update?
    Edit: oh...
  18. Stilldabomb

    Stilldabomb New Member

  19. hawkfalcon

    hawkfalcon New Member

    Updated :)
  20. Stilldabomb

    Stilldabomb New Member

    You should change this
    Code (java):

    private Listener listener = new Listener();
     
    to
    Code (JAVA):

    private MessageListener listener = new MessageListener();
     
    Code (JAVA):

    public class MessageListener implements Listener {
     
    And the MessageListener doesn't implement anything :p
    @hawkfalcon

Share This Page