-
Notifications
You must be signed in to change notification settings - Fork 0
Home
MattMX edited this page Nov 21, 2022
·
4 revisions
Adding repository and dependency.
Maven
<repository>
<id>pvphub-releases</id>
<name>PvPHub Development</name>
<url>https://maven.pvphub.me/releases</url>
</repository>
<dependency>
<groupId>co.pvphub</groupId>
<artifactId>ProtocolLibDsl</artifactId>
<version>-SNAPSHOT</version>
</dependency>
Gradle Groovy
maven {
url "https://maven.pvphub.me/releases"
}
implementation "co.pvphub:ProtocolLibDsl:-SNAPSHOT"
Gradle Kotlin
maven {
url = uri("https://maven.pvphub.me/releases")
}
implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT")
We can make packet listeners neat and quite handy along the program.
The functions packetReceiving
and packetSending
also return the PacketAdapter
and automatically register themselves with ProtcolLib.
// The event function invokes a PacketEvent that we can treat as "this"
packetReceiving(this, type = arrayOf(PacketType.Play.Client.CHAT)) {
val message = packet.strings.read(0)
if (isMuted(player)) {
println("[MUTED] ${player.name}: $message")
isCancelled = true
}
}
Without our DSL (Java)
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
manager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.CHAT) {
@Override
public void onPacketReceiving(PacketEvent event) {
Player player = event.getPlayer();
PacketContainer packet = event.getPacket();
if (isMuted(player)) {
System.out.println("[MUTED] " + player.getName() + ": " + packet.getStrings().read(0));
event.setCancelled(true);
}
}
});
We can call packet(type)
and use a builder in the block, defining the packet contents.
// Will send a kick packet to all connected players
packet(PacketType.Play.Server.KICK_DISCONNECT) {
chatComponents + "Kicked becaused naughty!".component() at 0
}.broadcast()
Without our DSL (Kotlin)
// As you can see, our DSL makes it much more easy to read!
val packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.KICK_DISCONNECT)
packet.chatComponents.write(0, WrappedChatComponent.fromText("Kicked because naughty!"))
ProtocolLibrary.getProtocolManager().broadcastServerPacket(packet)