我对bukkit插件开发很新,我想知道如何改变雪球/任何其他弹丸的速度。在我的代码中,当玩家与某个对象交互时,它会发出雪球,但我需要改变它的速度。帮助将不胜感激。
由于
代码:
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (!(event.getAction() == Action.RIGHT_CLICK_AIR)) return;
if (!(event.getItem().getType() == Material.BLAZE_ROD)) return;
Snowball snowball = player.launchProjectile(Snowball.class);
}
答案 0 :(得分:0)
现在你只是发射Snowball
弹丸。您必须向对象添加速度以更改其速度。这是通过编辑对象本身来完成的。
这是您应该使用的基本大纲。我无法对此进行测试,但它应该有助于指出正确的方向。
Snowball snowball = player.launchProjectile(Snowball.class);
// Get vectors
Vector i = target.getLocation().toVector();
Vector j = snowball.getLocation().toVector();
// Perform the subtraction, then normalize
Vector r = i.subtract(j);
Vector v = r.normalize();
// then, actually set the speed, in this case, by a magnitude of four
Vector velocity = v.multiply(4);
// Then, set the velocity of the snowball but updating the 'default' velocity of the snowball
snowball.setVelocity(snowball.setVelocity(velocity));
这可以通过编写复合表达式来缩小:
Snowball snowball = player.launchProjectile(Snowball.class);
Vector velocity = (target.getLocation().toVector().subtract(snowball.getLocation().toVector()).normalize()).multiply(4);
snowball.setVelocity(snowball.setVelocity(velocity));
希望这有帮助!