diff --git a/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java b/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java index 1038e1be2..91b144805 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/script/StructureScript.java @@ -87,6 +87,11 @@ public class StructureScript { return buffer.succeeded(); } + public boolean test(Location location, Random random, Rotation rotation) { + StructureBuffer buffer = new StructureBuffer(location); + return !block.apply(buffer, rotation, random, 0).equals(Block.ReturnLevel.FAIL); + } + public void executeInBuffer(Buffer buffer, Random random, Rotation rotation, int recursions) { block.apply(buffer, rotation, random, recursions); } diff --git a/common/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java b/common/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java new file mode 100644 index 000000000..2c4b04b74 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/async/AsyncStructureFinder.java @@ -0,0 +1,36 @@ +package com.dfsek.terra.async; + +import com.dfsek.terra.api.math.MathUtil; +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.math.vector.Vector3; +import com.dfsek.terra.api.platform.TerraPlugin; +import com.dfsek.terra.api.structures.structure.Rotation; +import com.dfsek.terra.api.util.FastRandom; +import com.dfsek.terra.biome.UserDefinedBiome; +import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; +import com.dfsek.terra.generation.items.TerraStructure; +import net.jafama.FastMath; +import org.jetbrains.annotations.NotNull; + +import java.util.Random; +import java.util.function.Consumer; + +public class AsyncStructureFinder extends AsyncFeatureFinder { + public AsyncStructureFinder(TerraBiomeGrid grid, TerraStructure target, @NotNull Location origin, int startRadius, int maxRadius, Consumer callback, TerraPlugin main) { + super(grid, target, origin, startRadius, maxRadius, callback, main); + setSearchSize(target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation()); + } + + @Override + public Vector3 finalizeVector(Vector3 orig) { + return target.getSpawn().getChunkSpawn(orig.getBlockX(), orig.getBlockZ(), world.getSeed()); + } + + @Override + public boolean isValid(int x, int z, TerraStructure target) { + Location spawn = target.getSpawn().getChunkSpawn(x, z, world.getSeed()).toLocation(world); + if(!((UserDefinedBiome) grid.getBiome(spawn)).getConfig().getStructures().contains(target)) return false; + Random random = new FastRandom(MathUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16), FastMath.floorDiv(spawn.getBlockZ(), 16), world.getSeed())); + return target.getStructure().get(random).test(spawn.setY(target.getSpawnStart().get(random)), random, Rotation.fromDegrees(90 * random.nextInt(4))); + } +} diff --git a/common/src/main/java/com/dfsek/terra/config/factories/StructureFactory.java b/common/src/main/java/com/dfsek/terra/config/factories/StructureFactory.java index f8c54728a..857103336 100644 --- a/common/src/main/java/com/dfsek/terra/config/factories/StructureFactory.java +++ b/common/src/main/java/com/dfsek/terra/config/factories/StructureFactory.java @@ -8,6 +8,6 @@ import com.dfsek.terra.generation.items.TerraStructure; public class StructureFactory implements TerraFactory { @Override public TerraStructure build(StructureTemplate config, TerraPlugin main) throws LoadException { - return new TerraStructure(config.getStructures(), config.getBound(), config.getY(), config.getSpawn(), config.getLoot(), config); + return new TerraStructure(config.getStructures(), config.getY(), config.getSpawn(), config.getLoot(), config); } } diff --git a/common/src/main/java/com/dfsek/terra/generation/items/TerraStructure.java b/common/src/main/java/com/dfsek/terra/generation/items/TerraStructure.java index f1b5bbb93..3fda0fbd6 100644 --- a/common/src/main/java/com/dfsek/terra/generation/items/TerraStructure.java +++ b/common/src/main/java/com/dfsek/terra/generation/items/TerraStructure.java @@ -12,15 +12,13 @@ import java.util.Map; // TODO: implementation public class TerraStructure { private final ProbabilityCollection structure; - private final Range bound; private final Range spawnStart; private final GridSpawn spawn; private final Map loot; private final StructureTemplate template; - public TerraStructure(ProbabilityCollection structures, Range bound, Range spawnStart, GridSpawn spawn, Map loot, StructureTemplate template) { + public TerraStructure(ProbabilityCollection structures, Range spawnStart, GridSpawn spawn, Map loot, StructureTemplate template) { this.structure = structures; - this.bound = bound; this.spawnStart = spawnStart; this.spawn = spawn; this.loot = loot; @@ -35,10 +33,6 @@ public class TerraStructure { return structure; } - public Range getBound() { - return bound; - } - public Range getSpawnStart() { return spawnStart; } diff --git a/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/LocateCommand.java b/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/LocateCommand.java index 342880ef9..a9badebe7 100644 --- a/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/LocateCommand.java +++ b/platforms/bukkit/src/main/java/com/dfsek/terra/bukkit/command/command/structure/LocateCommand.java @@ -1,8 +1,21 @@ package com.dfsek.terra.bukkit.command.command.structure; +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.math.vector.Vector3; +import com.dfsek.terra.async.AsyncStructureFinder; import com.dfsek.terra.bukkit.BukkitCommandSender; +import com.dfsek.terra.bukkit.BukkitWorld; +import com.dfsek.terra.bukkit.TerraBukkitPlugin; import com.dfsek.terra.bukkit.command.WorldCommand; import com.dfsek.terra.config.lang.LangUtil; +import com.dfsek.terra.generation.items.TerraStructure; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -11,6 +24,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.List; +import java.util.Objects; public class LocateCommand extends WorldCommand { private final boolean tp; @@ -32,15 +46,14 @@ public class LocateCommand extends WorldCommand { LangUtil.send("command.structure.invalid-radius", new BukkitCommandSender(sender), args[1]); return true; } - /* TerraStructure s; try { - s = Objects.requireNonNull(((TerraBukkitPlugin) getMain()).getWorld(world).getConfig().getStructure(id)); + s = Objects.requireNonNull(getMain().getWorld(new BukkitWorld(world)).getConfig().getStructure(id)); } catch(IllegalArgumentException | NullPointerException e) { - LangUtil.send("command.structure.invalid", sender, id); + //LangUtil.send("command.structure.invalid", sender, id); return true; } - Bukkit.getScheduler().runTaskAsynchronously(getMain(), new AsyncStructureFinder(((TerraBukkitPlugin) getMain()).getWorld(world).getGrid(), s, sender.getLocation(), 0, maxRadius, (location) -> { + Bukkit.getScheduler().runTaskAsynchronously((TerraBukkitPlugin) getMain(), new AsyncStructureFinder(getMain().getWorld(new BukkitWorld(world)).getGrid(), s, new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()), 0, maxRadius, (location) -> { if(sender.isOnline()) { if(location != null) { ComponentBuilder cm = new ComponentBuilder(String.format("The nearest %s is at ", id.toLowerCase())) @@ -48,14 +61,14 @@ public class LocateCommand extends WorldCommand { .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/minecraft:tp %s %d.0 %.2f %d.0", sender.getName(), location.getBlockX(), sender.getLocation().getY(), location.getBlockZ()))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {new TextComponent("Click to teleport")})) .color(ChatColor.GREEN) - .append(String.format(" (%.1f blocks away)", location.add(new Vector(0, sender.getLocation().getY(), 0)).distance(sender.getLocation().toVector())), ComponentBuilder.FormatRetention.NONE); + .append(String.format(" (%.1f blocks away)", location.add(new Vector3(0, sender.getLocation().getY(), 0)).distance( + new Vector3(sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()))), ComponentBuilder.FormatRetention.NONE); sender.spigot().sendMessage(cm.create()); } else sender.sendMessage("Unable to locate structure. "); } - }, (TerraBukkitPlugin) getMain())); + }, getMain())); - */ return true; }