mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-29 14:15:47 +00:00
Extract a CreativeInventory unit from Item
this will probably undergo further changes, but I'm primarily interested in just encapsulating the existing functionality for now.
This commit is contained in:
parent
41039cecc1
commit
7eb9b33fd6
@ -40,8 +40,8 @@ use pocketmine\event\server\CommandEvent;
|
||||
use pocketmine\event\server\DataPacketBroadcastEvent;
|
||||
use pocketmine\event\server\QueryRegenerateEvent;
|
||||
use pocketmine\inventory\CraftingManager;
|
||||
use pocketmine\inventory\CreativeInventory;
|
||||
use pocketmine\item\enchantment\Enchantment;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\lang\Language;
|
||||
use pocketmine\lang\LanguageNotFoundException;
|
||||
@ -1186,7 +1186,7 @@ class Server{
|
||||
BlockFactory::init();
|
||||
Enchantment::init();
|
||||
ItemFactory::init();
|
||||
Item::initCreativeItems();
|
||||
CreativeInventory::init();
|
||||
Biome::init();
|
||||
|
||||
$this->craftingManager = new CraftingManager();
|
||||
|
99
src/pocketmine/inventory/CreativeInventory.php
Normal file
99
src/pocketmine/inventory/CreativeInventory.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use pocketmine\item\Durable;
|
||||
use pocketmine\item\Item;
|
||||
use function file_get_contents;
|
||||
use function json_decode;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
|
||||
final class CreativeInventory{
|
||||
|
||||
/** @var Item[] */
|
||||
public static $creative = [];
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public static function init(){
|
||||
self::clear();
|
||||
|
||||
$creativeItems = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "creativeitems.json"), true);
|
||||
|
||||
foreach($creativeItems as $data){
|
||||
$item = Item::jsonDeserialize($data);
|
||||
if($item->getName() === "Unknown"){
|
||||
continue;
|
||||
}
|
||||
self::add($item);
|
||||
}
|
||||
}
|
||||
|
||||
public static function clear(){
|
||||
self::$creative = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[]
|
||||
*/
|
||||
public static function getAll() : array{
|
||||
return self::$creative;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @return Item|null
|
||||
*/
|
||||
public static function getItem(int $index) : ?Item{
|
||||
return self::$creative[$index] ?? null;
|
||||
}
|
||||
|
||||
public static function getItemIndex(Item $item) : int{
|
||||
foreach(self::$creative as $i => $d){
|
||||
if($item->equals($d, !($item instanceof Durable))){
|
||||
return $i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static function add(Item $item){
|
||||
self::$creative[] = clone $item;
|
||||
}
|
||||
|
||||
public static function remove(Item $item){
|
||||
$index = self::getItemIndex($item);
|
||||
if($index !== -1){
|
||||
unset(self::$creative[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function contains(Item $item) : bool{
|
||||
return self::getItemIndex($item) !== -1;
|
||||
}
|
||||
}
|
@ -165,7 +165,7 @@ class PlayerInventory extends BaseInventory{
|
||||
|
||||
$items = [];
|
||||
if(!$holder->isSpectator()){ //fill it for all gamemodes except spectator
|
||||
foreach(Item::getCreativeItems() as $i => $item){
|
||||
foreach(CreativeInventory::getAll() as $i => $item){
|
||||
$items[$i] = clone $item;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory\transaction\action;
|
||||
|
||||
use pocketmine\inventory\CreativeInventory;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Player;
|
||||
|
||||
@ -53,7 +54,7 @@ class CreativeInventoryAction extends InventoryAction{
|
||||
*/
|
||||
public function isValid(Player $source) : bool{
|
||||
return !$source->hasFiniteResources() and
|
||||
($this->actionType === self::TYPE_DELETE_ITEM or Item::getCreativeItemIndex($this->sourceItem) !== -1);
|
||||
($this->actionType === self::TYPE_DELETE_ITEM or CreativeInventory::getItemIndex($this->sourceItem) !== -1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,11 +48,8 @@ use pocketmine\utils\Binary;
|
||||
use function array_map;
|
||||
use function base64_decode;
|
||||
use function base64_encode;
|
||||
use function file_get_contents;
|
||||
use function get_class;
|
||||
use function hex2bin;
|
||||
use function json_decode;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
|
||||
class Item implements ItemIds, \JsonSerializable{
|
||||
public const TAG_ENCH = "ench";
|
||||
@ -92,66 +89,6 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
return ItemFactory::fromString($str);
|
||||
}
|
||||
|
||||
|
||||
/** @var Item[] */
|
||||
private static $creative = [];
|
||||
|
||||
public static function initCreativeItems(){
|
||||
self::clearCreativeItems();
|
||||
|
||||
$creativeItems = json_decode(file_get_contents(\pocketmine\RESOURCE_PATH . "vanilla" . DIRECTORY_SEPARATOR . "creativeitems.json"), true);
|
||||
|
||||
foreach($creativeItems as $data){
|
||||
$item = Item::jsonDeserialize($data);
|
||||
if($item->getName() === "Unknown"){
|
||||
continue;
|
||||
}
|
||||
self::addCreativeItem($item);
|
||||
}
|
||||
}
|
||||
|
||||
public static function clearCreativeItems(){
|
||||
Item::$creative = [];
|
||||
}
|
||||
|
||||
public static function getCreativeItems() : array{
|
||||
return Item::$creative;
|
||||
}
|
||||
|
||||
public static function addCreativeItem(Item $item){
|
||||
Item::$creative[] = clone $item;
|
||||
}
|
||||
|
||||
public static function removeCreativeItem(Item $item){
|
||||
$index = self::getCreativeItemIndex($item);
|
||||
if($index !== -1){
|
||||
unset(Item::$creative[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function isCreativeItem(Item $item) : bool{
|
||||
return Item::getCreativeItemIndex($item) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @return Item|null
|
||||
*/
|
||||
public static function getCreativeItem(int $index) : ?Item{
|
||||
return Item::$creative[$index] ?? null;
|
||||
}
|
||||
|
||||
public static function getCreativeItemIndex(Item $item) : int{
|
||||
foreach(Item::$creative as $i => $d){
|
||||
if($item->equals($d, !($item instanceof Durable))){
|
||||
return $i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** @var int */
|
||||
protected $id;
|
||||
/** @var int */
|
||||
|
Loading…
x
Reference in New Issue
Block a user