|
| 1 | +<?php |
| 2 | +/** |
| 3 | + ** CONFIG:config.yml |
| 4 | + **/ |
| 5 | +namespace aliuly\nechest; |
| 6 | + |
| 7 | +use pocketmine\plugin\PluginBase; |
| 8 | +use pocketmine\event\Listener; |
| 9 | + |
| 10 | +use pocketmine\utils\Config; |
| 11 | +use pocketmine\item\Item; |
| 12 | +use pocketmine\block\Block; |
| 13 | +use pocketmine\Player; |
| 14 | +use pocketmine\inventory\Inventory; |
| 15 | +use pocketmine\inventory\DoubleChestInventory; |
| 16 | +use pocketmine\inventory\ChestInventory; |
| 17 | +use pocketmine\tile\Tile; |
| 18 | +use pocketmine\tile\Chest; |
| 19 | +use pocketmine\math\Vector3; |
| 20 | + |
| 21 | + |
| 22 | +use pocketmine\event\player\PlayerQuitEvent; |
| 23 | +use pocketmine\event\inventory\InventoryCloseEvent; |
| 24 | +use pocketmine\event\inventory\InventoryOpenEvent; |
| 25 | +use pocketmine\event\block\BlockBreakEvent; |
| 26 | +use pocketmine\event\block\BlockPlaceEvent; |
| 27 | + |
| 28 | +use aliuly\nechest\common\mc; |
| 29 | + |
| 30 | +// OPEN |
| 31 | +//- PlayerInteractEvent; |
| 32 | +//- InventoryOpenEvent; |
| 33 | + |
| 34 | +//PUT IN CHEST|GET FROM CHEST |
| 35 | +//- InventoryTransactionEvent; |
| 36 | +//- EntityInventoryChangeEvent; |
| 37 | + |
| 38 | +// CLOSE |
| 39 | +//- InventoryCloseEvent; |
| 40 | + |
| 41 | + |
| 42 | +// |
| 43 | +//use pocketmine\level\Level; |
| 44 | +//use pocketmine\event\entity\EntityLevelChangeEvent; |
| 45 | +//use pocketmine\block\Block; |
| 46 | +//use pocketmine\Server; |
| 47 | +//use pocketmine\utils\TextFormat; |
| 48 | +//use pocketmine\scheduler\CallbackTask; |
| 49 | + |
| 50 | + |
| 51 | +class Main extends PluginBase implements Listener { |
| 52 | + protected $chests; // Array with active chests |
| 53 | + protected $base_block; |
| 54 | + protected $dbm; |
| 55 | + |
| 56 | + protected static function iName($player) { |
| 57 | + return strtolower($player->getName()); |
| 58 | + } |
| 59 | + protected static function chestId($obj) { |
| 60 | + if ($obj instanceof ChestInventory) $obj = $obj->getHolder(); |
| 61 | + if ($obj instanceof Chest) $obj = $obj->getBlock(); |
| 62 | + return implode(":",[$obj->getLevel()->getName(),(int)$obj->getX(),(int)$obj->getY(),(int)$obj->getZ()]); |
| 63 | + } |
| 64 | + |
| 65 | + public function onDisable() { |
| 66 | + if ($this->dbm !== null) $this->dbm->close(); |
| 67 | + $this->dbm = null; |
| 68 | + } |
| 69 | + public function onEnable(){ |
| 70 | + if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); |
| 71 | + $this->getServer()->getPluginManager()->registerEvents($this, $this); |
| 72 | + $this->dbm = null; |
| 73 | + if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); |
| 74 | + mc::plugin_init($this,$this->getFile()); |
| 75 | + $defaults = [ |
| 76 | + "version" => $this->getDescription()->getVersion(), |
| 77 | + "# settings" => "Configuration settings", |
| 78 | + "settings" => [ |
| 79 | + "# global" => "If true all worlds share the same NetherChest", |
| 80 | + "global" => false, |
| 81 | + "# particles" => "Decorate NetherChests...", |
| 82 | + "particles" => true, |
| 83 | + "# p-ticks" => "Particle ticks", |
| 84 | + "p-ticks" => 20, |
| 85 | + "# base-block" => "Block to use for the base", |
| 86 | + "base-block" => "NETHERRACK", |
| 87 | + ], |
| 88 | + "# backend" => "Use YamlMgr or MySqlMgr", |
| 89 | + "backend" => "YamlMgr", |
| 90 | + "# MySql" => "MySQL settings.", // Only used if backend is MySqlMgr to configure MySql settings |
| 91 | + "MySql" => [ |
| 92 | + "host" => "localhost", |
| 93 | + "user" => "nobody", |
| 94 | + "password" => "secret", |
| 95 | + "database" => "netherchestdb", |
| 96 | + "port" => 3306, |
| 97 | + ], |
| 98 | + ]; |
| 99 | + $cf = (new Config($this->getDataFolder()."config.yml", |
| 100 | + Config::YAML,$defaults))->getAll(); |
| 101 | + $backend = __NAMESPACE__."\\".$cf["backend"]; |
| 102 | + $this->dbm = new $backend($this,$cf); |
| 103 | + $this->getLogger()->info(mc::_("Using %1% as backend",$cf["backend"])); |
| 104 | + |
| 105 | + $bl = Item::fromString($cf["settings"]["base-block"]); |
| 106 | + if ($bl->getBlock()->getId() == Item::AIR) { |
| 107 | + $this->getLogger()->warning(mc::_("Invalid base-block %1%",$cf["settings"]["base-block"])); |
| 108 | + $this->base_block = Block::NETHERRACK; |
| 109 | + } else { |
| 110 | + $this->base_block = $bl->getBlock()->getId(); |
| 111 | + } |
| 112 | + |
| 113 | + $this->chests = []; |
| 114 | + if ($cf["settings"]["particles"]) { |
| 115 | + $this->getServer()->getScheduler()->scheduleRepeatingTask( |
| 116 | + new ParticleTask($this), |
| 117 | + $cf["settings"]["p-ticks"]); |
| 118 | + } |
| 119 | + } |
| 120 | + private function saveInventory(Player $player,Inventory $inv) { |
| 121 | + return $this->dbm->saveInventory($player,$inv); |
| 122 | + } |
| 123 | + private function loadInventory(Player $player,Inventory $inv) { |
| 124 | + return $this->dbm->loadInventory($player,$inv); |
| 125 | + } |
| 126 | + private function lockChest(Player $player,$obj){ |
| 127 | + $cid = self::chestId($obj); |
| 128 | + if (isset($this->chests[$cid])) return false; |
| 129 | + $this->chests[$cid] = self::iName($player); |
| 130 | + return true; |
| 131 | + } |
| 132 | + private function unlockChest(Player $player,$obj){ |
| 133 | + $cid = self::chestId($obj); |
| 134 | + if (!isset($this->chests[$cid])) return false; |
| 135 | + if ($this->chests[$cid] != self::iName($player)) return false; |
| 136 | + unset($this->chests[$cid]); |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + public function isNeChest(Inventory $inv) { |
| 141 | + if ($inv instanceof DoubleChestInventory) return false; |
| 142 | + if (!($inv instanceof ChestInventory)) return false; |
| 143 | + $tile = $inv->getHolder(); |
| 144 | + if (!($tile instanceof Chest)) return false; |
| 145 | + $bl = $tile->getBlock(); |
| 146 | + if ($bl->getId() != Block::CHEST) return false; |
| 147 | + if ($bl->getSide(Vector3::SIDE_DOWN)->getId() != $this->base_block) return false; |
| 148 | + return true; |
| 149 | + } |
| 150 | + public function onBlockPlaceEvent(BlockPlaceEvent $ev) { |
| 151 | + if ($ev->isCancelled()) return; |
| 152 | + $bl = $ev->getBlock(); |
| 153 | + if ($bl->getId() != Block::CHEST || $bl->getSide(Vector3::SIDE_DOWN)->getId() != $this->base_block) return; |
| 154 | + $ev->getPlayer()->sendMessage(mc::_("Placed a NetherChest")); |
| 155 | + } |
| 156 | + public function onBlockBreakEvent(BlockBreakEvent $ev) { |
| 157 | + if ($ev->isCancelled()) return; |
| 158 | + $bl = $ev->getBlock(); |
| 159 | + if ($bl->getId() == $this->base_block) $bl = $bl->getSide(Vector3::SIDE_UP); |
| 160 | + if ($bl->getId() == Block::CHEST) { |
| 161 | + $tile = $bl->getLevel()->getTile($bl); |
| 162 | + if ($tile == null) return; |
| 163 | + if (!($tile instanceof Chest)) return; |
| 164 | + $inv = $tile->getInventory(); |
| 165 | + if (!$this->isNeChest($inv)) return; |
| 166 | + $cid = self::chestId($inv); |
| 167 | + if (!isset($this->chests[$cid])) return; |
| 168 | + $ev->getPlayer()->sendTip(mc::_("That NetherChest is in use!")); |
| 169 | + $ev->setCancelled(); |
| 170 | + return; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public function onPlayerQuitEvent(PlayerQuitEvent $ev) { |
| 175 | + $pn = self::iName($ev->getPlayer()); |
| 176 | + foreach (array_keys($this->chests) as $cid) { |
| 177 | + if ($this->chests[$cid] == $pn) { |
| 178 | + unset($this->chests[$cid]); |
| 179 | + list($level,$x,$y,$z) = explode(":",$cid); |
| 180 | + $level = $this->getServer()->getLevelByName($level); |
| 181 | + if ($level == null) continue; |
| 182 | + $tile = $level->getTile(new Vector3($x,$y,$z)); |
| 183 | + if ($tile == null) continue; |
| 184 | + if (!($tile instanceof Chest)) continue; |
| 185 | + $inv = $tile->getInventory(); |
| 186 | + if (!$this->isNeChest($inv)) continue; |
| 187 | + // QUITING WHILE NETHER CHEST IS OPEN! |
| 188 | + $this->saveInventory($ev->getPlayer(),$inv); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + public function onInventoryCloseEvent(InventoryCloseEvent $ev) { |
| 193 | + $player = $ev->getPlayer(); |
| 194 | + $inv = $ev->getInventory(); |
| 195 | + if (!$this->isNeChest($inv)) return; |
| 196 | + if ($this->unlockChest($player,$inv)) { |
| 197 | + $player->sendMessage(mc::_("Closing NetherChest!")); |
| 198 | + $this->saveInventory($player,$inv); |
| 199 | + } |
| 200 | + } |
| 201 | + public function onInventoryOpenEvent(InventoryOpenEvent $ev) { |
| 202 | + if ($ev->isCancelled()) return; |
| 203 | + $player = $ev->getPlayer(); |
| 204 | + $inv = $ev->getInventory(); |
| 205 | + if (!$this->isNeChest($inv)) return; |
| 206 | + if (!$this->lockChest($player,$inv)) { |
| 207 | + $player->sendTip(mc::_("That NetherChest is in use!")); |
| 208 | + $ev->setCancelled(); |
| 209 | + return; |
| 210 | + } |
| 211 | + $player->sendMessage(mc::_("Opening NetherChest!")); |
| 212 | + $this->loadInventory($player,$inv); |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments