-2

I want to the player receive a random item in the Int[]

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {


Player player = (Player) sender;

if(commandLabel.equalsIgnoreCase("surprise")){
Random random = new Random();

PlayerInventory inventory = player.getInventory();
int ItensIds[] = {274, 313, 314, 315, 316};
ItemStack item = new ItemStack(Material.AIR);



for(int Ids : ItensIds){

    int randomNumber = random.nextInt(300);
    do{
        randomNumber = random.nextInt(300);
        item.setTypeId(Ids);
        inventory.addItem(item);
    } while(randomNumber == Ids);

    break;
}

}

Im trying to figure how it works but ends crashing the server or not working at all.

Im really new at this code stuff, please understand if I did something really wrong.

1 Answers1

2

"I want to the player receive a random item in the Int[]"

If you want to get random value from your int array (which is ItensIds[]), you can just do this:

int randomNumber = ItensIds[random.nextInt(ItensIds.length)];

You don't need loop or whatsoever.

DnR
  • 3,487
  • 3
  • 23
  • 31