Fix disconnect messages and progress on Configuration stage
All checks were successful
Build & Test / nix-build (push) Successful in 1m30s

This commit is contained in:
Kodi Craft 2024-09-06 12:07:14 +02:00
parent 1ced941440
commit 6495a246e0
Signed by: kodi
GPG Key ID: 69D9EED60B242822
3 changed files with 44 additions and 10 deletions

View File

@ -81,7 +81,7 @@ alias ElixirSense.Log
:ok -> state :ok -> state
{:error, reason} -> {:error, reason} ->
Logger.error("Error handling packet with ID #{id} in state #{connstate}: #{reason}") Logger.error("Error handling packet with ID #{id} in state #{connstate}: #{reason}")
send(self(), {:disconnect, "Error handling packet #{id}:\n#{reason}"}) send(self(), {:disconnect, "§cError handling packet #{id}:\n#{reason}"})
state state
newstate -> newstate ->
if is_map(newstate) do if is_map(newstate) do
@ -94,7 +94,7 @@ alias ElixirSense.Log
rescue rescue
e -> e ->
Logger.error("Error handling packet with ID #{id} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}") Logger.error("Error handling packet with ID #{id} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
send(self(), {:disconnect, "Error handling packet #{id}:\n#{e.message}"}) send(self(), {:disconnect, "§cError handling packet #{id}:\n#{Exception.format(:error, e, __STACKTRACE__)}"})
state state
end end
end end

View File

@ -25,7 +25,7 @@ defmodule Amethyst.ConnectionState.Configuration do
""" """
Macros.defpacket_clientbound :cookie_request, 0x00, 767, [identifier: :string] Macros.defpacket_clientbound :cookie_request, 0x00, 767, [identifier: :string]
Macros.defpacket_clientbound :clientbound_plugin_message, 0x01, 767, [channel: :string, data: :raw] Macros.defpacket_clientbound :clientbound_plugin_message, 0x01, 767, [channel: :string, data: :raw]
Macros.defpacket_clientbound :disconnect, 0x02, 767, [reason: :json] Macros.defpacket_clientbound :disconnect, 0x02, 767, [reason: :nbt]
Macros.defpacket_clientbound :finish_configuration, 0x03, 767, [] Macros.defpacket_clientbound :finish_configuration, 0x03, 767, []
Macros.defpacket_clientbound :clientbound_keep_alive, 0x04, 767, [id: :long] Macros.defpacket_clientbound :clientbound_keep_alive, 0x04, 767, [id: :long]
Macros.defpacket_clientbound :ping, 0x05, 767, [id: :int] Macros.defpacket_clientbound :ping, 0x05, 767, [id: :int]
@ -107,9 +107,43 @@ defmodule Amethyst.ConnectionState.Configuration do
]} ]}
] ]
def handle(%{packet_type: :serverbound_plugin_message, channel: "minecraft:brand", data: data}, 767, state) do
{[string], ""} = Amethyst.Minecraft.Read.start(data) |> Amethyst.Minecraft.Read.string() |> Amethyst.Minecraft.Read.stop()
Logger.debug("Received brand: #{string}")
send(self(), {:send_packet, %{
packet_type: :clientbound_plugin_message,
channel: "minecraft:brand",
data: Amethyst.Minecraft.Write.string("Amethyst")
}})
state |> Map.put(:brand, string)
end
def handle(%{
packet_type: :client_information,
locale: locale,
view_distance: view_distance,
chat_mode: chat_mode,
chat_colors: chat_colors,
displayed_skin_parts: displayed_skin_parts,
main_hand: main_hand,
text_filtering: text_filtering,
allow_server_listings: allow_server_listings
}, 767, state) do
Logger.debug("Received client information")
state
|> Map.put(:locale, locale)
|> Map.put(:view_distance, view_distance)
|> Map.put(:chat_mode, chat_mode)
|> Map.put(:chat_colors, chat_colors)
|> Map.put(:displayed_skin_parts, displayed_skin_parts)
|> Map.put(:main_hand, main_hand)
|> Map.put(:text_filtering, text_filtering)
|> Map.put(:allow_server_listings, allow_server_listings)
end
def disconnect(reason) do def disconnect(reason) do
%{packet_type: :disconnect, reason: %{ %{packet_type: :disconnect, reason: {:compound, %{
"text" => reason "text" => {:string, reason}
}} }}}
end end
end end

View File

@ -23,7 +23,7 @@ defmodule Amethyst.ConnectionState.Login do
@moduledoc """ @moduledoc """
This module contains the packets and logic for the Login state. This module contains the packets and logic for the Login state.
""" """
Macros.defpacket_clientbound :disconnect, 0x00, 767, [reason: :nbt] Macros.defpacket_clientbound :disconnect, 0x00, 767, [reason: :json]
Macros.defpacket_clientbound :encryption_request, 0x01, 767, [ Macros.defpacket_clientbound :encryption_request, 0x01, 767, [
server_id: :string, server_id: :string,
public_key: :byte_array, public_key: :byte_array,
@ -84,9 +84,9 @@ defmodule Amethyst.ConnectionState.Login do
def disconnect(reason) do def disconnect(reason) do
%{packet_type: :disconnect, reason: %{packet_type: :disconnect, reason:
{:compound, %{ %{
"text" => {:string, reason} "text" => reason
}} }
} }
end end
end end