Added spawn on square perimeter

This commit is contained in:
patinok
2012-05-12 20:05:18 +03:00
parent 5ba8954af7
commit 0ade0f99f5
2 changed files with 45 additions and 17 deletions

View File

@@ -75,48 +75,75 @@ public class RandomSpawn extends JavaPlugin{
int xmax = yamlHandler.worlds.getInt(worldName +".spawnarea.x-max", 100); int xmax = yamlHandler.worlds.getInt(worldName +".spawnarea.x-max", 100);
int zmin = yamlHandler.worlds.getInt(worldName +".spawnarea.z-min", -100); int zmin = yamlHandler.worlds.getInt(worldName +".spawnarea.z-min", -100);
int zmax = yamlHandler.worlds.getInt(worldName +".spawnarea.z-max", 100); int zmax = yamlHandler.worlds.getInt(worldName +".spawnarea.z-max", 100);
// Spawn area thickness near border. If 0 spawns whole area
int thickness = yamlHandler.worlds.getInt(worldName +".spawnarea.thickness", 0);
int xrand = 0; int xrand = 0;
int zrand = 0; int zrand = 0;
int y = -1; int y = -1;
do { do {
xrand = xmin + (int) ( Math.random()*(xmax - xmin) + 0.5 ); if (thickness <= 0) {
zrand = zmin + (int) ( Math.random()*(zmax - zmin) + 0.5 ); xrand = xmin + (int) ( Math.random()*(xmax - xmin + 1) );
y = getValidHighestBlock(world, xrand,zrand); zrand = zmin + (int) ( Math.random()*(zmax - zmin + 1) );
}while (y == -1); }
else {
int side = (int) (Math.random() * 4d);
int borderOffset = (int) (Math.random() * (double) thickness);
if (side == 0) {
xrand = xmin + borderOffset;
// Also balancing probability considering thickness
zrand = zmin + (int) ( Math.random() * (zmax - zmin + 1 - 2*thickness) ) + thickness;
}
else if (side == 1) {
xrand = xmax - borderOffset;
zrand = zmin + (int) ( Math.random() * (zmax - zmin + 1 - 2*thickness) ) + thickness;
}
else if (side == 2) {
xrand = xmin + (int) ( Math.random() * (xmax - xmin + 1) );
zrand = zmin + borderOffset;
}
else {
xrand = xmin + (int) ( Math.random() * (xmax - xmin + 1) );
zrand = zmax - borderOffset;
}
logDebug("Choosed side for spawn: " + side + "; x = " + xrand + "; z = " + zrand);
}
y = getValidHighestBlock(world, xrand, zrand);
} while (y == -1);
return new Location( return new Location(
world, world,
Double.parseDouble(Integer.toString(xrand)) + 0.5, Double.parseDouble(Integer.toString(xrand)),
Double.parseDouble(Integer.toString(y)), Double.parseDouble(Integer.toString(y)),
Double.parseDouble(Integer.toString(zrand)) + 0.5 Double.parseDouble(Integer.toString(zrand))
); );
} }
private int getValidHighestBlock(World world, int x, int z) { private int getValidHighestBlock(World world, int x, int z) {
world.getChunkAt(new Location(world, x, 0, z)).load(); world.getChunkAt(new Location(world, x, 0, z)).load();
int y = world.getHighestBlockYAt(x, z); int y = world.getHighestBlockYAt(x, z);
int blockid = world.getBlockTypeIdAt(x, y - 1, z); int blockid = world.getBlockTypeIdAt(x, y - 1, z);
if (blockid == 8) return -1; if (blockid == 8) return -1;
if (blockid == 9) return -1; if (blockid == 9) return -1;
if (blockid == 10) return -1; if (blockid == 10) return -1;
if (blockid == 11) return -1; if (blockid == 11) return -1;
if (blockid == 51) return -1; if (blockid == 51) return -1;
if (blockid == 18) return -1; if (blockid == 18) return -1;
blockid = world.getBlockTypeIdAt(x, y + 1, z); blockid = world.getBlockTypeIdAt(x, y + 1, z);
if (blockid == 81) return -1; if (blockid == 81) return -1;
return y; return y;
} }
// Methods for a save landing :) // Methods for a save landing :)
public void sendGround(Player player, Location location){ public void sendGround(Player player, Location location){
location.getChunk().load(); location.getChunk().load();
@@ -127,4 +154,4 @@ public class RandomSpawn extends JavaPlugin{
player.sendBlockChange(block.getLocation(), block.getType(), block.getData()); player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
} }
} }
} }

View File

@@ -11,4 +11,5 @@ exampleworld:
x-min: -100 x-min: -100
x-max: 100 x-max: 100
z-min: -100 z-min: -100
z-max: 100 z-max: 100
thickness: 1