mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-12 00:39:45 +00:00
ProjectileItem: get NBT as far away as possible
This commit is contained in:
parent
b3df5f4e95
commit
0ae357cf8f
@ -23,7 +23,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\Egg as EggEntity;
|
use pocketmine\entity\projectile\Egg as EggEntity;
|
||||||
|
use pocketmine\entity\projectile\Throwable;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
class Egg extends ProjectileItem{
|
class Egg extends ProjectileItem{
|
||||||
|
|
||||||
@ -31,8 +36,15 @@ class Egg extends ProjectileItem{
|
|||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProjectileEntityClass() : string{
|
protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable{
|
||||||
return EggEntity::class;
|
/** @var EggEntity $projectile */
|
||||||
|
$projectile = $factory->create(
|
||||||
|
EggEntity::class,
|
||||||
|
$location->getWorldNonNull(),
|
||||||
|
EntityFactory::createBaseNBT($location, $velocity, $location->yaw, $location->pitch),
|
||||||
|
$thrower
|
||||||
|
);
|
||||||
|
return $projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThrowForce() : float{
|
public function getThrowForce() : float{
|
||||||
|
@ -23,7 +23,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\EnderPearl as EnderPearlEntity;
|
use pocketmine\entity\projectile\EnderPearl as EnderPearlEntity;
|
||||||
|
use pocketmine\entity\projectile\Throwable;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
class EnderPearl extends ProjectileItem{
|
class EnderPearl extends ProjectileItem{
|
||||||
|
|
||||||
@ -31,8 +36,15 @@ class EnderPearl extends ProjectileItem{
|
|||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProjectileEntityClass() : string{
|
protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable{
|
||||||
return EnderPearlEntity::class;
|
/** @var EnderPearlEntity $projectile */
|
||||||
|
$projectile = $factory->create(
|
||||||
|
EnderPearlEntity::class,
|
||||||
|
$location->getWorldNonNull(),
|
||||||
|
EntityFactory::createBaseNBT($location, $velocity, $location->yaw, $location->pitch),
|
||||||
|
$thrower
|
||||||
|
);
|
||||||
|
return $projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThrowForce() : float{
|
public function getThrowForce() : float{
|
||||||
|
@ -23,12 +23,24 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\ExperienceBottle as ExperienceBottleEntity;
|
use pocketmine\entity\projectile\ExperienceBottle as ExperienceBottleEntity;
|
||||||
|
use pocketmine\entity\projectile\Throwable;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
class ExperienceBottle extends ProjectileItem{
|
class ExperienceBottle extends ProjectileItem{
|
||||||
|
|
||||||
public function getProjectileEntityClass() : string{
|
protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable{
|
||||||
return ExperienceBottleEntity::class;
|
/** @var ExperienceBottleEntity $projectile */
|
||||||
|
$projectile = $factory->create(
|
||||||
|
ExperienceBottleEntity::class,
|
||||||
|
$location->getWorldNonNull(),
|
||||||
|
EntityFactory::createBaseNBT($location, $velocity, $location->yaw, $location->pitch),
|
||||||
|
$thrower
|
||||||
|
);
|
||||||
|
return $projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThrowForce() : float{
|
public function getThrowForce() : float{
|
||||||
|
@ -24,43 +24,23 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
use pocketmine\entity\EntityFactory;
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\Throwable;
|
use pocketmine\entity\projectile\Throwable;
|
||||||
use pocketmine\event\entity\ProjectileLaunchEvent;
|
use pocketmine\event\entity\ProjectileLaunchEvent;
|
||||||
use pocketmine\math\Vector3;
|
use pocketmine\math\Vector3;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
use pocketmine\utils\Utils;
|
|
||||||
use pocketmine\world\sound\ThrowSound;
|
use pocketmine\world\sound\ThrowSound;
|
||||||
|
|
||||||
abstract class ProjectileItem extends Item{
|
abstract class ProjectileItem extends Item{
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the entity type that this projectile creates. This should return a ::class extending Throwable.
|
|
||||||
*
|
|
||||||
* @return string class extends Throwable
|
|
||||||
* @phpstan-return class-string<Throwable>
|
|
||||||
*/
|
|
||||||
abstract public function getProjectileEntityClass() : string;
|
|
||||||
|
|
||||||
abstract public function getThrowForce() : float;
|
abstract public function getThrowForce() : float;
|
||||||
|
|
||||||
/**
|
abstract protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable;
|
||||||
* Helper function to apply extra NBT tags to pass to the created projectile.
|
|
||||||
*/
|
|
||||||
protected function addExtraTags(CompoundTag $tag) : void{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onClickAir(Player $player, Vector3 $directionVector) : ItemUseResult{
|
public function onClickAir(Player $player, Vector3 $directionVector) : ItemUseResult{
|
||||||
$location = $player->getLocation();
|
$location = $player->getLocation();
|
||||||
$nbt = EntityFactory::createBaseNBT($player->getEyePos(), $directionVector, $location->yaw, $location->pitch);
|
|
||||||
$this->addExtraTags($nbt);
|
|
||||||
|
|
||||||
$class = $this->getProjectileEntityClass();
|
$projectile = $this->createEntity(EntityFactory::getInstance(), Location::fromObject($player->getEyePos(), $player->getWorld(), $location->yaw, $location->pitch), $directionVector, $player);
|
||||||
Utils::testValidInstance($class, Throwable::class);
|
|
||||||
|
|
||||||
/** @var Throwable $projectile */
|
|
||||||
$projectile = EntityFactory::getInstance()->create($class, $location->getWorldNonNull(), $nbt, $player);
|
|
||||||
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
|
$projectile->setMotion($projectile->getMotion()->multiply($this->getThrowForce()));
|
||||||
|
|
||||||
$projectileEv = new ProjectileLaunchEvent($projectile);
|
$projectileEv = new ProjectileLaunchEvent($projectile);
|
||||||
|
@ -23,7 +23,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\Snowball as SnowballEntity;
|
use pocketmine\entity\projectile\Snowball as SnowballEntity;
|
||||||
|
use pocketmine\entity\projectile\Throwable;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
class Snowball extends ProjectileItem{
|
class Snowball extends ProjectileItem{
|
||||||
|
|
||||||
@ -31,8 +36,15 @@ class Snowball extends ProjectileItem{
|
|||||||
return 16;
|
return 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProjectileEntityClass() : string{
|
protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable{
|
||||||
return SnowballEntity::class;
|
/** @var SnowballEntity $projectile */
|
||||||
|
$projectile = $factory->create(
|
||||||
|
SnowballEntity::class,
|
||||||
|
$location->getWorldNonNull(),
|
||||||
|
EntityFactory::createBaseNBT($location, $velocity, $location->yaw, $location->pitch),
|
||||||
|
$thrower
|
||||||
|
);
|
||||||
|
return $projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThrowForce() : float{
|
public function getThrowForce() : float{
|
||||||
|
@ -23,8 +23,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\EntityFactory;
|
||||||
|
use pocketmine\entity\Location;
|
||||||
use pocketmine\entity\projectile\SplashPotion as SplashPotionEntity;
|
use pocketmine\entity\projectile\SplashPotion as SplashPotionEntity;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\entity\projectile\Throwable;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
|
||||||
class SplashPotion extends ProjectileItem{
|
class SplashPotion extends ProjectileItem{
|
||||||
|
|
||||||
@ -32,15 +36,20 @@ class SplashPotion extends ProjectileItem{
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProjectileEntityClass() : string{
|
protected function createEntity(EntityFactory $factory, Location $location, Vector3 $velocity, Player $thrower) : Throwable{
|
||||||
return SplashPotionEntity::class;
|
/** @var SplashPotionEntity $projectile */
|
||||||
|
$projectile = $factory->create(
|
||||||
|
SplashPotionEntity::class,
|
||||||
|
$location->getWorldNonNull(),
|
||||||
|
EntityFactory::createBaseNBT($location, $velocity, $location->yaw, $location->pitch),
|
||||||
|
$thrower
|
||||||
|
);
|
||||||
|
|
||||||
|
$projectile->setPotionId($this->meta);
|
||||||
|
return $projectile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThrowForce() : float{
|
public function getThrowForce() : float{
|
||||||
return 0.5;
|
return 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addExtraTags(CompoundTag $tag) : void{
|
|
||||||
$tag->setShort("PotionId", $this->meta);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user