From c880ea95f3066d54f70b1cdb34ac9d99c4d228a1 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Tue, 1 Oct 2024 08:21:30 +0200 Subject: [PATCH] Implement player info update packet --- apps/amethyst/lib/states/macros.ex | 8 ++++- apps/amethyst/lib/states/play.ex | 52 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/apps/amethyst/lib/states/macros.ex b/apps/amethyst/lib/states/macros.ex index 86b3970..d52f767 100644 --- a/apps/amethyst/lib/states/macros.ex +++ b/apps/amethyst/lib/states/macros.ex @@ -95,6 +95,7 @@ defmodule Amethyst.ConnectionState.Macros do Enum.reduce(Map.get(packet, name), "", fn item, acc -> acc <> write_signature(item, signature) end) + {:literal, {type, value}} -> acc <> apply(Write, type, [value]) t -> acc <> apply(Write, t, [Map.get(packet, name)]) end end) @@ -104,7 +105,12 @@ defmodule Amethyst.ConnectionState.Macros do try do Enum.all?(signature, fn {name, type} -> case Map.get(packet, name, :missing) do - :missing -> throw {:missing, name} + :missing -> + if elem(type, 0) == :literal do + true + else + throw {:missing, name} + end value -> case type_matches(value, type) do true -> true false -> throw {:mismatch, name, value, type} diff --git a/apps/amethyst/lib/states/play.ex b/apps/amethyst/lib/states/play.ex index c00c81a..e91c06d 100644 --- a/apps/amethyst/lib/states/play.ex +++ b/apps/amethyst/lib/states/play.ex @@ -71,6 +71,58 @@ defmodule Amethyst.ConnectionState.Play do portal_cooldown: :varint, enforces_secure_chat: :bool, ] + Macros.defpacket_clientbound :player_info_update_add_player, 0x2E, 767, [ + actions: {:literal, 0x01}, + players: {:array, [ + uuid: :uuid, + name: :string, + properties: {:array, [ + name: :string, + value: :string, + signature: {:optional, :string}, + ]} + ]} + ] + Macros.defpacket_clientbound :player_info_update_initialize_chat, 0x2E, 767, [ + actions: {:literal, 0x02}, + players: {:array, [ + uuid: :uuid, + data: {:optional, {:compound, [ + chat_session_id: :uuid, + public_key_expiry_time: :long, + encoded_public_key: :byte_array, + public_key_signature: :byte_array + ]}} + ]} + ] + Macros.defpacket_clientbound :player_info_update_update_game_mode, 0x2E, 767, [ + actions: {:literal, 0x04}, + players: {:array, [ + uuid: :uuid, + gamemode: :varint + ]} + ] + Macros.defpacket_clientbound :player_info_update_update_listed, 0x2E, 767, [ + actions: {:literal, 0x08}, + players: {:array, [ + uuid: :uuid, + listed: :bool + ]} + ] + Macros.defpacket_clientbound :player_info_update_update_latency, 0x2E, 767, [ + actions: {:literal, 0x10}, + players: {:array, [ + uuid: :uuid, + ping: :varint, # Milliseconds + ]} + ] + Macros.defpacket_clientbound :player_info_update_update_display_name, 0x2E, 767, [ + actions: {:literal, 0x20}, + players: {:array, [ + uuid: :uuid, + display_name: {:optional, :nbt} + ]} + ] Macros.defpacket_clientbound :synchronize_player_position, 0x40, 767, [ x: :double, y: :double,