mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-25 12:54:03 +00:00
fix a bunch of bugs
This commit is contained in:
parent
5ed75731f2
commit
6964012464
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\block\tile;
|
namespace pocketmine\block\tile;
|
||||||
|
|
||||||
|
use pocketmine\data\SavedDataLoadingException;
|
||||||
use pocketmine\inventory\Inventory;
|
use pocketmine\inventory\Inventory;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\nbt\NBT;
|
use pocketmine\nbt\NBT;
|
||||||
@ -49,7 +50,13 @@ trait ContainerTrait{
|
|||||||
$newContents = [];
|
$newContents = [];
|
||||||
/** @var CompoundTag $itemNBT */
|
/** @var CompoundTag $itemNBT */
|
||||||
foreach($inventoryTag as $itemNBT){
|
foreach($inventoryTag as $itemNBT){
|
||||||
|
try{
|
||||||
$newContents[$itemNBT->getByte("Slot")] = Item::nbtDeserialize($itemNBT);
|
$newContents[$itemNBT->getByte("Slot")] = Item::nbtDeserialize($itemNBT);
|
||||||
|
}catch(SavedDataLoadingException $e){
|
||||||
|
//TODO: not the best solution
|
||||||
|
\GlobalLogger::get()->logException($e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$inventory->setContents($newContents);
|
$inventory->setContents($newContents);
|
||||||
|
|
||||||
|
@ -60,4 +60,6 @@ final class BlockDataUpgrader{
|
|||||||
|
|
||||||
return $this->blockStateUpgrader->upgrade($blockStateData);
|
return $this->blockStateUpgrader->upgrade($blockStateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getBlockStateUpgrader() : BlockStateUpgrader{ return $this->blockStateUpgrader; }
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,9 @@ final class ItemDataUpgrader{
|
|||||||
ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
|
ksort($this->idMetaUpgradeSchemas, SORT_NUMERIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws SavedDataLoadingException
|
||||||
|
*/
|
||||||
private function upgradeItemTypeNbt(CompoundTag $tag) : SavedItemData{
|
private function upgradeItemTypeNbt(CompoundTag $tag) : SavedItemData{
|
||||||
if(($nameIdTag = $tag->getTag(SavedItemData::TAG_NAME)) instanceof StringTag){
|
if(($nameIdTag = $tag->getTag(SavedItemData::TAG_NAME)) instanceof StringTag){
|
||||||
//Bedrock 1.6+
|
//Bedrock 1.6+
|
||||||
@ -98,6 +101,11 @@ final class ItemDataUpgrader{
|
|||||||
}elseif(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !== null){
|
}elseif(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !== null){
|
||||||
//this is a legacy blockitem represented by ID + meta
|
//this is a legacy blockitem represented by ID + meta
|
||||||
$blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
|
$blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
|
||||||
|
if($blockStateData === null){
|
||||||
|
throw new SavedDataLoadingException("Expected a blockstate to be associated with this block");
|
||||||
|
}
|
||||||
|
//the block data upgrader returns states from 1.18.10, which need to be updated to the current version the usual way
|
||||||
|
$blockStateData = $this->blockDataUpgrader->getBlockStateUpgrader()->upgrade($blockStateData);
|
||||||
}else{
|
}else{
|
||||||
//probably a standard item
|
//probably a standard item
|
||||||
$blockStateData = null;
|
$blockStateData = null;
|
||||||
@ -112,6 +120,7 @@ final class ItemDataUpgrader{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
|
* @throws SavedDataLoadingException
|
||||||
*/
|
*/
|
||||||
private static function deserializeListOfStrings(?ListTag $list, string $tagName) : array{
|
private static function deserializeListOfStrings(?ListTag $list, string $tagName) : array{
|
||||||
if($list === null){
|
if($list === null){
|
||||||
@ -129,6 +138,9 @@ final class ItemDataUpgrader{
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws SavedDataLoadingException
|
||||||
|
*/
|
||||||
public function upgradeItemStackNbt(CompoundTag $tag) : SavedItemStackData{
|
public function upgradeItemStackNbt(CompoundTag $tag) : SavedItemStackData{
|
||||||
$savedItemData = $this->upgradeItemTypeNbt($tag);
|
$savedItemData = $this->upgradeItemTypeNbt($tag);
|
||||||
try{
|
try{
|
||||||
@ -169,6 +181,6 @@ final class ItemDataUpgrader{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$id, $meta];
|
return [$newId, $newMeta];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,10 @@ final class ItemIdMetaUpgradeSchema{
|
|||||||
public function getPriority() : int{ return $this->priority; }
|
public function getPriority() : int{ return $this->priority; }
|
||||||
|
|
||||||
public function renameId(string $id) : ?string{
|
public function renameId(string $id) : ?string{
|
||||||
return $this->renamedIds[$id] ?? null;
|
return $this->renamedIds[mb_strtolower($id, 'US-ASCII')] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function remapMeta(string $id, int $meta) : ?string{
|
public function remapMeta(string $id, int $meta) : ?string{
|
||||||
return $this->remappedMetas[$id][$meta] ?? null;
|
return $this->remappedMetas[mb_strtolower($id, 'US-ASCII')][$meta] ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,20 +68,34 @@ final class R12ItemIdToBlockIdMap{
|
|||||||
return new self($builtMap);
|
return new self($builtMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
* @phpstan-var array<string, string>
|
||||||
|
*/
|
||||||
|
private array $itemToBlock = [];
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
* @phpstan-var array<string, string>
|
||||||
|
*/
|
||||||
|
private array $blockToItem = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string[] $itemToBlock
|
* @param string[] $itemToBlock
|
||||||
* @phpstan-param array<string, string> $itemToBlock
|
* @phpstan-param array<string, string> $itemToBlock
|
||||||
*/
|
*/
|
||||||
public function __construct(private array $itemToBlock){}
|
public function __construct(array $itemToBlock){
|
||||||
|
foreach($itemToBlock as $itemId => $blockId){
|
||||||
|
$this->itemToBlock[mb_strtolower($itemId, 'US-ASCII')] = $blockId;
|
||||||
|
$this->blockToItem[mb_strtolower($blockId, 'US-ASCII')] = $itemId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function itemIdToBlockId(string $itemId) : ?string{
|
public function itemIdToBlockId(string $itemId) : ?string{
|
||||||
return $this->itemToBlock[$itemId] ?? null;
|
return $this->itemToBlock[mb_strtolower($itemId, 'US-ASCII')] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function blockIdToItemId(string $blockId) : ?string{
|
public function blockIdToItemId(string $blockId) : ?string{
|
||||||
//we don't need this for any functionality, so we're not concerned about performance here
|
//we don't need this for any functionality, but it might be nice to have for debugging
|
||||||
//however, it might be nice to have for debugging
|
return $this->blockToItem[mb_strtolower($blockId, 'US-ASCII')] ?? null;
|
||||||
$itemId = array_search($blockId, $this->itemToBlock, true);
|
|
||||||
return $itemId !== false ? $itemId : null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ use pocketmine\block\BlockBreakInfo;
|
|||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
use pocketmine\block\VanillaBlocks;
|
use pocketmine\block\VanillaBlocks;
|
||||||
use pocketmine\data\bedrock\EnchantmentIdMap;
|
use pocketmine\data\bedrock\EnchantmentIdMap;
|
||||||
|
use pocketmine\data\bedrock\item\ItemTypeDeserializeException;
|
||||||
use pocketmine\data\bedrock\item\SavedItemStackData;
|
use pocketmine\data\bedrock\item\SavedItemStackData;
|
||||||
use pocketmine\data\SavedDataLoadingException;
|
use pocketmine\data\SavedDataLoadingException;
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
@ -661,13 +662,16 @@ class Item implements \JsonSerializable{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserializes an Item from an NBT CompoundTag
|
* Deserializes an Item from an NBT CompoundTag
|
||||||
* @throws NbtException
|
|
||||||
* @throws SavedDataLoadingException
|
* @throws SavedDataLoadingException
|
||||||
*/
|
*/
|
||||||
public static function nbtDeserialize(CompoundTag $tag) : Item{
|
public static function nbtDeserialize(CompoundTag $tag) : Item{
|
||||||
$itemData = GlobalItemDataHandlers::getUpgrader()->upgradeItemStackNbt($tag);
|
$itemData = GlobalItemDataHandlers::getUpgrader()->upgradeItemStackNbt($tag);
|
||||||
|
|
||||||
|
try{
|
||||||
$item = GlobalItemDataHandlers::getDeserializer()->deserialize($itemData->getTypeData());
|
$item = GlobalItemDataHandlers::getDeserializer()->deserialize($itemData->getTypeData());
|
||||||
|
}catch(ItemTypeDeserializeException $e){
|
||||||
|
throw new SavedDataLoadingException($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
$item->setCount($itemData->getCount());
|
$item->setCount($itemData->getCount());
|
||||||
if(($tagTag = $itemData->getTypeData()->getTag()) !== null){
|
if(($tagTag = $itemData->getTypeData()->getTag()) !== null){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user