also dont forget to animate it's trajectory so it moves in command blocks fashion, test for fireball with tag, if fireball with tag found, set invisible, spawn invisible armor stand holding visible sword on prefer to anchor/offset on the sword and rotating it flat and making it match so it looks realistic now just move the armorstand to the fireball every tick in PMMP api you can add a custom entity projectile extending fireball so it go straight with an invisible rotated armorstand holding a sword now when someone hold the sword you can spawn the armor stand entity which will shoot the sword straight forever
I do not know if it will work. PHP: /** * @param Player $shooter */ public function throwSword(Player $player) { $aimPos = $player->getDirectionVector(); $nbt = new CompoundTag("", [ "Pos" => new ListTag("Pos", [ new DoubleTag("", $player->x), new DoubleTag("", $player->y + $player->getEyeHeight()), new DoubleTag("", $player->z) ]), "Motion" => new ListTag("Motion", [ new DoubleTag("", $aimPos->x), new DoubleTag("", $aimPos->y), new DoubleTag("", $aimPos->z) ]), "Rotation" => new ListTag("Rotation", [ new FloatTag("", $player->yaw), new FloatTag("", $player->pitch) ]), "Health" => new ShortTag("Health", 5), "Item" => new CompoundTag("Item", [ "id" => new ShortTag("id", Item::IRON_SWORD), "Damage" => new ShortTag("Damage", 0), "Count" => new ByteTag("Count", 1), ]), "PickupDelay" => new ShortTag("PickupDelay", 0x7F), ]); $sword = Entity::createEntity("Item", $player->getLevel(), $nbt, $player); $sword->setMotion($sword->getMotion()->multiply(1.5)); $sword->spawnToAll(); $player->getInventory()->setItemInHand(Item::get(Item::AIR)); }
Ehh, you can cut down on a lot of code (repetition) there. Using Item entity may not be ideal, try a custom ArmorStand class like @Thunder33345 said. PHP: /** @var Player $player */$nbt = Entity::createBaseNBT($player->add(0, $player->getEyeHeight(), 0), $player->getDirectionVector(), $player->yaw, $player->pitch);$sword = Entity::createEntity("ThrowingKnife", $player->getLevel(), $nbt, $player);$sword->setMotion($sword->getMotion()->multiply(1.5));$sword->spawnToAll(); The ThrowingKnife class could extend the Projectile class like so. PHP: class ThrowingKnife extends Projectile{ public const NETWORK_ID = self::ARMOR_STAND; //dont forget the $width and $height protected function sendSpawnPacket(Player $player) : void{ parent::sendSpawnPacket($player); //send MobEquipmentPacket for Iron Sword }}
Ah, yes. The pose #8 with yaw = relative yaw - 75 seems suitable for a throwing knife. I think you'll need to lock the rotation. PHP: $nbt = Entity::createBaseNBT( $player->add(0, $player->getEyeHeight(), 0), $player->getDirectionVector(), $player->yaw - 75,//makes the sword point straight, instead of towards the right $player->pitch);//initEntity()$sword->getDataPropertyManager()->setByte(Entity::DATA_ARMOR_STAND_POSE_INDEX, 8);