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

@ -10,7 +10,7 @@ NCURSES_VERSION="5.9"
PHPNCURSES_VERSION="1.0.2"
PTHREADS_VERSION="2.0.7"
PHP_POCKETMINE_VERSION="0.0.4"
UOPZ_VERSION="2.0.3"
UOPZ_VERSION="2.0.4"
WEAKREF_VERSION="0.2.4"
PHPYAML_VERSION="1.1.1"
YAML_VERSION="0.1.4"

View File

@ -22,7 +22,9 @@
namespace pocketmine;
use pocketmine\metadata\MetadataValue;
use pocketmine\nbt\tag\Compound;
use pocketmine\plugin\Plugin;
class OfflinePlayer implements IPlayer{
@ -112,5 +114,21 @@ class OfflinePlayer implements IPlayer{
return $this->namedtag instanceof Compound;
}
public function setMetadata($metadataKey, MetadataValue $metadataValue){
$this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue);
}
public function getMetadata($metadataKey){
return $this->server->getPlayerMetadata()->getMetadata($this, $metadataKey);
}
public function hasMetadata($metadataKey){
return $this->server->getPlayerMetadata()->hasMetadata($this, $metadataKey);
}
public function removeMetadata($metadataKey, Plugin $plugin){
$this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin);
}
}

View File

