-
Notifications
You must be signed in to change notification settings - Fork 42
/
CrateTile.php
197 lines (173 loc) · 6.34 KB
/
CrateTile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
declare(strict_types=1);
namespace DaPigGuy\PiggyCrates\tiles;
use DaPigGuy\PiggyCrates\crates\Crate;
use DaPigGuy\PiggyCrates\PiggyCrates;
use DaPigGuy\PiggyCrates\tasks\RouletteTask;
use pocketmine\command\ConsoleCommandSender;
use pocketmine\item\Item;
use pocketmine\level\particle\FloatingTextParticle;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\Player;
use pocketmine\tile\Chest;
use pocketmine\utils\TextFormat;
/**
* Class CrateTile
* @package DaPigGuy\PiggyCrates\tiles
*/
class CrateTile extends Chest
{
/** @var string */
public $crateName;
/** @var Crate|null */
public $crateType;
/** @var bool */
public $isOpen = false;
/** @var Player|null */
public $currentPlayer;
/** @var array[] */
public $floatingTextParticles = [];
/**
* @return Crate|null
*/
public function getCrateType(): ?Crate
{
return $this->crateType;
}
/**
* @param Player $player
* @param Item $key
*/
public function openCrate(Player $player, Item $key): void
{
if (($crateType = $this->crateType) === null || ($level = $this->getLevel()) === null) return;
if ($this->isOpen) {
$player->sendTip(TextFormat::RED . "Crate is currently being opened.");
return;
}
if (count($player->getInventory()->getContents()) > $player->getInventory()->getSize() - $crateType->getDropCount()) {
$player->sendTip(TextFormat::RED . "You must have " . $crateType->getDropCount() . " empty slots.");
return;
}
$player->getInventory()->removeItem($key->setCount(1));
$pk = new BlockEventPacket();
$pk->x = $this->getFloorX();
$pk->y = $this->getFloorY();
$pk->z = $this->getFloorZ();
$pk->eventType = 1;
$pk->eventData = 1;
$level->broadcastPacketToViewers($this, $pk);
$this->getInventory()->clearAll();
$this->getInventory()->setItem(4, Item::get(Item::END_ROD, 0, 1));
$this->getInventory()->setItem(22, Item::get(Item::END_ROD, 0, 1));
$this->isOpen = true;
$this->currentPlayer = $player;
switch (PiggyCrates::$instance->getConfig()->getNested("crates.mode")) {
case "instant":
$this->closeCrate();
foreach ($crateType->getDrop($crateType->getDropCount()) as $drop) {
$player->getInventory()->addItem($drop->getItem());
foreach ($drop->getCommands() as $command) {
$player->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $player->getName(), $command));
}
}
foreach ($crateType->getCommands() as $command) {
$player->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{PLAYER}", $player->getName(), $command));
}
break;
case "roulette":
default:
PiggyCrates::$instance->getScheduler()->scheduleRepeatingTask(new RouletteTask($this), 1);
break;
}
}
public function closeCrate(): void
{
if (!$this->isOpen || ($level = $this->getLevel()) === null) return;
$pk = new BlockEventPacket();
$pk->x = $this->getFloorX();
$pk->y = $this->getFloorY();
$pk->z = $this->getFloorZ();
$pk->eventType = 1;
$pk->eventData = 0;
$level->broadcastPacketToViewers($this, $pk);
$this->isOpen = false;
$this->currentPlayer = null;
}
public function close(): void
{
foreach ($this->floatingTextParticles as $floatingTextParticle) {
$floatingTextParticle[1]->setInvisible();
if ($floatingTextParticle[0]->getLevel()) $floatingTextParticle[0]->getLevel()->addParticle($floatingTextParticle[1], [$floatingTextParticle[0]]);
}
parent::close();
}
/**
* @return bool
*/
public function isOpen(): bool
{
return $this->isOpen;
}
/**
* @return Player|null
*/
public function getCurrentPlayer(): ?Player
{
return $this->currentPlayer;
}
/**
* @param CompoundTag $nbt
*/
protected function readSaveData(CompoundTag $nbt): void
{
parent::readSaveData($nbt);
$this->crateName = $nbt->getString("CrateType");
$this->crateType = PiggyCrates::getCrate($this->crateName);
$this->scheduleUpdate();
}
/**
* @param CompoundTag $nbt
*/
protected function writeSaveData(CompoundTag $nbt): void
{
parent::writeSaveData($nbt);
$nbt->setString("CrateType", $this->crateName);
}
/**
* @param CompoundTag $nbt
*/
public function addAdditionalSpawnData(CompoundTag $nbt): void
{
parent::addAdditionalSpawnData($nbt);
$nbt->setString(self::TAG_ID, "Chest");
$nbt->setString(self::TAG_CUSTOM_NAME, ($this->crateType === null ? "Unknown" : $this->crateType->getName()) . " Crate");
}
/**
* @return bool
*/
public function onUpdate(): bool
{
if (!$this->closed && ($level = $this->getLevel()) !== null && $this->crateType !== null && $this->crateType->getFloatingText() !== "") {
foreach ($this->floatingTextParticles as $key => $floatingTextParticle) {
/** @var Player $player */
$player = $floatingTextParticle[0];
/** @var FloatingTextParticle $particle */
$particle = $floatingTextParticle[1];
if (!$player->isOnline() || $player->getLevel() !== $level) {
$particle->setInvisible();
$level->addParticle($particle, [$player]);
unset($this->floatingTextParticles[$key]);
}
}
foreach ($level->getPlayers() as $player) {
if (!isset($this->floatingTextParticles[$player->getName()])) {
$this->floatingTextParticles[$player->getName()] = [$player, new FloatingTextParticle($this->add(0.5, 1, 0.5), $this->crateType->getFloatingText())];
$level->addParticle($this->floatingTextParticles[$player->getName()][1], [$player]);
}
}
}
return !$this->closed;
}
}