Merge branch 'minor-next' of github.com:pmmp/PocketMine-MP into major-next

This commit is contained in:
Dylan K. Taylor
2025-09-20 18:09:32 +01:00
38 changed files with 1088 additions and 120 deletions

View File

@@ -44,7 +44,7 @@
"pocketmine/locale-data": "~2.25.0",
"pocketmine/log": "^0.4.0",
"pocketmine/math": "dev-major-next as 1.0.0",
"pocketmine/nbt": "~1.1.0",
"pocketmine/nbt": "~1.2.0",
"pocketmine/raklib": "~1.2.0",
"pocketmine/raklib-ipc": "~1.0.0",
"pocketmine/snooze": "^0.5.0",

17
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "29c0ef75af9772a73a671e153ca912f7",
"content-hash": "2b45f6c98ece3e7043727ce0717b7c4b",
"packages": [
{
"name": "adhocore/json-comment",
@@ -576,16 +576,16 @@
},
{
"name": "pocketmine/nbt",
"version": "1.1.1",
"version": "1.2.0",
"source": {
"type": "git",
"url": "https://github.com/pmmp/NBT.git",
"reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1"
"reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pmmp/NBT/zipball/c3c7b0a7295daeaf7873d90fed5c5d10381d12e1",
"reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1",
"url": "https://api.github.com/repos/pmmp/NBT/zipball/51b8d6a97065fb93e0b4f660b65164b6e1ed2fff",
"reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff",
"shasum": ""
},
"require": {
@@ -595,7 +595,8 @@
},
"require-dev": {
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "2.1.0",
"phpstan/phpstan": "2.1.27",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.5"
},
@@ -612,9 +613,9 @@
"description": "PHP library for working with Named Binary Tags",
"support": {
"issues": "https://github.com/pmmp/NBT/issues",
"source": "https://github.com/pmmp/NBT/tree/1.1.1"
"source": "https://github.com/pmmp/NBT/tree/1.2.0"
},
"time": "2025-03-09T01:46:03+00:00"
"time": "2025-09-19T18:09:30+00:00"
},
{
"name": "pocketmine/raklib",

View File

@@ -69,9 +69,8 @@ class Banner extends Spawnable{
$patternTypeIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $nbt->getListTag(self::TAG_PATTERNS);
$patterns = $nbt->getListTag(self::TAG_PATTERNS, CompoundTag::class);
if($patterns !== null){
/** @var CompoundTag $pattern */
foreach($patterns as $pattern){
$patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error
$patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME));

View File

@@ -34,6 +34,7 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\world\World;
use function count;
@@ -86,13 +87,18 @@ class ChiseledBookshelf extends Tile implements ContainerTile{
}
protected function loadItems(CompoundTag $tag) : void{
if(($inventoryTag = $tag->getTag(ContainerTile::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){
try{
$inventoryTag = $tag->getListTag(ContainerTile::TAG_ITEMS, CompoundTag::class);
}catch(UnexpectedTagTypeException){
//preserve the old behaviour of not throwing on wrong types
$inventoryTag = null;
}
if($inventoryTag !== null){
$inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
$newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $slot => $itemNBT){
try{
$count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);

View File

@@ -31,6 +31,7 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\world\Position;
/**
@@ -43,13 +44,18 @@ trait ContainerTileTrait{
abstract public function getRealInventory() : Inventory;
protected function loadItems(CompoundTag $tag) : void{
if(($inventoryTag = $tag->getTag(ContainerTile::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){
try{
$inventoryTag = $tag->getListTag(ContainerTile::TAG_ITEMS, CompoundTag::class);
}catch(UnexpectedTagTypeException){
//preserve the old behaviour of not throwing on wrong types
$inventoryTag = null;
}
if($inventoryTag !== null){
$inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
$newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $itemNBT){
try{
$newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT);

View File

@@ -0,0 +1,45 @@
<?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\data\bedrock;
use pocketmine\item\FireworkRocketType;
use pocketmine\utils\SingletonTrait;
final class FireworkRocketTypeIdMap{
use SingletonTrait;
/** @phpstan-use IntSaveIdMapTrait<FireworkRocketType> */
use IntSaveIdMapTrait;
private function __construct(){
foreach(FireworkRocketType::cases() as $case){
$this->register(match($case){
FireworkRocketType::SMALL_BALL => FireworkRocketTypeIds::SMALL_BALL,
FireworkRocketType::LARGE_BALL => FireworkRocketTypeIds::LARGE_BALL,
FireworkRocketType::STAR => FireworkRocketTypeIds::STAR,
FireworkRocketType::CREEPER => FireworkRocketTypeIds::CREEPER,
FireworkRocketType::BURST => FireworkRocketTypeIds::BURST,
}, $case);
}
}
}

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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\data\bedrock;
final class FireworkRocketTypeIds{
public const SMALL_BALL = 0;
public const LARGE_BALL = 1;
public const STAR = 2;
public const CREEPER = 3;
public const BURST = 4;
}

View File

@@ -40,6 +40,7 @@ use pocketmine\data\bedrock\PotionTypeIdMap;
use pocketmine\data\bedrock\SuspiciousStewTypeIdMap;
use pocketmine\item\Banner;
use pocketmine\item\Dye;
use pocketmine\item\FireworkStar;
use pocketmine\item\GoatHorn;
use pocketmine\item\Item;
use pocketmine\item\Medicine;
@@ -246,6 +247,7 @@ final class ItemSerializerDeserializerRegistrar{
$this->map1to1Item(Ids::EYE_ARMOR_TRIM_SMITHING_TEMPLATE, Items::EYE_ARMOR_TRIM_SMITHING_TEMPLATE());
$this->map1to1Item(Ids::FEATHER, Items::FEATHER());
$this->map1to1Item(Ids::FERMENTED_SPIDER_EYE, Items::FERMENTED_SPIDER_EYE());
$this->map1to1Item(Ids::FIREWORK_ROCKET, Items::FIREWORK_ROCKET());
$this->map1to1Item(Ids::FIRE_CHARGE, Items::FIRE_CHARGE());
$this->map1to1Item(Ids::FISHING_ROD, Items::FISHING_ROD());
$this->map1to1Item(Ids::FLINT, Items::FLINT());
@@ -501,6 +503,14 @@ final class ItemSerializerDeserializerRegistrar{
* in a unified manner.
*/
private function register1to1ItemWithMetaMappings() : void{
$this->map1to1ItemWithMeta(
Ids::FIREWORK_STAR,
Items::FIREWORK_STAR(),
function(FireworkStar $item, int $meta) : void{
// Colors will be defined by CompoundTag deserialization.
},
fn(FireworkStar $item) => DyeColorIdMap::getInstance()->toInvertedId($item->getExplosion()->getFlashColor())
);
$this->map1to1ItemWithMeta(
Ids::GOAT_HORN,
Items::GOAT_HORN(),

View File

@@ -29,16 +29,14 @@ use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\SavedItemData;
use pocketmine\data\bedrock\item\SavedItemStackData;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\nbt\NBT;
use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\utils\Binary;
use function assert;
use function array_map;
final class ItemDataUpgrader{
private const TAG_LEGACY_ID = "id"; //TAG_Short (or TAG_String for Java itemstacks)
@@ -169,26 +167,6 @@ final class ItemDataUpgrader{
return new SavedItemData($newNameId, $newMeta, $blockStateData, $tag->getCompoundTag(SavedItemData::TAG_TAG));
}
/**
* @return string[]
* @throws SavedDataLoadingException
*/
private static function deserializeListOfStrings(?ListTag $list, string $tagName) : array{
if($list === null){
return [];
}
if($list->getTagType() !== NBT::TAG_String){
throw new SavedDataLoadingException("Unexpected type of list for tag '$tagName', expected TAG_String");
}
$result = [];
foreach($list as $item){
assert($item instanceof StringTag);
$result[] = $item->getValue();
}
return $result;
}
/**
* @throws SavedDataLoadingException
*/
@@ -205,8 +183,8 @@ final class ItemDataUpgrader{
//optional
$slot = ($slotTag = $tag->getTag(SavedItemStackData::TAG_SLOT)) instanceof ByteTag ? Binary::unsignByte($slotTag->getValue()) : null;
$wasPickedUp = ($wasPickedUpTag = $tag->getTag(SavedItemStackData::TAG_WAS_PICKED_UP)) instanceof ByteTag ? $wasPickedUpTag->getValue() : null;
$canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON);
$canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY);
$canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON, StringTag::class);
$canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY, StringTag::class);
}catch(NbtException $e){
throw new SavedDataLoadingException($e->getMessage(), 0, $e);
}
@@ -216,8 +194,8 @@ final class ItemDataUpgrader{
$count,
$slot,
$wasPickedUp !== 0,
self::deserializeListOfStrings($canPlaceOnList, SavedItemStackData::TAG_CAN_PLACE_ON),
self::deserializeListOfStrings($canDestroyList, SavedItemStackData::TAG_CAN_DESTROY)
$canPlaceOnList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canPlaceOnList->getValue()),
$canDestroyList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canDestroyList->getValue())
);
}

View File

@@ -492,7 +492,7 @@ abstract class Entity{
new FloatTag($this->location->pitch)
]));
if(!($this instanceof Player)){
if(!($this instanceof NeverSavedWithChunkEntity)){
EntityFactory::getInstance()->injectSaveId(get_class($this), $nbt);
if($this->getNameTag() !== ""){

View File

@@ -25,7 +25,6 @@ namespace pocketmine\entity;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\math\Vector3;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\FloatTag;
@@ -59,11 +58,10 @@ final class EntityDataHelper{
public static function parseLocation(CompoundTag $nbt, World $world) : Location{
$pos = self::parseVec3($nbt, Entity::TAG_POS, false);
$yawPitch = $nbt->getTag(Entity::TAG_ROTATION);
if(!($yawPitch instanceof ListTag) || $yawPitch->getTagType() !== NBT::TAG_Float){
$generic = $nbt->getTag(Entity::TAG_ROTATION);
if(!($generic instanceof ListTag) || ($yawPitch = $generic->cast(FloatTag::class)) === null){
throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List<Float>");
}
/** @var FloatTag[] $values */
$values = $yawPitch->getValue();
if(count($values) !== 2){
throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'");
@@ -78,14 +76,13 @@ final class EntityDataHelper{
* @throws SavedDataLoadingException
*/
public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{
$pos = $nbt->getTag($tagName);
if($pos === null && $optional){
$generic = $nbt->getTag($tagName);
if($generic === null && $optional){
return Vector3::zero();
}
if(!($pos instanceof ListTag) || ($pos->getTagType() !== NBT::TAG_Double && $pos->getTagType() !== NBT::TAG_Float)){
if(!($generic instanceof ListTag) || ($pos = $generic->cast(DoubleTag::class) ?? $generic->cast(FloatTag::class)) === null){
throw new SavedDataLoadingException("'$tagName' should be a List<Double> or List<Float>");
}
/** @var DoubleTag[]|FloatTag[] $values */
$values = $pos->getValue();
if(count($values) !== 3){
throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag");

View File

@@ -321,12 +321,11 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$this->enderInventory = new SimpleInventory(27);
$this->initHumanData($nbt);
$inventoryTag = $nbt->getListTag(self::TAG_INVENTORY);
$inventoryTag = $nbt->getListTag(self::TAG_INVENTORY, CompoundTag::class);
if($inventoryTag !== null){
$inventoryItems = [];
$armorInventoryItems = [];
/** @var CompoundTag $item */
foreach($inventoryTag as $i => $item){
$slot = $item->getByte(SavedItemStackData::TAG_SLOT);
if($slot >= 0 && $slot < 9){ //Hotbar
@@ -350,11 +349,10 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this)
)));
$enderChestInventoryTag = $nbt->getListTag(self::TAG_ENDER_CHEST_INVENTORY);
$enderChestInventoryTag = $nbt->getListTag(self::TAG_ENDER_CHEST_INVENTORY, CompoundTag::class);
if($enderChestInventoryTag !== null){
$enderChestInventoryItems = [];
/** @var CompoundTag $item */
foreach($enderChestInventoryTag as $i => $item){
$enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
}

View File

@@ -181,8 +181,7 @@ abstract class Living extends Entity{
$this->setAirSupplyTicks($nbt->getShort(self::TAG_BREATH_TICKS, self::DEFAULT_BREATH_TICKS));
/** @var CompoundTag[]|ListTag|null $activeEffectsTag */
$activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS);
$activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS, CompoundTag::class);
if($activeEffectsTag !== null){
foreach($activeEffectsTag as $e){
$effect = EffectIdMap::getInstance()->fromId($e->getByte(self::TAG_EFFECT_ID));

View File

@@ -0,0 +1,36 @@
<?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\entity;
/**
* Decorator for entities that will never be saved with a chunk.
* Entities implementing this interface are not required to register a save ID.
*
* This differs from {@link Entity::canSaveWithChunk()} because it can't be changed after the entity is created.
* We can't use canSaveWithChunk() to decide whether an entity needs a save ID, but we can use an interface like this.
* An attribute would also work, but `instanceof NonSaveable` is easier.
*/
interface NeverSavedWithChunkEntity{
}

View File

@@ -0,0 +1,41 @@
<?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\entity\animation;
use pocketmine\entity\object\FireworkRocket;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
use pocketmine\network\mcpe\protocol\types\ActorEvent;
final class FireworkParticlesAnimation implements Animation{
public function __construct(
private FireworkRocket $entity
){}
public function encode() : array{
return [
ActorEventPacket::create($this->entity->getId(), ActorEvent::FIREWORK_PARTICLES, 0)
];
}
}

View File

@@ -0,0 +1,204 @@
<?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\entity\object;
use pocketmine\entity\animation\FireworkParticlesAnimation;
use pocketmine\entity\Entity;
use pocketmine\entity\EntitySizeInfo;
use pocketmine\entity\Explosive;
use pocketmine\entity\Living;
use pocketmine\entity\Location;
use pocketmine\entity\NeverSavedWithChunkEntity;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\FireworkRocket as FireworkItem;
use pocketmine\item\FireworkRocketExplosion;
use pocketmine\math\VoxelRayTrace;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\network\mcpe\protocol\types\CacheableNbt;
use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
use pocketmine\utils\Utils;
use pocketmine\world\sound\FireworkCrackleSound;
use pocketmine\world\sound\FireworkLaunchSound;
use function count;
use function sqrt;
class FireworkRocket extends Entity implements Explosive, NeverSavedWithChunkEntity{
public function getNetworkTypeId() : string{ return EntityIds::FIREWORKS_ROCKET; }
protected int $maxFlightTimeTicks;
/** @var FireworkRocketExplosion[] */
protected array $explosions = [];
/**
* @param FireworkRocketExplosion[] $explosions
*/
public function __construct(Location $location, int $maxFlightTimeTicks, array $explosions, ?CompoundTag $nbt = null){
if($maxFlightTimeTicks < 0){
throw new \InvalidArgumentException("Life ticks cannot be negative");
}
$this->maxFlightTimeTicks = $maxFlightTimeTicks;
$this->setExplosions($explosions);
parent::__construct($location, $nbt);
}
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
protected function getInitialDragMultiplier() : float{ return 0.0; }
protected function getInitialGravity() : float{ return 0.0; }
/**
* Returns the total number of ticks the firework will fly for before it explodes.
*/
public function getMaxFlightTimeTicks() : int{
return $this->maxFlightTimeTicks;
}
/**
* Sets the total number of ticks the firework will fly for before it explodes.
*
* @return $this
*/
public function setMaxFlightTimeTicks(int $maxFlightTimeTicks) : self{
if($maxFlightTimeTicks < 0){
throw new \InvalidArgumentException("Max flight time ticks cannot be negative");
}
$this->maxFlightTimeTicks = $maxFlightTimeTicks;
return $this;
}
/**
* @return FireworkRocketExplosion[]
*/
public function getExplosions() : array{
return $this->explosions;
}
/**
* @param FireworkRocketExplosion[] $explosions
*
* @return $this
*/
public function setExplosions(array $explosions) : self{
Utils::validateArrayValueType($explosions, function(FireworkRocketExplosion $_) : void{});
$this->explosions = $explosions;
return $this;
}
protected function onFirstUpdate(int $currentTick) : void{
parent::onFirstUpdate($currentTick);
$this->broadcastSound(new FireworkLaunchSound());
}
protected function entityBaseTick(int $tickDiff = 1) : bool{
$hasUpdate = parent::entityBaseTick($tickDiff);
if(!$this->isFlaggedForDespawn()){
//Don't keep accelerating long-lived fireworks - this gets very rapidly out of control and makes the server
//die. Vanilla fireworks will only live for about 52 ticks maximum anyway, so this only makes sure plugin
//created fireworks don't murder the server
if($this->ticksLived < 60){
$this->addMotion($this->motion->x * 0.15, 0.04, $this->motion->z * 0.15);
}
if($this->ticksLived >= $this->maxFlightTimeTicks){
$this->flagForDespawn();
$this->explode();
}
}
return $hasUpdate;
}
public function explode() : void{
if(($explosionCount = count($this->explosions)) !== 0){
$this->broadcastAnimation(new FireworkParticlesAnimation($this));
foreach($this->explosions as $explosion){
$this->broadcastSound($explosion->getType()->getExplosionSound());
if($explosion->willTwinkle()){
$this->broadcastSound(new FireworkCrackleSound());
}
}
$force = ($explosionCount * 2) + 5;
$world = $this->getWorld();
foreach($world->getCollidingEntities($this->getBoundingBox()->expandedCopy(5, 5, 5), $this) as $entity){
if(!$entity instanceof Living){
continue;
}
$position = $entity->getPosition();
$distance = $position->distanceSquared($this->location);
if($distance > 25){
continue;
}
//cast two rays - one to the entity's feet and another to halfway up its body (according to Java, anyway)
//this seems like it'd miss some cases but who am I to argue with vanilla logic :>
$height = $entity->getBoundingBox()->getYLength();
for($i = 0; $i < 2; $i++){
$target = $position->add(0, 0.5 * $i * $height, 0);
foreach(VoxelRayTrace::betweenPoints($this->location, $target) as $blockPos){
if($world->getBlock($blockPos)->calculateIntercept($this->location, $target) !== null){
continue 2; //obstruction, try another path
}
}
//no obstruction
$damage = $force * sqrt((5 - $position->distance($this->location)) / 5);
$ev = new EntityDamageByEntityEvent($this, $entity, EntityDamageEvent::CAUSE_ENTITY_EXPLOSION, $damage);
$entity->attack($ev);
break;
}
}
}
}
public function canBeCollidedWith() : bool{
return false;
}
protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData($properties);
$explosions = new ListTag();
foreach($this->explosions as $explosion){
$explosions->push($explosion->toCompoundTag());
}
$fireworksData = CompoundTag::create()
->setTag(FireworkItem::TAG_FIREWORK_DATA, CompoundTag::create()
->setTag(FireworkItem::TAG_EXPLOSIONS, $explosions)
);
$properties->setCompoundTag(EntityMetadataProperties::FIREWORK_ITEM, new CacheableNbt($fireworksData));
}
}

View File

@@ -39,7 +39,6 @@ use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\math\RayTraceResult;
use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
@@ -81,12 +80,11 @@ abstract class Projectile extends Entity{
$this->setHealth(1);
$this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage);
if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS)) !== null){
if($stuckOnBlockPosTag->getTagType() !== NBT::TAG_Int || count($stuckOnBlockPosTag) !== 3){
if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS, IntTag::class)) !== null){
if(count($stuckOnBlockPosTag) !== 3){
throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int");
}
/** @var IntTag[] $values */
$values = $stuckOnBlockPosTag->getValue();
$this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue());

View File

@@ -26,6 +26,7 @@ namespace pocketmine\event\player;
use pocketmine\block\BlockTypeIds;
use pocketmine\entity\Living;
use pocketmine\entity\object\FallingBlock;
use pocketmine\entity\object\FireworkRocket;
use pocketmine\entity\projectile\Trident;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageByChildEntityEvent;
@@ -164,7 +165,9 @@ class PlayerDeathEvent extends EntityDeathEvent{
case EntityDamageEvent::CAUSE_ENTITY_EXPLOSION:
if($deathCause instanceof EntityDamageByEntityEvent){
$e = $deathCause->getDamager();
if($e instanceof Living){
if($e instanceof FireworkRocket){
return KnownTranslationFactory::death_attack_fireworks($name);
}elseif($e instanceof Living){
return KnownTranslationFactory::death_attack_explosion_player($name, $e->getDisplayName());
}
}

View File

@@ -29,7 +29,6 @@ use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\BannerPatternTypeIdMap;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use function count;
@@ -92,9 +91,8 @@ class Banner extends ItemBlockWallOrFloor{
$colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $tag->getListTag(self::TAG_PATTERNS);
if($patterns !== null && $patterns->getTagType() === NBT::TAG_Compound){
/** @var CompoundTag $t */
$patterns = $tag->getListTag(self::TAG_PATTERNS, CompoundTag::class);
if($patterns !== null){
foreach($patterns as $t){
$patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error
$patternType = $patternIdMap->fromId($t->getString(self::TAG_PATTERN_NAME));

142
src/item/FireworkRocket.php Normal file
View File

@@ -0,0 +1,142 @@
<?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\item;
use pocketmine\block\Block;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\entity\Location;
use pocketmine\entity\object\FireworkRocket as FireworkEntity;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\player\Player;
use pocketmine\utils\Utils;
use function array_map;
use function mt_rand;
class FireworkRocket extends Item{
public const TAG_FIREWORK_DATA = "Fireworks"; //TAG_Compound
protected const TAG_FLIGHT_TIME_MULTIPLIER = "Flight"; //TAG_Byte
public const TAG_EXPLOSIONS = "Explosions"; //TAG_List
protected int $flightTimeMultiplier = 1;
/** @var FireworkRocketExplosion[] */
protected array $explosions = [];
/**
* Returns the value that will be used to calculate a randomized flight duration
* for the firework (equals the amount of gunpowder used in crafting the rocket).
*
* The higher this value, the longer the flight duration.
*/
public function getFlightTimeMultiplier() : int{
return $this->flightTimeMultiplier;
}
/**
* Sets the value that will be used to calculate a randomized flight duration
* for the firework.
*
* The higher this value, the longer the flight duration.
*
* @return $this
*/
public function setFlightTimeMultiplier(int $multiplier) : self{
if($multiplier < 1 || $multiplier > 127){
throw new \InvalidArgumentException("Flight time multiplier must be in range 1-127");
}
$this->flightTimeMultiplier = $multiplier;
return $this;
}
/**
* @return FireworkRocketExplosion[]
*/
public function getExplosions() : array{
return $this->explosions;
}
/**
* @param FireworkRocketExplosion[] $explosions
*
* @return $this
*/
public function setExplosions(array $explosions) : self{
Utils::validateArrayValueType($explosions, function(FireworkRocketExplosion $_) : void{});
$this->explosions = $explosions;
return $this;
}
public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, array &$returnedItems) : ItemUseResult{
//TODO: this would be nicer if Vector3::getSide() accepted floats for distance
$position = $blockClicked->getPosition()->addVector($clickVector)->addVector(Vector3::zero()->getSide($face)->multiply(0.15));
$randomDuration = (($this->flightTimeMultiplier + 1) * 10) + mt_rand(0, 12);
$entity = new FireworkEntity(Location::fromObject($position, $player->getWorld(), Utils::getRandomFloat() * 360, 90), $randomDuration, $this->explosions);
$entity->setOwningEntity($player);
$entity->setMotion(new Vector3(
(Utils::getRandomFloat() - Utils::getRandomFloat()) * 0.0023,
0.05,
(Utils::getRandomFloat() - Utils::getRandomFloat()) * 0.0023
));
$entity->spawnToAll();
$this->pop();
return ItemUseResult::SUCCESS;
}
protected function deserializeCompoundTag(CompoundTag $tag) : void{
parent::deserializeCompoundTag($tag);
$fireworkData = $tag->getCompoundTag(self::TAG_FIREWORK_DATA);
if($fireworkData === null){
throw new SavedDataLoadingException("Missing firework data");
}
$this->setFlightTimeMultiplier($fireworkData->getByte(self::TAG_FLIGHT_TIME_MULTIPLIER, 1));
if(($explosions = $fireworkData->getListTag(self::TAG_EXPLOSIONS, CompoundTag::class)) !== null){
foreach($explosions as $explosion){
$this->explosions[] = FireworkRocketExplosion::fromCompoundTag($explosion);
}
}
}
protected function serializeCompoundTag(CompoundTag $tag) : void{
parent::serializeCompoundTag($tag);
$fireworkData = CompoundTag::create();
$fireworkData->setByte(self::TAG_FLIGHT_TIME_MULTIPLIER, $this->flightTimeMultiplier);
$fireworkData->setTag(self::TAG_EXPLOSIONS, new ListTag(array_map(fn(FireworkRocketExplosion $e) => $e->toCompoundTag(), $this->explosions)));
$tag->setTag(self::TAG_FIREWORK_DATA, $fireworkData);
}
}

View File

@@ -0,0 +1,190 @@
<?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\item;
use pocketmine\block\utils\DyeColor;
use pocketmine\color\Color;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\data\bedrock\FireworkRocketTypeIdMap;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\Utils;
use function array_key_first;
use function chr;
use function count;
use function ord;
use function strlen;
class FireworkRocketExplosion{
protected const TAG_TYPE = "FireworkType"; //TAG_Byte
protected const TAG_COLORS = "FireworkColor"; //TAG_ByteArray
protected const TAG_FADE_COLORS = "FireworkFade"; //TAG_ByteArray
protected const TAG_TWINKLE = "FireworkFlicker"; //TAG_Byte
protected const TAG_TRAIL = "FireworkTrail"; //TAG_Byte
/**
* @throws SavedDataLoadingException
*/
public static function fromCompoundTag(CompoundTag $tag) : self{
$colors = self::decodeColors($tag->getByteArray(self::TAG_COLORS));
if(count($colors) === 0){
throw new SavedDataLoadingException("Colors list cannot be empty");
}
return new self(
FireworkRocketTypeIdMap::getInstance()->fromId($tag->getByte(self::TAG_TYPE)) ?? throw new SavedDataLoadingException("Invalid firework type"),
$colors,
self::decodeColors($tag->getByteArray(self::TAG_FADE_COLORS)),
$tag->getByte(self::TAG_TWINKLE, 0) !== 0,
$tag->getByte(self::TAG_TRAIL, 0) !== 0
);
}
/**
* @return DyeColor[]
* @phpstan-return list<DyeColor>
* @throws SavedDataLoadingException
*/
protected static function decodeColors(string $colorsBytes) : array{
$colors = [];
$dyeColorIdMap = DyeColorIdMap::getInstance();
for($i = 0, $len = strlen($colorsBytes); $i < $len; $i++){
$colorByte = ord($colorsBytes[$i]);
$color = $dyeColorIdMap->fromInvertedId($colorByte);
if($color !== null){
$colors[] = $color;
}else{
throw new SavedDataLoadingException("Unknown color $colorByte");
}
}
return $colors;
}
/**
* @param DyeColor[] $colors
*/
protected static function encodeColors(array $colors) : string{
$colorsBytes = "";
$dyeColorIdMap = DyeColorIdMap::getInstance();
foreach($colors as $color){
$colorsBytes .= chr($dyeColorIdMap->toInvertedId($color));
}
return $colorsBytes;
}
/**
* @param DyeColor[] $colors
* @param DyeColor[] $fadeColors
* @phpstan-param non-empty-list<DyeColor> $colors
* @phpstan-param list<DyeColor> $fadeColors
*/
public function __construct(
protected FireworkRocketType $type,
protected array $colors,
protected array $fadeColors = [],
protected bool $twinkle = false,
protected bool $trail = false
){
if(count($colors) === 0){
throw new \InvalidArgumentException("Colors list cannot be empty");
}
$colorsValidator = function(DyeColor $_) : void{};
Utils::validateArrayValueType($colors, $colorsValidator);
Utils::validateArrayValueType($fadeColors, $colorsValidator);
}
public function getType() : FireworkRocketType{
return $this->type;
}
/**
* Returns the colors of the particles.
*
* @return DyeColor[]
* @phpstan-return non-empty-list<DyeColor>
*/
public function getColors() : array{
return $this->colors;
}
/**
* Returns the flash color of the explosion.
*/
public function getFlashColor() : DyeColor{
return $this->colors[array_key_first($this->colors)];
}
/**
* Returns the mixure of colors from {@link FireworkRocketExplosion::getColors()})
*/
public function getColorMix() : Color{
/** @var Color[] $colors */
$colors = [];
foreach($this->colors as $dyeColor){
$colors[] = $dyeColor->getRgbValue();
}
return Color::mix(...$colors);
}
/**
* Returns the colors to which the particles will change their color after a few seconds.
* If it is empty, there will be no color change in the particles.
*
* @return DyeColor[]
* @phpstan-return list<DyeColor>
*/
public function getFadeColors() : array{
return $this->fadeColors;
}
/**
* Returns whether the explosion has a flickering effect.
*/
public function willTwinkle() : bool{
return $this->twinkle;
}
/**
* Returns whether the particles have a trail effect.
*/
public function getTrail() : bool{
return $this->trail;
}
public function toCompoundTag() : CompoundTag{
return CompoundTag::create()
->setByte(self::TAG_TYPE, FireworkRocketTypeIdMap::getInstance()->toId($this->type))
->setByteArray(self::TAG_COLORS, self::encodeColors($this->colors))
->setByteArray(self::TAG_FADE_COLORS, self::encodeColors($this->fadeColors))
->setByte(self::TAG_TWINKLE, $this->twinkle ? 1 : 0)
->setByte(self::TAG_TRAIL, $this->trail ? 1 : 0);
}
}

View File

@@ -0,0 +1,46 @@
<?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\item;
use pocketmine\world\sound\FireworkExplosionSound;
use pocketmine\world\sound\FireworkLargeExplosionSound;
use pocketmine\world\sound\Sound;
enum FireworkRocketType{
case SMALL_BALL;
case LARGE_BALL;
case STAR;
case CREEPER;
case BURST;
public function getExplosionSound() : Sound{
return match($this){
self::SMALL_BALL,
self::STAR,
self::CREEPER,
self::BURST => new FireworkExplosionSound(),
self::LARGE_BALL => new FireworkLargeExplosionSound(),
};
}
}

112
src/item/FireworkStar.php Normal file
View File

@@ -0,0 +1,112 @@
<?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\item;
use pocketmine\block\utils\DyeColor;
use pocketmine\color\Color;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\Binary;
class FireworkStar extends Item{
protected const TAG_EXPLOSION = "FireworksItem"; //TAG_Compound
protected const TAG_CUSTOM_COLOR = "customColor"; //TAG_Int
protected FireworkRocketExplosion $explosion;
protected ?Color $customColor = null;
public function __construct(ItemIdentifier $identifier, string $name){
parent::__construct($identifier, $name);
$this->explosion = new FireworkRocketExplosion(
FireworkRocketType::SMALL_BALL,
colors: [DyeColor::BLACK],
fadeColors: [],
twinkle: false,
trail: false
);
}
public function getExplosion() : FireworkRocketExplosion{
return $this->explosion;
}
/** @return $this */
public function setExplosion(FireworkRocketExplosion $explosion) : self{
$this->explosion = $explosion;
return $this;
}
/**
* Returns the displayed color of the item.
* The mixture of explosion colors, or the custom color if it is set.
*/
public function getColor() : Color{
return $this->customColor ?? $this->explosion->getColorMix();
}
/**
* Returns the displayed custom color of the item that overrides
* the mixture of explosion colors, or null is it is not set.
*/
public function getCustomColor() : ?Color{
return $this->customColor;
}
/**
* Sets the displayed custom color of the item that overrides
* the mixture of explosion colors, or removes if $color is null.
*
* @return $this
*/
public function setCustomColor(?Color $color) : self{
$this->customColor = $color;
return $this;
}
protected function deserializeCompoundTag(CompoundTag $tag) : void{
parent::deserializeCompoundTag($tag);
$explosionTag = $tag->getTag(self::TAG_EXPLOSION);
if(!$explosionTag instanceof CompoundTag){
throw new SavedDataLoadingException("Missing explosion data");
}
$this->explosion = FireworkRocketExplosion::fromCompoundTag($explosionTag);
$customColor = Color::fromARGB(Binary::unsignInt($tag->getInt(self::TAG_CUSTOM_COLOR)));
$color = $this->explosion->getColorMix();
if(!$customColor->equals($color)){ //check that $customColor is actually custom.
$this->customColor = $customColor;
}
}
protected function serializeCompoundTag(CompoundTag $tag) : void{
parent::serializeCompoundTag($tag);
$tag->setTag(self::TAG_EXPLOSION, $this->explosion->toCompoundTag());
$tag->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($this->getColor()->toARGB()));
}
}

View File

@@ -297,9 +297,8 @@ class Item implements \JsonSerializable{
$display = $tag->getCompoundTag(self::TAG_DISPLAY);
if($display !== null){
$this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName);
$lore = $display->getListTag(self::TAG_DISPLAY_LORE);
if($lore !== null && $lore->getTagType() === NBT::TAG_String){
/** @var StringTag $t */
$lore = $display->getListTag(self::TAG_DISPLAY_LORE, StringTag::class);
if($lore !== null){
foreach($lore as $t){
$this->lore[] = $t->getValue();
}
@@ -307,9 +306,8 @@ class Item implements \JsonSerializable{
}
$this->removeEnchantments();
$enchantments = $tag->getListTag(self::TAG_ENCH);
if($enchantments !== null && $enchantments->getTagType() === NBT::TAG_Compound){
/** @var CompoundTag $enchantment */
$enchantments = $tag->getListTag(self::TAG_ENCH, CompoundTag::class);
if($enchantments !== null){
foreach($enchantments as $enchantment){
$magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1);
$level = $enchantment->getShort(self::TAG_ENCH_LVL, 0);
@@ -326,17 +324,15 @@ class Item implements \JsonSerializable{
$this->blockEntityTag = $tag->getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
$this->canPlaceOn = [];
$canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON);
if($canPlaceOn !== null && $canPlaceOn->getTagType() === NBT::TAG_String){
/** @var StringTag $entry */
$canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON, StringTag::class);
if($canPlaceOn !== null){
foreach($canPlaceOn as $entry){
$this->canPlaceOn[$entry->getValue()] = $entry->getValue();
}
}
$this->canDestroy = [];
$canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY);
if($canDestroy !== null && $canDestroy->getTagType() === NBT::TAG_String){
/** @var StringTag $entry */
$canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY, StringTag::class);
if($canDestroy !== null){
foreach($canDestroy as $entry){
$this->canDestroy[$entry->getValue()] = $entry->getValue();
}

View File

@@ -347,8 +347,10 @@ final class ItemTypeIds{
public const SPRUCE_HANGING_SIGN = 20308;
public const WARPED_HANGING_SIGN = 20309;
public const TRIDENT = 20310;
public const FIREWORK_ROCKET = 20311;
public const FIREWORK_STAR = 20312;
public const FIRST_UNUSED_ITEM_ID = 20311;
public const FIRST_UNUSED_ITEM_ID = 20313;
private static int $nextDynamicId = self::FIRST_UNUSED_ITEM_ID;

View File

@@ -1366,6 +1366,9 @@ final class StringToItemParser extends StringToTParser{
$result->register("eye_drops", fn() => Items::MEDICINE()->setType(MedicineType::EYE_DROPS));
$result->register("feather", fn() => Items::FEATHER());
$result->register("fermented_spider_eye", fn() => Items::FERMENTED_SPIDER_EYE());
$result->register("firework_rocket", fn() => Items::FIREWORK_ROCKET());
$result->register("firework_star", fn() => Items::FIREWORK_STAR());
$result->register("fireworks", fn() => Items::FIREWORK_ROCKET());
$result->register("fire_charge", fn() => Items::FIRE_CHARGE());
$result->register("fish", fn() => Items::RAW_FISH());
$result->register("fishing_rod", fn() => Items::FISHING_ROD());

View File

@@ -168,6 +168,8 @@ use function strtolower;
* @method static Item EYE_ARMOR_TRIM_SMITHING_TEMPLATE()
* @method static Item FEATHER()
* @method static Item FERMENTED_SPIDER_EYE()
* @method static FireworkRocket FIREWORK_ROCKET()
* @method static FireworkStar FIREWORK_STAR()
* @method static FireCharge FIRE_CHARGE()
* @method static FishingRod FISHING_ROD()
* @method static Item FLINT()
@@ -511,6 +513,8 @@ final class VanillaItems{
self::register("experience_bottle", fn(IID $id) => new ExperienceBottle($id, "Bottle o' Enchanting"));
self::register("feather", fn(IID $id) => new Item($id, "Feather"));
self::register("fermented_spider_eye", fn(IID $id) => new Item($id, "Fermented Spider Eye"));
self::register("firework_rocket", fn(IID $id) => new FireworkRocket($id, "Firework Rocket"));
self::register("firework_star", fn(IID $id) => new FireworkStar($id, "Firework Star"));
self::register("fire_charge", fn(IID $id) => new FireCharge($id, "Fire Charge"));
self::register("fishing_rod", fn(IID $id) => new FishingRod($id, "Fishing Rod", [EnchantmentTags::FISHING_ROD]));
self::register("flint", fn(IID $id) => new Item($id, "Flint"));

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
@@ -167,14 +166,12 @@ abstract class WritableBookBase extends Item{
$pages = $tag->getListTag(self::TAG_PAGES);
if($pages !== null){
if($pages->getTagType() === NBT::TAG_Compound){ //PE format
/** @var CompoundTag $page */
foreach($pages as $page){
if(($compoundPages = $pages->cast(CompoundTag::class)) !== null){ //PE format
foreach($compoundPages as $page){
$this->pages[] = new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT), 'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME, ""));
}
}elseif($pages->getTagType() === NBT::TAG_String){ //PC format
/** @var StringTag $page */
foreach($pages as $page){
}elseif(($stringPages = $pages->cast(StringTag::class)) !== null){ //PC format
foreach($stringPages as $page){
$this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8'));
}
}

View File

@@ -42,6 +42,7 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\Tag;
use pocketmine\nbt\TreeRoot;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\GameMode as ProtocolGameMode;
@@ -225,15 +226,14 @@ class TypeConverter{
* We don't need to, and should not allow, sending nested inventories across the network.
*/
protected function stripContainedItemNonVisualNBT(CompoundTag $tag) : bool{
if(
($blockEntityInventoryTag = $tag->getTag(ContainerTile::TAG_ITEMS)) !== null &&
$blockEntityInventoryTag instanceof ListTag &&
$blockEntityInventoryTag->getTagType() === NBT::TAG_Compound &&
$blockEntityInventoryTag->count() > 0
){
try{
$blockEntityInventoryTag = $tag->getListTag(ContainerTile::TAG_ITEMS, CompoundTag::class);
}catch(UnexpectedTagTypeException){
return false;
}
if($blockEntityInventoryTag !== null && $blockEntityInventoryTag->count() > 0){
$stripped = new ListTag();
/** @var CompoundTag $itemTag */
foreach($blockEntityInventoryTag as $itemTag){
try{
$containedItem = Item::nbtDeserialize($itemTag);

View File

@@ -42,6 +42,7 @@ use pocketmine\entity\Entity;
use pocketmine\entity\Human;
use pocketmine\entity\Living;
use pocketmine\entity\Location;
use pocketmine\entity\NeverSavedWithChunkEntity;
use pocketmine\entity\object\ItemEntity;
use pocketmine\entity\projectile\Arrow;
use pocketmine\entity\Skin;
@@ -170,7 +171,7 @@ use const PHP_INT_MAX;
/**
* Main class that handles networking, recovery, and packet sending to the server part
*/
class Player extends Human implements CommandSender, ChunkListener, IPlayer{
class Player extends Human implements CommandSender, ChunkListener, IPlayer, NeverSavedWithChunkEntity{
use PermissibleDelegateTrait;
private const MOVES_PER_TICK = 2;

View File

@@ -42,6 +42,7 @@ use pocketmine\data\SavedDataLoadingException;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\NeverSavedWithChunkEntity;
use pocketmine\entity\object\ExperienceOrb;
use pocketmine\entity\object\ItemEntity;
use pocketmine\event\block\BlockBreakEvent;
@@ -2757,7 +2758,7 @@ class World implements ChunkManager{
throw new AssumptionFailedError("Found two different entities sharing entity ID " . $entity->getId());
}
}
if(!EntityFactory::getInstance()->isRegistered($entity::class) && !$entity instanceof Player){
if(!EntityFactory::getInstance()->isRegistered($entity::class) && !$entity instanceof NeverSavedWithChunkEntity){
//canSaveWithChunk is mutable, so that means it could be toggled after adding the entity and cause a crash
//later on. Better we just force all entities to have a save ID, even if it might not be needed.
throw new \LogicException("Entity " . $entity::class . " is not registered for a save ID in EntityFactory");

View File

@@ -87,13 +87,11 @@ trait LegacyAnvilChunkTrait{
}
$subChunks = [];
$subChunksTag = $chunk->getListTag("Sections") ?? [];
$subChunksTag = $chunk->getListTag("Sections", CompoundTag::class) ?? [];
foreach($subChunksTag as $subChunk){
if($subChunk instanceof CompoundTag){
$y = $subChunk->getByte("Y");
$subChunks[$y] = $this->deserializeSubChunk($subChunk, clone $biomes3d, new \PrefixedLogger($logger, "Subchunk y=$y"));
}
}
for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; ++$y){
if(!isset($subChunks[$y])){
$subChunks[$y] = new SubChunk(Block::EMPTY_STATE_ID, [], clone $biomes3d);

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\world\format\io\region;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
@@ -164,21 +163,11 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
* @throws CorruptedChunkException
*/
protected static function getCompoundList(string $context, ListTag $list) : array{
if($list->count() === 0){ //empty lists might have wrong types, we don't care
return [];
}
if($list->getTagType() !== NBT::TAG_Compound){
$compoundList = $list->cast(CompoundTag::class);
if($compoundList === null){
throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'");
}
$result = [];
foreach($list as $tag){
if(!($tag instanceof CompoundTag)){
//this should never happen, but it's still possible due to lack of native type safety
throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'");
}
$result[] = $tag;
}
return $result;
return $compoundList->getValue();
}
protected static function readFixedSizeByteArray(CompoundTag $chunk, string $tagName, int $length) : string{

View File

@@ -0,0 +1,35 @@
<?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\world\sound;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
class FireworkCrackleSound implements Sound{
public function encode(Vector3 $pos) : array{
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::TWINKLE, $pos, false)];
}
}

View File

@@ -0,0 +1,35 @@
<?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\world\sound;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
class FireworkExplosionSound implements Sound{
public function encode(Vector3 $pos) : array{
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::BLAST, $pos, false)];
}
}

View File

@@ -0,0 +1,35 @@
<?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\world\sound;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
class FireworkLargeExplosionSound implements Sound{
public function encode(Vector3 $pos) : array{
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::LARGE_BLAST, $pos, false)];
}
}

View File

@@ -0,0 +1,35 @@
<?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\world\sound;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
class FireworkLaunchSound implements Sound{
public function encode(Vector3 $pos) : array{
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::LAUNCH, $pos, false)];
}
}

View File

@@ -37,7 +37,6 @@ use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\ItemTypeNames;
use pocketmine\inventory\json\CreativeGroupData;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\TreeRoot;
@@ -554,8 +553,8 @@ class ParserPacketHandler extends PacketHandler{
if(!($tag instanceof CompoundTag)){
throw new AssumptionFailedError();
}
$idList = $tag->getTag("idlist");
if(!($idList instanceof ListTag) || $idList->getTagType() !== NBT::TAG_Compound){
$generic = $tag->getTag("idlist");
if(!($generic instanceof ListTag) || ($idList = $generic->cast(CompoundTag::class)) === null){
echo $tag . "\n";
throw new \RuntimeException("expected TAG_List<TAG_Compound>(\"idlist\") tag inside root TAG_Compound");
}
@@ -565,9 +564,6 @@ class ParserPacketHandler extends PacketHandler{
}
echo "updating legacy => string entity ID mapping table\n";
$map = [];
/**
* @var CompoundTag $thing
*/
foreach($idList as $thing){
$map[$thing->getString("id")] = $thing->getInt("rid");
}