I am trying to generate a random location in an arena, but it won't let me generate a negative number, how would i accomplish this if one of the points has either a x, y, or z that is negative? Code:java Pos1 = -704, 238, 1153Pos2 = -1078, -37, 652
Lets say you want a random number between -1000 and 1000. Just do this: Code:java Random random=new Random();int randomNumber=(random.nextInt(2000)-1000);
BillyGalbreath that's what im saying both my numbers are negative. I could use Math.abs to the 1st number but then it won't generate a number within the arena.
Ah, then use this formula: .nextInt(max - min + 1) + min; Min and max can be negative, as long as max is indeed > min.
well it didn't error, but it isn't generating a location inside of those specifications aka the arena.
BillyGalbreath here you go. Code:java Random r = new Random(); Vector min = arena.getCuboid().getMinimumPoint();Vector max = arena.getCuboid().getMaximumPoint();//randomly select a location btwn the min block x/y/z and the max block x/y/zLocation loc = new Location(arena.getSpawnLocations().get(0).getWorld(),r.nextInt(max.getBlockX() - min.getBlockX() + 1) + min.getBlockX(),r.nextInt(max.getBlockY() - min.getBlockY() + 1) + min.getBlockY(),r.nextInt(max.getBlockZ() - min.getBlockZ() + 1) + min.getBlockZ());//define locationfinal Location realLoc = new Location(loc.getWorld(), loc.getX(), loc.getWorld().getHighestBlockYAt(loc), loc.getZ());Block block = realLoc.getWorld().getBlockAt(realLoc);//get the blockblock.setType(Material.CHEST); if(block.getType() == Material.CHEST) {//test to see if it's the correct blockChest chest = (Chest) block.getState();//cast the block to chestchest.getBlockInventory().clear(); //clear chest inventory before adding to itchest = GameSetup.getInstance().fillChest(chest);//fill the chestchest.update(); } arena.sendSound(Sound.NOTE_PLING); arena.sendMessage(ChatColor.GOLD + "A surprise was spotted at " + realLoc.getBlockX() + ", " + realLoc.getBlockY() + ", " + realLoc.getBlockZ() + "!");
cfil360 Assuming your min is, say, -500, and your max is 500. You can generate the x and z coordinates using this formula, as explained above by BillyGalbreath, and then take the y from the highest block at that pair; Code:java Random rand = new Random();int x = rand.nextInt(1001) - 500; // random(max - min + 1) + minint z = rand.nextInt(1001) - 500;int y = currentWorld.getHighestBlockAt(x, z);Location spawnLocation = new Location(currentWorld, x, y, z); Edit: Ninja'd. And incorrect labeling.
Maurdekye you can look at my code above, the problem is that when i get the cuboid, it doesn't necessarily mean both numbers will be positive. In this case the x of both the min and x is negative.
cfil360 Your code is probably working correctly; the chests are likely just spawning inside the ground / up in the air, because you have a random Y coordinate. You'll want to use the generated x and z to get the highest block at that coordinate pair, and spawn a chest above that.
Hmm. Looks good to me. What makes you say its not generating a valid location? Have you tried outputting numbers in debug? Make sure max is actually max, what does x y z return? What are the two points calculated, etc. What maurdekye said makes perfect sense.
Maurdekye BillyGalbreath that isn't the problem. I am teleporting to the location as i have it print out the location. There is a chest there and it works perfectly fine, but it is outside of the coordinates. edit: I changed my code above to the complete code i am using. Sorry about the formatting, this java code tag doesn't maintain my format.
cfil360 Print out the generated numbers into the console, as well as your min and max values. There's likely a dissension somewhere in your code that's modifying those values before they reach their destination.
Maurdekye BillyGalbreath found out what i did wrong. I have the arena cuboid set to a square, but the arena itself is only a circle within that much larger square. I am sorry to waste your time. That thought never even crossed my mind. Thanks for your help.
cfil360 If you need to make a generated value is within that circle / cylinder / sphere, just check its distance from the centerpoint of the circle; if it's greater, then generate the number again until it's less.
Take a look at this top answer then. Should get your head in the right area. http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly