【问题标题】:(MC bukkit server) Right-clicking with specific item to trigger an event(MC bukkit server) 右键点击特定项目触发事件
【发布时间】:2023-03-27 16:35:02
【问题描述】:
public void arrowstick(PlayerInteractEvent event) {
        Player p = event.getPlayer();
            if(p.getInventory().getItemInMainHand().getType().equals(Material.STICK)){
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){

                Arrow ar = p.getWorld().spawn(p.getLocation(), Arrow.class);
                ar.setShooter(p);


                }

            }

    }

我在这里想要做的是,如果玩家手持棍子右键单击,就会射出一支箭。我似乎无法找到解决方案,有没有人知道我可能做错了什么?对不起,如果它是一个小而愚蠢的错误,它现在是凌晨 2 点,所以是的,希望你们能帮助

【问题讨论】:

  • 我不能从这里测试它,所以只是作为评论,但p.launchProjectile(Arrow.class);应该这样做。或者如果你想坚持你的方法,你需要ar.setVelocity(p.getEyeLocation().getDirection().multiply(2));(你可以将乘数更改为你需要的任何值)
  • @dly 好的,首先谢谢,它工作了,但还有一个小问题:你的 setvelocity 似乎不起作用,新生成的箭头直接掉到地上,我该如何解决? pastebin.com/ESZbVCWL
  • 如果在更改箭头之前输出箭头的速度会得到什么?另外,看看Vectors 可能是个好主意。这样你也可以使用玩家的getLocation().getDirection()
  • @dly 我该怎么做? (some var) 等于 a.getVelocity 并打印出来?另外,我如何能够将向量包含在这个概念中?顺便说一句,我注意到它在我体内产生了箭头,因为我们采取了玩家的位置来发射箭头,我们怎样才能让它在我面前产生?
  • 好吧,忽略我上面评论中的前两个问题,我明白了你想要做什么。如果我打印出Vector speed = a.getVelocity();imgur.com/a/HVozu,这就是结果

标签: java eclipse server minecraft bukkit


【解决方案1】:

我在这里为你编码:

public class Arrow extends JavaPlugin implements Listener {

    public void onEnable() {
        this.getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (p.getInventory().getItemInHand().getType() == Material.STICK) {
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                p.launchProjectile(Arrow.class);
            }
        }
    }

}

如果您只是将监听器复制到一个新类中,请不要忘记将其设为implements Listener 并注册监听器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多