本文介绍了Bukkit(Spigot API)侦听器没有响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在制作一个bukkit插件,该插件会显示在插件列表中,但是当我执行我想让代码不执行任何操作时。

I have been making a bukkit plugin, which shows up in the plugins list but when I do what I want the code to do nothing happens.

public class MyClass extends JavaPlugin implements Listener {

@EventHandler
public void onInteract(PlayerInteractEvent event) {
  Player player = event.getPlayer();
  if (player.isSneaking()) {
      player.sendMessage("Fire!");
      Arrow arrow = player.launchProjectile(Arrow.class);
      arrow.setShooter(player);
      arrow.setGravity(false);
      arrow.setSilent(true);
      arrow.setBounce(false);
      Block attach = arrow.getAttachedBlock();
      Location attachlocation = attach.getLocation();
      attachlocation.getWorld().createExplosion(attachlocation, 3);
            arrow.setVelocity((player.getEyeLocation().getDirection().multiply(1000)));
      }
   }
}


推荐答案

我看不到您注册您的收听者。 Bukkit需要知道哪些对象是侦听器(您没有这样做),并且需要知道要执行什么方法(带有 @EventHandler 批注)

I can't see you registering your listener. Bukkit needs to know what objects are listeners (you're not doing this) and it needs to know what methods to execute (with the @EventHandler annotation)

您可以使用PluginManager的方法。一个聪明的主意是在您的onEnable方法中执行此操作,以确保在您的插件启动后立即注册您的侦听器。

You can register the listener with PluginManager's registerEvents(Listener listener, Plugin plugin) method. A smart idea is to do this inside your onEnable method, to ensure your listener is registered as soon as your plugin starts.

public class MyClass extends JavaPlugin implements Listener {

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

    // rest of your code
}

这篇关于Bukkit(Spigot API)侦听器没有响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 03:07