Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created August 2, 2020 17:59
Show Gist options
  • Save PJZ9n/ca0bd0fd7f0cb39cd54843b2319705e0 to your computer and use it in GitHub Desktop.
Save PJZ9n/ca0bd0fd7f0cb39cd54843b2319705e0 to your computer and use it in GitHub Desktop.
チェスト関連でいろいろしたやつ
<?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\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\event\Event;
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;
use pocketmine\item\Item;
use pocketmine\item\ItemIds;
use pocketmine\lang\BaseLang;
use pocketmine\network\mcpe\convert\RuntimeBlockMapping;
use pocketmine\network\mcpe\protocol\SetPlayerGameTypePacket;
use pocketmine\network\mcpe\protocol\UpdateBlockPacket;
use pocketmine\Player;
use pocketmine\plugin\EventExecutor;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
use pocketmine\tile\Chest;
class Main extends PluginBase implements Listener, EventExecutor
{
/** @var BaseLang */
private $lang;
public function onEnable(): void
{
var_dump($this->getFile());
$this->saveDefaultConfig();
$configLanguage = (string)$this->getConfig()->get("language", "default");
$language = $configLanguage === "def" ? $this->getServer()->getLanguage()->getLang() : $configLanguage;
$localePath = $this->getFile() . "resources/locale/";
$this->lang = new BaseLang($language, $localePath, "jpn");
$this->getLogger()->info($this->lang->translateString("language.selected", [$this->lang->getName()]));
$this->getServer()->getPluginManager()->registerEvents($this, $this);
//$this->getServer()->getPluginManager()->registerEvent(Event::class, $this, EventPriority::NORMAL, $this, $this);
}
public function execute(Listener $listener, Event $event)
{
var_dump($event->getEventName());
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool
{
switch ($command->getName()) {
case "newchestshop":
if ($sender instanceof Player) {
$pk = new SetPlayerGameTypePacket();
$pk->gamemode = Player::getClientFriendlyGamemode(1);
$sender->dataPacket($pk);
$sender->sendMessage("ok");
}
return true;
}
return false;
}
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;
}
}
}
}
}
/*public function onEvent(Event $event): void
{
var_dump($event->getEventName());
}*/
public function onPlayerChestInventoryChange(PlayerChestInventoryChangeEvent $event): void
{
var_dump($event->getEventName());
/*var_dump(get_class($event->getSource()));
var_dump(get_class($event->getTarget()));
if ($event->getTarget() instanceof Air) {
return;
}*/
if ($event->getTarget()->getId() === Item::AIR) {
return;
}
$player = $event->getPlayerCursorInventory()->getHolder();
$inventory = $event->getChestInventory();
$chestBlock = $inventory->getHolder()->getBlock();
$chestPos = $chestBlock->asPosition();
/*$old = clone $player->asLocation();
$player->teleport(new Vector3(2555, 255, 2555));
$player->teleport($old);*/
$pk = new UpdateBlockPacket();
$pk->x = $chestBlock->x;
$pk->y = $chestBlock->y;
$pk->z = $chestBlock->z;
$pk->blockRuntimeId = RuntimeBlockMapping::toStaticRuntimeId(ItemIds::AIR);
$pk->flags = UpdateBlockPacket::FLAG_NONE;
$player->sendDataPacket($pk);
//if ($inventory->getHolder()->isPaired()) {
$pair = $inventory->getHolder()->getPair();
//}
$this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (int $currentTick) use ($inventory, $chestBlock, $player, $pair): void {
$pk = new UpdateBlockPacket();
$pk->x = $chestBlock->x;
$pk->y = $chestBlock->y;
$pk->z = $chestBlock->z;
$pk->blockRuntimeId = RuntimeBlockMapping::toStaticRuntimeId($chestBlock->getId(), $chestBlock->getDamage());
$pk->flags = UpdateBlockPacket::FLAG_NONE;
$player->sendDataPacket($pk);
if ($pair instanceof Chest) {
$inventory->getHolder()->unpair();
$inventory->getHolder()->pairWith($pair);
}
}), 2);
/*$pk = new UpdateBlockPacket();
$pk->x = $chestPos->x;
$pk->y = $chestPos->y;
$pk->z = $chestPos->z;
$pk->blockRuntimeId = RuntimeBlockMapping::toStaticRuntimeId($chestBlock->getBlock()->getItemId());
$pk->flags = UpdateBlockPacket::FLAG_NONE;
$player->sendDataPacket($pk);*/
/*$chunkX = $chestPos->getX() >> 4;
$chunkZ = $chestPos->getZ() >> 4;
$nc = $player->getLevel()->getChunk($chunkX, $chunkZ)->networkSerialize();
$player->sendChunk($chunkX, $chunkZ, $nc);*/
//$player->removeWindow($inventory);
/*$p = new ContainerClosePacket();
$p->windowId = 0;
$player->sendDataPacket($p);*/
/*foreach ($inventory->getViewers() as $viewer) {
var_dump($viewer->getName());
}
$this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (int $currentTick) use ($player, $inventory): void {
$inventory->close($player);
}), 20);*/
//$event->setCancelled(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment