Solved Random drop (chance)

Discussion in 'Plugin Development' started by EDSEL, Aug 25, 2019.

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

    EDSEL

    What to do to give each item a chance?

    this is my code:

    Code:
        @EventHandler
        public void onRandomBreakWithWOODPICKAXE(BlockBreakEvent e) {
            Block b = e.getBlock();
            Material m = e.getBlock().getType();
            Location l = e.getBlock().getLocation().add(0.5, 0, 0.5);
            Material tool = e.getPlayer().getInventory().getItemInHand().getType();
    
            if (tool != null && tool == Material.WOOD_PICKAXE
                    && m == Material.COBBLESTONE) {
                e.setCancelled(true);
                b.getDrops().clear();
                b.setType(Material.AIR);
    
                Random r = new Random();
                int chance = r.nextInt(101);
    
                if (chance <= 10) {
                    l.getWorld().dropItemNaturally(l,
                            new ItemStack(Material.DIAMOND));
                    return;
                } else if (chance == 60) {
                    l.getWorld().dropItemNaturally(l,
                            new ItemStack(Material.COBBLESTONE));
                    return;
    
                } else if (chance >= 30) {
                    l.getWorld().dropItemNaturally(l,
                            new ItemStack(Material.IRON_INGOT, 1));
                    return;
                }
    
            }
        }
    
    }
     
  2. Offline

    KarimAKL

    @EDSEL Do you want all of them to have the same % chance?
     
  3. Offline

    EDSEL

    No , for example, diamonds were 10% and iron 30%
     
  4. Offline

    CraftCreeper6

    @EDSEL
    You'll want something called relative probability. It's the chance based on the percentage you give an item that you will pick a certain thing.

    It looks something like this:
    Code:
    public Item getRandom() {
    
            int index = rand.nextInt(totalSum)
            int sum = 0
            int i=0
            while(sum < index ) {
                 sum = sum + items.get(i++).relativeProb
            }
            return items.get(Math.max(0,i-1))
        }
    
    where items is a list filled with serialized classes.

    The class can be any structure you like but here's how I would do it:

    Code:
    public class Item
    {
    
        public Material material
        public float probability
           
        public Item(Material m, float prob)
        {
            this.material = m
            this.probability = prob
        }
       
    }
    
    (I would suggest getters and setters rather than public values but this is an example)

    This way, upon startup of your plugin, you can load the probabilities of each Item and select them with the relative probability.

    getRandom() will return a random Item from the items list and use item.material to access the material that was selected.
     
    EDSEL likes this.
  5. Offline

    EDSEL

    thank you so much
     
    CraftCreeper6 likes this.
Thread Status:
Not open for further replies.

Share This Page