Implement receiving chat messages
All checks were successful
Build & Test / nix-build (push) Successful in 1m46s

This commit is contained in:
Kodi Craft 2024-10-08 15:34:57 +02:00
parent 7f68a95e28
commit 29014aa45f
Signed by: kodi
GPG Key ID: 69D9EED60B242822
4 changed files with 38 additions and 1 deletions

View File

@ -88,6 +88,17 @@ defmodule Amethyst.Game do
mod.accept_teleport(self(), id, refs) mod.accept_teleport(self(), id, refs)
end end
@doc """ @doc """
`chat/3` is called when a player sends a chat message. Note that it is not how slash commands should be handled.
- 'from' is the PID of the player's connection process (see `login/3`).
- 'message' is the chat message sent by the player.
- `state_refs` are your references (see `instantiate/1`)
"""
@callback chat(from :: pid(), message :: binary(), state_refs :: map()) :: :ok
def chat(%{:mod => mod, :refs => refs}, message) do
mod.chat(self(), message, refs)
end
@doc """
The terrain of a specific chunk column. This is automatically used to load chunks for a player. The terrain of a specific chunk column. This is automatically used to load chunks for a player.
For now, this data must be formatted as a 3D list, indexed as [y][z][x]. For now, this data must be formatted as a 3D list, indexed as [y][z][x].

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
# TODO!!!: REDO THIS WHOLE THING AGAIN IT'S A MESS
defmodule Amethyst.ConnectionState.Macros do defmodule Amethyst.ConnectionState.Macros do
@moduledoc """ @moduledoc """
Useful macros for defining packets. Useful macros for defining packets.
@ -56,6 +58,13 @@ defmodule Amethyst.ConnectionState.Macros do
else else
{[nil | acc], rest, :reversed} {[nil | acc], rest, :reversed}
end end
{:optional, {:raw, length}} ->
{[exists], rest} = Read.start(rest) |> Read.bool() |> Read.stop()
if exists do
Read.raw({acc, rest, :reversed}, length)
else
{[nil | acc], rest, :reversed}
end
{:optional, t} -> {:optional, t} ->
{[exists], rest} = Read.start(rest) |> Read.bool() |> Read.stop() {[exists], rest} = Read.start(rest) |> Read.bool() |> Read.stop()
if exists do if exists do
@ -63,6 +72,10 @@ defmodule Amethyst.ConnectionState.Macros do
else else
{[nil | acc], rest, :reversed} {[nil | acc], rest, :reversed}
end end
{:fixed_bitset, length} ->
Read.fixed_bitset({acc, rest, :reversed}, length)
{:raw, length} ->
Read.raw({acc, rest, :reversed}, length)
{:array, signature} -> {:array, signature} ->
{[count], rest} = Read.start(rest) |> Read.varint() |> Read.stop() {[count], rest} = Read.start(rest) |> Read.varint() |> Read.stop()
if count == 0 do if count == 0 do

View File

@ -161,7 +161,7 @@ defmodule Amethyst.ConnectionState.Play do
message: :string, message: :string,
timestamp: :long, timestamp: :long,
salt: :long, salt: :long,
signature: {:optional, :byte_array}, signature: {:optional, {:raw, 256}},
message_count: :varint, message_count: :varint,
acknowledged: {:fixed_bitset, 20} acknowledged: {:fixed_bitset, 20}
] ]
@ -241,6 +241,12 @@ defmodule Amethyst.ConnectionState.Play do
end end
end end
def handle(%{packet_type: :chat_message, message: msg, timestamp: _, salt: _, signature: _, message_count: _, acknowledged: _}, 767, state) do
# We will never support message signing
state |> Map.get(:game) |> Amethyst.Game.chat(msg)
:ok
end
def handle(%{packet_type: :keep_alive, id: id}, 767, state) do def handle(%{packet_type: :keep_alive, id: id}, 767, state) do
ka = state |> Map.get(:keepalive) ka = state |> Map.get(:keepalive)
send(ka, {:respond, id}) send(ka, {:respond, id})

View File

@ -39,11 +39,18 @@ defmodule Example.Game do
:ok :ok
end end
@impl true
def chat(from, message, _state_refs) do
Logger.info("Player at #{inspect(from)} said: #{inspect(message)}")
:ok
end
@impl true @impl true
def joinable?(_refs) do def joinable?(_refs) do
true true
end end
@impl true @impl true
def chunk(_from, {cx, cz}, _state_refs) do def chunk(_from, {cx, cz}, _state_refs) do
# Logger.info("Player at #{inspect(from)} wants to know chunk #{cx}, #{cz}") # Logger.info("Player at #{inspect(from)} wants to know chunk #{cx}, #{cz}")