Created
August 5, 2020 11:29
-
-
Save PJZ9n/f4ccf9fc639a299a43220ea784816e12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2020 PJZ9n. | |
* | |
* This file is part of NewChestShop. | |
* | |
* NewChestShop is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* NewChestShop is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with NewChestShop. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
declare(strict_types=1); | |
namespace pjz9n\newchestshop; | |
use pocketmine\event\inventory\InventoryTransactionEvent; | |
use pocketmine\event\Listener; | |
use pocketmine\inventory\ChestInventory; | |
use pocketmine\inventory\PlayerCursorInventory; | |
use pocketmine\inventory\transaction\action\SlotChangeAction; | |
use pocketmine\inventory\transaction\TransactionValidationException; | |
class TransactionListener implements Listener | |
{ | |
public function onInventoryTransaction(InventoryTransactionEvent $event): void | |
{ | |
$tra = $event->getTransaction(); | |
try { | |
$tra->validate();//TODO | |
} catch (TransactionValidationException $exception) { | |
return; | |
} | |
if (count($tra->getActions()) !== 2) { | |
//TODO | |
return; | |
} | |
//プレイヤーの取得 | |
$player = null; | |
foreach ($tra->getActions() as $act) { | |
if ($act instanceof SlotChangeAction) { | |
$pinv = $act->getInventory(); | |
if ($pinv instanceof PlayerCursorInventory) { | |
$player = $pinv->getHolder(); | |
break; | |
} | |
} | |
} | |
//インベントリから取ったアイテムを取得 | |
if ($player !== null) { | |
foreach ($tra->getActions() as $act) { | |
if ($act instanceof SlotChangeAction) { | |
$inv = $act->getInventory(); | |
if ($inv instanceof ChestInventory) { | |
$calledEvent = new PlayerChestInventoryChangeEvent($pinv, $inv, $act->getSourceItem(), $act->getTargetItem()); | |
$calledEvent->call(); | |
$event->setCancelled($calledEvent->isCancelled()); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment