Added Level->dropExperience() and API for splitting XP into orb sizes

This commit is contained in:
Dylan K. Taylor
2018-01-04 19:32:54 +00:00
parent a84910f04c
commit 0f30467f62
2 changed files with 78 additions and 0 deletions

View File

@ -31,6 +31,7 @@ use pocketmine\block\BlockFactory;
use pocketmine\entity\Effect;
use pocketmine\entity\Entity;
use pocketmine\entity\Item as DroppedItem;
use pocketmine\entity\object\ExperienceOrb;
use pocketmine\entity\projectile\Arrow;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
@ -1699,6 +1700,41 @@ class Level implements ChunkManager, Metadatable{
return null;
}
/**
* Drops XP orbs into the world for the specified amount, splitting the amount into several orbs if necessary.
*
* @param Vector3 $pos
* @param int $amount
*
* @return ExperienceOrb[]
*/
public function dropExperience(Vector3 $pos, int $amount) : array{
/** @var ExperienceOrb[] $orbs */
$orbs = [];
foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){
$nbt = Entity::createBaseNBT(
$pos,
$this->temporalVector->setComponents((lcg_value() * 0.2 - 0.1) * 2, lcg_value() * 0.4, (lcg_value() * 0.2 - 0.1) * 2),
lcg_value() * 360,
0
);
$nbt->setShort(ExperienceOrb::TAG_VALUE_PC, $split);
$orb = Entity::createEntity("XPOrb", $this, $nbt);
if($orb === null){
continue;
}
$orb->spawnToAll();
if($orb instanceof ExperienceOrb){
$orbs[] = $orb;
}
}
return $orbs;
}
/**
* Checks if the level spawn protection radius will prevent the player from using items or building at the specified
* Vector3 position.