Skip to content

Commit aba1e27

Browse files
committed
feat: store worlds in players
1 parent 8a9e8cf commit aba1e27

File tree

17 files changed

+997
-705
lines changed

17 files changed

+997
-705
lines changed

packages/cpp/src/generated/player_events.pb.cc

Lines changed: 293 additions & 242 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cpp/src/generated/player_events.pb.h

Lines changed: 432 additions & 322 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node/src/generated/player_events.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
88
import { Address, BlockPos, BlockState, DamageSource, EntityRef, HealingSource, ItemStack, Rotation, Vec3, WorldRef, } from "./common.js";
99
export const protobufPackage = "df.plugin";
1010
function createBasePlayerJoinEvent() {
11-
return { playerUuid: "", name: "" };
11+
return { playerUuid: "", name: "", world: undefined };
1212
}
1313
export const PlayerJoinEvent = {
1414
encode(message, writer = new BinaryWriter()) {
@@ -18,6 +18,9 @@ export const PlayerJoinEvent = {
1818
if (message.name !== "") {
1919
writer.uint32(18).string(message.name);
2020
}
21+
if (message.world !== undefined) {
22+
WorldRef.encode(message.world, writer.uint32(26).fork()).join();
23+
}
2124
return writer;
2225
},
2326
decode(input, length) {
@@ -41,6 +44,13 @@ export const PlayerJoinEvent = {
4144
message.name = reader.string();
4245
continue;
4346
}
47+
case 3: {
48+
if (tag !== 26) {
49+
break;
50+
}
51+
message.world = WorldRef.decode(reader, reader.uint32());
52+
continue;
53+
}
4454
}
4555
if ((tag & 7) === 4 || tag === 0) {
4656
break;
@@ -53,6 +63,7 @@ export const PlayerJoinEvent = {
5363
return {
5464
playerUuid: isSet(object.playerUuid) ? globalThis.String(object.playerUuid) : "",
5565
name: isSet(object.name) ? globalThis.String(object.name) : "",
66+
world: isSet(object.world) ? WorldRef.fromJSON(object.world) : undefined,
5667
};
5768
},
5869
toJSON(message) {
@@ -63,6 +74,9 @@ export const PlayerJoinEvent = {
6374
if (message.name !== "") {
6475
obj.name = message.name;
6576
}
77+
if (message.world !== undefined) {
78+
obj.world = WorldRef.toJSON(message.world);
79+
}
6680
return obj;
6781
},
6882
create(base) {
@@ -72,6 +86,9 @@ export const PlayerJoinEvent = {
7286
const message = createBasePlayerJoinEvent();
7387
message.playerUuid = object.playerUuid ?? "";
7488
message.name = object.name ?? "";
89+
message.world = (object.world !== undefined && object.world !== null)
90+
? WorldRef.fromPartial(object.world)
91+
: undefined;
7592
return message;
7693
},
7794
};

packages/node/src/generated/player_events.ts

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/php/src/Commands/CommandSender.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dragonfly\PluginLib\Commands;
44

5+
use Df\Plugin\WorldRef;
56
use Dragonfly\PluginLib\Actions\Actions;
67
use Dragonfly\PluginLib\Entity\Player;
78

@@ -10,7 +11,8 @@ public function __construct(
1011
public string $uuid,
1112
public string $name,
1213
Actions $actions,
14+
?WorldRef $world = null,
1315
) {
14-
parent::__construct($uuid, $name, $actions);
16+
parent::__construct($uuid, $name, $actions, $world);
1517
}
1618
}

packages/php/src/Entity/Player.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use Df\Plugin\ItemStack;
66
use Df\Plugin\Vec3;
7+
use Df\Plugin\WorldRef;
78
use Dragonfly\PluginLib\Actions\Actions;
89

910
final class Player {
1011
public function __construct(
1112
private string $uuid,
1213
private string $name,
1314
private Actions $actions,
15+
private ?WorldRef $world = null,
1416
) {}
1517

1618
public function getUuid(): string {
@@ -21,6 +23,14 @@ public function getName(): string {
2123
return $this->name;
2224
}
2325

26+
public function getWorld(): ?WorldRef {
27+
return $this->world;
28+
}
29+
30+
public function setWorld(?WorldRef $world): void {
31+
$this->world = $world;
32+
}
33+
2434
public function sendMessage(string $message): void {
2535
$this->actions->chatToUuid($this->uuid, $message);
2636
}

packages/php/src/Events/EventContext.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public function player(string $uuid, string $name = ''): Player {
9494
}
9595

9696
public function commandSender(string $uuid, string $name): CommandSender {
97-
return new CommandSender($uuid, $name, $this->getActions());
97+
$world = $this->server->getPlayerWorld($uuid);
98+
return new CommandSender($uuid, $name, $this->getActions(), $world);
9899
}
99100

100101
/**

packages/php/src/PluginBase.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ private function registerPlayerTracking(): void {
519519
$this->addEventHandler(EventType::PLAYER_JOIN, function (string $eventId, EventEnvelope $event): void {
520520
$payload = $event->getPlayerJoin();
521521
if ($payload !== null) {
522-
$this->server->addPlayer($payload->getPlayerUuid(), $payload->getName());
522+
$world = method_exists($payload, 'getWorld') ? $payload->getWorld() : null;
523+
$this->server->addPlayer($payload->getPlayerUuid(), $payload->getName(), $world);
523524
}
524525
});
525526

@@ -530,5 +531,16 @@ private function registerPlayerTracking(): void {
530531
$this->server->removePlayer($payload->getPlayerUuid());
531532
}
532533
});
534+
535+
// Track world changes
536+
$this->addEventHandler(EventType::PLAYER_CHANGE_WORLD, function (string $eventId, EventEnvelope $event): void {
537+
$payload = $event->getPlayerChangeWorld();
538+
if ($payload !== null && method_exists($payload, 'getAfter')) {
539+
$world = $payload->getAfter();
540+
if ($world !== null) {
541+
$this->server->setPlayerWorld($payload->getPlayerUuid(), $world);
542+
}
543+
}
544+
});
533545
}
534546
}

packages/php/src/Server/Server.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dragonfly\PluginLib\Server;
44

5+
use Df\Plugin\WorldRef;
56
use Dragonfly\PluginLib\Actions\Actions;
67
use Dragonfly\PluginLib\Entity\Player;
78

@@ -12,7 +13,7 @@
1213
* when using PluginBase.
1314
*/
1415
final class Server {
15-
/** @var array<string, string> uuid => name */
16+
/** @var array<string, Player> uuid => Player instance */
1617
private array $players = [];
1718

1819
/** @var array<string, string> lowercase name => uuid */
@@ -25,17 +26,33 @@ public function __construct(
2526
/**
2627
* Register a player as online. Called automatically on PlayerJoin.
2728
*/
28-
public function addPlayer(string $uuid, string $name): void {
29-
$this->players[$uuid] = $name;
29+
public function addPlayer(string $uuid, string $name, ?WorldRef $world = null): void {
30+
$this->players[$uuid] = new Player($uuid, $name, $this->actions, $world);
3031
$this->nameIndex[strtolower($name)] = $uuid;
3132
}
3233

34+
/**
35+
* Update a player's world. Called on world change events.
36+
*/
37+
public function setPlayerWorld(string $uuid, WorldRef $world): void {
38+
if (isset($this->players[$uuid])) {
39+
$this->players[$uuid]->setWorld($world);
40+
}
41+
}
42+
43+
/**
44+
* Get a player's current world.
45+
*/
46+
public function getPlayerWorld(string $uuid): ?WorldRef {
47+
return $this->players[$uuid]?->getWorld();
48+
}
49+
3350
/**
3451
* Remove a player from the registry. Called automatically on PlayerQuit.
3552
*/
3653
public function removePlayer(string $uuid): void {
3754
if (isset($this->players[$uuid])) {
38-
$name = $this->players[$uuid];
55+
$name = $this->players[$uuid]->getName();
3956
unset($this->nameIndex[strtolower($name)]);
4057
unset($this->players[$uuid]);
4158
}
@@ -45,10 +62,7 @@ public function removePlayer(string $uuid): void {
4562
* Get a player by UUID. Returns null if not online.
4663
*/
4764
public function getPlayer(string $uuid): ?Player {
48-
if (!isset($this->players[$uuid])) {
49-
return null;
50-
}
51-
return new Player($uuid, $this->players[$uuid], $this->actions);
65+
return $this->players[$uuid] ?? null;
5266
}
5367

5468
/**
@@ -59,8 +73,7 @@ public function getPlayerByName(string $name): ?Player {
5973
if (!isset($this->nameIndex[$lower])) {
6074
return null;
6175
}
62-
$uuid = $this->nameIndex[$lower];
63-
return new Player($uuid, $this->players[$uuid], $this->actions);
76+
return $this->players[$this->nameIndex[$lower]] ?? null;
6477
}
6578

6679
/**
@@ -69,11 +82,7 @@ public function getPlayerByName(string $name): ?Player {
6982
* @return Player[]
7083
*/
7184
public function getOnlinePlayers(): array {
72-
$result = [];
73-
foreach ($this->players as $uuid => $name) {
74-
$result[] = new Player($uuid, $name, $this->actions);
75-
}
76-
return $result;
85+
return array_values($this->players);
7786
}
7887

7988
/**
@@ -101,9 +110,8 @@ public function isOnlineByName(string $name): bool {
101110
* Broadcast a message to all online players.
102111
*/
103112
public function broadcastMessage(string $message): void {
104-
foreach ($this->players as $uuid => $_) {
105-
$this->actions->chatToUuid($uuid, $message);
113+
foreach ($this->players as $player) {
114+
$player->sendMessage($message);
106115
}
107116
}
108117
}
109-

0 commit comments

Comments
 (0)