Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Last active July 23, 2020 20:11
Show Gist options
  • Save PJZ9n/6ef7b03f15acd720c1bc512e1d612502 to your computer and use it in GitHub Desktop.
Save PJZ9n/6ef7b03f15acd720c1bc512e1d612502 to your computer and use it in GitHub Desktop.
チェストをプレイヤーが操作したことを検知する
<?php
public function on(InventoryTransactionEvent $event): void
{
$tra = $event->getTransaction();
if (count($tra->getActions()) !== 2) {
//TODO
return;
}
//プレイヤーの取得
$player = null;
foreach ($tra->getActions() as $act) {
if ($act instanceof SlotChangeAction) {
$inv = $act->getInventory();
if ($inv instanceof PlayerCursorInventory) {
$player = $inv->getHolder();
}
}
}
//インベントリから取ったアイテムを取得
if ($player !== null) {
foreach ($tra->getActions() as $act) {
if ($act instanceof SlotChangeAction) {
$inv = $act->getInventory();
if ($inv instanceof ChestInventory) {
$chest = $inv->getHolder();
//$chestはChestのTile、つまりPositionのインスタンスだから適当に使ってね(笑)
$player->sendMessage("Chest: " . $chest->asPosition()->__toString());
$player->sendMessage("Source: " . $act->getSourceItem()->__toString());
$player->sendMessage("Target: " . $act->getTargetItem()->__toString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment