Solved Randomizing

Discussion in 'Plugin Development' started by Coopah, Apr 17, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Coopah

    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.
     
  2. Offline

    MrSnare

    Code:java
    1.  
    2. Random rand = new Random();
    3. int index = rand.nextInt(list.size());
    4. ItemStack itemStack = list.get(index);
     
  3. Offline

    CaLxCyMru

    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
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if (!(sender instanceof Player)) {
    3. sender.sendMessage("You are not permitted to use this command in console!");
    4. return true;
    5. }
    6.  
    7. Player player = (Player)sender;
    8. ItemStack[] itemStacks = new ItemStack[]{new ItemStack(Material.DIAMOND,5),new ItemStack(Material.GOLD_INGOT,7)};
    9. if(cmd.getName().equalsIgnoreCase("random")){
    10. int r = new Random().nextInt(itemStacks.length);
    11. player.getInventory().addItem(itemStacks[r]);
    12. player.sendMessage(ChatColor.GREEN + "Random item added, you recived some " + ChatColor.BLUE + itemStacks[r].getType().toString().toLowerCase().replaceAll("_", " "));
    13. return true;
    14. }
    15. return false;
    16. }
     
    Coopah likes this.
  4. Offline

    Coopah

    CaLxCyMru
    Thanks man! Now how would I get the amount and put it in the message like "blablabla player has won 32 gold ingots"?
     
  5. Offline

    CaLxCyMru

    Coopah No problem, Glad I could help!

    If you want it to be a random amount, just use:
    Code:java
    1. 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
    1. player.sendMessage(ChatColor.GREEN + "Random item added, you recived " + itemStacks[r].getAmount() + ChatColor.BLUE + itemStacks[r].getType().toString().toLowerCase().replaceAll("_", " "));
    2. // 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 likes this.
  6. Offline

    Coopah

    CaLxCyMru
    Thanks man, really appreciate the help!
     
    CaLxCyMru likes this.
  7. Offline

    CaLxCyMru

    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
     
Thread Status:
Not open for further replies.

Share This Page