Getting random drops from list

Discussion in 'Plugin Development' started by slater96, Sep 21, 2012.

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

    slater96

    In my config I have this
    Items: 383@120:1,268:1,285:1,354:1
    Which represents the random drops that you get when killing a mob. At the moment it gives you all of them items, but I would like it to only give you one out of them randomly so like you kill a mob and get one villager spawn egg, not a spawn egg, sword and whatever the other id's are. I've used random before but not sure how to use that with this.
    String[] drops = plugin.getConfig().getString("RandomDrops.Items").split(",");
     
  2. Offline

    kyle1320

    slater96
    Code:
    Random random = new Random();
    String ranDrop = drops[random.nextInt(drops.length)];
    would make ranDrop a random string from that list
     
  3. Offline

    Netizen

    If you want all of the drops to be equally likely, and you want an item to always drop. Try something like this:
    Code:
    #import java.util.Random;
     
    ...
    Random rand = new Random()
    String itemThatDrops = drops[rand.nextInt(drops.length)];
    
    If you wanted something more complex like what standard MMO's have. Each item needs a % chance to drop and you'll have to iterate through your drop list one item at a time and testing if it would drop.
     
  4. Offline

    slater96

    Hmm ok thanks for the reply. Still not too sure how to implement that in my code. Anyone help me please?
    Code:
    String[] drops = plugin.getConfig().getString("RandomDrops.Items").split(",");
                        String randomDrop = drops[random.nextInt(drops.length)];
                        for (int i = 0; i < drops.length; i++) {
                            int itemid = 0;
                            int amount = 0;
                            int datavalue = 0;
                            if (drops[i].contains("@")) {
                                String[] drops2 = drops[i].split("@");
                                if (drops2[1].contains(":")) {
                                    String[] drops3 = drops2[1].split(":");
                                    try {
                                        itemid = Integer.parseInt(drops2[0]);
                                        datavalue = Integer.parseInt(drops3[0]);
                                        amount = Integer.parseInt(drops3[1]);
                                    } catch (NumberFormatException e) {
                                        //
                                    }
                                } else {
                                    try {
                                        itemid = Integer.parseInt(drops2[0]);
                                        datavalue = Integer.parseInt(drops2[1]);
                                        amount = 1;
                                    } catch (NumberFormatException e) {
                                        //
                                    }
                                }
                            } else if (drops[i].contains(":")) {
                                String[] drops2 = drops[i].split(":");
                                try {
                                    itemid = Integer.parseInt(drops2[0]);
                                    amount = Integer.parseInt(drops2[1]);
                                } catch (NumberFormatException e) {
                                    //
                                }
                            } else {
                                try {
                                    itemid = Integer.parseInt(drops[i]);
                                    amount = 1;
                                } catch (NumberFormatException e) {
                                    //
                                }
                            }
                            if (amount > 0 || itemid > 0) {
                                if (datavalue == 0) {
                                    Material m = Material.getMaterial(itemid);
                                    if (m == null) {
                                        for (Player p : Bukkit.getOnlinePlayers()) {
                                            if (p.isOp()) {
                                                p.sendMessage(ChatColor.RED + "Item ID not found: " + itemid);
                                            }
                                        }
                                        return;
                                    }
                                    event.getDrops().add(new ItemStack(itemid, amount));
                                } else {
                                    Material m = Material.getMaterial(itemid);
                                    if (m == null) {
                                        for (Player player : Bukkit.getOnlinePlayers()) {
                                            if (player.isOp()) {
                                                player.sendMessage(ChatColor.RED + "Item ID not found: " + itemid);
                                            }
                                        return;
                                        }
                                    }
                                    event.getDrops().add(new ItemStack(itemid, amount, (short) datavalue));
                                }
                            }
                        }
     
  5. Offline

    slater96

    Any help please?
     
  6. Offline

    Netizen

    You're almost there, just replace your for loop to only look at the line you randomly selected. It's hard to edit on the website, but I've tried to just remove the outer for loop and replace any mention of drops with randomDrop. If I missed anything I apologize..

    Code:
    String[] drops = plugin.getConfig().getString("RandomDrops.Items").split(",");
    String randomDrop = drops[random.nextInt(drops.length)];
    int itemid = 0;
    int amount = 0;
    int datavalue = 0;
    if (randomDrop.contains("@")) {
    String[] drops2 = randomDrop.split("@");
    if (drops2[1].contains(":")) {
    String[] drops3 = drops2[1].split(":");
    try {
    itemid = Integer.parseInt(drops2[0]);
    datavalue = Integer.parseInt(drops3[0]);
    amount = Integer.parseInt(drops3[1]);
    } catch (NumberFormatException e) {
    //
    }
    } else {
    try {
    itemid = Integer.parseInt(drops2[0]);
    datavalue = Integer.parseInt(drops2[1]);
    amount = 1;
    } catch (NumberFormatException e) {
    //
    }
    }
    } else if (randomDrop.contains(":")) {
    String[] drops2 = randomDrop.split(":");
    try {
    itemid = Integer.parseInt(drops2[0]);
    amount = Integer.parseInt(drops2[1]);
    } catch (NumberFormatException e) {
    //
    }
    } else {
    try {
    itemid = Integer.parseInt(randomDrop);
    amount = 1;
    } catch (NumberFormatException e) {
    //
    }
    }
    // Finished selecting random drop, giving to player
    if (amount > 0 || itemid > 0) {
    if (datavalue == 0) {
    Material m = Material.getMaterial(itemid);
    if (m == null) {
    for (Player p : Bukkit.getOnlinePlayers()) {
    if (p.isOp()) {
    p.sendMessage(ChatColor.RED + "Item ID not found: " + itemid);
    }
    }
    return;
    }
    event.getDrops().add(new ItemStack(itemid, amount));
    } else {
    Material m = Material.getMaterial(itemid);
    if (m == null) {
    for (Player player : Bukkit.getOnlinePlayers()) {
    if (player.isOp()) {
    player.sendMessage(ChatColor.RED + "Item ID not found: " + itemid);
    }
    return;
    }
    }
    event.getDrops().add(new ItemStack(itemid, amount, (short) datavalue));
    }
    }
    
    edit: tried to format it correctly.
     
  7. Offline

    slater96

    That's super thanks, working great :D
     
Thread Status:
Not open for further replies.

Share This Page