I'm making a plugin for a Minecraft server that will let a player select two locations (x1, z1 (first location): x2, z2 (second location)) and allow them to set this area in between the two points (rectangular/square) to randomaly teleport them anywhere in the given locations.
For the sake of simplicity, I will leave most of the code out and just give you the segment I'm having trouble with. Below, this code will (on the event that a player joins the server) teleport them inside that area.
I've setup some dummy data inside nextInt()
so you can understand the math.
Location 1 (x1, z1): -424, 2888
Location 2 (x2, z2): 4248, 3016
Above are the locations in the proram segment below. (Think of "z
" as "y
" on a graph).
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event){
Player player = event.getPlayer();
int x = 0, y = 0, z = 0;
Random randLocation = new Random();
player.sendMessage(ChatColor.RED + "TELEPORTING TO WASTELAND..");
x = randLocation.nextInt(((2888 - 424) + 1) + 424);
z = randLocation.nextInt(((4248 - 3016) + 1) + 3016);
Location location = player.getLocation();
location.setX(x);
location.setZ(z);
location.setY(player.getWorld().getHighestBlockAt(x, z).getY());
player.teleport(location);
}
The problem is, sometimes one (or maybe both) of the locations have a negative value. I have tried and tried different methods of coming up with these numbers but I am stumped.
QUESTION: Is there anyway to make Java select a random number between 2 givens?
Example:
randomLocation.nextInt(x1, x2);
randomLocation.nextInt(z1, z2);