Skip to content

Commit

Permalink
Added auto-resetting and disabled storage-mode=2
Browse files Browse the repository at this point in the history
  • Loading branch information
falkirks committed Apr 25, 2017
1 parent 0150038 commit 5fd04a3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 44 deletions.
65 changes: 61 additions & 4 deletions src/falkirks/minereset/Mine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@
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;
private $data;
private $name;
private $isResetting;

private $resetInterval;

private $api;


/**
* Mine constructor.
* @param MineManager $api
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
}
Expand Down
74 changes: 34 additions & 40 deletions src/falkirks/minereset/MineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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();
Expand All @@ -49,6 +49,7 @@ protected function loadMines(): array{
}
return $out;
}

/**
* WARNING
* This function is for internal use only.
Expand All @@ -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 &gt;= 5.0.0)<br/>
Expand All @@ -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 &gt;= 5.0.0)<br/>
Expand All @@ -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();
}
Expand All @@ -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.");
}
Expand All @@ -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;
Expand All @@ -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()
];
}
/**
Expand All @@ -191,9 +192,6 @@ protected function mineToData(Mine $mine){
* <b>Traversable</b>
*/
public function getIterator(){
if($this->flag >= 2){
return new \ArrayIterator($this->loadMines());
}
return new \ArrayIterator($this->mines);
}

Expand All @@ -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
*/
Expand All @@ -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();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/falkirks/minereset/MineReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,6 +114,7 @@ public function getRegionBlockerListener(): RegionBlockerListener{
return $this->regionBlockerListener;
}


public static function supportsChunkSetting(): bool {
return static::$supportsChunkSetting;
}
Expand Down

0 comments on commit 5fd04a3

Please sign in to comment.