Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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<falcon::Player> bot = falcon::Server::spawnSimulatedPlayer("Steve", level, {0, 80, 0}))
bot->removeSimulated();
```

### Administration
Expand Down
10 changes: 10 additions & 0 deletions java/src/main/java/falcon/api/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
15 changes: 15 additions & 0 deletions java/src/main/java/falcon/api/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public Optional<Player> 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<Player> 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));
Expand Down
10 changes: 10 additions & 0 deletions native/include/falcon/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions native/include/falcon/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player> 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<FalconGameMode>(gameMode));
if (handle == nullptr)
return std::nullopt;
return Player(handle);
}

static void broadcast(const std::string &message) {
detail::api().broadcastMessage(message.c_str());
}
Expand Down
5 changes: 5 additions & 0 deletions native/include/falcon/falcon_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions wasm/falcon.wit
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ interface players {
entity-player: func(entity: entity) -> option<player>;
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>;
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;
Expand Down
Loading