mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Fixed MetadataStore
This commit is contained in:
parent
696c67f541
commit
ea414ea72d
@ -43,45 +43,45 @@ class BlockMetadataStore extends MetadataStore{
|
||||
return $block->x . ":" . $block->y . ":" . $block->z . ":" . $metadataKey;
|
||||
}
|
||||
|
||||
public function getMetadata($block, $metadataKey){
|
||||
if(!($block instanceof Block)){
|
||||
public function getMetadata(Metadatable $subject, string $metadataKey){
|
||||
if(!($subject instanceof Block)){
|
||||
throw new \InvalidArgumentException("Object must be a Block");
|
||||
}
|
||||
if($block->getLevel() === $this->owningLevel){
|
||||
return parent::getMetadata($block, $metadataKey);
|
||||
if($subject->getLevel() === $this->owningLevel){
|
||||
return parent::getMetadata($subject, $metadataKey);
|
||||
}else{
|
||||
throw new \InvalidStateException("Block does not belong to world " . $this->owningLevel->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function hasMetadata($block, $metadataKey){
|
||||
if(!($block instanceof Block)){
|
||||
public function hasMetadata(Metadatable $subject, string $metadataKey) : bool{
|
||||
if(!($subject instanceof Block)){
|
||||
throw new \InvalidArgumentException("Object must be a Block");
|
||||
}
|
||||
if($block->getLevel() === $this->owningLevel){
|
||||
return parent::hasMetadata($block, $metadataKey);
|
||||
if($subject->getLevel() === $this->owningLevel){
|
||||
return parent::hasMetadata($subject, $metadataKey);
|
||||
}else{
|
||||
throw new \InvalidStateException("Block does not belong to world " . $this->owningLevel->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function removeMetadata($block, $metadataKey, Plugin $owningPlugin){
|
||||
if(!($block instanceof Block)){
|
||||
public function removeMetadata(Metadatable $subject, string $metadataKey, Plugin $owningPlugin){
|
||||
if(!($subject instanceof Block)){
|
||||
throw new \InvalidArgumentException("Object must be a Block");
|
||||
}
|
||||
if($block->getLevel() === $this->owningLevel){
|
||||
parent::removeMetadata($block, $metadataKey, $owningPlugin);
|
||||
if($subject->getLevel() === $this->owningLevel){
|
||||
parent::removeMetadata($subject, $metadataKey, $owningPlugin);
|
||||
}else{
|
||||
throw new \InvalidStateException("Block does not belong to world " . $this->owningLevel->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function setMetadata($block, $metadataKey, MetadataValue $newMetadatavalue){
|
||||
if(!($block instanceof Block)){
|
||||
public function setMetadata(Metadatable $subject, string $metadataKey, MetadataValue $newMetadataValue){
|
||||
if(!($subject instanceof Block)){
|
||||
throw new \InvalidArgumentException("Object must be a Block");
|
||||
}
|
||||
if($block->getLevel() === $this->owningLevel){
|
||||
parent::setMetadata($block, $metadataKey, $newMetadatavalue);
|
||||
if($subject->getLevel() === $this->owningLevel){
|
||||
parent::setMetadata($subject, $metadataKey, $newMetadataValue);
|
||||
}else{
|
||||
throw new \InvalidStateException("Block does not belong to world " . $this->owningLevel->getName());
|
||||
}
|
||||
|
@ -30,19 +30,17 @@ use pocketmine\plugin\Plugin;
|
||||
use pocketmine\plugin\PluginException;
|
||||
|
||||
abstract class MetadataStore{
|
||||
/** @var \WeakMap[] */
|
||||
/** @var \SplObjectStorage[] */
|
||||
private $metadataMap;
|
||||
|
||||
/**
|
||||
* Adds a metadata value to an object.
|
||||
*
|
||||
* @param mixed $subject
|
||||
* @param Metadatable $subject
|
||||
* @param string $metadataKey
|
||||
* @param MetadataValue $newMetadataValue
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){
|
||||
public function setMetadata(Metadatable $subject, string $metadataKey, MetadataValue $newMetadataValue){
|
||||
$owningPlugin = $newMetadataValue->getOwningPlugin();
|
||||
if($owningPlugin === null){
|
||||
throw new PluginException("Plugin cannot be null");
|
||||
@ -50,8 +48,8 @@ abstract class MetadataStore{
|
||||
|
||||
$key = $this->disambiguate($subject, $metadataKey);
|
||||
if(!isset($this->metadataMap[$key])){
|
||||
//$entry = new \WeakMap();
|
||||
$this->metadataMap[$key] = new \SplObjectStorage();//$entry;
|
||||
$entry = new \SplObjectStorage();
|
||||
$this->metadataMap[$key] = $entry;
|
||||
}else{
|
||||
$entry = $this->metadataMap[$key];
|
||||
}
|
||||
@ -62,14 +60,12 @@ abstract class MetadataStore{
|
||||
* Returns all metadata values attached to an object. If multiple
|
||||
* have attached metadata, each will value will be included.
|
||||
*
|
||||
* @param mixed $subject
|
||||
* @param string $metadataKey
|
||||
* @param Metadatable $subject
|
||||
* @param string $metadataKey
|
||||
*
|
||||
* @return MetadataValue[]
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getMetadata($subject, $metadataKey){
|
||||
public function getMetadata(Metadatable $subject, string $metadataKey){
|
||||
$key = $this->disambiguate($subject, $metadataKey);
|
||||
if(isset($this->metadataMap[$key])){
|
||||
return $this->metadataMap[$key];
|
||||
@ -81,27 +77,23 @@ abstract class MetadataStore{
|
||||
/**
|
||||
* Tests to see if a metadata attribute has been set on an object.
|
||||
*
|
||||
* @param mixed $subject
|
||||
* @param string $metadataKey
|
||||
* @param Metadatable $subject
|
||||
* @param string $metadataKey
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function hasMetadata($subject, $metadataKey){
|
||||
public function hasMetadata(Metadatable $subject, string $metadataKey) : bool{
|
||||
return isset($this->metadataMap[$this->disambiguate($subject, $metadataKey)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a metadata item owned by a plugin from a subject.
|
||||
*
|
||||
* @param mixed $subject
|
||||
* @param string $metadataKey
|
||||
* @param Plugin $owningPlugin
|
||||
*
|
||||
* @throws \Exception
|
||||
* @param Metadatable $subject
|
||||
* @param string $metadataKey
|
||||
* @param Plugin $owningPlugin
|
||||
*/
|
||||
public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){
|
||||
public function removeMetadata(Metadatable $subject, string $metadataKey, Plugin $owningPlugin){
|
||||
$key = $this->disambiguate($subject, $metadataKey);
|
||||
if(isset($this->metadataMap[$key])){
|
||||
unset($this->metadataMap[$key][$owningPlugin]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user