ExperienceOrb: Require providing XP value in constructor

This commit is contained in:
Dylan K. Taylor 2021-06-19 19:42:13 +01:00
parent 981b0285d1
commit fc70b625b3
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 17 additions and 13 deletions

View File

@ -47,6 +47,7 @@ use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
@ -93,7 +94,14 @@ final class EntityFactory{
}, ['ThrownExpBottle', 'minecraft:xp_bottle'], EntityLegacyIds::XP_BOTTLE);
$this->register(ExperienceOrb::class, function(World $world, CompoundTag $nbt) : ExperienceOrb{
return new ExperienceOrb(EntityDataHelper::parseLocation($nbt, $world), $nbt);
$value = 1;
if(($valuePcTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PC)) instanceof ShortTag){ //PC
$value = $valuePcTag->getValue();
}elseif(($valuePeTag = $nbt->getTag(ExperienceOrb::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
$value = $valuePeTag->getValue();
}
return new ExperienceOrb(EntityDataHelper::parseLocation($nbt, $world), $value, $nbt);
}, ['XPOrb', 'minecraft:xp_orb'], EntityLegacyIds::XP_ORB);
$this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{

View File

@ -26,6 +26,7 @@ namespace pocketmine\entity\object;
use pocketmine\entity\Entity;
use pocketmine\entity\EntitySizeInfo;
use pocketmine\entity\Human;
use pocketmine\entity\Location;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
@ -98,7 +99,12 @@ class ExperienceOrb extends Entity{
protected $targetPlayerRuntimeId = null;
/** @var int */
protected $xpValue = 1;
protected $xpValue;
public function __construct(Location $location, int $xpValue, ?CompoundTag $nbt = null){
$this->xpValue = $xpValue;
parent::__construct($location, $nbt);
}
protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
@ -106,15 +112,6 @@ class ExperienceOrb extends Entity{
parent::initEntity($nbt);
$this->age = $nbt->getShort("Age", 0);
$value = 1;
if(($valuePcTag = $nbt->getTag(self::TAG_VALUE_PC)) instanceof ShortTag){ //PC
$value = $valuePcTag->getValue();
}elseif(($valuePeTag = $nbt->getTag(self::TAG_VALUE_PE)) instanceof IntTag){ //PE save format
$value = $valuePeTag->getValue();
}
$this->setXpValue($value);
}
public function saveNBT() : CompoundTag{

View File

@ -1616,9 +1616,8 @@ class World implements ChunkManager{
$orbs = [];
foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){
$orb = new ExperienceOrb(Location::fromObject($pos, $this, lcg_value() * 360, 0));
$orb = new ExperienceOrb(Location::fromObject($pos, $this, lcg_value() * 360, 0), $split);
$orb->setXpValue($split);
$orb->setMotion(new Vector3((lcg_value() * 0.2 - 0.1) * 2, lcg_value() * 0.4, (lcg_value() * 0.2 - 0.1) * 2));
$orb->spawnToAll();