Skip to content
This repository was archived by the owner on Jul 11, 2018. It is now read-only.

Commit 3d1ae91

Browse files
committed
NetherChests-0.1.0
1 parent cd8435f commit 3d1ae91

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

NetherChests/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# NetherChests
3+
4+
An Ender Chest implementation
5+
6+
## Permission nodes
7+
8+
?

NetherChests/plugin.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
main: aliuly\nechest\Main
2+
api: 1.12.0
3+
load: POSTWORLD
4+
5+
name: NetherChests
6+
description: An Ender Chest type plugin
7+
version: 0.1.0
8+
author: aliuly
9+
10+
permissions:
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace aliuly\nechest;
3+
use pocketmine\scheduler\PluginTask;
4+
use pocketmine\plugin\PluginBase;
5+
use pocketmine\tile\Chest;
6+
use pocketmine\level\particle\SmokeParticle;
7+
use pocketmine\math\Vector3;
8+
9+
10+
class ParticleTask extends PluginTask{
11+
protected static function randy($p,$r,$o) {
12+
return $p+(mt_rand()/mt_getrandmax())*$r+$o;
13+
}
14+
protected static function randVector(Vector3 $center) {
15+
return new Vector3(self::randy($center->getX(),0.6,-0.3),
16+
self::randy($center->getY(),0.2,1),
17+
self::randy($center->getZ(),0.6,-0.3));
18+
}
19+
public function onRun($currentTick){
20+
if ($this->owner->isDisabled()) return;
21+
foreach ($this->owner->getServer()->getLevels() as $lv) {
22+
foreach ($lv->getTiles() as $tile) {
23+
if (!($tile instanceof Chest)) continue;
24+
if (!($this->owner->isNeChest($tile->getInventory()))) continue;
25+
$lv->addParticle(new SmokeParticle(self::randVector($tile)));
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)