bukkit拦截commandblock的方法

由于bukkit没有提供BlockCommandPreprocessEvent之类的事件(bukkit pr: https://github.com/Bukkit/Bukkit/pull/998 ),故只能监听BlockRedstoneEvent。

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onBlockRedstoneEvent(BlockRedstoneEvent event) {
        Block block = event.getBlock();
        if (block.getType() == Material.COMMAND && block.getState() instanceof CommandBlock) {
            CommandBlock cmdblock = (CommandBlock) block.getState();
            String cmd=cmdblock.getCommand(); // commandblock内的命令
            boolean allow=......; // 是否允许命令执行
            
            if (!allow){
                // 取消事件
                event.setNewCurrent(0);
            }
        }
    }


你可能感兴趣的:(minecraft,bukkit)