Clean up Async thing finders

This commit is contained in:
dfsek 2020-11-08 19:38:49 -07:00
parent b1a954b433
commit 7d395a4347
5 changed files with 133 additions and 125 deletions

View File

@ -22,7 +22,6 @@ public class TerraProfiler extends WorldProfiler {
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "StructurePasteTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "SnowTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "CaveBlockUpdate");
profilerMap.put(w, this);
}
public static TerraProfiler fromWorld(World w) {

View File

@ -1,69 +1,21 @@
package com.dfsek.terra.async;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.config.lang.LangUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.generation.GenerationPhase;
import java.util.function.Consumer;
/**
* Runnable that locates a biome asynchronously
*/
public class AsyncBiomeFinder implements Runnable {
private final TerraBiomeGrid grid;
private final int centerX;
private final int centerZ;
private final int startRadius;
private final int maxRadius;
private final Biome target;
private final Player p;
private final boolean tp;
public class AsyncBiomeFinder extends AsyncFeatureFinder<Biome> {
public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, Player p, int startRadius, int maxRadius, boolean tp) {
this.grid = grid;
this.target = target;
this.p = p;
this.centerX = p.getLocation().getBlockX();
this.centerZ = p.getLocation().getBlockZ();
this.startRadius = startRadius;
this.maxRadius = maxRadius;
this.tp = tp;
}
@Override
public void run() {
int x = centerX;
int z = centerZ;
int run = 1;
boolean toggle = true;
main:
for(int i = startRadius; i < maxRadius; i++) {
for(int j = 0; j < run; j++) {
if(checkBiome(x, z).equals(target)) break main;
if(toggle) x++;
else x--;
}
for(int j = 0; j < run; j++) {
if(checkBiome(x, z).equals(target)) break main;
if(toggle) z++;
else z--;
}
run++;
toggle = !toggle;
}
if(p.isOnline()) {
if(checkBiome(x, z).equals(target)) {
LangUtil.send("command.biome.biome-found", p, String.valueOf(x), String.valueOf(z));
if(tp) {
int finalX = x;
int finalZ = z;
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> p.teleport(new Location(p.getWorld(), finalX, p.getLocation().getY(), finalZ)));
}
} else LangUtil.send("command.biome.unable-to-locate", p);
}
public AsyncBiomeFinder(TerraBiomeGrid grid, Biome target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
super(grid, target, origin, startRadius, maxRadius, callback);
}
/**
@ -73,7 +25,13 @@ public class AsyncBiomeFinder implements Runnable {
* @param z Z coordinate
* @return Biome at coordinates
*/
private Biome checkBiome(int x, int z) {
return grid.getBiome(x, z, GenerationPhase.POST_GEN);
@Override
public boolean isValid(int x, int z, Biome target) {
return getGrid().getBiome(x, z, GenerationPhase.POST_GEN).equals(target);
}
@Override
public Vector finalizeVector(Vector orig) {
return orig;
}
}

View File

@ -0,0 +1,96 @@
package com.dfsek.terra.async;
import com.dfsek.terra.Terra;
import com.dfsek.terra.biome.TerraBiomeGrid;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
public abstract class AsyncFeatureFinder<T> implements Runnable {
private final TerraBiomeGrid grid;
private final T target;
private final int startRadius;
private final int maxRadius;
private final int centerX;
private final int centerZ;
private final World world;
private final Consumer<Vector> callback;
private int searchSize = 1;
public AsyncFeatureFinder(TerraBiomeGrid grid, T target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
this.grid = grid;
this.target = target;
this.startRadius = startRadius;
this.maxRadius = maxRadius;
this.centerX = origin.getBlockX();
this.centerZ = origin.getBlockZ();
this.world = origin.getWorld();
this.callback = callback;
}
@Override
public void run() {
int x = centerX;
int z = centerZ;
x /= searchSize;
z /= searchSize;
int run = 1;
boolean toggle = true;
boolean found = false;
main:
for(int i = startRadius; i < maxRadius; i++) {
for(int j = 0; j < run; j++) {
if(isValid(x, z, target)) {
found = true;
break main;
}
if(toggle) x += 1;
else x -= 1;
}
for(int j = 0; j < run; j++) {
if(isValid(x, z, target)) {
found = true;
break main;
}
if(toggle) z += 1;
else z -= 1;
}
run++;
toggle = !toggle;
}
Vector finalSpawn = found ? finalizeVector(new Vector(x, 0, z)) : null;
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> callback.accept(finalSpawn));
}
public abstract Vector finalizeVector(Vector orig);
public abstract boolean isValid(int x, int z, T target);
public T getTarget() {
return target;
}
public World getWorld() {
return world;
}
public TerraBiomeGrid getGrid() {
return grid;
}
public int getSearchSize() {
return searchSize;
}
public void setSearchSize(int searchSize) {
this.searchSize = searchSize;
}
}

View File

@ -1,12 +1,10 @@
package com.dfsek.terra.async;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.genconfig.structure.StructureConfig;
import com.dfsek.terra.structure.Structure;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.Vector;
@ -18,68 +16,10 @@ import java.util.function.Consumer;
/**
* Runnable to locate structures asynchronously
*/
public class AsyncStructureFinder implements Runnable {
private final TerraBiomeGrid grid;
private final StructureConfig target;
private final int startRadius;
private final int maxRadius;
private final int centerX;
private final int centerZ;
private final long seed;
private final World world;
private final Consumer<Vector> callback;
public class AsyncStructureFinder extends AsyncFeatureFinder<StructureConfig> {
public AsyncStructureFinder(TerraBiomeGrid grid, StructureConfig target, @NotNull Location origin, int startRadius, int maxRadius, Consumer<Vector> callback) {
this.grid = grid;
this.target = target;
this.startRadius = startRadius;
this.maxRadius = maxRadius;
this.centerX = origin.getBlockX();
this.centerZ = origin.getBlockZ();
this.world = origin.getWorld();
assert world != null;
this.seed = world.getSeed();
this.callback = callback;
}
@Override
public void run() {
int x = centerX;
int z = centerZ;
final int wid = target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation();
x /= wid;
z /= wid;
int run = 1;
boolean toggle = true;
boolean found = false;
Vector spawn = null;
main:
for(int i = startRadius; i < maxRadius; i++) {
for(int j = 0; j < run; j++) {
spawn = target.getSpawn().getChunkSpawn(x, z, seed);
if(isValidSpawn(spawn.getBlockX(), spawn.getBlockZ())) {
found = true;
break main;
}
if(toggle) x += 1;
else x -= 1;
}
for(int j = 0; j < run; j++) {
spawn = target.getSpawn().getChunkSpawn(x, z, seed);
if(isValidSpawn(spawn.getBlockX(), spawn.getBlockZ())) {
found = true;
break main;
}
if(toggle) z += 1;
else z -= 1;
}
run++;
toggle = !toggle;
}
final Vector finalSpawn = found ? spawn : null;
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> callback.accept(finalSpawn));
super(grid, target, origin, startRadius, maxRadius, callback);
setSearchSize(target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation());
}
/**
@ -89,9 +29,10 @@ public class AsyncStructureFinder implements Runnable {
* @param z Z coordinate
* @return Whether location is a valid spawn for StructureConfig
*/
private boolean isValidSpawn(int x, int z) {
Location spawn = target.getSpawn().getNearestSpawn(x, z, world.getSeed()).toLocation(world);
if(!TerraWorld.getWorld(world).getConfig().getBiome((UserDefinedBiome) grid.getBiome(spawn)).getStructures().contains(target))
public boolean isValid(int x, int z, StructureConfig target) {
World world = getWorld();
Location spawn = target.getSpawn().getChunkSpawn(x, z, world.getSeed()).toLocation(world);
if(!TerraWorld.getWorld(world).getConfig().getBiome((UserDefinedBiome) getGrid().getBiome(spawn)).getStructures().contains(target))
return false;
Random r2 = new Random(spawn.hashCode());
Structure struc = target.getStructure(r2);
@ -104,4 +45,9 @@ public class AsyncStructureFinder implements Runnable {
}
return false;
}
@Override
public Vector finalizeVector(Vector orig) {
return getTarget().getSpawn().getNearestSpawn(orig.getBlockX() * getSearchSize(), orig.getBlockZ() * getSearchSize(), getWorld().getSeed());
}
}

View File

@ -7,6 +7,7 @@ import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.generation.TerraChunkGenerator;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -43,7 +44,15 @@ public class BiomeLocateCommand extends WorldCommand {
LangUtil.send("command.biome.invalid", sender, id);
return true;
}
Bukkit.getScheduler().runTaskAsynchronously(Terra.getInstance(), new AsyncBiomeFinder(TerraWorld.getWorld(world).getGrid(), b, sender, 0, maxRadius, tp));
Bukkit.getScheduler().runTaskAsynchronously(Terra.getInstance(), new AsyncBiomeFinder(TerraWorld.getWorld(world).getGrid(), b, sender.getLocation(), 0, maxRadius, location -> {
if(location != null) {
LangUtil.send("command.biome.biome-found", sender, String.valueOf(location.getBlockX()), String.valueOf(location.getBlockZ()));
if(tp) {
Location l = new Location(sender.getWorld(), location.getX(), sender.getLocation().getY(), location.getZ());
Bukkit.getScheduler().runTask(Terra.getInstance(), () -> sender.teleport(l));
}
} else LangUtil.send("command.biome.unable-to-locate", sender);
}));
return true;
}