(1.17)How to use random numbers?

Discussion in 'Spigot Plugin Help' started by electcheese, Aug 21, 2021.

  1. I am making a dagger that has a 20% chance to drop one of the attacked player's items.
    Code (Java):
    @EventHandler
        public void onHit(EntityDamageByEntityEvent event) {
            Player thief = (Player) event.getDamager();
            Player player = (Player) event.getEntity();
         
            if(thief.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD)) {
                if(thief.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains(ChatColor.DARK_GRAY + "Thief's Dagger")) {
                    if(thief.getInventory().getItemInMainHand().getItemMeta().hasLore()) {
                     
                    }
                     
                }
            }
        }

    I am just not sure how to start this.
     
    • Agree Agree x 1
    • Creative Creative x 1
  2. Code (Text):
    if (new Random().nextInt(100) + 1 <= 20) { // do stuff }
    You have to import java.util.Random
     
  3. I can't figure out how to get the number that is generated to get that slot in the players inventory to remove the item.
     
  4. Random.nextInt
     
  5. I mean from here to get the number in a variable.
    Code (Java):
    if(new Random().nextInt(100) + 1 <= 20) {
                         
                        }
    How would I save the number as a variable.
     
  6. Random random = new Random();
    int number = random.nextInt...;
     
  7. How would I get a random slot in the inventory and yoink an item?
     
  8. Would this work?(I don't have anyone to test it on). Is the logic flawed or does it look like it will work?
    Code (Java):
    if(thief.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD)) {
                if(thief.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains(ChatColor.DARK_GRAY + "Thief's Dagger")) {
                    if(thief.getInventory().getItemInMainHand().getItemMeta().hasLore()) {
                        Random random = new Random();
                        int number = random.nextInt(100);
                        if(number + 1 <= 20) {
                            player.getInventory().getItem(number).setAmount(player.getInventory().getItem(number).getAmount() - 1);
                            ItemStack item = player.getInventory().getItem(number);
                            player.getWorld().dropItemNaturally(player.getLocation(), item);
                               
                           
                        }
                           
                       
                    }
                       
                }
            }
        }
     
  9. Now, it works but the world drops a whole stack, and the player loses 1 item. How do I make the world drop one item only?
    Code (Java):
    @EventHandler
        public void onHit(EntityDamageByEntityEvent event) {
            Player thief = (Player) event.getDamager();
            Player player = (Player) event.getEntity();
           
            if(thief.getInventory().getItemInMainHand().getType().equals(Material.IRON_SWORD)) {
                if(thief.getInventory().getItemInMainHand().getItemMeta().getDisplayName().contains(ChatColor.DARK_GRAY + "Thief's Dagger")) {
                    if(thief.getInventory().getItemInMainHand().getItemMeta().hasLore()) {
                        Random random = new Random();
                        int number = random.nextInt(36);
                        if(new Random().nextInt(100) + 1 <= 20) {
                            if(number + 1 <= 36 && player.getInventory().getItem(number).getAmount() > 0){
                                   
                                    player.getInventory().getItem(number).setAmount(player.getInventory().getItem(number).getAmount() - 1);
                                    ItemStack item = player.getInventory().getItem(number), 1;
                                    player.getWorld().dropItemNaturally(player.getLocation(), item);
                                    thief.sendMessage("You did it.");
                               
                            }
                        }
                           
                       
                }
            }
        }