-2

Heyo! I want to code a little plugin that gives a player money if he mines stone. The plugin can give the money via a console command. But that shouldn't be everytime so I want to generate a random number. Here is my code:

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;

public class stone implements Listener {
    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player p = (Player) event.getPlayer();
        Block b = (Block) event.getBlock();
        if (b.getType() == Material.STONE) {
            int random = (int)(Math.random() * 25 + 1);
            //generate random number between 0 and 25
            //if it's for example 8
            //do this:
            p.sendMessage("You found 3 Coins in the Stone!");
            ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
            String command = "money give "+ p.getName() +" 2";
            Bukkit.dispatchCommand(console, command);

        }

    }

}
  • 1
    Hi! Try looking at https://stackoverflow.com/q/363681/8954291 for more info on how to generate random numbers – Jakob Lovern Feb 28 '22 at 19:36
  • I know how to generate the random number but how do I check if it's 8? – Elektrodemon NL Feb 28 '22 at 19:47
  • @ElektrodemonNL - Where do you attempt to generate a random time? Keep in mind that you must use a proper seed for the RNG, otherwise, you will generate the same number. You check if the value is 8 after you generate it. – Security Hound Feb 28 '22 at 19:47
  • I added the random number. How do I check if random is 8? – Elektrodemon NL Feb 28 '22 at 19:51
  • @ElektrodemonNL - Use an if conditional on the random variable. [You should look at ways to properly generate random numbers. You are likely going to see a huge performance hit by using your current method.](https://stackoverflow.com/questions/38838172/percentage-using-random/38838299). There are versions of those methods designed to work in their own thread. – Security Hound Feb 28 '22 at 20:33

1 Answers1

0

It appears that all you need here is an if statement below your random number generation like so:

if (random == 8) {
   // ... your code block for awarding coins
}

Note that I have only used == 8 here as this is what you have suggested in your original question. A cleaner alternative would be to create a constant field within this class so that it is more descriptive when checking. See the example below:

final int stonePayout = 8; 

public void onBlockBreak(BlockBreakEvent event) {
    ...
    if (random == stonePayout) {
        p.sendMessage("You found 3 Coins in the Stone!");
        ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
        String command = "money give "+ p.getName() +" 2";
        Bukkit.dispatchCommand(console, command);
    }
    
}

Here, if ever you wish to reuse/change your random number check, you can do so easily by changing the value of stonePayout rather than searching for an explicit 8.