Added a helper function Entity->createBaseNBT() to cut down on boilerplate code

This commit is contained in:
Dylan K. Taylor
2017-10-19 17:36:51 +01:00
parent 67c6fca0ed
commit 50be26958a
7 changed files with 69 additions and 141 deletions

View File

@ -28,11 +28,6 @@ use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\EntityShootBowEvent;
use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\level\sound\LaunchSound;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\Player;
class Bow extends Tool{
@ -54,26 +49,13 @@ class Bow extends Tool{
return false;
}
$directionVector = $player->getDirectionVector();
$nbt = new CompoundTag("", [
new ListTag("Pos", [
new DoubleTag("", $player->x),
new DoubleTag("", $player->y + $player->getEyeHeight()),
new DoubleTag("", $player->z)
]),
new ListTag("Motion", [
new DoubleTag("", $directionVector->x),
new DoubleTag("", $directionVector->y),
new DoubleTag("", $directionVector->z)
]),
new ListTag("Rotation", [
//yaw/pitch for arrows taken crosswise, not along the arrow shaft.
new FloatTag("", ($player->yaw > 180 ? 360 : 0) - $player->yaw), //arrow yaw must range from -180 to +180
new FloatTag("", -$player->pitch)
]),
new ShortTag("Fire", $player->isOnFire() ? 45 * 60 : 0)
]);
$nbt = Entity::createBaseNBT(
$player->add(0, $player->getEyeHeight(), 0),
$player->getDirectionVector(),
($player->yaw > 180 ? 360 : 0) - $player->yaw,
-$player->pitch
);
$nbt->setShort("Fire", $player->isOnFire() ? 45 * 60 : 0);
$diff = $player->getItemUseDuration();
$p = $diff / 20;

View File

@ -28,10 +28,6 @@ use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\level\sound\LaunchSound;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\Player;
abstract class ProjectileItem extends Item{
@ -41,25 +37,12 @@ abstract class ProjectileItem extends Item{
abstract public function getThrowForce() : float;
public function onClickAir(Player $player, Vector3 $directionVector) : bool{
$nbt = new CompoundTag("", [
new ListTag("Pos", [
new DoubleTag("", $player->x),
new DoubleTag("", $player->y + $player->getEyeHeight()),
new DoubleTag("", $player->z)
]),
new ListTag("Motion", [
new DoubleTag("", $directionVector->x),
new DoubleTag("", $directionVector->y),
new DoubleTag("", $directionVector->z)
]),
new ListTag("Rotation", [
new FloatTag("", $player->yaw),
new FloatTag("", $player->pitch)
]),
]);
$nbt = Entity::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $directionVector, $player->yaw, $player->pitch);
$projectile = Entity::createEntity($this->getProjectileEntityType(), $player->getLevel(), $nbt, $player);
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
if($projectile !== null){
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
}
$this->count--;

View File

@ -27,11 +27,6 @@ use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
class SpawnEgg extends Item{
@ -40,25 +35,10 @@ class SpawnEgg extends Item{
}
public function onActivate(Level $level, Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos) : bool{
$nbt = new CompoundTag("", [
new ListTag("Pos", [
new DoubleTag("", $blockReplace->getX() + 0.5),
new DoubleTag("", $blockReplace->getY()),
new DoubleTag("", $blockReplace->getZ() + 0.5)
]),
new ListTag("Motion", [
new DoubleTag("", 0),
new DoubleTag("", 0),
new DoubleTag("", 0)
]),
new ListTag("Rotation", [
new FloatTag("", lcg_value() * 360),
new FloatTag("", 0)
]),
]);
$nbt = Entity::createBaseNBT($blockReplace->add(0.5, 0, 0.5), null, lcg_value() * 360, 0);
if($this->hasCustomName()){
$nbt->CustomName = new StringTag("CustomName", $this->getCustomName());
$nbt->setString("CustomName", $this->getCustomName());
}
$entity = Entity::createEntity($this->meta, $level, $nbt);