I know how I can spawn a snowball. But how can I throw it? PHP: $pos = $player->getPosition(); $nbt = new CompoundTag("",[ new ListTag("Pos",[ new DoubleTag("",$pos->getX()), new DoubleTag("",$pos->getY()+$player->getEyeHeight()), new DoubleTag("",$pos->getZ()), ]), new ListTag("Motion", [ new DoubleTag("", 2*-sin($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)), new DoubleTag("", 2*-sin($player->pitch / 180 * M_PI)), new DoubleTag("", 2*cos($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)), ]), new ListTag("Rotation", [ new FloatTag("", lcg_value() * 360), new FloatTag("",0), ]) ]); $entity = Entity::createEntity('Snowball', $pos->getLevel()->getChunk($pos->getX() >> 4, $pos->getZ() >> 4), $nbt); $entity->spawnToAll();
The snowball despawns instantly PHP: $nbt = new CompoundTag("",[ new ListTag("Pos",[ new DoubleTag("",$pos->getX()), new DoubleTag("",$pos->getY()+$player->getEyeHeight()), new DoubleTag("",$pos->getZ()), ]), new ListTag("Motion", [ new DoubleTag("", 2*-sin($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)), new DoubleTag("", 2*-sin($player->pitch / 180 * M_PI)), new DoubleTag("", 2*cos($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI)), ]), new ListTag("Rotation", [ new FloatTag("", lcg_value() * 360), new FloatTag("",0), ]) ]); $entity = Entity::createEntity('Snowball', $pos->getLevel()->getChunk($pos->getX() >> 4, $pos->getZ() >> 4), $nbt);$entity->setMotion($entity->getMotion()->multiply(2)); $entity->spawnToAll();
what about entity collide event? (not exact) you can add a custom NBT to be able to determine if it's custom snowball spawned when you check for the NBT
I really wonder why ppl still struggle to do this while the code is already written and ready to be adapted. https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Player.php#L2124-L2160
I don't know why, but it isn't working PHP: $pos = $player->getPosition();$aimPos = new Vector3( -sin($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI), -sin($player->pitch / 180 * M_PI), cos($player->yaw / 180 * M_PI) * cos($player->pitch / 180 * M_PI) );$nbt = new CompoundTag("", [ "Pos" => new ListTag("Pos", [ new DoubleTag("", $pos->x), new DoubleTag("", $pos->y + $player->getEyeHeight()), new DoubleTag("", $pos->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) ]), ]); $f = 1.5; $snowball = Entity::createEntity("Snowball", $pos->getLevel()->getChunk($pos->getX() >> 4, $pos->getZ() >> 4), $nbt); $snowball->setMotion($snowball->getMotion()->multiply($f)); $snowball->spawnToAll();
WTF? $pos->getLevel()->getChunk($pos->getX() >> 4, $pos->getZ() >> 4) is that anywhere in the code i showed you? no. and there is a reason for that https://github.com/pmmp/PocketMine-MP/commit/c21197ef176166b1c2c19b091cf84664aa878c00
What would be correct? This isn't working: PHP: $snowball = Entity::createEntity("Snowball", $player->getLevel(), $nbt, $player);
On EntityDespawnEvent, get the Entity, check if it is an instance of Snowball, then get the position of it.