PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
MetadataStore.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
25 namespace pocketmine\metadata;
26 
29 
30 abstract class MetadataStore{
32  private $metadataMap = [];
33 
43  public function setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue){
44  $owningPlugin = $newMetadataValue->getOwningPlugin();
45  if($owningPlugin === null){
46  throw new PluginException("Plugin cannot be null");
47  }
48 
49  $key = $this->disambiguate($subject, $metadataKey);
50  if(!isset($this->metadataMap[$key])){
51  $entry = new \WeakMap();
52  $this->metadataMap[$key] = $entry;
53  }else{
54  $entry = $this->metadataMap[$key];
55  }
56  $entry[$owningPlugin] = $newMetadataValue;
57  }
58 
70  public function getMetadata($subject, $metadataKey){
71  $key = $this->disambiguate($subject, $metadataKey);
72  if(isset($this->metadataMap[$key])){
73  return $this->metadataMap[$key];
74  }else{
75  return [];
76  }
77  }
78 
89  public function hasMetadata($subject, $metadataKey){
90  return isset($this->metadataMap[$this->disambiguate($subject, $metadataKey)]);
91  }
92 
102  public function removeMetadata($subject, $metadataKey, Plugin $owningPlugin){
103  $key = $this->disambiguate($subject, $metadataKey);
104  if(isset($this->metadataMap[$key])){
105  unset($this->metadataMap[$key][$owningPlugin]);
106  if($this->metadataMap[$key]->count() === 0){
107  unset($this->metadataMap[$key]);
108  }
109  }
110  }
111 
119  public function invalidateAll(Plugin $owningPlugin){
121  foreach($this->metadataMap as $values){
122  if(isset($values[$owningPlugin])){
123  $values[$owningPlugin]->invalidate();
124  }
125  }
126  }
127 
139  public abstract function disambiguate(Metadatable $subject, $metadataKey);
140 }
hasMetadata($subject, $metadataKey)
getMetadata($subject, $metadataKey)
removeMetadata($subject, $metadataKey, Plugin $owningPlugin)
setMetadata($subject, $metadataKey, MetadataValue $newMetadataValue)
disambiguate(Metadatable $subject, $metadataKey)