diff --git a/README.md b/README.md index b062b08..ec4eca9 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,9 @@ falcon::Server::setDifficulty(falcon::Difficulty::Hard); ### Entities and players Mobs expose their equipment, riding, tags and attack target; players their spawn point, language, platform, skin, -camera and item cooldowns. `Player::setSkin` gives a player a classic skin of at most 128 by 128 pixels: +camera and item cooldowns. `Player::setSkin` gives a player a classic skin of at most 128 by 128 pixels. +`Server::spawnSimulatedPlayer` brings in a player the server plays, as the game's simulated players: it joins +with the usual message, shows in the player list and leaves with `Player::removeSimulated`: ```cpp zombie.setEquipment(falcon::EquipmentSlot::Head, *falcon::Item::create("minecraft:diamond_helmet")); @@ -261,6 +263,9 @@ player.setSpawnPoint(level, {0, 80, 0}); player.sendMessage("Your language: " + player.language()); player.setCamera("minecraft:free", {0, 100, 0}, {30, 90, 0}, 2.0f, falcon::CameraEase::InOutSine); player.fadeCamera(0.5f, 1.0f, 0.5f, 0x000000); + +if (std::optional bot = falcon::Server::spawnSimulatedPlayer("Steve", level, {0, 80, 0})) + bot->removeSimulated(); ``` ### Administration diff --git a/java/src/main/java/falcon/api/Player.java b/java/src/main/java/falcon/api/Player.java index 74b7029..7411853 100644 --- a/java/src/main/java/falcon/api/Player.java +++ b/java/src/main/java/falcon/api/Player.java @@ -55,6 +55,16 @@ public GameMode gameMode() { return GameMode.of(api().playerGameMode(mHandle)); } + /** Whether the server plays this player, as one from Server#spawnSimulatedPlayer. */ + public boolean isSimulated() { + return api().playerIsSimulated(mHandle) != 0; + } + + /** Makes a simulated player leave as a player who disconnects; false for any other player. */ + public boolean removeSimulated() { + return api().playerRemoveSimulated(mHandle) != 0; + } + public void setGameMode(GameMode gameMode) { api().playerSetGameMode(mHandle, gameMode.value()); } diff --git a/java/src/main/java/falcon/api/Server.java b/java/src/main/java/falcon/api/Server.java index db159b7..8b4b8aa 100644 --- a/java/src/main/java/falcon/api/Server.java +++ b/java/src/main/java/falcon/api/Server.java @@ -40,6 +40,21 @@ public Optional findPlayer(String name) { } } + /** + * Brings in a player played by the server, as the game's simulated players: it joins like anyone, shows in the + * player list and stays until removed. Empty when the name or the place is refused. + */ + public Optional spawnSimulatedPlayer(String name, Level level, Vec3 position, float yaw, GameMode gameMode) { + try (Arena arena = Arena.ofConfined()) { + MemorySegment player = api().serverSpawnSimulatedPlayer(Interop.text(arena, name), level.handle(), + Interop.vec3(arena, position), yaw, gameMode.value()); + if (Interop.isNull(player)) { + return Optional.empty(); + } + return Optional.of(new Player(mContext, player)); + } + } + public void broadcast(String message) { try (Arena arena = Arena.ofConfined()) { api().broadcastMessage(Interop.text(arena, message)); diff --git a/native/include/falcon/Player.hpp b/native/include/falcon/Player.hpp index 014c78f..1b9a10b 100644 --- a/native/include/falcon/Player.hpp +++ b/native/include/falcon/Player.hpp @@ -95,6 +95,16 @@ namespace falcon { detail::api().playerKick(mHandle, reason.c_str()); } + /** Whether the server plays this player, as one from Server::spawnSimulatedPlayer. */ + bool isSimulated() const { + return detail::api().playerIsSimulated(mHandle) != 0; + } + + /** Makes a simulated player leave as a player who disconnects; false for any other player. */ + bool removeSimulated() const { + return detail::api().playerRemoveSimulated(mHandle) != 0; + } + Entity entity() const; GameMode gameMode() const { diff --git a/native/include/falcon/Server.hpp b/native/include/falcon/Server.hpp index 72f0edc..20048cd 100644 --- a/native/include/falcon/Server.hpp +++ b/native/include/falcon/Server.hpp @@ -149,6 +149,20 @@ namespace falcon { return Player(handle); } + /** + * Brings in a player played by the server, as the game's simulated players: it joins like anyone, shows in + * the player list and stays until removed. Empty when the name or the place is refused. + */ + static std::optional spawnSimulatedPlayer(const std::string &name, const Level &level, + const Vec3 &position, float yaw = 0.0f, + GameMode gameMode = GameMode::Survival) { + FalconPlayer *handle = detail::api().serverSpawnSimulatedPlayer( + name.c_str(), level.handle(), position.raw(), yaw, static_cast(gameMode)); + if (handle == nullptr) + return std::nullopt; + return Player(handle); + } + static void broadcast(const std::string &message) { detail::api().broadcastMessage(message.c_str()); } diff --git a/native/include/falcon/falcon_api.h b/native/include/falcon/falcon_api.h index d95f3c0..88e3a82 100644 --- a/native/include/falcon/falcon_api.h +++ b/native/include/falcon/falcon_api.h @@ -794,6 +794,11 @@ typedef struct FalconServerApi { int (*entitySetNbt)(FalconEntity *entity, const uint8_t *data, uint32_t length); int (*playerSetSkin)(FalconPlayer *player, const char *skinId, const uint8_t *rgba, uint32_t length, uint32_t width, uint32_t height); + + FalconPlayer *(*serverSpawnSimulatedPlayer)(const char *name, FalconLevel *level, FalconVec3 position, float yaw, + FalconGameMode gameMode); + int (*playerIsSimulated)(FalconPlayer *player); + int (*playerRemoveSimulated)(FalconPlayer *player); } FalconServerApi; typedef int (*FalconPluginEntry)(const FalconServerApi *api, FalconPlugin *plugin, FalconPluginCallbacks *callbacks); diff --git a/wasm/falcon.wit b/wasm/falcon.wit index f87407d..f2723fd 100644 --- a/wasm/falcon.wit +++ b/wasm/falcon.wit @@ -185,6 +185,13 @@ interface players { entity-player: func(entity: entity) -> option; player-game-mode: func(player: player) -> game-mode; player-set-game-mode: func(player: player, game-mode: game-mode); + /// A player played by the server, as the game's simulated players: it joins like anyone, shows in the player + /// list and stays until removed. `none` when the name or the place is refused. + server-spawn-simulated-player: func(name: string, level: level, position: vec3, yaw: f32, + game-mode: game-mode) -> option; + player-is-simulated: func(player: player) -> bool; + /// Makes a simulated player leave as a player who disconnects; false for any other player. + player-remove-simulated: func(player: player) -> bool; player-xuid: func(player: player) -> string; player-uuid: func(player: player) -> string; player-address: func(player: player) -> string;