# 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 logic for the Status stage of the server.
"""
require Logger
use Amethyst.Server
alias Amethyst.Minecraft.Read
alias Amethyst.Minecraft.Write
## 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