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:
BungeeCord:
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.
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.
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:
Spoiler
Initializing Example:
Sending Example:
Listener Example:
Code (Text):
public PacketManager packetManager;
public void onEnable(){
packetManager = new BukkitPacketManager(this, "MyChannelName");
packetManager.registerPacket(PacketPlayerUpdatePoints.class);
packetManager.registerListener(new IncomingPacketHandler());
}
public void onEnable(){
packetManager = new BukkitPacketManager(this, "MyChannelName");
packetManager.registerPacket(PacketPlayerUpdatePoints.class);
packetManager.registerListener(new IncomingPacketHandler());
}
Code (Text):
packetManager.sendPacket(new PacketPlayer(Bukkit.getPlayer("iKeirNez"), new PacketPlayerUpdatePoints(50)));
Code (Text):
public class IncomingPacketHandler implements PacketListener {
@PacketHandler
public void onPointsUpdate(PacketPlayerUpdatePoints packet){
Player player = packet.getSender().getBukkitPlayer();
int points = packet.points;
// do something to update points
}
}
@PacketHandler
public void onPointsUpdate(PacketPlayerUpdatePoints packet){
Player player = packet.getSender().getBukkitPlayer();
int points = packet.points;
// do something to update points
}
}
Spoiler
Initializing Example:
Sending Example:
Listener Example:
Code (Text):
public PacketManager packetManager;
public void onEnable(){
packetManager = new BungeeCordPacketManager(this, "MyChannelName");
packetManager.registerPacket(PacketPlayerUpdatePoints.class);
packetManager.registerListener(new IncomingPacketHandler());
}
public void onEnable(){
packetManager = new BungeeCordPacketManager(this, "MyChannelName");
packetManager.registerPacket(PacketPlayerUpdatePoints.class);
packetManager.registerListener(new IncomingPacketHandler());
}
Code (Text):
packetManager.sendPacket(new PacketPlayer(ProxyServer.getInstance().getPlayer("iKeirNez"), new PacketPlayerUpdatePoints(50)));
Code (Text):
public class IncomingPacketHandler implements PacketListener {
@PacketHandler
public void onPointsUpdate(PacketPlayerUpdatePoints packet){
ProxiedPlayer player = packet.getSender().getBungeePlayer();
int points = packet.points;
// do something to update points
}
}
@PacketHandler
public void onPointsUpdate(PacketPlayerUpdatePoints packet){
ProxiedPlayer player = packet.getSender().getBungeePlayer();
int points = packet.points;
// do something to update points
}
}
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 (Text):
public class PacketPlayerUpdatePoints extends StandardPacket {
public int points;
public PacketPlayerUpdatePoints(){}
public PacketPlayerUpdatePoints(int point){
this.change = change;
}
public void handle(DataInputStream dataInputStream) throws IOException {
this.point = dataInputStream.readInt();
}
public PacketWriter write() throws IOException {
PacketWriter packetWriter = new PacketWriter(this);
packetWriter.writeInt(points);
return packetWriter;
}
}
public int points;
public PacketPlayerUpdatePoints(){}
public PacketPlayerUpdatePoints(int point){
this.change = change;
}
public void handle(DataInputStream dataInputStream) throws IOException {
this.point = dataInputStream.readInt();
}
public PacketWriter write() throws IOException {
PacketWriter packetWriter = new PacketWriter(this);
packetWriter.writeInt(points);
return packetWriter;
}
}
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.
Quick facts
- Edition: Minecraft Java
- File type: .jar
- How to install: Install the matching mod loader (Forge, Fabric or NeoForge) for your Minecraft version. → Download the .jar. → Put it in the .minecraft/mods folder and launch that loader profile.
- Where to get it: Opens on Spigot — not every file is mirrored on our own servers.
Install steps are the general flow for this file type — How to install Minecraft Java mods & modpacks walks through it step by step.
PluginMessageAPI+ is a free Minecraft Java mod. Downloaded 397 times (via Spigot). Download it and open it directly in the game.