Some experimentation
All checks were successful
Build & Test / nix-build (pull_request) Successful in 2m37s
Build & Test / nix-build (push) Successful in 2m39s

This commit is contained in:
Kodi Craft 2024-10-04 09:45:23 +02:00
parent 53243c14fa
commit fb0099352b
Signed by: kodi
GPG Key ID: 69D9EED60B242822
6 changed files with 56 additions and 23 deletions

View File

@ -126,7 +126,7 @@ defmodule Amethyst.ConnectionHandler do
import Amethyst.NBT.Write
alias Amethyst.Minecraft.Write
chunk_array = Amethyst.API.Game.chunk(Map.get(state, :game), chunk)
chunk_array = Amethyst.Game.chunk(Map.get(state, :game), chunk)
{cx, cz} = chunk
# TODO: Actually do heightmaps
@ -227,8 +227,12 @@ defmodule Amethyst.ConnectionHandler do
end
rescue
e ->
Logger.error("Error handling packet with ID #{inspect(id, base: :hex)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
send(self(), {:disconnect, "§cError handling packet #{inspect(id, base: :hex)}:\n#{Exception.format(:error, e, __STACKTRACE__)}"})
if Mix.env() == :dev do
Logger.error("Error handling packet with ID #{inspect(id, base: :hex)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
else
send(self(), {:disconnect, "§cError handling packet #{inspect(id, base: :hex)}:\n#{Exception.format(:error, e, __STACKTRACE__)}"})
Logger.error("Error handling packet with ID #{inspect(id, base: :hex)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
end
state
end
end

View File

@ -14,7 +14,7 @@
# 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.API.Game do
defmodule Amethyst.Game do
@moduledoc """
This behaviour should be implemented by any Amethyst Game. It additionally
contains functions that the internal connection handler code uses to more

View File

@ -271,7 +271,7 @@ defmodule Amethyst.ConnectionState.Configuration do
send(self(), {:set_state, Amethyst.ConnectionState.Play})
game = Application.fetch_env!(:amethyst, :default_game) |> Amethyst.GameCoordinator.find_or_create()
state = state |> Map.put(:game, game)
login = Amethyst.API.Game.login(game, state)
login = Amethyst.Game.login(game, state)
case login do
:reject ->
send(self(), {:disconnect, "Default game rejected connection"})
@ -291,7 +291,7 @@ defmodule Amethyst.ConnectionState.Configuration do
dimension_type: 0,
dimension_name: "minecraft:overworld",
hashed_seed: 0,
game_mode: 3,
game_mode: 1,
previous_game_mode: -1,
is_debug: false,
is_flat: false,

View File

@ -176,31 +176,60 @@ defmodule Amethyst.ConnectionState.Play do
pitch: :float,
on_ground: :bool # I don't understand their obsession with this...
]
Macros.defpacket_serverbound :set_player_on_ground, 0x1D, 767, [on_ground: :bool]
Macros.defpacket_serverbound :player_command, 0x25, 767, [eid: :varint, action_id: :varint, jump_boost: :varint]
def handle(%{packet_type: :confirm_teleportation, teleport_id: id}, 767, state) do
Amethyst.API.Game.accept_teleport(state[:game], id)
Amethyst.Game.accept_teleport(state[:game], id)
:ok
end
def handle(%{packet_type: :set_player_position_and_rotation, x: x, feet_y: y, z: z, yaw: yaw, pitch: pitch, on_ground: _ground}, 767, state) do
# I don't know why we would ever trust on_ground here... the server computes that
Amethyst.API.Game.player_position(state[:game], {x, y, z})
Amethyst.API.Game.player_rotation(state[:game], {yaw, pitch})
:ok
end
def handle(%{packet_type: :set_player_position, x: x, feet_y: y, z: z, on_ground: _ground}, 767, state) do
# I don't know why we would ever trust on_ground here... the server computes that
Amethyst.API.Game.player_position(state[:game], {x, y, z})
:ok
end
def handle(%{packet_type: :set_player_rotation, yaw: yaw, pitch: pitch, on_ground: _ground}, 767, state) do
# I don't know why we would ever trust on_ground here... the server computes that
Amethyst.API.Game.player_rotation(state[:game], {yaw, pitch})
Amethyst.Game.player_position(state[:game], {x, y, z})
Amethyst.Game.player_rotation(state[:game], {yaw, pitch})
:ok
end
def handle(%{packet_type: :serverbound_plugin_message, channel: channel, data: _}, 767, _state) do
Logger.debug("Got plugin message on #{channel}")
end
def handle(%{packet_type: :set_player_position, x: x, feet_y: y, z: z, on_ground: _ground}, 767, state) do
# I don't know why we would ever trust on_ground here... the server computes that
Amethyst.Game.player_position(state[:game], {x, y, z})
:ok
end
def handle(%{packet_type: :set_player_rotation, yaw: yaw, pitch: pitch, on_ground: _ground}, 767, state) do
# I don't know why we would ever trust on_ground here... the server computes that
Amethyst.Game.player_rotation(state[:game], {yaw, pitch})
:ok
end
def handle(%{packet_type: :set_player_on_ground, on_ground: _}, 767, _state) do
:ok # Again, don't trust the client for something we can compute
end
def handle(%{packet_type: :player_command, eid: _eid, action_id: aid, jump_boost: _horse_jump}, 767, state) do
# TODO: Actually handle these events
case aid do
0 -> # Start sneaking
:ok
1 -> # Stop sneaking
:ok
2 -> # Leave bed
:ok
3 -> # Start sprinting
:ok
4 -> # Stop sprinting
:ok
5 -> # Start horse jump
:ok
6 -> # Stop horse jump
:ok
7 -> # Open vehicle inventory
:ok
8 -> # Start elytra flying
:ok
_ -> raise RuntimeError, "Unknown Player Command Action ID"
end
end
def disconnect(reason) do
%{

View File

@ -1,5 +1,5 @@
defmodule GameCoordinatorTestGame do
@behaviour Amethyst.API.Game
@behaviour Amethyst.Game
@moduledoc """
This module is a sample game for the purpose of

View File

@ -1,6 +1,6 @@
defmodule Example.Game do
require Logger
@behaviour Amethyst.API.Game
@behaviour Amethyst.Game
@impl true
def instantiate(supervisor) do
@ -40,7 +40,7 @@ defmodule Example.Game do
end
@impl true
def chunk(from, {x, z}, _state_refs) do
def chunk(_from, {_x, _z}, _state_refs) do
# Logger.info("Player at #{inspect(from)} wants to know chunk #{x}, #{z}")
(0..255) |> Enum.map(fn y ->
if y < 5 do
@ -49,7 +49,7 @@ defmodule Example.Game do
end)
else
(0..15) |> Enum.map(fn _z ->
(0..15) |> Enum.map(fn _x -> 0 end)
(0..15) |> Enum.map(fn _x -> 1 end)
end)
end
end)