Sem-Bauke
Sem-Bauke

Reputation: 3

Add method inside command using dispatchCommand

I am trying to insert method that provides a random string out of an array and execute it in a Minecraft command.

The error

It says that java: 'void' type not allowed here when adding + operator inbetween the strings with the method randomKey().

I develope the plugin inside 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() + " ");
        }


    }```

Upvotes: 0

Views: 30

Answers (1)

BluePrion
BluePrion

Reputation: 26

you have to return something in order to use a function in an expression, this is the fix which I recommend base on your code :

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];
}

Upvotes: 1

Related Questions