我正在尝试从数组中插入提供随机字符串的方法,并在Minecraft命令中执行它。

错误

它说,使用randomKey()方法在字符串之间添加+运算符时,此处不允许使用java:'void'类型。

我在IntelliJ中开发了插件。

public class events implements Listener{




    public static void main(String[] args)
    {
        randomKey();
    }

    public static void randomKey(){
        String[] crates ={"Basic", "Classic", "Crazy", "Galactic"};
        Random random = new Random();
        int RandomNumber = random.nextInt(crates.length);
        System.out.println(crates[RandomNumber]);
    }


    @EventHandler
    public  void keyGiver(BlockBreakEvent event ){



        //get block type grass

        Block block = event.getBlock();
        Material material  = block.getType();

        Player player = event.getPlayer();



        if(material.equals(Material.GRASS)){

            getServer().dispatchCommand(getServer().getConsoleSender(), "cc give Physical"+randomKey()+" 1 " + player.getName() + " ");
        }


    }```

最佳答案

您必须返回某些内容才能在表达式中使用函数,
这是我根据您的代码建议的修复程序:

public static String randomKey(){
    String[] crates ={"Basic", "Classic", "Crazy", "Galactic"};
    Random random = new Random();
    int RandomNumber = random.nextInt(crates.length);
    System.out.println(crates[RandomNumber]);
    return crates[RandomNumber];
}

10-07 16:13