picking a random direction without a loop

This commit is contained in:
BuildTools 2016-10-22 20:51:34 -04:00
parent 8624563a2b
commit 1e17a1dab6
2 changed files with 19 additions and 5 deletions

View file

@ -1,5 +1,8 @@
package alisolarflare; package alisolarflare;
import java.util.ArrayList;
import java.util.Arrays;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -28,6 +31,9 @@ public class RandomTP implements CommandExecutor{
//every 4 players who use it will be teleported near each other. //every 4 players who use it will be teleported near each other.
//ex. iie > 1200, ali -> 1210, byz -> 1190, charles -> 1195, wind -> 300, zan -> 310, etc //ex. iie > 1200, ali -> 1210, byz -> 1190, charles -> 1195, wind -> 300, zan -> 310, etc
public void conflictRtp(Player player, World world, Location minLocation, Location maxLocation){ public void conflictRtp(Player player, World world, Location minLocation, Location maxLocation){
//INIT - xDifference, xAverage //INIT - xDifference, xAverage
int xdifference = minLocation.getBlockX() - maxLocation.getBlockX(); int xdifference = minLocation.getBlockX() - maxLocation.getBlockX();
int xAverage = (int) Math.floor(minLocation.getBlockX() + maxLocation.getBlockX() / 2); int xAverage = (int) Math.floor(minLocation.getBlockX() + maxLocation.getBlockX() / 2);
@ -37,7 +43,7 @@ public class RandomTP implements CommandExecutor{
int zAverage = (int) Math.floor(minLocation.getBlockZ() + maxLocation.getBlockZ()); int zAverage = (int) Math.floor(minLocation.getBlockZ() + maxLocation.getBlockZ());
//CHECK - Reset Cycle //CHECK - Reset Cycle
if ((northUsed || southUsed || eastUsed || westUsed) == false){ if (!(northUsed || southUsed || eastUsed || westUsed)){
//Tries 20 times to find a location //Tries 20 times to find a location
for(int i = 0; i < 20; i ++){ for(int i = 0; i < 20; i ++){
@ -52,9 +58,10 @@ public class RandomTP implements CommandExecutor{
//CHECKS - if ground is safe //CHECKS - if ground is safe
boolean groundIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ).getType() != Material.WATER; boolean groundIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ).getType() != Material.WATER;
boolean northIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ-cr).getType() != Material.WATER; boolean northIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ-cr).getType() != Material.WATER;
boolean eastIsSafe = world.getHighestBlockAt(attemptedX+cr, attemptedZ).getType() != Material.WATER;
boolean southIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ+cr).getType() != Material.WATER; boolean southIsSafe = world.getHighestBlockAt(attemptedX, attemptedZ+cr).getType() != Material.WATER;
boolean westIsSafe = world.getHighestBlockAt(attemptedX-cr, attemptedZ).getType() != Material.WATER; boolean westIsSafe = world.getHighestBlockAt(attemptedX-cr, attemptedZ).getType() != Material.WATER;
boolean eastIsSafe = world.getHighestBlockAt(attemptedX+cr, attemptedZ).getType() != Material.WATER;
//TRANSFER - data to class //TRANSFER - data to class
if (groundIsSafe && (northIsSafe || southIsSafe || eastIsSafe || westIsSafe)){ if (groundIsSafe && (northIsSafe || southIsSafe || eastIsSafe || westIsSafe)){
@ -73,8 +80,16 @@ public class RandomTP implements CommandExecutor{
} }
String dir = "north"; String dir = "north";
ArrayList<String> directions = new ArrayList<String>();
if(!northUsed) directions.add("north");
if(!southUsed) directions.add("south");
if(!westUsed) directions.add("west");
if(!eastUsed) directions.add("east");
dir = directions.get((int) Math.floor(Math.random() * directions.size()));
//CHOOSES A RANDOM DIRECTION //CHOOSES A RANDOM DIRECTION
for(int i = 0; i < 1000; i++){ /*for(int i = 0; i < 1000; i++){
double randomDirection = Math.random(); double randomDirection = Math.random();
if (randomDirection < 0.25){ if (randomDirection < 0.25){
if(northUsed){ if(northUsed){
@ -102,7 +117,7 @@ public class RandomTP implements CommandExecutor{
} }
} }
} }*/
//TELEPORT - teleports player to the conflict point //TELEPORT - teleports player to the conflict point

View file

@ -2,7 +2,6 @@ package iie;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;