Skip to content

Commit

Permalink
CS
Browse files Browse the repository at this point in the history
  • Loading branch information
AIPTU committed Oct 22, 2021
1 parent 4382b56 commit aaf2da3
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions src/HazardTeam/AutoClearChunk/AutoClearChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,84 +20,84 @@

class AutoClearChunk extends PluginBase implements Listener
{
private array $worlds = [];

public function onEnable(): void
{
$this->checkConfig();

$interval = $this->getConfig()->get("clear-interval", 600);

foreach (array_diff(scandir($this->getServer()->getDataPath() . "worlds"), ["..", "."]) as $levelName) {
if (!in_array($levelName, $this->getConfig()->getAll()["blacklisted-worlds"], true)) {
$this->worlds[] = $levelName;
}
}

$this->getScheduler()->scheduleDelayedRepeatingTask(new ClosureTask(
function (int $currentTick): void {
$this->clearChunk();
}
), 20 * $interval, 20 * $interval);

$this->getServer()->getPluginManager()->registerEvents($this, $this);

UpdateNotifier::checkUpdate($this->getDescription()->getName(), $this->getDescription()->getVersion());
}

private function checkConfig(): void
{
$this->saveDefaultConfig();

foreach ([
"clear-interval" => "integer",
"message" => "string",
"blacklisted-worlds" => "array"
] as $option => $expectedType) {
if (($type = gettype($this->getConfig()->getNested($option))) != $expectedType) {
throw new \TypeError("Config error: Option ($option) must be of type $expectedType, $type was given");
}
}
}

public function onLevelChange(EntityLevelChangeEvent $event): void
{
$entity = $event->getEntity();
$levelName = $event->getOrigin()->getFolderName();

if ($event->isCancelled()) {
return;
}

if (!$entity instanceof Player) {
return;
}

if (!in_array($levelName, $this->getConfig()->getAll()["blacklisted-worlds"], true)) {
$this->worlds[] = $levelName;
}
}

public function clearChunk(): void
{
$cleared = 0;

foreach ($this->worlds as $world) {
$worlds = $this->getServer()->getLevelByName($world);

if ($worlds !== null) {
foreach ($worlds->getChunks() as $chunk) {
$count = count($worlds->getChunkPlayers($chunk->getX(), $chunk->getZ())); //Check if the player is in the chunk

if ($count === 0) {
$cleared += 1;
$worlds->unloadChunk($chunk->getX(), $chunk->getZ());
}
}
}
}

$message = TextFormat::colorize($this->getConfig()->get("message", "Successfully cleared {COUNT} chunks"));
$this->getServer()->broadcastMessage(str_replace("{COUNT}", (string) $cleared, $message));
}
private array $worlds = [];

public function onEnable(): void
{
$this->checkConfig();

$interval = $this->getConfig()->get('clear-interval', 600);

foreach (array_diff(scandir($this->getServer()->getDataPath() . 'worlds'), ['..', '.']) as $levelName) {
if (!in_array($levelName, $this->getConfig()->getAll()['blacklisted-worlds'], true)) {
$this->worlds[] = $levelName;
}
}

$this->getScheduler()->scheduleDelayedRepeatingTask(new ClosureTask(
function (int $currentTick): void {
$this->clearChunk();
}
), 20 * $interval, 20 * $interval);

$this->getServer()->getPluginManager()->registerEvents($this, $this);

UpdateNotifier::checkUpdate($this->getDescription()->getName(), $this->getDescription()->getVersion());
}

public function onLevelChange(EntityLevelChangeEvent $event): void
{
$entity = $event->getEntity();
$levelName = $event->getOrigin()->getFolderName();

if ($event->isCancelled()) {
return;
}

if (!$entity instanceof Player) {
return;
}

if (!in_array($levelName, $this->getConfig()->getAll()['blacklisted-worlds'], true)) {
$this->worlds[] = $levelName;
}
}

public function clearChunk(): void
{
$cleared = 0;

foreach ($this->worlds as $world) {
$worlds = $this->getServer()->getLevelByName($world);

if ($worlds !== null) {
foreach ($worlds->getChunks() as $chunk) {
$count = count($worlds->getChunkPlayers($chunk->getX(), $chunk->getZ())); //Check if the player is in the chunk

if ($count === 0) {
++$cleared;
$worlds->unloadChunk($chunk->getX(), $chunk->getZ());
}
}
}
}

$message = TextFormat::colorize($this->getConfig()->get('message', 'Successfully cleared {COUNT} chunks'));
$this->getServer()->broadcastMessage(str_replace('{COUNT}', (string) $cleared, $message));
}

private function checkConfig(): void
{
$this->saveDefaultConfig();

foreach ([
'clear-interval' => 'integer',
'message' => 'string',
'blacklisted-worlds' => 'array',
] as $option => $expectedType) {
if (($type = gettype($this->getConfig()->getNested($option))) !== $expectedType) {
throw new \TypeError("Config error: Option ({$option}) must be of type {$expectedType}, {$type} was given");
}
}
}
}

0 comments on commit aaf2da3

Please sign in to comment.