|
| 1 | +<?php |
| 2 | +namespace aliuly\nechest; |
| 3 | + |
| 4 | +use pocketmine\plugin\PluginBase; |
| 5 | +use pocketmine\event\Listener; |
| 6 | + |
| 7 | +use pocketmine\utils\Config; |
| 8 | +use pocketmine\item\Item; |
| 9 | +use pocketmine\block\Block; |
| 10 | +use pocketmine\Player; |
| 11 | +use pocketmine\inventory\Inventory; |
| 12 | +use pocketmine\inventory\DoubleChestInventory; |
| 13 | +use pocketmine\inventory\ChestInventory; |
| 14 | +use pocketmine\tile\Tile; |
| 15 | +use pocketmine\tile\Chest; |
| 16 | +use pocketmine\math\Vector3; |
| 17 | + |
| 18 | + |
| 19 | +use pocketmine\event\player\PlayerQuitEvent; |
| 20 | +use pocketmine\event\inventory\InventoryCloseEvent; |
| 21 | +use pocketmine\event\inventory\InventoryOpenEvent; |
| 22 | +use pocketmine\event\block\BlockPlaceEvent; |
| 23 | + |
| 24 | +// OPEN |
| 25 | +//- PlayerInteractEvent; |
| 26 | +//- InventoryOpenEvent; |
| 27 | + |
| 28 | +//PUT IN CHEST|GET FROM CHEST |
| 29 | +//- InventoryTransactionEvent; |
| 30 | +//- EntityInventoryChangeEvent; |
| 31 | + |
| 32 | +// CLOSE |
| 33 | +//- InventoryCloseEvent; |
| 34 | + |
| 35 | + |
| 36 | +// |
| 37 | +//use pocketmine\level\Level; |
| 38 | +//use pocketmine\event\entity\EntityLevelChangeEvent; |
| 39 | +//use pocketmine\block\Block; |
| 40 | +//use pocketmine\Server; |
| 41 | +//use pocketmine\utils\TextFormat; |
| 42 | +//use pocketmine\scheduler\CallbackTask; |
| 43 | + |
| 44 | + |
| 45 | +class Main extends PluginBase implements Listener { |
| 46 | + protected $chests; // Array with active chests |
| 47 | + protected $base_block = 87; |
| 48 | + protected $pp_int = 30; |
| 49 | + |
| 50 | + protected static function iName($player) { |
| 51 | + return strtolower($player->getName()); |
| 52 | + } |
| 53 | + protected static function chestId($obj) { |
| 54 | + if ($obj instanceof ChestInventory) $obj = $obj->getHolder(); |
| 55 | + if ($obj instanceof Chest) $obj = $obj->getBlock(); |
| 56 | + return implode(":",[$obj->getLevel()->getName(),(int)$obj->getX(),(int)$obj->getY(),(int)$obj->getZ()]); |
| 57 | + } |
| 58 | + |
| 59 | + public function onEnable(){ |
| 60 | + if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); |
| 61 | + $this->getServer()->getPluginManager()->registerEvents($this, $this); |
| 62 | + $this->chests = []; |
| 63 | + $this->getServer()->getScheduler()->scheduleRepeatingTask( |
| 64 | + new ParticleTask($this),$this->pp_int); |
| 65 | + } |
| 66 | + private function saveInventory(Player $player,Inventory $inv) { |
| 67 | + $n = trim(strtolower($player->getName())); |
| 68 | + if ($n === "") return false; |
| 69 | + $d = substr($n,0,1); |
| 70 | + if (!is_dir($this->getDataFolder().$d)) mkdir($this->getDataFolder().$d); |
| 71 | + |
| 72 | + $path =$this->getDataFolder().$d."/".$n.".yml"; |
| 73 | + $cfg = new Config($path,Config::YAML); |
| 74 | + $yaml = $cfg->getAll(); |
| 75 | + $ln = trim(strtolower($player->getLevel()->getName())); |
| 76 | + |
| 77 | + $yaml[$ln] = []; |
| 78 | + |
| 79 | + foreach ($inv->getContents() as $slot=>&$item) { |
| 80 | + $yaml[$ln][$slot] = implode(":",[ $item->getId(), |
| 81 | + $item->getDamage(), |
| 82 | + $item->getCount() ]); |
| 83 | + } |
| 84 | + $inv->clearAll(); |
| 85 | + $cfg->setAll($yaml); |
| 86 | + $cfg->save(); |
| 87 | + return true; |
| 88 | + } |
| 89 | + private function loadInventory(Player $player,Inventory $inv) { |
| 90 | + $n = trim(strtolower($player->getName())); |
| 91 | + if ($n === "") return false; |
| 92 | + $d = substr($n,0,1); |
| 93 | + $path =$this->getDataFolder().$d."/".$n.".yml"; |
| 94 | + if (!is_file($path)) return false; |
| 95 | + |
| 96 | + $cfg = new Config($path,Config::YAML); |
| 97 | + $yaml = $cfg->getAll(); |
| 98 | + $ln = trim(strtolower($player->getLevel()->getName())); |
| 99 | + |
| 100 | + if (!isset($yaml[$ln])) return false; |
| 101 | + |
| 102 | + $inv->clearAll(); |
| 103 | + foreach($yaml[$ln] as $slot=>$t) { |
| 104 | + list($id,$dam,$cnt) = explode(":",$t); |
| 105 | + $item = Item::get($id,$dam,$cnt); |
| 106 | + $inv->setItem($slot,$item); |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + private function lockChest(Player $player,$obj){ |
| 111 | + $cid = self::chestId($obj); |
| 112 | + if (isset($this->chests[$cid])) return false; |
| 113 | + $this->chests[$cid] = self::iName($player); |
| 114 | + return true; |
| 115 | + } |
| 116 | + private function unlockChest(Player $player,$obj){ |
| 117 | + $cid = self::chestId($obj); |
| 118 | + if (!isset($this->chests[$cid])) return false; |
| 119 | + if ($this->chests[$cid] != self::iName($player)) return false; |
| 120 | + unset($this->chests[$cid]); |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + public function isNeChest(Inventory $inv) { |
| 125 | + if ($inv instanceof DoubleChestInventory) return false; |
| 126 | + if (!($inv instanceof ChestInventory)) return false; |
| 127 | + $tile = $inv->getHolder(); |
| 128 | + if (!($tile instanceof Chest)) return false; |
| 129 | + $bl = $tile->getBlock(); |
| 130 | + if ($bl->getId() != Block::CHEST) return false; |
| 131 | + if ($bl->getSide(Vector3::SIDE_DOWN)->getId() != $this->base_block) return false; |
| 132 | + return true; |
| 133 | + } |
| 134 | + public function onBlockPlaceEvent(BlockPlaceEvent $ev) { |
| 135 | + if ($ev->isCancelled()) return; |
| 136 | + $bl = $ev->getBlock(); |
| 137 | + if ($bl->getId() != Block::CHEST || $bl->getSide(Vector3::SIDE_DOWN)->getId() != $this->base_block) return; |
| 138 | + $ev->getPlayer()->sendMessage("Placed a Nether Chest"); |
| 139 | + } |
| 140 | + |
| 141 | + public function onPlayerQuitEvent(PlayerQuitEvent $ev) { |
| 142 | + $pn = self::iName($ev->getPlayer()); |
| 143 | + foreach (array_keys($this->chests) as $cid) { |
| 144 | + if ($this->chests[$cid] == $pn) unset($this->chests[$cid]); |
| 145 | + } |
| 146 | + } |
| 147 | + public function onInventoryCloseEvent(InventoryCloseEvent $ev) { |
| 148 | + $player = $ev->getPlayer(); |
| 149 | + $inv = $ev->getInventory(); |
| 150 | + if (!$this->isNeChest($inv)) return; |
| 151 | + if ($this->unlockChest($player,$inv)) { |
| 152 | + $player->sendMessage("Closing NetherChest!"); |
| 153 | + $this->saveInventory($player,$inv); |
| 154 | + } |
| 155 | + } |
| 156 | + public function onInventoryOpenEvent(InventoryOpenEvent $ev) { |
| 157 | + if ($ev->isCancelled()) return; |
| 158 | + $player = $ev->getPlayer(); |
| 159 | + $inv = $ev->getInventory(); |
| 160 | + if (!$this->isNeChest($inv)) return; |
| 161 | + if (!$this->lockChest($player,$inv)) { |
| 162 | + $player->sendTip("That Nether Chest is in use!"); |
| 163 | + $ev->setCancelled(); |
| 164 | + return; |
| 165 | + } |
| 166 | + $player->sendMessage("Opening NetherChest!"); |
| 167 | + $this->loadInventory($player,$inv); |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments