Changes to server format
All checks were successful
Build & Test / nix-build (push) Successful in 1m12s

This commit is contained in:
Kodi Craft 2024-07-09 18:57:09 +02:00
parent 38da310fd8
commit ec9f9c87ac
Signed by: kodi
GPG Key ID: 69D9EED60B242822
5 changed files with 28 additions and 16 deletions

View File

@ -227,17 +227,19 @@ defmodule Amethyst.Server.Configuration do
end end
## HANDLING ## HANDLING
def handle({:serverbound_plugin_message, channel, data}, client) do def handle({:serverbound_plugin_message, channel, data}, client, state) do
handle_plugin_message(channel, data, client) handle_plugin_message(channel, data, client, state)
end end
def handle(tuple, _) do def handle(tuple, _) do
Logger.error("Unhandled but known packet #{elem(tuple, 0)}") Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
end end
defp handle_plugin_message("minecraft:brand", data, _client) do defp handle_plugin_message("minecraft:brand", data, _client, state) do
{[brand], ""} = Read.start(data) |> Read.string |> Read.stop {[brand], ""} = Read.start(data) |> Read.string |> Read.stop
Logger.info("Client using brand: #{brand}") Logger.info("Client using brand: #{brand}")
{:ok, [{:brand, brand} | state]}
end end
defp handle_plugin_message("amethyst:hello", _data, _client) do defp handle_plugin_message("amethyst:hello", _data, _client, state) do
Logger.info("Client is Amethyst aware! Hello!") Logger.info("Client is Amethyst aware! Hello!")
{:ok, [{:amethyst, true} | state]}
end end
end end

View File

@ -51,12 +51,15 @@ require Logger
defmacro __using__(_opts) do defmacro __using__(_opts) do
quote do quote do
@spec serve(:gen_tcp.socket()) :: no_return() @spec serve(:gen_tcp.socket()) :: no_return()
def serve(client) do def serve(client, state \\ []) do
{id, data} = Amethyst.Server.Generic.get_packet(client) {id, data} = Amethyst.Server.Generic.get_packet(client)
packet = deserialize(id, data) packet = deserialize(id, data)
Logger.debug("Got packet #{inspect(packet)}") Logger.debug("Got packet #{inspect(packet)}")
handle(packet, client) {result, state} = handle(packet, client, state)
serve(client) if result != :ok do
Logger.warning("Handler returned result #{result}")
end
serve(client, state)
end end

View File

@ -48,8 +48,8 @@ defmodule Amethyst.Server.Handshake do
## HANDLING ## HANDLING
# Handshake https://wiki.vg/Protocol#Handshake # Handshake https://wiki.vg/Protocol#Handshake
@spec handle(any(), any()) :: no_return() @spec handle(any(), any(), any()) :: no_return()
def handle({:handshake, 767, addr, port, next}, client) do def handle({:handshake, 767, addr, port, next}, client, _state) do
Logger.info("Got handshake, version 767 on #{addr}:#{port}. Wants to move to #{next}") Logger.info("Got handshake, version 767 on #{addr}:#{port}. Wants to move to #{next}")
case next do case next do
:status -> Amethyst.Server.Status.serve(client) :status -> Amethyst.Server.Status.serve(client)
@ -57,7 +57,8 @@ defmodule Amethyst.Server.Handshake do
_ -> raise RuntimeError, "Unhandled move to next mode #{next}" _ -> raise RuntimeError, "Unhandled move to next mode #{next}"
end end
end end
def handle(tuple, _) do def handle(tuple, _, state) do
Logger.error("Unhandled but known packet #{elem(tuple, 0)}") Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
{:unhandled, state}
end end
end end

View File

@ -104,7 +104,7 @@ defmodule Amethyst.Server.Login do
## HANDLING ## HANDLING
# Login Start https://wiki.vg/Protocol#Login_Start # Login Start https://wiki.vg/Protocol#Login_Start
def handle({:login_start, name, uuid}, client) do def handle({:login_start, name, uuid}, client, state) do
Logger.info("Logging in #{name} (#{uuid})") Logger.info("Logging in #{name} (#{uuid})")
if Application.fetch_env!(:amethyst, :encryption) do if Application.fetch_env!(:amethyst, :encryption) do
raise RuntimeError, "Encryption is currently unsupported." # TODO: Implement encryption raise RuntimeError, "Encryption is currently unsupported." # TODO: Implement encryption
@ -115,11 +115,14 @@ defmodule Amethyst.Server.Login do
else else
transmit({:login_success, uuid, name, [], false}, client) transmit({:login_success, uuid, name, [], false}, client)
end end
{:ok, state}
end end
def handle({:login_acknowledged}, client) do def handle({:login_acknowledged}, client, state) do
Amethyst.Server.Configuration.serve(client) Amethyst.Server.Configuration.serve(client)
{:ok, state}
end end
def handle(tuple, _) do def handle(tuple, _, state) do
Logger.error("Unhandled but known packet #{elem(tuple, 0)}") Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
{:unhandled, state}
end end
end end

View File

@ -52,7 +52,7 @@ defmodule Amethyst.Server.Status do
## HANDLING ## HANDLING
# Status Request https://wiki.vg/Protocol#Status_Request # Status Request https://wiki.vg/Protocol#Status_Request
def handle({:status_request}, client) do def handle({:status_request}, client, state) do
# We want to make this more dynamic in the future, but this works for now # We want to make this more dynamic in the future, but this works for now
packet = {:status_response, ~s({ packet = {:status_response, ~s({
"version": {"name": "1.21", "protocol": 767}, "version": {"name": "1.21", "protocol": 767},
@ -63,12 +63,15 @@ defmodule Amethyst.Server.Status do
"preventsChatReports": true "preventsChatReports": true
})} })}
transmit(packet, client) transmit(packet, client)
{:ok, state}
end end
def handle({:ping_request, payload}, client) do def handle({:ping_request, payload}, client, state) do
packet = {:ping_response, payload} packet = {:ping_response, payload}
transmit(packet, client) transmit(packet, client)
{:ok, state}
end end
def handle(tuple, _) do def handle(tuple, _, state) do
Logger.error("Unhandled but known packet #{elem(tuple, 0)}") Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
{:unhandled, state}
end end
end end