diff --git a/apps/amethyst/lib/apps/connection_handler.ex b/apps/amethyst/lib/apps/connection_handler.ex index 497a735..70ad8bf 100644 --- a/apps/amethyst/lib/apps/connection_handler.ex +++ b/apps/amethyst/lib/apps/connection_handler.ex @@ -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 diff --git a/apps/amethyst/lib/api/game.ex b/apps/amethyst/lib/game.ex similarity index 99% rename from apps/amethyst/lib/api/game.ex rename to apps/amethyst/lib/game.ex index 3dc1453..cebfd9c 100644 --- a/apps/amethyst/lib/api/game.ex +++ b/apps/amethyst/lib/game.ex @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -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 diff --git a/apps/amethyst/lib/states/configuration.ex b/apps/amethyst/lib/states/configuration.ex index 7426b65..47f19b2 100644 --- a/apps/amethyst/lib/states/configuration.ex +++ b/apps/amethyst/lib/states/configuration.ex @@ -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, diff --git a/apps/amethyst/lib/states/play.ex b/apps/amethyst/lib/states/play.ex index c7dface..cd4d824 100644 --- a/apps/amethyst/lib/states/play.ex +++ b/apps/amethyst/lib/states/play.ex @@ -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 %{ diff --git a/apps/amethyst/test/game_coordinator_test.exs b/apps/amethyst/test/game_coordinator_test.exs index 5963864..9762c9d 100644 --- a/apps/amethyst/test/game_coordinator_test.exs +++ b/apps/amethyst/test/game_coordinator_test.exs @@ -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 diff --git a/apps/example_game/lib/example/game.ex b/apps/example_game/lib/example/game.ex index bcf83e7..ce7beab 100644 --- a/apps/example_game/lib/example/game.ex +++ b/apps/example_game/lib/example/game.ex @@ -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)