Added object metadata for Plugins, use WeakMap on perms

This commit is contained in:
Shoghi Cervantes
2014-05-19 20:07:27 +02:00
parent 88f9347093
commit 99818a26f5
16 changed files with 540 additions and 19 deletions

View File

@ -0,0 +1,72 @@
<?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\metadata;
use pocketmine\Block\Block;
use pocketmine\level\Level;
use pocketmine\plugin\Plugin;
class BlockMetadataStore extends MetadataStore{
/** @var Level */
private $owningLevel;
public function __construct(Level $owningLevel){
$this->owningLevel = $owningLevel;
}
/** @noinspection PhpHierarchyChecksInspection */
public function disambiguate(Block $block, $metadataKey){
return $block->x . ":" . $block->y . ":" . $block->z . ":" . $metadataKey;
}
public function getMetadata(Block $block, $metadataKey){
if($block->getLevel() === $this->owningLevel){
return parent::getMetadata($block, $metadataKey);
}else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName());
}
}
public function hasMetadata(Block $block, $metadataKey){
if($block->getLevel() === $this->owningLevel){
return parent::hasMetadata($block, $metadataKey);
}else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName());
}
}
public function removeMetadata(Block $block, $metadataKey, Plugin $owningPlugin){
if($block->getLevel() === $this->owningLevel){
parent::hasMetadata($block, $metadataKey, $owningPlugin);
}else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName());
}
}
public function setMetadata(Block $block, $metadataKey, MetadataValue $newMetadatavalue){
if($block->getLevel() === $this->owningLevel){
parent::setMetadata($block, $metadataKey, $newMetadatavalue);
}else{
trigger_error("Block does not belong to world " . $this->owningLevel->getName());
}
}
}

View File

@ -0,0 +1,32 @@
<?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\metadata;
use pocketmine\entity\Entity;
class EntityMetadataStore extends MetadataStore{
/** @noinspection PhpHierarchyChecksInspection */
public function disambiguate(Entity $entity, $metadataKey){
return $entity->getID() . ":" . $metadataKey;
}
}

View File

@ -0,0 +1,32 @@
<?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\metadata;
use pocketmine\level\Level;
class LevelMetadataStore extends MetadataStore{
/** @noinspection PhpHierarchyChecksInspection */
public function disambiguate(Level $level, $metadataKey){
return strtolower($level->getName()) . ":" . $metadataKey;
}
}

View File

@ -0,0 +1,130 @@
<?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/
*
*
*/
/**
* Saves extra data on runtime for different items
*/
namespace pocketmine\metadata;
use pocketmine\plugin\Plugin;
abstract class MetadataStore{
/** @var \WeakMap[] */
private $metadataMap = [];
/**
* Adds a metadata value to an object.
*
* @param mixed $subject
* @param string $metadataKey
* @param MetadataValue $newMetadataValue
*/
public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){
$owningPlugin = $newMetadataValue->getOwningPlugin();
if($owningPlugin === null){
trigger_error("Plugin cannot be null", E_USER_WARNING);
return;
}
$key = $this->disambiguate($subject, $newMetadataValue);
if(!isset($this->metadataMap[$key])){
$entry = new \WeakMap();
$this->metadataMap[$key] = $entry;
}else{
$entry = $this->metadataMap[$key];
}
$entry[$owningPlugin] = $newMetadataValue;
}
/**
* 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
*
* @return MetadataValue[]
*/
public function getMetadata($subject, $metadataKey){
$key = $this->disambiguate($subject, $metadataKey);
if(isset($this->metadataMap[$key])){
return $this->metadataMap[$key];
}else{
return [];
}
}
/**
* Tests to see if a metadata attribute has been set on an object.
*
* @param mixed $subject
* @param string $metadataKey
*
* @return bool
*/
public function hasMetadata($subject, $metadataKey){
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
*/
public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){
$key = $this->disambiguate($subject, $metadataKey);
if(isset($this->metadataMap[$key])){
unset($this->metadataMap[$key][$owningPlugin]);
if($this->metadataMap[$key]->count() === 0){
unset($this->metadataMap[$key]);
}
}
}
/**
* Invalidates all metadata in the metadata store that originates from the
* given plugin. Doing this will force each invalidated metadata item to
* be recalculated the next time it is accessed.
*
* @param Plugin $owningPlugin
*/
public function invalidateAll(Plugin $owningPlugin){
/** @var $values MetadataValue[] */
foreach($this->metadataMap as $values){
if(isset($values[$owningPlugin])){
$values[$owningPlugin]->invalidate();
}
}
}
/**
* 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
*
* @return string
*/
public abstract function disambiguate($subject, $metadataKey);
}

View File

@ -0,0 +1,53 @@
<?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\metadata;
use pocketmine\plugin\Plugin;
abstract class MetadataValue{
/** @var \WeakRef<Plugin> */
protected $owningPlugin;
protected function __construct(Plugin $owningPlugin){
$this->owningPlugin = new \WeakRef($owningPlugin);
}
/**
* @return Plugin
*/
public function getOwningPlugin(){
return $this->owningPlugin->get();
}
/**
* Fetches the value of this metadata item.
*
* @return mixed
*/
public abstract function value();
/**
* Invalidates this metadata item, forcing it to recompute when next
* accessed.
*/
public abstract function invalidate();
}

View File

@ -0,0 +1,69 @@
<?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\metadata;
use pocketmine\plugin\Plugin;
interface Metadatable{
/**
* Sets a metadata value in the implementing object's metadata store.
*
* @param string $metadataKey
* @param MetadataValue $newMetadataValue
*
* @return void
*/
public function setMetadata($metadataKey, MetadataValue $newMetadataValue);
/**
* Returns a list of previously set metadata values from the implementing
* object's metadata store.
*
* @param string $metadataKey
*
* @return MetadataValue[]
*/
public function getMetadata($metadataKey);
/**
* Tests to see whether the implementing object contains the given
* metadata value in its metadata store.
*
* @param string $metadataKey
*
* @return boolean
*/
public function hasMetadata($metadataKey);
/**
* Removes the given metadata value from the implementing object's
* metadata store.
*
* @param string $metadataKey
* @param Plugin $owningPlugin
*
* @return void
*/
public function removeMetadata($metadataKey, Plugin $owningPlugin);
}

View File

@ -0,0 +1,32 @@
<?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\metadata;
use pocketmine\OfflinePlayer;
class PlayerMetadataStore extends MetadataStore{
/** @noinspection PhpHierarchyChecksInspection */
public function disambiguate(OfflinePlayer $player, $metadataKey){
return strtolower($player->getName()) . ":" . $metadataKey;
}
}