Ok, how would I go about this. I want to have a list of a bunch of items e.g gold, diamond, iron etc. Then I want to pick a random one of those. So I would have a list of items then when I typed a certain command it would pick a random one out of them then I could give it to a player.
Code:java Random rand = new Random(); int index = rand.nextInt(list.size()); ItemStack itemStack = list.get(index);
Okay, first you need to make your command. After this, you need to create a ItemStack[] ( In the example below I have called mine itemStacks) and set it to the ItemStacks you want the player to receive (This could be a diamond with a custom name or multiple diamonds!). Then, use Java's Util.Random() to randomly select one of the ItemStacks from itemStacks by using itemStacks.lengh; Then, use player.getInventory().addItem(itemStacks[r]). Example: Code:java public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ if (!(sender instanceof Player)) { sender.sendMessage("You are not permitted to use this command in console!"); return true; } Player player = (Player)sender; ItemStack[] itemStacks = new ItemStack[]{new ItemStack(Material.DIAMOND,5),new ItemStack(Material.GOLD_INGOT,7)}; if(cmd.getName().equalsIgnoreCase("random")){ int r = new Random().nextInt(itemStacks.length); player.getInventory().addItem(itemStacks[r]); player.sendMessage(ChatColor.GREEN + "Random item added, you recived some " + ChatColor.BLUE + itemStacks[r].getType().toString().toLowerCase().replaceAll("_", " ")); return true; } return false; }
CaLxCyMru Thanks man! Now how would I get the amount and put it in the message like "blablabla player has won 32 gold ingots"?
Coopah No problem, Glad I could help! If you want it to be a random amount, just use: Code:java int random = new Random().nextInt(64) // We use 64 as its the max stack size, in most cases. Then apply that where its needed when crafting a ItemStack. If you want to get the amount that was added to the players Inventory, its simple! Code:java player.sendMessage(ChatColor.GREEN + "Random item added, you recived " + itemStacks[r].getAmount() + ChatColor.BLUE + itemStacks[r].getType().toString().toLowerCase().replaceAll("_", " "));// We use this: itemStacks[r].getAmount() to return the amount of item's in the random ItemStack, Simple! If you have any more problems, feel free to PM me.
Coopah No problem. Shoot me a PM if you need any other problems sorted. I'll write you up a little explanation on why it works, and how to do it. Regrads, -CaL