mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 16:29:40 +00:00
Split up ChunkLoader and ChunkListener
This commit is contained in:
parent
2c0f91ce50
commit
923b1ad9a6
@ -84,6 +84,7 @@ use pocketmine\item\WritableBook;
|
|||||||
use pocketmine\item\WrittenBook;
|
use pocketmine\item\WrittenBook;
|
||||||
use pocketmine\lang\TextContainer;
|
use pocketmine\lang\TextContainer;
|
||||||
use pocketmine\lang\TranslationContainer;
|
use pocketmine\lang\TranslationContainer;
|
||||||
|
use pocketmine\level\ChunkListener;
|
||||||
use pocketmine\level\ChunkLoader;
|
use pocketmine\level\ChunkLoader;
|
||||||
use pocketmine\level\format\Chunk;
|
use pocketmine\level\format\Chunk;
|
||||||
use pocketmine\level\Level;
|
use pocketmine\level\Level;
|
||||||
@ -174,7 +175,7 @@ use const PHP_INT_MAX;
|
|||||||
/**
|
/**
|
||||||
* Main class that handles networking, recovery, and packet sending to the server part
|
* Main class that handles networking, recovery, and packet sending to the server part
|
||||||
*/
|
*/
|
||||||
class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, IPlayer{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks a supplied username and checks it is valid.
|
* Checks a supplied username and checks it is valid.
|
||||||
@ -920,6 +921,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
|||||||
unset($this->usedChunks[$index]);
|
unset($this->usedChunks[$index]);
|
||||||
}
|
}
|
||||||
$level->unregisterChunkLoader($this, $x, $z);
|
$level->unregisterChunkLoader($this, $x, $z);
|
||||||
|
$level->unregisterChunkListener($this, $x, $z);
|
||||||
unset($this->loadQueue[$index]);
|
unset($this->loadQueue[$index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -977,6 +979,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
|||||||
|
|
||||||
$this->usedChunks[$index] = false;
|
$this->usedChunks[$index] = false;
|
||||||
$this->level->registerChunkLoader($this, $X, $Z, true);
|
$this->level->registerChunkLoader($this, $X, $Z, true);
|
||||||
|
$this->level->registerChunkListener($this, $X, $Z);
|
||||||
|
|
||||||
if(!$this->level->populateChunk($X, $Z)){
|
if(!$this->level->populateChunk($X, $Z)){
|
||||||
continue;
|
continue;
|
||||||
@ -1840,6 +1843,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
|||||||
|
|
||||||
//load the spawn chunk so we can see the terrain
|
//load the spawn chunk so we can see the terrain
|
||||||
$level->registerChunkLoader($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, true);
|
$level->registerChunkLoader($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4, true);
|
||||||
|
$level->registerChunkListener($this, $spawn->getFloorX() >> 4, $spawn->getFloorZ() >> 4);
|
||||||
if($spawnReset){
|
if($spawnReset){
|
||||||
$spawn = $level->getSafeSpawn($spawn);
|
$spawn = $level->getSafeSpawn($spawn);
|
||||||
}
|
}
|
||||||
@ -2880,6 +2884,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
|||||||
foreach($this->usedChunks as $index => $d){
|
foreach($this->usedChunks as $index => $d){
|
||||||
Level::getXZ($index, $chunkX, $chunkZ);
|
Level::getXZ($index, $chunkX, $chunkZ);
|
||||||
$this->level->unregisterChunkLoader($this, $chunkX, $chunkZ);
|
$this->level->unregisterChunkLoader($this, $chunkX, $chunkZ);
|
||||||
|
$this->level->unregisterChunkListener($this, $chunkX, $chunkZ);
|
||||||
foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){
|
foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){
|
||||||
$entity->despawnFrom($this);
|
$entity->despawnFrom($this);
|
||||||
}
|
}
|
||||||
|
80
src/pocketmine/level/ChunkListener.php
Normal file
80
src/pocketmine/level/ChunkListener.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?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/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\level;
|
||||||
|
|
||||||
|
use pocketmine\block\Block;
|
||||||
|
use pocketmine\level\format\Chunk;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface allows you to listen for events occurring on or in specific chunks. This will receive events for any
|
||||||
|
* chunks which it is registered to listen to.
|
||||||
|
*
|
||||||
|
* @see Level::registerChunkListener()
|
||||||
|
* @see Level::unregisterChunkListener()
|
||||||
|
*
|
||||||
|
* WARNING: When you're done with the listener, make sure you unregister it from all chunks it's listening to, otherwise
|
||||||
|
* the object will not be destroyed.
|
||||||
|
* The listener WILL NOT be unregistered when chunks are unloaded. You need to do this yourself when you're done with
|
||||||
|
* a chunk.
|
||||||
|
*/
|
||||||
|
interface ChunkListener{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when a Chunk is replaced by a new one
|
||||||
|
*
|
||||||
|
* @param Chunk $chunk
|
||||||
|
*/
|
||||||
|
public function onChunkChanged(Chunk $chunk);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when a registered chunk is loaded
|
||||||
|
*
|
||||||
|
* @param Chunk $chunk
|
||||||
|
*/
|
||||||
|
public function onChunkLoaded(Chunk $chunk);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when a registered chunk is unloaded
|
||||||
|
*
|
||||||
|
* @param Chunk $chunk
|
||||||
|
*/
|
||||||
|
public function onChunkUnloaded(Chunk $chunk);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when a registered chunk is populated
|
||||||
|
* Usually it'll be sent with another call to onChunkChanged()
|
||||||
|
*
|
||||||
|
* @param Chunk $chunk
|
||||||
|
*/
|
||||||
|
public function onChunkPopulated(Chunk $chunk);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called when a block changes in a registered chunk
|
||||||
|
*
|
||||||
|
* @param Block|Vector3 $block
|
||||||
|
*/
|
||||||
|
public function onBlockChanged(Vector3 $block);
|
||||||
|
}
|
@ -23,19 +23,14 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\level;
|
namespace pocketmine\level;
|
||||||
|
|
||||||
use pocketmine\block\Block;
|
|
||||||
use pocketmine\level\format\Chunk;
|
|
||||||
use pocketmine\math\Vector3;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If you want to keep chunks loaded and receive notifications on a specific area,
|
* If you want to keep chunks loaded, implement this interface and register it into Level. This will also tick chunks.
|
||||||
* extend this class and register it into Level. This will also tick chunks.
|
|
||||||
*
|
*
|
||||||
* Register Level->registerChunkLoader($this, $chunkX, $chunkZ)
|
* @see Level::registerChunkLoader()
|
||||||
* Unregister Level->unregisterChunkLoader($this, $chunkX, $chunkZ)
|
* @see Level::unregisterChunkLoader()
|
||||||
*
|
*
|
||||||
* WARNING: When moving this object around in the world or destroying it,
|
* WARNING: When moving this object around in the world or destroying it,
|
||||||
* be sure to free the existing references from Level, otherwise you'll leak memory.
|
* be sure to unregister the loader from chunks you're not using, otherwise you'll leak memory.
|
||||||
*/
|
*/
|
||||||
interface ChunkLoader{
|
interface ChunkLoader{
|
||||||
|
|
||||||
@ -48,42 +43,4 @@ interface ChunkLoader{
|
|||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
public function getZ();
|
public function getZ();
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will be called when a Chunk is replaced by a new one
|
|
||||||
*
|
|
||||||
* @param Chunk $chunk
|
|
||||||
*/
|
|
||||||
public function onChunkChanged(Chunk $chunk);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will be called when a registered chunk is loaded
|
|
||||||
*
|
|
||||||
* @param Chunk $chunk
|
|
||||||
*/
|
|
||||||
public function onChunkLoaded(Chunk $chunk);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will be called when a registered chunk is unloaded
|
|
||||||
*
|
|
||||||
* @param Chunk $chunk
|
|
||||||
*/
|
|
||||||
public function onChunkUnloaded(Chunk $chunk);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will be called when a registered chunk is populated
|
|
||||||
* Usually it'll be sent with another call to onChunkChanged()
|
|
||||||
*
|
|
||||||
* @param Chunk $chunk
|
|
||||||
*/
|
|
||||||
public function onChunkPopulated(Chunk $chunk);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will be called when a block changes in a registered chunk
|
|
||||||
*
|
|
||||||
* @param Block|Vector3 $block
|
|
||||||
*/
|
|
||||||
public function onBlockChanged(Vector3 $block);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -179,6 +179,9 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
/** @var Player[][] */
|
/** @var Player[][] */
|
||||||
private $playerLoaders = [];
|
private $playerLoaders = [];
|
||||||
|
|
||||||
|
/** @var ChunkListener[][] */
|
||||||
|
private $chunkListeners = [];
|
||||||
|
|
||||||
/** @var ClientboundPacket[][] */
|
/** @var ClientboundPacket[][] */
|
||||||
private $chunkPackets = [];
|
private $chunkPackets = [];
|
||||||
/** @var ClientboundPacket[] */
|
/** @var ClientboundPacket[] */
|
||||||
@ -680,6 +683,52 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a listener to receive events on a chunk.
|
||||||
|
*
|
||||||
|
* @param ChunkListener $listener
|
||||||
|
* @param int $chunkX
|
||||||
|
* @param int $chunkZ
|
||||||
|
*/
|
||||||
|
public function registerChunkListener(ChunkListener $listener, int $chunkX, int $chunkZ) : void{
|
||||||
|
$hash = Level::chunkHash($chunkX, $chunkZ);
|
||||||
|
if(isset($this->chunkListeners[$hash])){
|
||||||
|
$this->chunkListeners[$hash][spl_object_id($listener)] = $listener;
|
||||||
|
}else{
|
||||||
|
$this->chunkListeners[$hash] = [spl_object_id($listener) => $listener];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a chunk listener previously registered.
|
||||||
|
* @see Level::registerChunkListener()
|
||||||
|
*
|
||||||
|
* @param ChunkListener $listener
|
||||||
|
* @param int $chunkX
|
||||||
|
* @param int $chunkZ
|
||||||
|
*/
|
||||||
|
public function unregisterChunkListener(ChunkListener $listener, int $chunkX, int $chunkZ) : void{
|
||||||
|
$hash = Level::chunkHash($chunkX, $chunkZ);
|
||||||
|
if(isset($this->chunkListeners[$hash])){
|
||||||
|
unset($this->chunkListeners[$hash][spl_object_id($listener)]);
|
||||||
|
if(empty($this->chunkListeners[$hash])){
|
||||||
|
unset($this->chunkListeners[$hash]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all the listeners attached to this chunk.
|
||||||
|
*
|
||||||
|
* @param int $chunkX
|
||||||
|
* @param int $chunkZ
|
||||||
|
*
|
||||||
|
* @return ChunkListener[]
|
||||||
|
*/
|
||||||
|
public function getChunkListeners(int $chunkX, int $chunkZ) : array{
|
||||||
|
return $this->chunkListeners[Level::chunkHash($chunkX, $chunkZ)] ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*
|
*
|
||||||
@ -1539,8 +1588,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
}
|
}
|
||||||
$this->changedBlocks[$chunkHash][$relativeBlockHash] = $block;
|
$this->changedBlocks[$chunkHash][$relativeBlockHash] = $block;
|
||||||
|
|
||||||
foreach($this->getChunkLoaders($x >> 4, $z >> 4) as $loader){
|
foreach($this->getChunkListeners($x >> 4, $z >> 4) as $listener){
|
||||||
$loader->onBlockChanged($block);
|
$listener->onBlockChanged($block);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($update){
|
if($update){
|
||||||
@ -2229,8 +2278,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
if(($oldChunk === null or !$oldChunk->isPopulated()) and $chunk->isPopulated()){
|
if(($oldChunk === null or !$oldChunk->isPopulated()) and $chunk->isPopulated()){
|
||||||
(new ChunkPopulateEvent($this, $chunk))->call();
|
(new ChunkPopulateEvent($this, $chunk))->call();
|
||||||
|
|
||||||
foreach($this->getChunkLoaders($x, $z) as $loader){
|
foreach($this->getChunkListeners($x, $z) as $listener){
|
||||||
$loader->onChunkPopulated($chunk);
|
$listener->onChunkPopulated($chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2301,8 +2350,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
if(!$this->isChunkInUse($chunkX, $chunkZ)){
|
if(!$this->isChunkInUse($chunkX, $chunkZ)){
|
||||||
$this->unloadChunkRequest($chunkX, $chunkZ);
|
$this->unloadChunkRequest($chunkX, $chunkZ);
|
||||||
}else{
|
}else{
|
||||||
foreach($this->getChunkLoaders($chunkX, $chunkZ) as $loader){
|
foreach($this->getChunkListeners($chunkX, $chunkZ) as $listener){
|
||||||
$loader->onChunkChanged($chunk);
|
$listener->onChunkChanged($chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2617,8 +2666,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($this->isChunkInUse($x, $z)){
|
if($this->isChunkInUse($x, $z)){
|
||||||
foreach($this->getChunkLoaders($x, $z) as $loader){
|
foreach($this->getChunkListeners($x, $z) as $listener){
|
||||||
$loader->onChunkLoaded($chunk);
|
$listener->onChunkLoaded($chunk);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
$this->server->getLogger()->debug("Newly loaded chunk $x $z has no loaders registered, will be unloaded at next available opportunity");
|
$this->server->getLogger()->debug("Newly loaded chunk $x $z has no loaders registered, will be unloaded at next available opportunity");
|
||||||
@ -2678,8 +2727,8 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->getChunkLoaders($x, $z) as $loader){
|
foreach($this->getChunkListeners($x, $z) as $listener){
|
||||||
$loader->onChunkUnloaded($chunk);
|
$listener->onChunkUnloaded($chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
$chunk->onUnload();
|
$chunk->onUnload();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user