Skip to content
MattMX edited this page Nov 21, 2022 · 4 revisions

Getting started

Adding repository and dependency.

Maven

Declare Repository

<repository>
  <id>pvphub-releases</id>
  <name>PvPHub Development</name>
  <url>https://maven.pvphub.me/releases</url>
</repository>

Declare Dependency

<dependency>
    <groupId>co.pvphub</groupId>
    <artifactId>ProtocolLibDsl</artifactId>
    <version>-SNAPSHOT</version>
</dependency>
Gradle Groovy

Declare Repository

maven {
    url "https://maven.pvphub.me/releases"
}

Declare Dependency

implementation "co.pvphub:ProtocolLibDsl:-SNAPSHOT"
Gradle Kotlin

Declare Repository

maven {
    url = uri("https://maven.pvphub.me/releases")
}

Declare Dependency

implementation("co.pvphub:ProtocolLibDsl:-SNAPSHOT")

Creating a Packet Listener

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);
        }
    }
});

Creating a Packet

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)