@ -41,6 +41,7 @@ use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\math\Vector3;
use pocketmine\metadata\MetadataValue;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
@ -2569,6 +2570,22 @@ class Player extends Human implements CommandSender, IPlayer{
$this->dataPacket($pk);
}
public function setMetadata($metadataKey, MetadataValue $metadataValue){
$this->server->getPlayerMetadata()->setMetadata($this, $metadataKey, $metadataValue);
}
public function getMetadata($metadataKey){
return $this->server->getPlayerMetadata()->getMetadata($this, $metadataKey);
}
public function hasMetadata($metadataKey){
return $this->server->getPlayerMetadata()->hasMetadata($this, $metadataKey);
}
public function removeMetadata($metadataKey, Plugin $plugin){
$this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin);
}
/**
* Handles a RakNet Packet
*

View File

@ -46,6 +46,9 @@ use pocketmine\level\generator\Normal;
use pocketmine\level\Level;
use pocketmine\level\LevelImport;
use pocketmine\level\WorldGenerator;
use pocketmine\metadata\EntityMetadataStore;
use pocketmine\metadata\LevelMetadataStore;
use pocketmine\metadata\PlayerMetadataStore;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
@ -132,6 +135,15 @@ class Server{
/** @var RCON */
private $rcon;
/** @var EntityMetadataStore */
private $entityMetadata;
/** @var PlayerMetadataStore */
private $playerMetadata;
/** @var LevelMetadataStore */
private $levelMetadata;
/**
* Counts the ticks since the server start
*
@ -427,6 +439,27 @@ class Server{
return $this->autoloader;
}
/**
* @return EntityMetadataStore
*/
public function getEntityMetadata(){
return $this->entityMetadata;
}
/**
* @return PlayerMetadataStore
*/
public function getPlayerMetadata(){
return $this->playerMetadata;
}
/**
* @return LevelMetadataStore
*/
public function getLevelMetadata(){
return $this->levelMetadata;
}
/**
* @return PluginManager
*/
@ -1140,6 +1173,10 @@ class Server{
@mkdir($this->dataPath . "players/", 0777);
@mkdir($this->pluginPath, 0777);
$this->entityMetadata = new EntityMetadataStore();
$this->playerMetadata = new PlayerMetadataStore();
$this->levelMetadata = new LevelMetadataStore();
$this->operators = new Config($this->dataPath . "ops.txt", Config::ENUM);
$this->whitelist = new Config($this->dataPath . "white-list.txt", Config::ENUM);
if(file_exists($this->dataPath . "banned.txt") and !file_exists($this->dataPath . "banned-players.txt")){

View File

@ -27,9 +27,10 @@ namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\metadata\Metadatable;
use pocketmine\Player;
abstract class Block extends Position{
abstract class Block extends Position implements Metadatable{
const AIR = 0;
const STONE = 1;
const GRASS = 2;

View File

@ -33,6 +33,8 @@ use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3 as Vector3;
use pocketmine\metadata\Metadatable;
use pocketmine\metadata\MetadataValue;
use pocketmine\nbt\tag\Compound;
use pocketmine\network\protocol\MoveEntityPacket_PosRot;
use pocketmine\network\protocol\MovePlayerPacket;
@ -42,9 +44,10 @@ use pocketmine\network\protocol\SetTimePacket;
use pocketmine\Network;
use pocketmine\Player;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
abstract class Entity extends Position{
abstract class Entity extends Position implements Metadatable{
public static $entityCount = 1;
/**
@ -110,6 +113,9 @@ abstract class Entity extends Position{
protected $fireProof;
private $invulnerable;
/** @var Server */
protected $server;
public $closed;
public static function get($entityID){
@ -127,6 +133,7 @@ abstract class Entity extends Position{
$this->closed = false;
$this->namedtag = $nbt;
$this->level = $level;
$this->server = Server::getInstance();
$this->boundingBox = new AxisAlignedBB(0, 0, 0, 0, 0, 0);
$this->setPositionAndRotation(new Vector3($this->namedtag["Pos"][0], $this->namedtag["Pos"][1], $this->namedtag["Pos"][2]), $this->namedtag->Rotation[0], $this->namedtag->Rotation[1]);
@ -315,8 +322,6 @@ abstract class Entity extends Position{
Entity::$needUpdate[$this->id] = $this;
}
public abstract function getMetadata();
public function setOnFire($seconds){
$ticks = $seconds * 20;
if($ticks > $this->fireTicks){
@ -587,8 +592,26 @@ abstract class Entity extends Position{
}
}
abstract public function getData();
public function __destruct(){
$this->close();
}
public function setMetadata($metadataKey, MetadataValue $metadataValue){
$this->server->getEntityMetadata()->setMetadata($this, $metadataKey, $metadataValue);
}
public function getMetadata($metadataKey){
return $this->server->getEntityMetadata()->getMetadata($this, $metadataKey);
}
public function hasMetadata($metadataKey){
return $this->server->getEntityMetadata()->hasMetadata($this, $metadataKey);
}
public function removeMetadata($metadataKey, Plugin $plugin){
$this->server->getEntityMetadata()->removeMetadata($this, $metadataKey, $plugin);
}
}

View File

@ -143,7 +143,7 @@ class Human extends Creature implements ProjectileSource, InventorySource{
$pk->pitch = 0;
$pk->unknown1 = 0;
$pk->unknown2 = 0;
$pk->metadata = $this->getMetadata();
$pk->metadata = $this->getData();
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
@ -262,7 +262,7 @@ class Human extends Creature implements ProjectileSource, InventorySource{
}
}
public function getMetadata(){ //TODO
public function getData(){ //TODO
$flags = 0;
$flags |= $this->fireTicks > 0 ? 1 : 0;
//$flags |= ($this->crouched === true ? 0b10:0) << 1;

View File

@ -45,6 +45,10 @@ class Position extends Vector3{
return new Position($pos->x, $pos->y, $pos->z, $level);
}
public function getLevel(){
return $this->level;
}
/**
* Returns a side Vector
*

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;
}
}

View File

@ -55,12 +55,12 @@ class PluginManager{
/**
* @var Permission[]
*/
protected $defaultPerms = array();
protected $defaultPerms = [];
/**
* @var Permission[]
*/
protected $defaultPermsOp = array();
protected $defaultPermsOp = [];
/**
* @var Permissible[]
@ -70,12 +70,12 @@ class PluginManager{
/**
* @var Permissible[]
*/
protected $defSubs = array();
protected $defSubs;
/**
* @var Permissible[]
*/
protected $defSubsOp = array();
protected $defSubsOp;
/**
* @var PluginLoader[]
@ -89,6 +89,8 @@ class PluginManager{
public function __construct(Server $server, SimpleCommandMap $commandMap){
$this->server = $server;
$this->commandMap = $commandMap;
$this->defSubs = new \WeakMap();
$this->defSubsOp = new \WeakMap();
}
/**
@ -405,10 +407,9 @@ class PluginManager{
*/
public function subscribeToPermission($permission, Permissible $permissible){
if(!isset($this->permSubs[$permission])){
//TODO: Use WeakRef
$this->permSubs[$permission] = array();
$this->permSubs[$permission] = new \WeakRef();
}
$this->permSubs[$permission][spl_object_hash($permissible)] = $permissible;
$this->permSubs[$permission][$permissible] = true;
}
/**
@ -417,7 +418,7 @@ class PluginManager{
*/
public function unsubscribeFromPermission($permission, Permissible $permissible){
if(isset($this->permSubs[$permission])){
unset($this->permSubs[$permission][spl_object_hash($permissible)]);
unset($this->permSubs[$permission][$permissible]);
}
}
@ -440,9 +441,9 @@ class PluginManager{
*/
public function subscribeToDefaultPerms($op, Permissible $permissible){
if($op === true){
$this->defSubsOp[spl_object_hash($permissible)] = $permissible;
$this->defSubsOp[$permissible] = true;
}else{
$this->defSubs[spl_object_hash($permissible)] = $permissible;
$this->defSubs[$permissible] = true;
}
}
@ -452,9 +453,9 @@ class PluginManager{
*/
public function unsubscribeFromDefaultPerms($op, Permissible $permissible){
if($op === true){
unset($this->defSubsOp[spl_object_hash($permissible)]);
unset($this->defSubsOp[$permissible]);
}else{
unset($this->defSubs[spl_object_hash($permissible)]);
unset($this->defSubs[$permissible]);
}
}