Implement callback for accept_teleport
All checks were successful
Build & Test / nix-build (pull_request) Successful in 2m43s
Build & Test / nix-build (push) Successful in 2m46s

This commit is contained in:
Kodi Craft 2024-09-17 19:50:21 +02:00
parent 53fe25043d
commit f79e0728e6
Signed by: kodi
GPG Key ID: 69D9EED60B242822
4 changed files with 20 additions and 11 deletions

View File

@ -60,6 +60,18 @@ defmodule Amethyst.API.Game do
mod.player_position(self(), {x, y, z}, refs)
end
@doc """
`accept_teleport/3` is called when a client accepts a teleportation as sent by the Synchronize Player Position
packet (TODO: Teleport Player API). This lets you know that the client is now where you expect it to be.
- 'from' is the PID of the player's connection process (see `login/3`).
- 'id' is the teleport ID (TODO: Teleport Player API)
- 'state_refs' are your references (see `instantiate/1`)
"""
@callback accept_teleport(from :: pid(), id :: integer(), state_refs :: map()) :: :ok
def accept_teleport(%{:mod => mod, :refs => refs}, id) do
mod.accept_teleport(self(), id, refs)
end
@doc """
Whether or not this game instance can be joined by a new player. This should include basic logic such as
if joining makes sense, for instance if the game is full or if the game has already started.
"""

View File

@ -64,9 +64,6 @@ defmodule Amethyst.ConnectionHandler do
Logger.debug("Sending packet #{inspect(packet)}")
send_packet(socket, connstate, packet, version)
loop(socket, connstate, version, state)
{:tp_done, id} ->
# TODO: Implement a proper teleportation API
Logger.debug("Accepted teleportation #{inspect(id)}")
after 0 ->
receive do
{:packet, id, data} ->

View File

@ -62,14 +62,8 @@ defmodule Amethyst.ConnectionState.Play do
Macros.defpacket_serverbound :confirm_teleportation, 0x00, 747, [teleport_id: :varint]
def handle(%{packet_type: :confirm_teleportation, teleport_id: id}, 747, state) do
if state[:teleport_id] == id do
# The client has accepted our teleportation, we can let the connection process know
send(self(), {:tp_done, id})
state |> Map.put(:teleport_id, nil)
else
# The client has not yet accepted our teleportation, we aren't done yet
state
end
Amethyst.API.Game.accept_teleport(state[:game], id)
:ok
end
def disconnect(reason) do

View File

@ -21,6 +21,12 @@ defmodule Example.Game do
:ok
end
@impl true
def accept_teleport(from, id, _state_refs) do
Logger.info("Player at #{inspect(from)} accepted teleport #{inspect(id)}")
:ok
end
@impl true
def joinable?(_refs) do
true