Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/main/java/me/josvth/randomspawn/RandomSpawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public Location chooseSpawn(World world){
double xmax = yamlHandler.worlds.getDouble(worldName +".spawnarea.x-max", 100);
double zmin = yamlHandler.worlds.getDouble(worldName +".spawnarea.z-min", -100);
double zmax = yamlHandler.worlds.getDouble(worldName +".spawnarea.z-max", 100);

double ymin = yamlHandler.worlds.getDouble(worldName +".spawnarea.y-min", 0);
double ymax = yamlHandler.worlds.getDouble(worldName +".spawnarea.y-max", 255);
if (ymin > ymax){
ymin = 0;
ymax = 255;
}
if (ymin < 0) ymin = 0;
if (ymax > 255) ymax = 255;

// Spawn area thickness near border. If 0 spawns whole area
int thickness = yamlHandler.worlds.getInt(worldName +".spawnarea.thickness", 0);

Expand All @@ -108,7 +116,9 @@ public Location chooseSpawn(World world){
xrand = xcenter + Math.cos(phi) * r;
zrand = zcenter + Math.sin(phi) * r;

y = getValidHighestY(world, xrand, zrand, blacklist);
xrand = (int)xrand + 0.5;
zrand = (int)zrand + 0.5;
y = getValidHighestY(world, xrand, zrand, ymin, ymax, blacklist);

} while (y == -1);

Expand All @@ -121,8 +131,10 @@ public Location chooseSpawn(World world){

xrand = xmin + Math.random()*(xmax - xmin + 1);
zrand = zmin + Math.random()*(zmax - zmin + 1);

y = getValidHighestY(world, xrand, zrand, blacklist);

xrand = (int)xrand + 0.5;
zrand = (int)zrand + 0.5;
y = getValidHighestY(world, xrand, zrand, ymin, ymax, blacklist);

} while (y == -1);

Expand Down Expand Up @@ -150,7 +162,9 @@ else if (side == 2) {
zrand = zmax - borderOffset;
}

y = getValidHighestY(world, xrand, zrand, blacklist);
xrand = (int)xrand + 0.5;
zrand = (int)zrand + 0.5;
y = getValidHighestY(world, xrand, zrand, ymin, ymax, blacklist);

} while (y == -1);

Expand All @@ -162,7 +176,7 @@ else if (side == 2) {
return location;
}

private double getValidHighestY(World world, double x, double z, List<Integer> blacklist) {
private double getValidHighestY(World world, double x, double z, double ymin, double ymax, List<Integer> blacklist) {

world.getChunkAt(new Location(world, x, 0, z)).load();

Expand All @@ -179,17 +193,24 @@ private double getValidHighestY(World world, double x, double z, List<Integer> b
}
if(y == 127) return -1;
}else{
y = 257;
while(y >= 0 && blockid == 0){
y = ymax + 1;
while(blockid == 0){
y--;
if(y < ymin) return -1;
blockid = world.getBlockTypeIdAt((int) x, (int) y, (int) z);
}
if(y == 0) return -1;
//Make sure there's air in the two spaces above the selected location
if (y < 256){
if (world.getBlockTypeIdAt((int) x, (int) y + 1, (int) z) != 0) return -1;
if (y < 255){
if (world.getBlockTypeIdAt((int) x, (int) y + 2, (int) z) != 0) return -1;
}
}
}

if (blacklist.contains(blockid)) return -1;
if (blacklist.contains(81) && world.getBlockTypeIdAt((int) x, (int) (y+1), (int) z) == 81) return -1; // Check for cacti

return y;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/worlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ exampleworld:
x-max: 100
z-min: -100
z-max: 100
y-min: 0
y-max: 255
thickness: 0
spawnblacklist:
- 8
Expand Down