From 5fd04a331dfa89fb3432097708123a20957934f0 Mon Sep 17 00:00:00 2001 From: Falkirks Date: Tue, 25 Apr 2017 08:26:21 -0700 Subject: [PATCH] Added auto-resetting and disabled storage-mode=2 --- src/falkirks/minereset/Mine.php | 65 ++++++++++++++++++++-- src/falkirks/minereset/MineManager.php | 74 ++++++++++++-------------- src/falkirks/minereset/MineReset.php | 2 + 3 files changed, 97 insertions(+), 44 deletions(-) diff --git a/src/falkirks/minereset/Mine.php b/src/falkirks/minereset/Mine.php index 1631be8..571426c 100644 --- a/src/falkirks/minereset/Mine.php +++ b/src/falkirks/minereset/Mine.php @@ -2,16 +2,21 @@ namespace falkirks\minereset; use falkirks\minereset\task\ResetTask; +use pocketmine\command\ConsoleCommandSender; use pocketmine\level\format\Chunk; use pocketmine\level\Level; use pocketmine\level\Position; use pocketmine\math\Vector3; +use pocketmine\scheduler\PluginTask; /** * Class Mine + * + * Programmer note: Mine objects have no state. They can be generated arbitrarily from serialized data. + * * @package falkirks\minereset\mine */ -class Mine{ +class Mine extends PluginTask { private $pointA; private $pointB; private $level; @@ -19,8 +24,11 @@ class Mine{ private $name; private $isResetting; + private $resetInterval; + private $api; + /** * Mine constructor. * @param MineManager $api @@ -29,16 +37,45 @@ class Mine{ * @param string $level * @param string $name * @param array $data + * @param int $resetInterval */ - public function __construct(MineManager $api, Vector3 $pointA, Vector3 $pointB, $level, string $name, array $data = []){ + public function __construct(MineManager $api, Vector3 $pointA, Vector3 $pointB, $level, string $name, array $data = [], int $resetInterval = -1){ + parent::__construct($api->getApi()); + $this->pointA = $pointA; $this->pointB = $pointB; $this->level = $level; $this->data = $data; $this->name = $name; + $this->resetInterval = $resetInterval; $this->api = $api; $this->isResetting = false; + $this->register(); + + } + + + /** + * INTERNAL USE ONLY + */ + public function register(){ + if($this->getHandler() === null && $this->resetInterval > 0){ + $this->getApi()->getApi()->getServer()->getScheduler()->scheduleRepeatingTask($this, 20 * $this->resetInterval); + } + } + + /** + * INTERNAL USE ONLY + */ + public function destroy(){ + if($this->getHandler() !== null) { + $this->getApi()->getApi()->getServer()->getScheduler()->cancelTask($this->getTaskId()); + } + } + + public function onRun($currentTick){ + $this->reset(); } /** @@ -118,8 +155,12 @@ public function isResetting(){ return $this->isResetting; } - public function reset(){ - if(!$this->isResetting() && $this->getLevel() !== null){ + /** + * @param bool $force NOT TESTED + * @return bool + */ + public function reset($force = false){ + if((!$this->isResetting() || $force) && $this->getLevel() !== null){ $this->isResetting = true; $chunks = []; @@ -140,6 +181,22 @@ public function reset(){ return false; } + /** + * @return int + */ + public function getResetInterval(): int{ + return $this->resetInterval; + } + + /** + * @param int $resetInterval + */ + public function setResetInterval(int $resetInterval){ + $this->resetInterval = $resetInterval; + $this->destroy(); + $this->register(); + } + public function doneReset(){ $this->isResetting = false; } diff --git a/src/falkirks/minereset/MineManager.php b/src/falkirks/minereset/MineManager.php index 45e7b05..eafb49b 100644 --- a/src/falkirks/minereset/MineManager.php +++ b/src/falkirks/minereset/MineManager.php @@ -11,10 +11,7 @@ class MineManager implements \ArrayAccess, \IteratorAggregate, \Countable { const MEMORY_TILL_CLOSE = 0; const FLUSH_ON_CHANGE = 1; - /** - * This option is pretty scary :( - */ - const NO_MEMORY_STORE = 2; + /** @var MineReset */ private $api; /** @var DataStore */ @@ -32,6 +29,9 @@ public function __construct(MineReset $api, DataStore $store, $flag = MineManage $this->mines = $this->loadMines(); } } + /** + * @deprecated + */ protected function reloadStore(){ if($this->flag >= 2 && $this->store instanceof Reloadable){ $this->store->reload(); @@ -49,6 +49,7 @@ protected function loadMines(): array{ } return $out; } + /** * WARNING * This function is for internal use only. @@ -75,11 +76,7 @@ public function saveAll(){ * The return value will be casted to boolean if non-boolean was returned. */ public function offsetExists($offset){ - $this->reloadStore(); - if(isset($this->mines[$offset]) || ($this->flag >= 2 && $this->store->exists($offset))){ - return true; - } - return false; + return isset($this->mines[$offset]); } /** * (PHP 5 >= 5.0.0)
@@ -91,11 +88,7 @@ public function offsetExists($offset){ * @return mixed Can return all value types. */ public function offsetGet($offset){ - if($this->flag >= 2){ - $this->reloadStore(); - return $this->mineFromData($offset, $this->store->get($offset)); - } - return isset($this->mines[$offset]) ? $this->mines[$offset] : null; + return $this->mines[$offset] ?? null; } /** * (PHP 5 >= 5.0.0)
@@ -111,10 +104,13 @@ public function offsetGet($offset){ */ public function offsetSet($offset, $value){ if($value instanceof Mine && $value->getName() === $offset) { - if($this->flag < 2) { - $this->mines[$offset] = $value; + + if(isset($this->mines[$offset]) && $value !== $this->mines[$offset] && $this->mines[$offset] instanceof Mine){ + $this->mines[$offset]->destroy(); } - if ($this->flag >= 1) { + + $this->mines[$offset] = $value; + if ($this->flag === 1) { $this->store->add($offset, $this->mineToData($value)); $this->saveStore(); } @@ -133,24 +129,27 @@ public function offsetSet($offset, $value){ * @return void */ public function offsetUnset($offset){ - if($this->flag < 2){ + if(isset($this->mines[$offset])) { + if ($this->mines[$offset] instanceof Mine) { + $this->mines[$offset]->destroy(); + } unset($this->mines[$offset]); - } - if($this->flag >= 1){ - $this->store->remove($offset); - $this->saveStore(); + if ($this->flag === 1) { + $this->store->remove($offset); + $this->saveStore(); + } } } /** * This method requires the key of the warp in order - * to construct a warp object + * to construct a mine object * @param $name * @param array $array * @return Mine * @throws \Exception */ protected function mineFromData($name, array $array){ - if(count($array) === 8) { + if(count($array) === 9 || count($array) === 8) { if(!$this->getApi()->getServer()->isLevelLoaded($array[7])){ $this->api->getLogger()->warning("A mine with the name " . TextFormat::AQUA . $name . TextFormat::RESET . " is connected to a level which is not loaded. You won't be able to use it until you load the level correctly."); } @@ -159,7 +158,8 @@ protected function mineFromData($name, array $array){ new Vector3(max($array[0], $array[1]), max($array[2], $array[3]), max($array[4], $array[5])), $array[7], $name, - $array[6] ?? []); + (is_array($array[6]) ? $array[6] : []), + $array[8] ?? -1); } $this->api->getLogger()->critical("A mine with the name " . TextFormat::AQUA . $name . TextFormat::RESET . " is incomplete. It will be removed automatically when your server stops."); return null; @@ -180,7 +180,8 @@ protected function mineToData(Mine $mine){ $mine->getPointA()->getZ(), $mine->getPointB()->getZ(), (count($mine->getData()) > 0 ? $mine->getData() : false), - $mine->getLevelName() + $mine->getLevelName(), + $mine->getResetInterval() ]; } /** @@ -191,9 +192,6 @@ protected function mineToData(Mine $mine){ * Traversable */ public function getIterator(){ - if($this->flag >= 2){ - return new \ArrayIterator($this->loadMines()); - } return new \ArrayIterator($this->mines); } @@ -206,22 +204,20 @@ public function count(){ * Returns the current storage-mode * ##### * MEMORY_TILL_CLOSE = 0 - * Warps are loaded into memory when the server starts and are + * Mines are loaded into memory when the server starts and are * held there until the server closes. When the server closes * they are converted back into YAML. This new YAML will - * replace warps.yml, this means that changes are lost and + * replace mines.yml, this means that changes are lost and * warps which fail to load are discarded. * * * FLUSH_ON_CHANGE = 1 - * Warps are loaded into memory when the server starts. Whenever a - * warp is updated, it will be updated in the warps.yml. When the - * server closes, the warps file is NOT overwritten. + * Mines are loaded into memory when the server starts. Whenever a + * mine is updated, it will be updated in the mines.yml. When the + * server closes, the mines file is NOT overwritten. * * NO_MEMORY_STORE = 2 - * Warps are never "stored" in memory. They are converted on demand - * between YAML and object format. Any changes made to the config - * will be available right away in the server and vice versa. + * THIS IS NOT SUPPORTED * #### * @return int */ @@ -243,9 +239,7 @@ public function getStore(): DataStore{ public function setStore(DataStore $store){ $this->saveAll(); $this->store = $store; - if($this->flag < 2){ - $this->mines = $this->loadMines(); - } + $this->mines = $this->loadMines(); } /** diff --git a/src/falkirks/minereset/MineReset.php b/src/falkirks/minereset/MineReset.php index a17f3d7..7348543 100644 --- a/src/falkirks/minereset/MineReset.php +++ b/src/falkirks/minereset/MineReset.php @@ -14,6 +14,7 @@ use falkirks\minereset\listener\RegionBlockerListener; use falkirks\minereset\store\EntityStore; use falkirks\minereset\store\YAMLStore; +use falkirks\minereset\task\ScheduledResetTaskPool; use pocketmine\level\Level; use pocketmine\plugin\PluginBase; use pocketmine\utils\Config; @@ -113,6 +114,7 @@ public function getRegionBlockerListener(): RegionBlockerListener{ return $this->regionBlockerListener; } + public static function supportsChunkSetting(): bool { return static::$supportsChunkSetting; }