amethyst/lib/servers/status.ex

86 lines
2.7 KiB
Elixir
Raw Normal View History

2024-07-08 14:45:13 +02:00
# 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 <https://www.gnu.org/licenses/>.
defmodule Amethyst.Server.Status do
@moduledoc """
2024-07-08 16:57:49 +02:00
This module contains the logic for the Status stage of the server.
2024-07-08 14:45:13 +02:00
"""
require Logger
use Amethyst.Server
2024-07-08 14:45:13 +02:00
alias Amethyst.Minecraft.Read
alias Amethyst.Minecraft.Write
2024-07-10 09:11:45 +02:00
@impl true
def init(state) do
state
end
2024-07-08 14:45:13 +02:00
## DESERIALIZATION
2024-07-10 09:11:45 +02:00
@impl true
2024-07-08 14:45:13 +02:00
# 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, <<data::binary>>) 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
2024-07-10 09:11:45 +02:00
@impl true
2024-07-08 14:45:13 +02:00
# 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) <> <<payload::64-big-signed>>
end
def serialize(packet) do
raise ArgumentError, "Tried serializing unknown packet #{inspect(packet)}"
end
## HANDLING
2024-07-10 09:11:45 +02:00
@impl true
2024-07-08 14:45:13 +02:00
# Status Request https://wiki.vg/Protocol#Status_Request
2024-07-09 18:57:09 +02:00
def handle({:status_request}, client, state) do
2024-07-08 14:45:13 +02:00
# 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)
2024-07-09 18:57:09 +02:00
{:ok, state}
2024-07-08 14:45:13 +02:00
end
2024-07-09 18:57:09 +02:00
def handle({:ping_request, payload}, client, state) do
2024-07-08 14:45:13 +02:00
packet = {:ping_response, payload}
transmit(packet, client)
2024-07-09 18:57:09 +02:00
{:ok, state}
2024-07-08 14:45:13 +02:00
end
2024-07-09 18:57:09 +02:00
def handle(tuple, _, state) do
2024-07-08 14:45:13 +02:00
Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
2024-07-09 18:57:09 +02:00
{:unhandled, state}
2024-07-08 14:45:13 +02:00
end
end