【问题标题】:How do I put a runnable in an if statement?如何在 if 语句中放入可运行对象?
【发布时间】:2021-07-03 07:36:58
【问题描述】:

是否可以在if语句中放置一个runnable,因为我需要它,如果玩家在它下面有一个轮廓分明的砂岩,那么它应该在5秒后取出块,是否可以这样做或者我需要另一种方式吗? Intellij IDEA,java 插件 minecraft。代码:

 if (player.getLocation().getBlock().getRelative(BlockFace.DOWN).getTypeId() == 179) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    loc.getBlock().setType(Material.AIR);
                }
            }.runTaskLater(plugin, 100);
        }

【问题讨论】:

    标签: java intellij-idea plugins minecraft bukkit


    【解决方案1】:

    我在纯核心 java 上试过这个,它工作正常

     int x = 10;
        Timer timer = new Timer("Timer");
        if (x > 0) {
            TimerTask task = new TimerTask() {
                public void run() {
                    System.out.println("Task performed on: " + new Date() + "n" + "Thread's name: "
                            + Thread.currentThread().getName());
                    timer.cancel();
                }
            };
    
            long delay = 1000L;
            timer.schedule(task, delay);
        }
    output Task performed on: Sat Jul 03 13:49:54 IST 2021nThread's name: Timer
            
    

    所以我假设你提到的也应该有效

    【讨论】:

    • 块应该消失的时间是多少? intellijidea 没有给我错误,但是当我在 minecraft 1.8 中尝试它时,它在那里不起作用。
    • 我添加了计时器和时间表
    【解决方案2】:

    我测试了代码,它工作正常。我将 BukkitRunnable 分配给一个变量,以便在某些特殊情况下有机会取消任务,但如果您希望可以将其删除。

    我认为问题出在 Runnable 主体中,您忘记在玩家当前位置下方移动一个街区,并尝试将空气设置为空气。另外我建议使用 getType() 而不是 getTypeId()。

    希望它会像我的情况一样正常工作。 :)

    Block blockUnder = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    
                            if (blockUnder.getType() == Material.CHISELED_SANDSTONE) {
                                BukkitTask task = new BukkitRunnable() {
                                    @Override
                                    public void run() {
                                        blockUnder.setType(Material.AIR);
                                    }
                                }.runTaskLater(plugin, 20L * 5L);
                            }
    

    【讨论】:

    • 我必须使用 getTypeId 因为:无法解析符号 'CHISELED_SANDSTONE'。
    • 哦,我看到你在 Minecraft 1.8 中工作。然后只需替换回您的版本,看看它是否有效。
    • 它可以工作,但它应该适用于 1.8 版。
    猜你喜欢
    • 2021-01-01
    • 2017-12-22
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多