You need to enable JavaScript to run this app. Minecraft Spigot插件开发中InventoryCloseEvent不生效的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft Spigot插件开发中InventoryCloseEvent不生效的问题求助

InventoryCloseEvent无响应的直接原因

你提供的代码里,onCloseInventory方法完全缺失了@EventHandler注解。Spigot的事件调度机制只会识别监听器类中、被@EventHandler标记的public方法作为事件绑定逻辑,没有这个注解的方法哪怕参数匹配事件类型,也不会被事件系统挂载,自然全程不会触发。

完整问题排查步骤
  • 检查监听器类的注册逻辑:确认插件主类的onEnable方法中,已经通过getServer().getPluginManager().registerEvents(监听器实例, 插件主类实例)完成了监听器类的注册,如果整个类都没注册,哪怕方法加了注解也不会触发事件。
  • 检查监听器类的修饰符:监听器类必须是public访问权限,否则Spigot的事件扫描器无法穿透类访问到内部的事件方法。
  • 检查事件方法的签名规范:要求方法为public修饰、返回值为void、参数有且仅有1个对应事件类型,多余参数、非public修饰、带返回值的方法都会被事件系统跳过。
  • 排查插件启动报错:查看控制台日志,确认插件加载阶段没有因为语法错误、类加载异常提前终止,否则后续的事件注册逻辑根本不会执行。
现有代码的潜在高风险问题

哪怕修了事件不触发的问题,下面的隐患也会引发服务器异常:

  • 无限制的重复任务创建:onInteraction中每次触发交互就启动一个新的循环任务,没有存储任务引用做终止逻辑,短时间内会堆积大量后台任务,拖垮服务器性能。
  • 危险的全局任务取消:Bukkit.getScheduler().cancelAllTasks()会无差别取消服务器上所有插件创建的所有定时任务,会直接破坏其他插件的正常运行逻辑,绝对不能在业务代码中使用。
  • 任务执行逻辑无校验:循环任务中直接调用玩家发消息的方法,没有判断玩家是否在线、是否已经退出服务器,玩家离线后任务还会继续运行,引发空指针异常。
修复后的参考实现
public class GuiInteractionListener implements Listener {
    // 给每个玩家绑定专属任务ID,实现精准任务取消
    private final Map<UUID, Integer> playerBoundTask = new HashMap<>();

    @EventHandler
    public void onInteraction(PlayerInteractEvent event) {
        // 仅响应右键空气/右键方块的交互,避免无意义触发
        if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }
        Player player = event.getPlayer();
        // 先清理该玩家之前遗留的未终止任务
        if (playerBoundTask.containsKey(player.getUniqueId())) {
            Bukkit.getScheduler().cancelTask(playerBoundTask.get(player.getUniqueId()));
        }
        // 启动新的循环任务
        int taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(
                FriendsGUI.getInstance(),
                () -> {
                    // 加在线校验,避免离线后报错
                    if (player.isOnline()) {
                        player.sendMessage("Hello " + player.getName());
                    }
                },
                0,
                20
        );
        playerBoundTask.put(player.getUniqueId(), taskId);
    }

    // 补上缺失的@EventHandler注解
    @EventHandler
    public void onCloseInventory(InventoryCloseEvent event) {
        Player player = (Player) event.getPlayer();
        player.sendMessage("lul");
        Bukkit.getScheduler().scheduleSyncDelayedTask(
                FriendsGUI.getInstance(),
                () -> {
                    if (player.isOnline()) {
                        player.sendMessage("Goodbye  " + player.getName());
                    }
                    // 仅取消该玩家绑定的任务,不影响其他运行中的逻辑
                    if (playerBoundTask.containsKey(player.getUniqueId())) {
                        int boundTaskId = playerBoundTask.remove(player.getUniqueId());
                        Bukkit.getScheduler().cancelTask(boundTaskId);
                    }
                },
                0
        );
    }
}

对应的主类onEnable中必须补充注册逻辑:

@Override
public void onEnable() {
    // 注册监听器到Spigot事件系统
    getServer().getPluginManager().registerEvents(new GuiInteractionListener(), this);
}

内容的提问来源于stack exchange,提问作者lucky bad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.13 09:54:04