# Amethyst - An experimental Minecraft server written in Elixir. # Copyright (C) 2024 KodiCraft # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . defmodule Amethyst.Server.Status do @moduledoc """ This module contains the Status logic, this is not really its own login state since the client is only asking a bit of information. """ require Logger alias Amethyst.Minecraft.Read alias Amethyst.Minecraft.Write @spec serve(:gen_tcp.socket()) :: no_return() def serve(client) do {id, data} = Amethyst.Server.Generic.get_packet(client) packet = deserialize(id, data) Logger.debug("Got packet #{inspect(packet)}") handle(packet, client) serve(client) end def transmit(packet, client) do Logger.debug("Transmitting #{inspect(packet)}") data = serialize(packet) length = byte_size(data) |> Write.varint() :gen_tcp.send(client, length <> data) end ## DESERIALIZATION # Status Request https://wiki.vg/Protocol#Status_Request def deserialize(0x00, _) do {:status_request} end # Ping Request https://wiki.vg/Protocol#Ping_Request def deserialize(0x01, <>) do {[payload], ""} = Read.start(data) |> Read.long() |> Read.stop() {:ping_request, payload} end def deserialize(type, _) do raise RuntimeError, "Got unknown packet type #{type}!" end ## SERIALIZATION # Status Response https://wiki.vg/Protocol#Status_Response def serialize({:status_response, data}) do Write.varint(0x00) <> Write.string(data) end def serialize({:ping_response, payload}) do Write.varint(0x01) <> <> end def serialize(packet) do raise ArgumentError, "Tried serializing unknown packet #{inspect(packet)}" end ## HANDLING # Status Request https://wiki.vg/Protocol#Status_Request def handle({:status_request}, client) do # We want to make this more dynamic in the future, but this works for now packet = {:status_response, ~s({ "version": {"name": "1.21", "protocol": 767}, "players": {"max": -1, "online": 69, "sample": [{"name": "§dAmethyst§r", "id": "4566e69f-c907-48ee-8d71-d7ba5aa00d20"}]}, "description": {"text":"Amethyst is an experimental server written in Elixir"}, "enforcesSecureChat": false, "previewsChat": false, "preventsChatReports": true })} transmit(packet, client) end def handle({:ping_request, payload}, client) do packet = {:ping_response, payload} transmit(packet, client) end def handle(tuple, _) do Logger.error("Unhandled but known packet #{elem(tuple, 0)}") end end