This commit is contained in:
iie 2017-05-31 21:54:00 +02:00 committed by
parent b8e9810e18
commit 0b3743cb01

View file

@ -22,17 +22,19 @@ public class Main extends JavaPlugin
private double size, private double size,
usableSize, usableSize,
borderCenterX, borderCenterX,
borderCenterZ; borderCenterZ,
x,z;
private int centerX, centerZ, centerY,
northZ, southZ, eastX, westX,
northY, southY, eastY, westY;
private int x,z, private Material centerGroundMaterial, centerFeetMaterial, centerHeadMaterial,
centerY, northY, southY, eastY, westY, northGroundMaterial, northFeetMaterial, northHeadMaterial,
northZ, southZ, eastX, westX; southGroundMaterial, southFeetMaterial, southHeadMaterial,
eastGroundMaterial, eastFeetMaterial, eastHeadMaterial,
private Material centerGroundMaterial, centerHeadMaterial, westGroundMaterial, westFeetMaterial, westHeadMaterial;
northGroundMaterial, northHeadMaterial,
southGroundMaterial, southHeadMaterial,
eastGroundMaterial, eastHeadMaterial,
westGroundMaterial, westHeadMaterial;
private Location center, private Location center,
north, north,
@ -54,21 +56,61 @@ public class Main extends JavaPlugin
public void onEnable() public void onEnable()
{ {
world = (CraftWorld) Bukkit.getWorld("World");
border = world.getHandle().getWorldBorder();
newLocation();
getCommand("randomtp").setExecutor(this); getCommand("randomtp").setExecutor(this);
}
world = (CraftWorld) Bukkit.getWorld("World");
border = world.getHandle().getWorldBorder();
newLocation();
}
/*================================================================================================*/ /*================================================================================================*/
public boolean onCommand(CommandSender sender, Command label, String command, String[] args) public boolean onCommand(CommandSender sender, Command label, String command, String[] args)
{ {
if (sender.isOp()) return rtp(Bukkit.getPlayer(args[0])); if (sender.isOp()) return rtp(Bukkit.getPlayer(args[0])); else return false;
}
/*================================================================================================*/
public synchronized boolean rtp(Player player)
{
if (player == null)
return false;
else return false; //if border has changed, or no positions available, find new location
if ((centerUsed && northUsed && southUsed && eastUsed && westUsed) ||
(borderCenterX != border.getCenterX() ||
borderCenterZ != border.getCenterZ() ||
size != border.getSize())
&& !newLocation())
{
//message player and return false if unable to find new location
player.sendMessage("§c could not find a location in 10,000 attempts");
player.sendMessage("§c (sorry bud... I did try!)");
return false;
}
//randomly select one of the open positions and teleport the player there, then remove the position
switch(availableDirections.charAt(dir = (int) Math.floor(Math.random() * availableDirections.length())))
{
case (char) 1: player.teleport(center); centerUsed = true; break;
case (char) 2: player.teleport(north ); northUsed = true; break;
case (char) 3: player.teleport(south ); southUsed = true; break;
case (char) 4: player.teleport(east ); eastUsed = true; break;
case (char) 5: player.teleport(west ); westUsed = true; break;
}
availableDirections.deleteCharAt(dir);
//imply that our server has a personality
player.sendMessage("§7 *poof*");
//if all 5 positions have been teleported to, choose a new location
if (centerUsed && northUsed && southUsed && eastUsed && westUsed)
newLocation();
return true;
} }
/*================================================================================================*/ /*================================================================================================*/
@ -80,127 +122,116 @@ public class Main extends JavaPlugin
borderCenterX = border.getCenterX(); borderCenterX = border.getCenterX();
borderCenterZ = border.getCenterZ(); borderCenterZ = border.getCenterZ();
//MAXIMUM TEN THOUSAND ATTEMPTS //maximum ten thousand attempts
for (int i = 0; i < 10000; i++) for (int i = 0; i < 10000; i++)
{ {
//RANDOMLY CHOOSE AN X AND Z WITHIN WORLD BORDER //choose an x and z inside the current world border, allowing a margin for the outer positions
x = (int) (Math.floor((Math.random() - 0.5) * usableSize) + border.getCenterX()); centerX = (int) (Math.floor((Math.random() - 0.5) * usableSize) + border.getCenterX());
z = (int) (Math.floor((Math.random() - 0.5) * usableSize) + border.getCenterZ()); centerZ = (int) (Math.floor((Math.random() - 0.5) * usableSize) + border.getCenterZ());
//GET OTHER COORDINATES //get center of block
centerY = world.getHighestBlockYAt( x , z ); x = centerX + .5;
northZ = z - radius; northY = world.getHighestBlockYAt( x , northZ ); z = centerZ + .5;
southZ = z + radius; southY = world.getHighestBlockYAt( x , southZ );
eastX = x + radius; eastY = world.getHighestBlockYAt( eastX , z );
westX = x - radius; westY = world.getHighestBlockYAt( westX , z );
//GET MATERIALS FOR GROUND AND HEAD-HEIGHT BLOCKS AT EACH POSITION //get other coordinates
centerGroundMaterial = world.getBlockAt( x , centerY -1 , z ).getType(); northZ = centerZ - radius;
northGroundMaterial = world.getBlockAt( x , northY -1 , northZ ).getType(); southZ = centerZ + radius;
southGroundMaterial = world.getBlockAt( x , southY -1 , southZ ).getType(); eastX = centerX + radius;
eastGroundMaterial = world.getBlockAt( eastX , eastY -1 , z ).getType(); westX = centerX - radius;
westGroundMaterial = world.getBlockAt( westX , westY -1 , z ).getType();
centerHeadMaterial = world.getBlockAt( x , centerY +1 , z ).getType(); centerY = world.getHighestBlockYAt( centerX , centerZ );
northHeadMaterial = world.getBlockAt( x , northY +1 , northZ ).getType(); northY = world.getHighestBlockYAt( centerX , northZ );
southHeadMaterial = world.getBlockAt( x , southY +1 , southZ ).getType(); southY = world.getHighestBlockYAt( centerX , southZ );
eastHeadMaterial = world.getBlockAt( eastX , eastY +1 , z ).getType(); eastY = world.getHighestBlockYAt( eastX , centerZ );
westHeadMaterial = world.getBlockAt( westX , westY +1 , z ).getType(); westY = world.getHighestBlockYAt( westX , centerZ );
//CONFIRM THAT ALL FIVE POSITIONS ARE ON SOLID GROUND WITH AIR AT HEAD HEIGHT //get materials for ground, feet-height and head-height blocks at each of the five positions
if (centerHeadMaterial .equals(Material.AIR) && centerGroundMaterial = world.getBlockAt( centerX , centerY -1 , centerZ ).getType();
northHeadMaterial .equals(Material.AIR) && northGroundMaterial = world.getBlockAt( centerX , northY -1 , northZ ).getType();
southHeadMaterial .equals(Material.AIR) && southGroundMaterial = world.getBlockAt( centerX , southY -1 , southZ ).getType();
eastHeadMaterial .equals(Material.AIR) && eastGroundMaterial = world.getBlockAt( eastX , eastY -1 , centerZ ).getType();
westHeadMaterial .equals(Material.AIR) && westGroundMaterial = world.getBlockAt( westX , westY -1 , centerZ ).getType();
!centerGroundMaterial.equals(Material.STATIONARY_WATER) && centerFeetMaterial = world.getBlockAt( centerX , centerY , centerZ ).getType();
!northGroundMaterial .equals(Material.STATIONARY_WATER) && northFeetMaterial = world.getBlockAt( centerX , northY , northZ ).getType();
!southGroundMaterial .equals(Material.STATIONARY_WATER) && southFeetMaterial = world.getBlockAt( centerX , southY , southZ ).getType();
!eastGroundMaterial .equals(Material.STATIONARY_WATER) && eastFeetMaterial = world.getBlockAt( eastX , eastY , centerZ ).getType();
!westGroundMaterial .equals(Material.STATIONARY_WATER) && westFeetMaterial = world.getBlockAt( westX , westY , centerZ ).getType();
!centerGroundMaterial.equals(Material.WATER) && centerHeadMaterial = world.getBlockAt( centerX , centerY +1 , centerZ ).getType();
!northGroundMaterial .equals(Material.WATER) && northHeadMaterial = world.getBlockAt( centerX , northY +1 , northZ ).getType();
!southGroundMaterial .equals(Material.WATER) && southHeadMaterial = world.getBlockAt( centerX , southY +1 , southZ ).getType();
!eastGroundMaterial .equals(Material.WATER) && eastHeadMaterial = world.getBlockAt( eastX , eastY +1 , centerZ ).getType();
!westGroundMaterial .equals(Material.WATER) && westHeadMaterial = world.getBlockAt( westX , westY +1 , centerZ ).getType();
//test that all five positions are on solid ground with air at head height
if (centerHeadMaterial == Material.AIR &&
northHeadMaterial == Material.AIR &&
southHeadMaterial == Material.AIR &&
eastHeadMaterial == Material.AIR &&
westHeadMaterial == Material.AIR &&
!centerGroundMaterial.equals(Material.STATIONARY_LAVA) && centerFeetMaterial == Material.AIR &&
!northGroundMaterial .equals(Material.STATIONARY_LAVA) && northFeetMaterial == Material.AIR &&
!southGroundMaterial .equals(Material.STATIONARY_LAVA) && southFeetMaterial == Material.AIR &&
!eastGroundMaterial .equals(Material.STATIONARY_LAVA) && eastFeetMaterial == Material.AIR &&
!westGroundMaterial .equals(Material.STATIONARY_LAVA) && westFeetMaterial == Material.AIR &&
!centerGroundMaterial.equals(Material.LAVA) && centerGroundMaterial != Material.AIR &&
!northGroundMaterial .equals(Material.LAVA) && northGroundMaterial != Material.AIR &&
!southGroundMaterial .equals(Material.LAVA) && southGroundMaterial != Material.AIR &&
!eastGroundMaterial .equals(Material.LAVA) && eastGroundMaterial != Material.AIR &&
!westGroundMaterial .equals(Material.LAVA) && westGroundMaterial != Material.AIR &&
!centerGroundMaterial.equals(Material.AIR) && centerGroundMaterial != Material.STATIONARY_WATER &&
!northGroundMaterial .equals(Material.AIR) && northGroundMaterial != Material.STATIONARY_WATER &&
!southGroundMaterial .equals(Material.AIR) && southGroundMaterial != Material.STATIONARY_WATER &&
!eastGroundMaterial .equals(Material.AIR) && eastGroundMaterial != Material.STATIONARY_WATER &&
!westGroundMaterial .equals(Material.AIR)) westGroundMaterial != Material.STATIONARY_WATER &&
centerGroundMaterial != Material.WATER &&
northGroundMaterial != Material.WATER &&
southGroundMaterial != Material.WATER &&
eastGroundMaterial != Material.WATER &&
westGroundMaterial != Material.WATER &&
centerGroundMaterial != Material.STATIONARY_LAVA &&
northGroundMaterial != Material.STATIONARY_LAVA &&
southGroundMaterial != Material.STATIONARY_LAVA &&
eastGroundMaterial != Material.STATIONARY_LAVA &&
westGroundMaterial != Material.STATIONARY_LAVA &&
centerGroundMaterial != Material.LAVA &&
northGroundMaterial != Material.LAVA &&
southGroundMaterial != Material.LAVA &&
eastGroundMaterial != Material.LAVA &&
westGroundMaterial != Material.LAVA)
{ {
//IF LOCATION VALID, SET NEW POSITIONS AND RESET TRACKING VARIABLES //set new positions and reset
center = world.getBlockAt( x , centerY , z ).getLocation(); center = new Location( world, x , (double) centerY , z );
north = world.getBlockAt( x , northY , northZ ).getLocation(); north = new Location( world, x , (double) northY , northZ + .5 );
south = world.getBlockAt( x , southY , southZ ).getLocation(); south = new Location( world, x , (double) southY , southZ + .5 );
east = world.getBlockAt( eastX , eastY , z ).getLocation(); east = new Location( world, eastX + .5 , (double) eastY , z );
west = world.getBlockAt( westX , westY , z ).getLocation(); west = new Location( world, westX + .5 , (double) westY , z );
availableDirections.setLength(0); availableDirections.setLength(0);
availableDirections.append(chars); availableDirections.append(chars);
centerUsed = northUsed = southUsed = eastUsed = westUsed = false; centerUsed =
northUsed =
southUsed =
eastUsed =
westUsed = false;
return true; return true;
} }
} }
centerUsed = northUsed = southUsed = eastUsed = westUsed = true; centerUsed =
northUsed =
southUsed =
eastUsed =
westUsed = true;
return false; return false;
} }
/*================================================================================================*/
public synchronized boolean rtp(Player player)
{
if (player == null)
return false;
//IF BORDER HAS CHANGED, OR NO POSITIONS AVAILABLE, FIND NEW LOCATION
if ((centerUsed && northUsed && southUsed && eastUsed && westUsed) ||
(borderCenterX != border.getCenterX() ||
borderCenterZ != border.getCenterZ() ||
size != border.getSize())
&& !newLocation())
{
//MESSAGE PLAYER AND RETURN FALSE IF UNABLE TO FIND NEW LOCATION.
player.sendMessage("could not find location in 10,000 attempts");
player.sendMessage("... sorry bud. I did try.");
return false;
}
//RANDOMLY SELECT ONE OF THE OPEN POSITIONS AND TELEPORT THE PLAYER THERE. THEN, REMOVE THE POSITION
switch(availableDirections.charAt(dir = (int) Math.floor(Math.random() * availableDirections.length())))
{
case (char) 1: player.teleport(center); centerUsed = true; break;
case (char) 2: player.teleport(north ); northUsed = true; break;
case (char) 3: player.teleport(south ); southUsed = true; break;
case (char) 4: player.teleport(east ); eastUsed = true; break;
case (char) 5: player.teleport(west ); westUsed = true; break;
}
availableDirections.deleteCharAt(dir);
//IF ALL 5 POSITIONS HAVE BEEN TELEPORTED TO, CHOOSE NEW LOCATION
if (centerUsed && northUsed && southUsed && eastUsed && westUsed)
newLocation();
return true;
}
} }