From 683ab8d2cdf3e6314e2d2663372e6fc4d5fbeffa Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Fri, 23 May 2014 22:30:37 +0200 Subject: [PATCH] Fixed invalid metadata and updated weak references on PluginManager --- .../metadata/BlockMetadataStore.php | 6 ++- .../metadata/EntityMetadataStore.php | 6 ++- .../metadata/LevelMetadataStore.php | 6 ++- src/pocketmine/metadata/MetadataStore.php | 10 ++-- .../metadata/PlayerMetadataStore.php | 6 ++- src/pocketmine/plugin/PluginManager.php | 53 ++++++++++++++----- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/pocketmine/metadata/BlockMetadataStore.php b/src/pocketmine/metadata/BlockMetadataStore.php index a906bad7b..162a47190 100644 --- a/src/pocketmine/metadata/BlockMetadataStore.php +++ b/src/pocketmine/metadata/BlockMetadataStore.php @@ -33,8 +33,10 @@ class BlockMetadataStore extends MetadataStore{ $this->owningLevel = $owningLevel; } - /** @noinspection PhpHierarchyChecksInspection */ - public function disambiguate(Block $block, $metadataKey){ + public function disambiguate(Metadatable $block, $metadataKey){ + if(!($block instanceof Block)){ + throw new \InvalidArgumentException("Argument must be a Block instance"); + } return $block->x . ":" . $block->y . ":" . $block->z . ":" . $metadataKey; } diff --git a/src/pocketmine/metadata/EntityMetadataStore.php b/src/pocketmine/metadata/EntityMetadataStore.php index 02080f12e..21c5c6937 100644 --- a/src/pocketmine/metadata/EntityMetadataStore.php +++ b/src/pocketmine/metadata/EntityMetadataStore.php @@ -25,8 +25,10 @@ use pocketmine\entity\Entity; class EntityMetadataStore extends MetadataStore{ - /** @noinspection PhpHierarchyChecksInspection */ - public function disambiguate(Entity $entity, $metadataKey){ + public function disambiguate(Metadatable $entity, $metadataKey){ + if(!($entity instanceof Entity)){ + throw new \InvalidArgumentException("Argument must be an Entity instance"); + } return $entity->getID() . ":" . $metadataKey; } } \ No newline at end of file diff --git a/src/pocketmine/metadata/LevelMetadataStore.php b/src/pocketmine/metadata/LevelMetadataStore.php index f97ff6f75..733cbb41a 100644 --- a/src/pocketmine/metadata/LevelMetadataStore.php +++ b/src/pocketmine/metadata/LevelMetadataStore.php @@ -25,8 +25,10 @@ use pocketmine\level\Level; class LevelMetadataStore extends MetadataStore{ - /** @noinspection PhpHierarchyChecksInspection */ - public function disambiguate(Level $level, $metadataKey){ + public function disambiguate(Metadatable $level, $metadataKey){ + if(!($level instanceof Level)){ + throw new \InvalidArgumentException("Argument must be a Level instance"); + } return strtolower($level->getName()) . ":" . $metadataKey; } } \ No newline at end of file diff --git a/src/pocketmine/metadata/MetadataStore.php b/src/pocketmine/metadata/MetadataStore.php index adf25363d..68ebe7b24 100644 --- a/src/pocketmine/metadata/MetadataStore.php +++ b/src/pocketmine/metadata/MetadataStore.php @@ -45,7 +45,7 @@ abstract class MetadataStore{ return; } - $key = $this->disambiguate($subject, $newMetadataValue); + $key = $this->disambiguate($subject, $metadataKey); if(!isset($this->metadataMap[$key])){ $entry = new \WeakMap(); $this->metadataMap[$key] = $entry; @@ -122,10 +122,12 @@ abstract class MetadataStore{ * Creates a unique name for the object receiving metadata by combining * unique data from the subject with a metadataKey. * - * @param mixed $subject - * @param string $metadataKey + * @param Metadatable $subject + * @param string $metadataKey * * @return string + * + * @throws \InvalidArgumentException */ - public abstract function disambiguate($subject, $metadataKey); + public abstract function disambiguate(Metadatable $subject, $metadataKey); } \ No newline at end of file diff --git a/src/pocketmine/metadata/PlayerMetadataStore.php b/src/pocketmine/metadata/PlayerMetadataStore.php index e8204f7c5..da45fb2c1 100644 --- a/src/pocketmine/metadata/PlayerMetadataStore.php +++ b/src/pocketmine/metadata/PlayerMetadataStore.php @@ -25,8 +25,10 @@ use pocketmine\OfflinePlayer; class PlayerMetadataStore extends MetadataStore{ - /** @noinspection PhpHierarchyChecksInspection */ - public function disambiguate(OfflinePlayer $player, $metadataKey){ + public function disambiguate(Metadatable $player, $metadataKey){ + if(!($player instanceof OfflinePlayer)){ + throw new \InvalidArgumentException("Argument must be an OfflinePlayer instance"); + } return strtolower($player->getName()) . ":" . $metadataKey; } } \ No newline at end of file diff --git a/src/pocketmine/plugin/PluginManager.php b/src/pocketmine/plugin/PluginManager.php index 0edf171f1..d562a87f8 100644 --- a/src/pocketmine/plugin/PluginManager.php +++ b/src/pocketmine/plugin/PluginManager.php @@ -70,12 +70,12 @@ class PluginManager{ /** * @var Permissible[] */ - protected $defSubs; + protected $defSubs = []; /** * @var Permissible[] */ - protected $defSubsOp; + protected $defSubsOp = []; /** * @var PluginLoader[] @@ -89,8 +89,6 @@ class PluginManager{ public function __construct(Server $server, SimpleCommandMap $commandMap){ $this->server = $server; $this->commandMap = $commandMap; - $this->defSubs = new \WeakMap(); - $this->defSubsOp = new \WeakMap(); } /** @@ -408,9 +406,9 @@ class PluginManager{ */ public function subscribeToPermission($permission, Permissible $permissible){ if(!isset($this->permSubs[$permission])){ - $this->permSubs[$permission] = new \WeakRef(); + $this->permSubs[$permission] = []; } - $this->permSubs[$permission][$permissible] = true; + $this->permSubs[$permission][spl_object_hash($permissible)] = new \WeakRef($permissible); } /** @@ -419,7 +417,7 @@ class PluginManager{ */ public function unsubscribeFromPermission($permission, Permissible $permissible){ if(isset($this->permSubs[$permission])){ - unset($this->permSubs[$permission][$permissible]); + unset($this->permSubs[$permission][spl_object_hash($permissible)]); } } @@ -430,7 +428,16 @@ class PluginManager{ */ public function getPermissionSubscriptions($permission){ if(isset($this->permSubs[$permission])){ - return $this->permSubs[$permission]; + $subs = []; + foreach($this->permSubs[$permission] as $k => $perm){ + /** @var \WeakRef $perm */ + if($perm->valid()){ + $subs[] = $perm->get(); + }else{ + unset($this->permSubs[$permission][$k]); + } + } + return $subs; } return []; @@ -442,9 +449,9 @@ class PluginManager{ */ public function subscribeToDefaultPerms($op, Permissible $permissible){ if($op === true){ - $this->defSubsOp[$permissible] = true; + $this->defSubsOp[spl_object_hash($permissible)] = new \WeakRef($permissible); }else{ - $this->defSubs[$permissible] = true; + $this->defSubs[spl_object_hash($permissible)] = new \WeakRef($permissible); } } @@ -454,9 +461,9 @@ class PluginManager{ */ public function unsubscribeFromDefaultPerms($op, Permissible $permissible){ if($op === true){ - unset($this->defSubsOp[$permissible]); + unset($this->defSubsOp[spl_object_hash($permissible)]); }else{ - unset($this->defSubs[$permissible]); + unset($this->defSubs[spl_object_hash($permissible)]); } } @@ -466,11 +473,29 @@ class PluginManager{ * @return Permissible[] */ public function getDefaultPermSubscriptions($op){ + $subs = []; + if($op === true){ - return $this->defSubsOp; + foreach($this->defSubsOp as $k => $perm){ + /** @var \WeakRef $perm */ + if($perm->valid()){ + $subs[] = $perm->get(); + }else{ + unset($this->defSubsOp[$k]); + } + } }else{ - return $this->defSubs; + foreach($this->defSubs as $k => $perm){ + /** @var \WeakRef $perm */ + if($perm->valid()){ + $subs[] = $perm->get(); + }else{ + unset($this->defSubs[$k]); + } + } } + + return $subs; } /**