Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.56 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace InventoryMenu;
  6.  
  7.  
  8. use pocketmine\event\Listener;
  9. use pocketmine\event\player\PlayerInteractEvent;
  10. use pocketmine\event\player\PlayerJoinEvent;
  11. use pocketmine\event\player\PlayerQuitEvent;
  12. use pocketmine\event\server\DataPacketReceiveEvent;
  13. use pocketmine\item\Item;
  14. use pocketmine\math\Vector3;
  15. use pocketmine\nbt\NBT;
  16. use pocketmine\nbt\tag\CompoundTag;
  17. use pocketmine\nbt\tag\IntTag;
  18. use pocketmine\nbt\tag\StringTag;
  19. use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
  20. use pocketmine\network\mcpe\protocol\ContainerClosePacket;
  21. use pocketmine\network\mcpe\protocol\ContainerOpenPacket;
  22. use pocketmine\network\mcpe\protocol\InventoryContentPacket;
  23. use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
  24. use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
  25. use pocketmine\network\mcpe\protocol\UpdateBlockPacket;
  26. use pocketmine\Player;
  27. use pocketmine\plugin\PluginBase;
  28.  
  29. class Main extends PluginBase implements Listener
  30. {
  31.  
  32.     /** @var  array */
  33.     private $chest;
  34.     /** @var  array */
  35.     private $items;
  36.  
  37.     public function onEnable(): void
  38.     {
  39.         $this->getServer()->getPluginManager()->registerEvents($this, $this);
  40.     }
  41.  
  42.     public function onDataPacketReceive(DataPacketReceiveEvent $e): void
  43.     {
  44.         $packet = $e->getPacket();
  45.         $player = $e->getPlayer();
  46.         if ($packet instanceof InventoryTransactionPacket) {
  47.             if (!isset($this->chest[$player->getName()]) or !isset($packet->actions[0])) return;
  48.             /** @var NetworkInventoryAction $action */
  49.             $action = $packet->actions[0];
  50.             //NOTE: now you can get item from $packet->actions
  51.             if ($packet->transactionType === 0 && $this->chest[$player->getName()]) {
  52.                 //$pk = new ContainerClosePacket();
  53.                 //$pk->windowId = 10;
  54.                 //$player->dataPacket($pk);
  55.  
  56.                 /** @var Item $item */
  57.                 $item = $this->items[$player->getName()][1];
  58.                 $this->items[$player->getName()][1] = $item->setCount($item->getCount() - 1);
  59.                 //[1] is slot
  60.                 $pk2 = new InventoryContentPacket;
  61.                 $pk2->windowId = 10;
  62.                 $pk2->items = $this->items[$player->getName()];
  63.                 $player->dataPacket($pk2);
  64.                 switch ($this->chest[$player->getName()][0]) { //chest id
  65.                     case 1:
  66.                         switch ($action->inventorySlot) {
  67.                             case 0:
  68.                                 $player->sendMessage("You select stone block");
  69.                                 break;
  70.                             case 1:
  71.                                 $player->sendMessage("You select grass block"); //todo: change action
  72.                                 break;
  73.                         }
  74.                         break;
  75.                     case 2:
  76.                         switch ($action->inventorySlot) {
  77.                             case 0:
  78.                                 $player->sendMessage("You select sky wars");
  79.                                 break;
  80.                             case 1:
  81.                                 $player->sendMessage("You select hunger games");
  82.                                 break;
  83.                         }
  84.                         break;
  85.                 }
  86.             }
  87.         } elseif ($packet instanceof ContainerClosePacket) {
  88.             if (!isset($this->chest[$player->getName()])) return;
  89.             /** @var Vector3 $v3 */
  90.             $v3 = $this->chest[$player->getName()][1];
  91.             $this->updateBlock($player, $player->getLevel()->getBlock($v3)->getId(), $v3);
  92.             if (isset($this->chest[$player->getName()][2])) {
  93.                 $v3 = $v3->setComponents($v3->x + 1, $v3->y, $v3->z);
  94.                 $this->updateBlock($player, $player->getLevel()->getBlock($v3)->getId(), $v3);
  95.             }
  96.             $this->clearData($player);
  97.         }
  98.     }
  99.  
  100.     public function onClick(PlayerInteractEvent $e): void
  101.     {
  102.         if ($e->getAction() == PlayerInteractEvent::RIGHT_CLICK_BLOCK) {
  103.             $player = $e->getPlayer();
  104.             switch ($e->getItem()->getId()) {
  105.                 case Item::COMPASS:
  106.                     $this->createChest($player, [Item::get(1, 0, 1), Item::get(2, 0, 64)], 1, "Select block");
  107.                     break;
  108.                 case Item::CLOCK:
  109.                     $this->createChest($player, [Item::get(267, 0, 1)->setCustomName("§eSkyWars"), Item::get(276, 0, 1)->setCustomName("§aHungerGames")], 2, "Select mini game", true);
  110.                     break;
  111.             }
  112.         }
  113.     }
  114.  
  115.     public function onPlayerJoin(PlayerJoinEvent $e): void
  116.     {
  117.         $inv = $e->getPlayer()->getInventory();
  118.         $inv->clearAll();
  119.         $inv->setItem(0, Item::get(Item::COMPASS, 0, 1)->setCustomName("Select block"));
  120.         $inv->setItem(1, Item::get(Item::CLOCK, 0, 1)->setCustomName("Select mini game"));
  121.     }
  122.  
  123.     public function onPlayerQuit(PlayerQuitEvent $e): void
  124.     {
  125.         $this->clearData($e->getPlayer());
  126.     }
  127.  
  128.     /**
  129.      * @param Player $player
  130.      * @param array $items  items array
  131.      * @param int $id       chest id
  132.      * @param string $title chest title
  133.      * @param bool $double  double chest or not? default = false
  134.      */
  135.     public function createChest(Player $player, array $items, int $id, string $title, bool $double = false): void
  136.     {
  137.         $this->clearData($player);
  138.  
  139.         $v3 = $this->getVector($player);
  140.         $this->chest[$player->getName()] = [$id, $v3];
  141.         $this->updateBlock($player, 54, $v3);
  142.  
  143.         $nbt = new NBT(NBT::LITTLE_ENDIAN);
  144.         if ($double) {
  145.             $this->chest[$player->getName()][2] = true;
  146.             $this->updateBlock($player, 54, new Vector3($v3->x + 1, $v3->y, $v3->z));
  147.             $nbt->setData(new CompoundTag(
  148.                 "", [
  149.                     new StringTag("CustomName", $title),
  150.                     new IntTag("pairx", $v3->x + 1),
  151.                     new IntTag("pairz", $v3->z)
  152.                 ]
  153.             ));
  154.         } else {
  155.             $nbt->setData(new CompoundTag("", [new StringTag("CustomName", $title)]));
  156.         }
  157.         $pk = new BlockEntityDataPacket;
  158.         $pk->x = $v3->x;
  159.         $pk->y = $v3->y;
  160.         $pk->z = $v3->z;
  161.         $pk->namedtag = $nbt->write(true);
  162.         $player->dataPacket($pk);
  163.  
  164.         if ($double) usleep(51000);
  165.  
  166.         $pk1 = new ContainerOpenPacket;
  167.         $pk1->windowId = 10;
  168.         $pk1->type = 0;
  169.         $pk1->x = $v3->x;
  170.         $pk1->y = $v3->y;
  171.         $pk1->z = $v3->z;
  172.         $player->dataPacket($pk1);
  173.  
  174.         $pk2 = new InventoryContentPacket();
  175.         $pk2->windowId = 10;
  176.         $pk2->items = $items;
  177.         $player->dataPacket($pk2);
  178.         $this->items[$player->getName()] = $items;
  179.     }
  180.  
  181.     public function updateBlock(Player $player, int $id, Vector3 $v3): void
  182.     {
  183.         $pk = new UpdateBlockPacket;
  184.         $pk->x = $v3->x;
  185.         $pk->y = $v3->y;
  186.         $pk->z = $v3->z;
  187.         $pk->blockId = $id;
  188.         $pk->blockData = 0xb << 4 | (0 & 0xf);
  189.         $player->dataPacket($pk);
  190.     }
  191.  
  192.     public function getVector(Player $player): Vector3
  193.     {
  194.         return new Vector3(intval($player->x), intval($player->y) - 2, intval($player->z));
  195.     }
  196.  
  197.     public function clearData(Player $player): void
  198.     {
  199.         if (isset($this->chest[$player->getName()])) unset($this->chest[$player->getName()]);
  200.         if (isset($this->items[$player->getName()])) unset($this->items[$player->getName()]);
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement