Added ChunkLoadEvent, ChunkUnloadEvent, ChunkPopulateEvent

This commit is contained in:
Shoghi Cervantes 2014-08-02 17:19:33 +02:00
parent 7ddfd4394d
commit eee7e659e9
6 changed files with 176 additions and 10 deletions

View File

@ -0,0 +1,47 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/**
* Level related events
*/
namespace pocketmine\event\level;
use pocketmine\level\format\FullChunk;
abstract class ChunkEvent extends LevelEvent{
/** @var FullChunk */
private $chunk;
/**
* @param FullChunk $chunk
*/
public function __construct(FullChunk $chunk){
parent::__construct($chunk->getLevel()->getLevel());
$this->chunk = $chunk;
}
/**
* @return FullChunk
*/
public function getChunk(){
return $this->chunk;
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\event\level;
use pocketmine\level\format\FullChunk;
/**
* Called when a Chunk is loaded
*/
class ChunkLoadEvent extends ChunkEvent{
public static $handlerList = null;
private $newChunk;
public function __construct(FullChunk $chunk, $newChunk){
parent::__construct($chunk);
$this->newChunk = (bool) $newChunk;
}
/**
* @return bool
*/
public function isNewChunk(){
return $this->newChunk;
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\event\level;
/**
* Called when a Chunk is populated (after receiving it on the main thread)
*/
class ChunkPopulateEvent extends ChunkEvent{
public static $handlerList = null;
}

View File

@ -0,0 +1,30 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\event\level;
use pocketmine\event\Cancellable;
/**
* Called when a Chunk is unloaded
*/
class ChunkUnloadEvent extends ChunkEvent implements Cancellable{
public static $handlerList = null;
}

View File

@ -23,7 +23,7 @@ namespace pocketmine\event\level;
use pocketmine\event\Cancellable;
/**
* Called when a Level is saved
* Called when a Level is unloaded
*/
class LevelUnloadEvent extends LevelEvent implements Cancellable{
public static $handlerList = null;

View File

@ -30,6 +30,9 @@ use pocketmine\entity\DroppedItem;
use pocketmine\entity\Entity;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\level\ChunkLoadEvent;
use pocketmine\event\level\ChunkPopulateEvent;
use pocketmine\event\level\ChunkUnloadEvent;
use pocketmine\event\level\LevelSaveEvent;
use pocketmine\event\level\LevelUnloadEvent;
use pocketmine\event\level\SpawnChangeEvent;
@ -1294,8 +1297,13 @@ class Level implements ChunkManager, Metadatable{
}
public function generateChunkCallback($x, $z, FullChunk $chunk){
$oldChunk = $this->getChunkAt($x, $z);
unset($this->chunkGenerationQueue["$x:$z"]);
$this->setChunk($x, $z, $chunk);
if(!($oldChunk instanceof FullChunk) or ($oldChunk->isPopulated() === false and $chunk->isPopulated())){
$this->server->getPluginManager()->callEvent(new ChunkPopulateEvent($this->getChunkAt($x, $z)));
}
}
public function setChunk($x, $z, FullChunk $chunk){
@ -1513,27 +1521,29 @@ class Level implements ChunkManager, Metadatable{
* @return bool
*/
public function loadChunk($x, $z, $generate = true){
if($generate === true){
return $this->getChunkAt($x, $z, true) instanceof FullChunk;
if(isset($this->chunks[$index = Level::chunkHash($x, $z)])){
return true;
}
$this->cancelUnloadChunkRequest($x, $z);
$chunk = $this->provider->getChunk($x, $z, false);
$chunk = $this->provider->getChunk($x, $z, $generate);
if($chunk instanceof FullChunk){
$this->chunks[Level::chunkHash($x, $z)] = $chunk;
return true;
$this->chunks[$index] = $chunk;
}else{
$this->timings->syncChunkLoadTimer->startTiming();
$this->provider->loadChunk($x, $z);
$this->provider->loadChunk($x, $z, $generate);
$this->timings->syncChunkLoadTimer->stopTiming();
if(($chunk = $this->provider->getChunk($x, $z)) instanceof FullChunk){
$this->chunks[Level::chunkHash($x, $z)] = $chunk;
return true;
$this->chunks[$index] = $chunk;
}else{
return false;
}
return false;
}
$this->server->getPluginManager()->callEvent(new ChunkLoadEvent($chunk, !$chunk->isGenerated()));
return true;
}
protected function queueUnloadChunk($x, $z){
@ -1558,6 +1568,12 @@ class Level implements ChunkManager, Metadatable{
if($safe === true and $this->isChunkInUse($x, $z)){
return false;
}
$this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($this->getChunkAt($x, $z)));
if($ev->isCancelled()){
return false;
}
$this->timings->doChunkUnload->startTiming();
unset($this->chunks[$index = Level::chunkHash($x, $z)]);