From 82fd3b540eb8b252a236187ab566bd091db84674 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 8 Aug 2017 22:15:44 +0100 Subject: [PATCH 1/6] Fixed a mistake in Block->isBreakable() doc --- src/pocketmine/block/Block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 9c7cb65937..764eb3dcfc 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -436,7 +436,7 @@ class Block extends Position implements BlockIds, Metadatable{ } /** - * Returns if the item can be broken with an specific Item + * Returns if the block can be broken with an specific Item * * @param Item $item * From e8bd0c3e09cfaaf77aa293654ed101d4735cea8e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 10 Aug 2017 18:02:01 +0100 Subject: [PATCH 2/6] add typehints to Metadatable interface and implementations, fix missing return for Block->hasMetadata() reported in #1285 --- src/pocketmine/OfflinePlayer.php | 12 ++++++------ src/pocketmine/Player.php | 12 ++++++------ src/pocketmine/block/Block.php | 16 +++++++++------- src/pocketmine/entity/Entity.php | 8 ++++---- src/pocketmine/level/Level.php | 12 ++++++------ src/pocketmine/metadata/Metadatable.php | 12 ++++-------- 6 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/pocketmine/OfflinePlayer.php b/src/pocketmine/OfflinePlayer.php index 4f7b4105e6..09d76dd405 100644 --- a/src/pocketmine/OfflinePlayer.php +++ b/src/pocketmine/OfflinePlayer.php @@ -119,20 +119,20 @@ class OfflinePlayer implements IPlayer, Metadatable{ return $this->namedtag instanceof CompoundTag; } - public function setMetadata($metadataKey, MetadataValue $metadataValue){ - $this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue); + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ + $this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } - public function getMetadata($metadataKey){ + public function getMetadata(string $metadataKey){ return $this->server->getPlayerMetadata()->getMetadata($this, $metadataKey); } - public function hasMetadata($metadataKey){ + public function hasMetadata(string $metadataKey) : bool{ return $this->server->getPlayerMetadata()->hasMetadata($this, $metadataKey); } - public function removeMetadata($metadataKey, Plugin $plugin){ - $this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin); + public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ + $this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 05a4ac6fa2..94d85aaa17 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -3855,20 +3855,20 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - public function setMetadata($metadataKey, MetadataValue $metadataValue){ - $this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue); + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ + $this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } - public function getMetadata($metadataKey){ + public function getMetadata(string $metadataKey){ return $this->server->getPlayerMetadata()->getMetadata($this, $metadataKey); } - public function hasMetadata($metadataKey){ + public function hasMetadata(string $metadataKey) : bool{ return $this->server->getPlayerMetadata()->hasMetadata($this, $metadataKey); } - public function removeMetadata($metadataKey, Plugin $plugin){ - $this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin); + public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ + $this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } public function onChunkChanged(Chunk $chunk){ diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 764eb3dcfc..fcc50afd67 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -830,13 +830,13 @@ class Block extends Position implements BlockIds, Metadatable{ return MovingObjectPosition::fromBlock($this->x, $this->y, $this->z, $f, $vector->add($this->x, $this->y, $this->z)); } - public function setMetadata($metadataKey, MetadataValue $metadataValue){ + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->setMetadata($this, $metadataKey, $metadataValue); + $this->getLevel()->getBlockMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } } - public function getMetadata($metadataKey){ + public function getMetadata(string $metadataKey){ if($this->getLevel() instanceof Level){ return $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); } @@ -844,15 +844,17 @@ class Block extends Position implements BlockIds, Metadatable{ return null; } - public function hasMetadata($metadataKey){ + public function hasMetadata(string $metadataKey) : bool{ if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); + return $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); } + + return false; } - public function removeMetadata($metadataKey, Plugin $plugin){ + public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ if($this->getLevel() instanceof Level){ - $this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $plugin); + $this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } } } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 0e1aa99240..4d690b8f83 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1872,19 +1872,19 @@ abstract class Entity extends Location implements Metadatable{ $this->close(); } - public function setMetadata($metadataKey, MetadataValue $metadataValue){ + public function setMetadata(string $metadataKey, MetadataValue $metadataValue){ $this->server->getEntityMetadata()->setMetadata($this, $metadataKey, $metadataValue); } - public function getMetadata($metadataKey){ + public function getMetadata(string $metadataKey){ return $this->server->getEntityMetadata()->getMetadata($this, $metadataKey); } - public function hasMetadata($metadataKey){ + public function hasMetadata(string $metadataKey) : bool{ return $this->server->getEntityMetadata()->hasMetadata($this, $metadataKey); } - public function removeMetadata($metadataKey, Plugin $plugin){ + public function removeMetadata(string $metadataKey, Plugin $plugin){ $this->server->getEntityMetadata()->removeMetadata($this, $metadataKey, $plugin); } diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index b702e29414..136b78bcb0 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -2907,20 +2907,20 @@ class Level implements ChunkManager, Metadatable{ } } - public function setMetadata($metadataKey, MetadataValue $metadataValue){ - $this->server->getLevelMetadata()->setMetadata($this, $metadataKey, $metadataValue); + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ + $this->server->getLevelMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } - public function getMetadata($metadataKey){ + public function getMetadata(string $metadataKey){ return $this->server->getLevelMetadata()->getMetadata($this, $metadataKey); } - public function hasMetadata($metadataKey){ + public function hasMetadata(string $metadataKey) : bool{ return $this->server->getLevelMetadata()->hasMetadata($this, $metadataKey); } - public function removeMetadata($metadataKey, Plugin $plugin){ - $this->server->getLevelMetadata()->removeMetadata($this, $metadataKey, $plugin); + public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ + $this->server->getLevelMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } public function addEntityMotion(int $chunkX, int $chunkZ, int $entityId, float $x, float $y, float $z){ diff --git a/src/pocketmine/metadata/Metadatable.php b/src/pocketmine/metadata/Metadatable.php index b3e1c6c41a..c43b9ce8e6 100644 --- a/src/pocketmine/metadata/Metadatable.php +++ b/src/pocketmine/metadata/Metadatable.php @@ -32,10 +32,8 @@ interface Metadatable{ * * @param string $metadataKey * @param MetadataValue $newMetadataValue - * - * @return void */ - public function setMetadata($metadataKey, MetadataValue $newMetadataValue); + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue); /** * Returns a list of previously set metadata values from the implementing @@ -45,7 +43,7 @@ interface Metadatable{ * * @return MetadataValue[] */ - public function getMetadata($metadataKey); + public function getMetadata(string $metadataKey); /** * Tests to see whether the implementing object contains the given @@ -55,7 +53,7 @@ interface Metadatable{ * * @return bool */ - public function hasMetadata($metadataKey); + public function hasMetadata(string $metadataKey) : bool; /** * Removes the given metadata value from the implementing object's @@ -63,9 +61,7 @@ interface Metadatable{ * * @param string $metadataKey * @param Plugin $owningPlugin - * - * @return void */ - public function removeMetadata($metadataKey, Plugin $owningPlugin); + public function removeMetadata(string $metadataKey, Plugin $owningPlugin); } \ No newline at end of file From 0c798222a4af61fd5dac719466469dd336453466 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 10 Aug 2017 18:04:44 +0100 Subject: [PATCH 3/6] ... blame PhpStorm EAP --- src/pocketmine/block/Block.php | 2 +- src/pocketmine/entity/Entity.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index fcc50afd67..da971e24d4 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -846,7 +846,7 @@ class Block extends Position implements BlockIds, Metadatable{ public function hasMetadata(string $metadataKey) : bool{ if($this->getLevel() instanceof Level){ - return $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); + return $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); } return false; diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 4d690b8f83..43e069674e 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1872,8 +1872,9 @@ abstract class Entity extends Location implements Metadatable{ $this->close(); } - public function setMetadata(string $metadataKey, MetadataValue $metadataValue){ - $this->server->getEntityMetadata()->setMetadata($this, $metadataKey, $metadataValue); + + public function setMetadata(string $metadataKey, MetadataValue $newMetadataValue){ + $this->server->getEntityMetadata()->setMetadata($this, $metadataKey, $newMetadataValue); } public function getMetadata(string $metadataKey){ @@ -1884,8 +1885,8 @@ abstract class Entity extends Location implements Metadatable{ return $this->server->getEntityMetadata()->hasMetadata($this, $metadataKey); } - public function removeMetadata(string $metadataKey, Plugin $plugin){ - $this->server->getEntityMetadata()->removeMetadata($this, $metadataKey, $plugin); + public function removeMetadata(string $metadataKey, Plugin $owningPlugin){ + $this->server->getEntityMetadata()->removeMetadata($this, $metadataKey, $owningPlugin); } public function __toString(){ From ae3a8a54935e2196d813f4dcc2836c482bec06f3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 14 Aug 2017 14:12:06 +0100 Subject: [PATCH 4/6] Added capability to unblock addresses without a restart --- src/pocketmine/command/defaults/PardonIpCommand.php | 1 + src/pocketmine/network/AdvancedSourceInterface.php | 5 +++++ src/pocketmine/network/Network.php | 6 ++++++ src/pocketmine/network/mcpe/RakLibInterface.php | 4 ++++ src/raklib | 2 +- 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/command/defaults/PardonIpCommand.php b/src/pocketmine/command/defaults/PardonIpCommand.php index 76cc5544f5..cfe7571edb 100644 --- a/src/pocketmine/command/defaults/PardonIpCommand.php +++ b/src/pocketmine/command/defaults/PardonIpCommand.php @@ -50,6 +50,7 @@ class PardonIpCommand extends VanillaCommand{ if(preg_match("/^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$/", $args[0])){ $sender->getServer()->getIPBans()->remove($args[0]); + $sender->getServer()->getNetwork()->unblockAddress($args[0]); Command::broadcastCommandMessage($sender, new TranslationContainer("commands.unbanip.success", [$args[0]])); }else{ $sender->sendMessage(new TranslationContainer("commands.unbanip.invalid")); diff --git a/src/pocketmine/network/AdvancedSourceInterface.php b/src/pocketmine/network/AdvancedSourceInterface.php index b85ff8b993..cf553081ec 100644 --- a/src/pocketmine/network/AdvancedSourceInterface.php +++ b/src/pocketmine/network/AdvancedSourceInterface.php @@ -34,6 +34,11 @@ interface AdvancedSourceInterface extends SourceInterface{ */ public function blockAddress(string $address, int $timeout = 300); + /** + * @param string $address + */ + public function unblockAddress(string $address); + /** * @param Network $network */ diff --git a/src/pocketmine/network/Network.php b/src/pocketmine/network/Network.php index 6b60ddd80f..4f1ce278e8 100644 --- a/src/pocketmine/network/Network.php +++ b/src/pocketmine/network/Network.php @@ -171,4 +171,10 @@ class Network{ $interface->blockAddress($address, $timeout); } } + + public function unblockAddress(string $address){ + foreach($this->advancedInterfaces as $interface){ + $interface->unblockAddress($address); + } + } } diff --git a/src/pocketmine/network/mcpe/RakLibInterface.php b/src/pocketmine/network/mcpe/RakLibInterface.php index 863caaf147..2e57a27cdd 100644 --- a/src/pocketmine/network/mcpe/RakLibInterface.php +++ b/src/pocketmine/network/mcpe/RakLibInterface.php @@ -152,6 +152,10 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{ $this->interface->blockAddress($address, $timeout); } + public function unblockAddress(string $address){ + $this->interface->unblockAddress($address); + } + public function handleRaw($address, $port, $payload){ $this->server->handlePacket($address, $port, $payload); } diff --git a/src/raklib b/src/raklib index ddd953f0e2..cd31b332c3 160000 --- a/src/raklib +++ b/src/raklib @@ -1 +1 @@ -Subproject commit ddd953f0e2a6577dd39f0a450546dad12a472b01 +Subproject commit cd31b332c38f98bafa238771caf1583f8461f7f8 From ebb71fc6c5d75adc23f538a4dfcfc96b3f9b2f14 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 15 Aug 2017 19:48:19 +0100 Subject: [PATCH 5/6] Typehints and docs for MemoryManager --- src/pocketmine/MemoryManager.php | 64 +++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/pocketmine/MemoryManager.php b/src/pocketmine/MemoryManager.php index 0cb9268cec..355bcf38f6 100644 --- a/src/pocketmine/MemoryManager.php +++ b/src/pocketmine/MemoryManager.php @@ -33,27 +33,45 @@ class MemoryManager{ /** @var Server */ private $server; + /** @var int */ private $memoryLimit; + /** @var int */ private $globalMemoryLimit; + /** @var int */ private $checkRate; + /** @var int */ private $checkTicker = 0; + /** @var bool */ private $lowMemory = false; + /** @var bool */ private $continuousTrigger = true; + /** @var int */ private $continuousTriggerRate; + /** @var int */ private $continuousTriggerCount = 0; + /** @var int */ private $continuousTriggerTicker = 0; + /** @var int */ private $garbageCollectionPeriod; + /** @var int */ private $garbageCollectionTicker = 0; + /** @var bool */ private $garbageCollectionTrigger; + /** @var bool */ private $garbageCollectionAsync; + /** @var int */ private $chunkRadiusOverride; + /** @var bool */ private $chunkCollect; + /** @var bool */ private $chunkTrigger; + /** @var bool */ private $chunkCache; + /** @var bool */ private $cacheTrigger; public function __construct(Server $server){ @@ -116,10 +134,16 @@ class MemoryManager{ gc_enable(); } + /** + * @return bool + */ public function isLowMemory() : bool{ return $this->lowMemory; } + /** + * @return bool + */ public function canUseChunkCache() : bool{ return !($this->lowMemory and $this->chunkTrigger); } @@ -132,10 +156,18 @@ class MemoryManager{ * @return int */ public function getViewDistance(int $distance) : int{ - return $this->lowMemory ? min($this->chunkRadiusOverride, $distance) : $distance; + return $this->lowMemory ? (int) min($this->chunkRadiusOverride, $distance) : $distance; } - public function trigger($memory, $limit, $global = false, $triggerCount = 0){ + /** + * Triggers garbage collection and cache cleanup to try and free memory. + * + * @param int $memory + * @param int $limit + * @param bool $global + * @param int $triggerCount + */ + public function trigger(int $memory, int $limit, bool $global = false, int $triggerCount = 0){ $this->server->getLogger()->debug(sprintf("[Memory Manager] %sLow memory triggered, limit %gMB, using %gMB", $global ? "Global " : "", round(($limit / 1024) / 1024, 2), round(($memory / 1024) / 1024, 2))); if($this->cacheTrigger){ @@ -161,6 +193,9 @@ class MemoryManager{ $this->server->getLogger()->debug(sprintf("[Memory Manager] Freed %gMB, $cycles cycles", round(($ev->getMemoryFreed() / 1024) / 1024, 2))); } + /** + * Called every tick to update the memory manager state. + */ public function check(){ Timings::$memoryManagerTimer->startTiming(); @@ -198,7 +233,10 @@ class MemoryManager{ Timings::$memoryManagerTimer->stopTiming(); } - public function triggerGarbageCollector(){ + /** + * @return int + */ + public function triggerGarbageCollector() : int{ Timings::$garbageCollectorTimer->startTiming(); if($this->garbageCollectionAsync){ @@ -215,7 +253,14 @@ class MemoryManager{ return $cycles; } - public function dumpServerMemory($outputFolder, $maxNesting, $maxStringSize){ + /** + * Dumps the server memory into the specified output folder. + * + * @param string $outputFolder + * @param int $maxNesting + * @param int $maxStringSize + */ + public function dumpServerMemory(string $outputFolder, int $maxNesting, int $maxStringSize){ $hardLimit = ini_get('memory_limit'); ini_set('memory_limit', '-1'); gc_disable(); @@ -328,7 +373,16 @@ class MemoryManager{ gc_enable(); } - private function continueDump($from, &$data, &$objects, &$refCounts, $recursion, $maxNesting, $maxStringSize){ + /** + * @param mixed $from + * @param mixed &$data + * @param object[] &$objects + * @param int[] &$refCounts + * @param int $recursion + * @param int $maxNesting + * @param int $maxStringSize + */ + private function continueDump($from, &$data, array &$objects, array &$refCounts, int $recursion, int $maxNesting, int $maxStringSize){ if($maxNesting <= 0){ $data = "(error) NESTING LIMIT REACHED"; return; From 9a0b3a6e22e023747237bf2399ba89f33ff7db7f Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 16 Aug 2017 12:28:44 +0100 Subject: [PATCH 6/6] Updated RakLib submodule --- src/raklib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raklib b/src/raklib index cd31b332c3..5d1c052cc6 160000 --- a/src/raklib +++ b/src/raklib @@ -1 +1 @@ -Subproject commit cd31b332c38f98bafa238771caf1583f8461f7f8 +Subproject commit 5d1c052cc6668576d116a7ab246820d75c885382