跳至主要內容

事件监听器

小于 1 分钟约 149 字全民制作人ikun

事件监听器

事件监听器

事件监听器需要实现Listener接口,然后在方法上面加@EventHandler注解

public class MyListener implements Listener {
    @EventHandler
    void onBlockPlace(BlockPlaceEvent event) {
        Block block = event.getBlockPlaced();
        System.out.println("方块类型:" + block.getType() + " 方块坐标:" + block.getLocation());
    }
}

写完了,需要注册,在我们的主类中进行注册

public final class Auth extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(new MyListener(), this);
    }

取消注册:

image-20240530171824192
image-20240530171824192

自定义事件

创建一个自己的事件

@EqualsAndHashCode(callSuper = true)
@Data
public class MyEvent extends Event {

    private static final HandlerList HANDLERS = new HandlerList();

    private Player player;

    public MyEvent(Player player) {
        this.player = player;
    }

    @Override
    @NotNull
    public HandlerList getHandlers() {
        return HANDLERS;
    }
}
上次编辑于:
贡献者: yunfeidog