[API] PluginMessageAPI+

Discussion in 'Resources' started by iKeirNez, Dec 31, 2013.

Thread Status:
Not open for further replies.
  1. Backstory: I wrote this whilst working on a private project for a client. We had to do a lot of Bukkit -> BungeeCord communication and this became a real pain. Plugin Messages are long to write and you can often end up with issues using them. So I wrote this little API to ease the use of plugin messages.

    Before I begin explaining how it works, lets from now on, refer to plugin messages as Packets

    How Does It Work?: In short a Packet class is used to load and send data. You would have this api installed on whichever software you use it (Bukkit/BungeeCord etc). This api handles the sending and receiving of the packets. You use a Bukkit-like listener to receive these packets and a simple API to send it.

    Usage: The usage of this API is very similar on Bukkit as it is to BungeeCord or any other implementations I include in the future but I'll show an example of each anyway.

    Bukkit:
    Show Spoiler
    Initializing Example:
    Code:java
    1. public PacketManager packetManager;
    2.  
    3. public void onEnable(){
    4. packetManager = new BukkitPacketManager(this, "MyChannelName");
    5. packetManager.registerPacket(PacketPlayerUpdatePoints.class);
    6. packetManager.registerListener(new IncomingPacketHandler());
    7. }


    Sending Example:
    Code:java
    1. packetManager.sendPacket(new PacketPlayer(Bukkit.getPlayer("iKeirNez"), new PacketPlayerUpdatePoints(50)));


    Listener Example:
    Code:java
    1. public class IncomingPacketHandler implements PacketListener {
    2. @PacketHandler
    3. public void onPointsUpdate(PacketPlayerUpdatePoints packet){
    4. Player player = packet.getSender().getBukkitPlayer();
    5. int points = packet.points;
    6.  
    7. // do something to update points
    8. }
    9. }


    BungeeCord:
    Show Spoiler
    Initializing Example:
    Code:java
    1. public PacketManager packetManager;
    2. public void onEnable(){
    3. packetManager = new BungeeCordPacketManager(this, "MyChannelName");
    4. packetManager.registerPacket(PacketPlayerUpdatePoints.class);
    5. packetManager.registerListener(new IncomingPacketHandler());
    6. }


    Sending Example:
    Code:java
    1. packetManager.sendPacket(new PacketPlayer(ProxyServer.getInstance().getPlayer("iKeirNez"), new PacketPlayerUpdatePoints(50)));


    Listener Example:
    Code:java
    1. public class IncomingPacketHandler implements PacketListener {
    2. @PacketHandler
    3. public void onPointsUpdate(PacketPlayerUpdatePoints packet){
    4. ProxiedPlayer player = packet.getSender().getBungeePlayer();
    5. int points = packet.points;
    6.  
    7. // do something to update points
    8. }
    9. }


    The Packet Class: This is probably the most important part of using this API, creating you're own Packet classes. For the above example I would use a Packet class that looks like this.

    Code:java
    1. public class PacketPlayerUpdatePoints extends StandardPacket {
    2.  
    3. public int points;
    4.  
    5. public PacketPlayerUpdatePoints(){}
    6.  
    7. public PacketPlayerUpdatePoints(int point){
    8. this.change = change;
    9. }
    10.  
    11. public void handle(DataInputStream dataInputStream) throws IOException {
    12. this.point = dataInputStream.readInt();
    13. }
    14.  
    15. public PacketWriter write() throws IOException {
    16. PacketWriter packetWriter = new PacketWriter(this);
    17. packetWriter.writeInt(points);
    18. return packetWriter;
    19. }
    20. }


    Remember this packet class MUST be included in any module in which you send or receive otherwise this will not be able to function. A good way to do this would be with Maven by creating a Common module and then included that module in each other module jar.

    GitHub: https://github.com/iKeirNez/PluginMessageAPI-Plus

    Let me know if you use this API in any of you're Projects/Servers and I'll keep a list somewhere of projects/servers that use this. If you need any help or clarification on how to use this then feel free to post a reply or pm me.
     
    Vexil, Windy Day, Caprei and 5 others like this.
  2. Offline

    GermanCoding

    DrJava But this resource is for Bukkit OR Bungee :p
    iKeirNez Really useful resource! Thank you for that.
     

  3. Thanks, I'm looking to expand upon this soon too!
     
  4. Offline

    MiniDigger

    iKeirNez does this work for bukkit <-> bukkit through bungee without an bungee plugin?
     

  5. Yes, it uses BungeeCord's built in forward channel so you don't need to install a BungeeCord plugin. You can simply use

    Code:java
    1. packetManager.sendForwardPacket(new PacketPlayer(player), packet, "theServerYouWishToSendThisTo");


    I'll add support for the ForwardAll channel soon.
     
    MiniDigger likes this.
  6. Offline

    MiniDigger


  7. Yep, let me know how it goes :)
     
  8. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Removed posts on unofficial builds and 'cracked' servers.
     
    iKeirNez likes this.
  9. Offline

    GermanCoding

    That is really nice, but what is the "theServerYouWishToSendThisTo" argument? The server ip/adress or the name which is defined in bungee?
     

  10. The name defined in bungee
     
  11. Offline

    GermanCoding

    iKeirNez
    Ok thanks!
     
  12. Updated example usage to match latest version
     
  13. Offline

    BungeeTheCookie

    iKeirNez

    Would this work with modified versions of bungeecord and will it be able to send plugin messages to different server versions? Or is it compatible with only one specific version of bungeecord?
     
  14. This should be compatible with most versions of BungeeCord (as it only uses the API, not any internals) and yes it can be sent to different versions of BungeeCord as long as the same version of PluginMessageAPI+ is installed (even different versions of PluginMessageAPI+ may work, but I can't guarantee it.).
     
Thread Status:
Not open for further replies.

Share This Page