Skip to content

Commit b0612b9

Browse files
committed
feat: add server to get players, and add player wrappers
1 parent 0052561 commit b0612b9

8 files changed

Lines changed: 320 additions & 25 deletions

File tree

examples/plugins/php/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "project",
55
"require": {
66
"php": ">=8.1",
7-
"dragonfly-plugins/plugin-sdk": "^0.0.3",
7+
"dragonfly-plugins/plugin-sdk": "^0.0.5",
88
"grpc/grpc": "1.74.0",
99
"google/protobuf": "^3.25"
1010
},

examples/plugins/php/src/EffectCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EffectCommand extends Command {
2424
public function execute(CommandSender $sender, EventContext $ctx): void {
2525
$effectId = $this->resolveEffectId($this->effect);
2626
if ($effectId === null) {
27-
$ctx->chatToUuid($sender->uuid, "§cUnknown effect: {$this->effect}");
27+
$sender->sendMessage("§cUnknown effect: {$this->effect}");
2828
return;
2929
}
3030

@@ -33,8 +33,8 @@ public function execute(CommandSender $sender, EventContext $ctx): void {
3333
$show = $this->showParticles->getOr(true);
3434
$durationMs = $seconds * 1000;
3535

36-
$ctx->addEffectUuid($sender->uuid, $effectId, $level, $durationMs, $show);
37-
$ctx->chatToUuid($sender->uuid, "Applied effect " . $this->enumName($effectId) . " (id {$effectId}) level {$level} for {$seconds}s" . ($show ? '' : ' (hidden)'));
36+
$sender->addEffect($effectId, $level, $durationMs, $show);
37+
$sender->sendMessage("Applied effect " . $this->enumName($effectId) . " (id {$effectId}) level {$level} for {$seconds}s" . ($show ? '' : ' (hidden)'));
3838
}
3939

4040
/**

examples/plugins/php/src/HelloPlugin.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
require_once __DIR__ . '/../vendor/autoload.php';
1111

12-
use Df\Plugin\EventType;
1312
use Df\Plugin\PlayerJoinEvent;
1413
use Df\Plugin\ItemStack;
1514
use Df\Plugin\ItemCategory;
1615
use Df\Plugin\ChatEvent;
1716
use Df\Plugin\CommandEvent;
1817
use Df\Plugin\PlayerAttackEntityEvent;
1918
use Df\Plugin\PlayerAttackEntityMutation;
19+
use Df\Plugin\PlayerJumpEvent;
20+
use Df\Plugin\Sound;
2021
use Dragonfly\PluginLib\PluginBase;
2122
use Dragonfly\PluginLib\Events\EventContext;
2223
use Dragonfly\PluginLib\Events\Listener;
@@ -44,11 +45,12 @@ public function onEnable(): void {
4445
}
4546

4647
public function onPlayerJoin(PlayerJoinEvent $e, EventContext $ctx): void {
48+
$player = $ctx->getPlayer();
4749
$stack = new ItemStack();
4850
$stack->setName('vasar:pokemon');
4951
$stack->setMeta(0);
5052
$stack->setCount(1);
51-
$ctx->giveItemUuid($e->getPlayerUuid(), $stack);
53+
$player->giveItem($stack);
5254
}
5355

5456
public function onChat(ChatEvent $chat, EventContext $ctx): void {
@@ -60,23 +62,24 @@ public function onChat(ChatEvent $chat, EventContext $ctx): void {
6062
}
6163

6264
if (str_starts_with($text, '!cheer ')) {
63-
$ctx->chat('🥂 ' . substr($text, 7));
65+
$ctx->getPlayer()->sendMessage('🥂 ' . substr($text, 7));
6466
return;
6567
}
6668
}
6769

6870
public function onCommand(CommandEvent $command, EventContext $ctx): void {
71+
$player = $ctx->getPlayer();
6972
switch ($command->getRaw()) {
7073
case '/cheers':
71-
$ctx->chatToUuid($command->getPlayerUuid(), 'Cheers from the PHP plugin!');
74+
$player->sendMessage('Cheers from the PHP plugin!');
7275
break;
7376
case '/pokemon':
74-
$ctx->chatToUuid($command->getPlayerUuid(), 'You have been given a Pokemon item!');
77+
$player->sendMessage('You have been given a Pokemon item!');
7578
$stack = new ItemStack();
7679
$stack->setName('vasar:pokemon');
7780
$stack->setMeta(0);
7881
$stack->setCount(1);
79-
$ctx->giveItemUuid($command->getPlayerUuid(), $stack);
82+
$player->giveItem($stack);
8083
break;
8184
}
8285
}
@@ -87,6 +90,13 @@ public function onPlayerAttackEntity(PlayerAttackEntityEvent $e, EventContext $c
8790
$mutation->setHeight(0.6);
8891
$ctx->playerAttackEntity($mutation);
8992
}
93+
94+
public function onPlayerJump(PlayerJumpEvent $e, EventContext $ctx): void {
95+
$player = $ctx->getPlayer();
96+
$position = $e->getPosition();
97+
$player->playSound(Sound::EXPLOSION, $position, 1.0, 1.0);
98+
}
99+
90100
}
91101

92102
$plugin = new HelloPlugin();

packages/php/src/Commands/CommandSender.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace Dragonfly\PluginLib\Commands;
44

5-
class CommandSender {
5+
use Dragonfly\PluginLib\Actions\Actions;
6+
use Dragonfly\PluginLib\Entity\Player;
7+
8+
class CommandSender extends Player {
69
public function __construct(
710
public string $uuid,
811
public string $name,
9-
) {}
12+
Actions $actions,
13+
) {
14+
parent::__construct($uuid, $name, $actions);
15+
}
1016
}

packages/php/src/Entity/Player.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Dragonfly\PluginLib\Entity;
4+
5+
use Df\Plugin\ItemStack;
6+
use Df\Plugin\Vec3;
7+
use Dragonfly\PluginLib\Actions\Actions;
8+
9+
final class Player {
10+
public function __construct(
11+
private string $uuid,
12+
private string $name,
13+
private Actions $actions,
14+
) {}
15+
16+
public function getUuid(): string {
17+
return $this->uuid;
18+
}
19+
20+
public function getName(): string {
21+
return $this->name;
22+
}
23+
24+
public function sendMessage(string $message): void {
25+
$this->actions->chatToUuid($this->uuid, $message);
26+
}
27+
28+
public function teleport(?Vec3 $position = null, ?Vec3 $rotation = null): void {
29+
$this->actions->teleportUuid($this->uuid, $position, $rotation);
30+
}
31+
32+
public function kick(string $reason): void {
33+
$this->actions->kickUuid($this->uuid, $reason);
34+
}
35+
36+
public function setGameMode(int $gameMode): void {
37+
$this->actions->setGameModeUuid($this->uuid, $gameMode);
38+
}
39+
40+
public function giveItem(ItemStack $item): void {
41+
$this->actions->giveItemUuid($this->uuid, $item);
42+
}
43+
44+
public function clearInventory(): void {
45+
$this->actions->clearInventoryUuid($this->uuid);
46+
}
47+
48+
public function setHeldItems(?ItemStack $main = null, ?ItemStack $offhand = null): void {
49+
$this->actions->setHeldItemsUuid($this->uuid, $main, $offhand);
50+
}
51+
52+
public function setHealth(float $health, ?float $maxHealth = null): void {
53+
$this->actions->setHealthUuid($this->uuid, $health, $maxHealth);
54+
}
55+
56+
public function setFood(int $food): void {
57+
$this->actions->setFoodUuid($this->uuid, $food);
58+
}
59+
60+
public function setExperience(?int $level = null, ?float $progress = null, ?int $amount = null): void {
61+
$this->actions->setExperienceUuid($this->uuid, $level, $progress, $amount);
62+
}
63+
64+
public function setVelocity(Vec3 $velocity): void {
65+
$this->actions->setVelocityUuid($this->uuid, $velocity);
66+
}
67+
68+
public function addEffect(int $effectType, int $level, int $durationMs, bool $showParticles = true): void {
69+
$this->actions->addEffectUuid($this->uuid, $effectType, $level, $durationMs, $showParticles);
70+
}
71+
72+
public function removeEffect(int $effectType): void {
73+
$this->actions->removeEffectUuid($this->uuid, $effectType);
74+
}
75+
76+
public function sendTitle(string $title, ?string $subtitle = null, ?int $fadeInMs = null, ?int $durationMs = null, ?int $fadeOutMs = null): void {
77+
$this->actions->sendTitleUuid($this->uuid, $title, $subtitle, $fadeInMs, $durationMs, $fadeOutMs);
78+
}
79+
80+
public function sendPopup(string $message): void {
81+
$this->actions->sendPopupUuid($this->uuid, $message);
82+
}
83+
84+
public function sendTip(string $message): void {
85+
$this->actions->sendTipUuid($this->uuid, $message);
86+
}
87+
88+
public function playSound(int $sound, ?Vec3 $position = null, ?float $volume = null, ?float $pitch = null): void {
89+
$this->actions->playSoundUuid($this->uuid, $sound, $position, $volume, $pitch);
90+
}
91+
92+
public function executeCommand(string $command): void {
93+
$this->actions->executeCommandUuid($this->uuid, $command);
94+
}
95+
}

packages/php/src/Events/EventContext.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Dragonfly\PluginLib\Events;
44

55
use Df\Plugin\EventResult;
6-
use Df\Plugin\EventType;
76
use Dragonfly\PluginLib\Actions\Actions;
87
use Dragonfly\PluginLib\Actions\ActionsTrait;
8+
use Dragonfly\PluginLib\Commands\CommandSender;
9+
use Dragonfly\PluginLib\Entity\Player;
10+
use Dragonfly\PluginLib\Server\Server;
911
use Dragonfly\PluginLib\StreamSender;
1012

1113
final class EventContext {
@@ -14,12 +16,15 @@ final class EventContext {
1416

1517
private bool $handled = false;
1618
private ?Actions $actions = null;
19+
private ?Player $player = null;
1720

1821
public function __construct(
1922
private string $pluginId,
2023
private string $eventId,
2124
private StreamSender $sender,
25+
private Server $server,
2226
private bool $expectsResponse,
27+
private ?object $payload = null,
2328
) {}
2429

2530
protected function getActions(): Actions {
@@ -83,4 +88,36 @@ public function respondWith(object $mutation): void {
8388
public function onActionResult(string $correlationId, callable $handler): void {
8489
$this->sender->onActionResult($correlationId, $handler);
8590
}
91+
92+
public function player(string $uuid, string $name = ''): Player {
93+
return new Player($uuid, $name, $this->getActions());
94+
}
95+
96+
public function commandSender(string $uuid, string $name): CommandSender {
97+
return new CommandSender($uuid, $name, $this->getActions());
98+
}
99+
100+
/**
101+
* Get the Server instance for accessing online players.
102+
*/
103+
public function getServer(): Server {
104+
return $this->server;
105+
}
106+
107+
/**
108+
* Get the player associated with this event.
109+
* Returns null if the event doesn't have a player (e.g., world events).
110+
*/
111+
public function getPlayer(): ?Player {
112+
if ($this->player !== null) {
113+
return $this->player;
114+
}
115+
if ($this->payload === null || !method_exists($this->payload, 'getPlayerUuid')) {
116+
return null;
117+
}
118+
$uuid = $this->payload->getPlayerUuid();
119+
$name = method_exists($this->payload, 'getName') ? $this->payload->getName() : '';
120+
$this->player = new Player($uuid, $name, $this->getActions());
121+
return $this->player;
122+
}
86123
}

0 commit comments

Comments
 (0)