Merge branch 'minor-next' into major-next

This commit is contained in:
Dylan K. Taylor
2023-05-30 16:15:56 +01:00
13 changed files with 391 additions and 483 deletions

View File

@ -27,16 +27,24 @@ use pocketmine\crafting\CraftingManagerFromDataHelper;
use pocketmine\crafting\json\ItemStackData;
use pocketmine\data\bedrock\BedrockDataFiles;
use pocketmine\item\Item;
use pocketmine\utils\DestructorCallbackTrait;
use pocketmine\utils\Filesystem;
use pocketmine\utils\ObjectSet;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
final class CreativeInventory{
use SingletonTrait;
use DestructorCallbackTrait;
/** @var Item[] */
private array $creative = [];
/** @phpstan-var ObjectSet<\Closure() : void> */
private ObjectSet $contentChangedCallbacks;
private function __construct(){
$this->contentChangedCallbacks = new ObjectSet();
$creativeItems = CraftingManagerFromDataHelper::loadJsonArrayOfObjectsFile(
BedrockDataFiles::CREATIVEITEMS_JSON,
ItemStackData::class
@ -57,6 +65,7 @@ final class CreativeInventory{
*/
public function clear() : void{
$this->creative = [];
$this->onContentChange();
}
/**
@ -86,6 +95,7 @@ final class CreativeInventory{
*/
public function add(Item $item) : void{
$this->creative[] = clone $item;
$this->onContentChange();
}
/**
@ -96,10 +106,22 @@ final class CreativeInventory{
$index = $this->getItemIndex($item);
if($index !== -1){
unset($this->creative[$index]);
$this->onContentChange();
}
}
public function contains(Item $item) : bool{
return $this->getItemIndex($item) !== -1;
}
/** @phpstan-return ObjectSet<\Closure() : void> */
public function getContentChangedCallbacks() : ObjectSet{
return $this->contentChangedCallbacks;
}
private function onContentChange() : void{
foreach($this->contentChangedCallbacks as $callback){
$callback();
}
}
}