ProtocolLib

3M Downloads

sending custom map data

birthdates opened this issue · 0 comments

commented

Make sure you're doing the following

  • [ ✓] You're using the latest build for your server version
  • [ ✓] This isn't an issue caused by another plugin
  • [ ✓] You've checked for duplicate issues
  • [ ✓] You didn't use /reload

Warning: I am using Kotlin which has similar syntax to Java but don't confuse the two

Describe the question
I am currently trying to send the PacketType.Play.Server.MAP for a map in a player's hand. I believe this to be the wrapper for the ClientboundMapItemDataPacket packet.

The current code I have to send this packet is:

typealias ServerPackets = PacketType.Play.Server
val protocolManager: ProtocolManager = ProtocolLibrary.getProtocolManager()

val packet: PacketContainer = protocolManager.createPacket(ServerPackets.MAP)
packet.integers.write(0, mapId) // Map ID
packet.bytes.write(0, 0) // Scale (0 for default)
packet.booleans.write(0, false) // No tracking position

API method(s) used
ProtocolLibrary.getProtocolManager() as protocolManager, protocolManager.createPacket(), protocolManager.sendServerPacket()

Expected behavior
The definition for ClientboundMapItemDataPacket is:

public record ClientboundMapItemDataPacket(
    MapId mapId, byte scale, boolean locked, 
    Optional<List<MapDecoration>> decorations, Optional<MapItemSavedData.MapPatch> colorPatch
) implements Packet<ClientGamePacketListener>

However, if you haven't noticed, I have only set 3/5 of these fields. For my current use case, the decorations field is unnecessary. However, the colorPatch field is used to store the image on the map (color array). I would expect there to be a wrapper for this field type but there is not.

Code
If you are wondering why I'm even using this packet over just Player.sendMap(), it's because I am also sending the map with packets:

private fun sendHeldItemPacket(player: Player, mapItem: ItemStack, protocolManager: ProtocolManager) {
    val packet = protocolManager.createPacket(ServerPackets.ENTITY_EQUIPMENT)
    packet.integers.write(0, player.entityId) // The player's entity ID
    packet.slotStackPairLists.write(0, listOf(Pair(EnumWrappers.ItemSlot.MAINHAND, mapItem)))
    protocolManager.sendServerPacket(player, packet)
}

Additional context
N/A