From f088928483032d924f29c6b055df08556a4dbb4d Mon Sep 17 00:00:00 2001 From: dfsek Date: Sun, 19 Dec 2021 23:13:57 -0700 Subject: [PATCH] clean up Structure API --- .../addons/flora/flora/gen/TerraFlora.java | 12 --- .../terra/addons/ore/ores/VanillaOre.java | 13 --- .../shortcut/block/SingletonStructure.java | 16 ---- .../addons/sponge/SpongeSchematicAddon.java | 2 +- .../terra/addons/sponge/SpongeStructure.java | 49 +---------- .../terrascript/script/StructureScript.java | 30 +------ .../script/functions/StructureFunction.java | 29 +++++-- .../dfsek/terra/api/structure/Structure.java | 4 - .../dfsek/terra/api/world/BufferedWorld.java | 82 +++++++++++++++++++ .../dfsek/terra/api/world/WritableWorld.java | 3 + 10 files changed, 112 insertions(+), 128 deletions(-) create mode 100644 common/api/core/src/main/java/com/dfsek/terra/api/world/BufferedWorld.java diff --git a/common/addons/config-flora/src/main/java/com/dfsek/terra/addons/flora/flora/gen/TerraFlora.java b/common/addons/config-flora/src/main/java/com/dfsek/terra/addons/flora/flora/gen/TerraFlora.java index 1fd9daa95..4f5df7e6c 100644 --- a/common/addons/config-flora/src/main/java/com/dfsek/terra/addons/flora/flora/gen/TerraFlora.java +++ b/common/addons/config-flora/src/main/java/com/dfsek/terra/addons/flora/flora/gen/TerraFlora.java @@ -19,13 +19,11 @@ import com.dfsek.terra.api.block.state.properties.base.Properties; import com.dfsek.terra.api.block.state.properties.enums.Direction; import com.dfsek.terra.api.noise.NoiseSampler; import com.dfsek.terra.api.structure.Structure; -import com.dfsek.terra.api.structure.buffer.Buffer; import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.collection.MaterialSet; import com.dfsek.terra.api.util.collection.ProbabilityCollection; import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.world.WritableWorld; -import com.dfsek.terra.api.world.chunk.Chunk; public class TerraFlora implements Structure { @@ -80,16 +78,6 @@ public class TerraFlora implements Structure { return id; } - @Override - public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) { - return generate(location, world, random, rotation); - } - - @Override - public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) { - return generate(buffer.getOrigin(), world, random, rotation); - } - @Override public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { boolean doRotation = testRotation.size() > 0; diff --git a/common/addons/config-ore/src/main/java/com/dfsek/terra/addons/ore/ores/VanillaOre.java b/common/addons/config-ore/src/main/java/com/dfsek/terra/addons/ore/ores/VanillaOre.java index 53a74bd10..cf58bca9f 100644 --- a/common/addons/config-ore/src/main/java/com/dfsek/terra/addons/ore/ores/VanillaOre.java +++ b/common/addons/config-ore/src/main/java/com/dfsek/terra/addons/ore/ores/VanillaOre.java @@ -15,12 +15,10 @@ import java.util.Random; import com.dfsek.terra.api.block.BlockType; import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.structure.Structure; -import com.dfsek.terra.api.structure.buffer.Buffer; import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.collection.MaterialSet; import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.world.WritableWorld; -import com.dfsek.terra.api.world.chunk.Chunk; public class VanillaOre implements Structure { @@ -41,17 +39,6 @@ public class VanillaOre implements Structure { this.materials = materials; } - @Override - public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) { - return false; - } - - @Override - public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) { - generate(buffer.getOrigin(), world, random); - return false; - } - @Override public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { generate(location, world, random); diff --git a/common/addons/structure-block-shortcut/src/main/java/com/dfsek/terra/addons/palette/shortcut/block/SingletonStructure.java b/common/addons/structure-block-shortcut/src/main/java/com/dfsek/terra/addons/palette/shortcut/block/SingletonStructure.java index 2b065dfde..cb6e60735 100644 --- a/common/addons/structure-block-shortcut/src/main/java/com/dfsek/terra/addons/palette/shortcut/block/SingletonStructure.java +++ b/common/addons/structure-block-shortcut/src/main/java/com/dfsek/terra/addons/palette/shortcut/block/SingletonStructure.java @@ -2,13 +2,9 @@ package com.dfsek.terra.addons.palette.shortcut.block; import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.structure.Structure; -import com.dfsek.terra.api.structure.buffer.Buffer; -import com.dfsek.terra.api.structure.buffer.items.BufferedBlock; import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.world.WritableWorld; -import com.dfsek.terra.api.world.chunk.Chunk; -import com.dfsek.terra.api.world.chunk.generation.util.Palette; import java.util.Random; @@ -20,18 +16,6 @@ public class SingletonStructure implements Structure { this.blockState = blockState; } - @Override - public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) { - world.setBlockState(location, blockState); - return true; - } - - @Override - public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) { - world.setBlockState(buffer.getOrigin(), blockState); - return true; - } - @Override public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { world.setBlockState(location, blockState); diff --git a/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeSchematicAddon.java b/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeSchematicAddon.java index 5051ed4ee..7c83b534a 100644 --- a/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeSchematicAddon.java +++ b/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeSchematicAddon.java @@ -96,7 +96,7 @@ public class SpongeSchematicAddon implements AddonInitializer { } } - return new SpongeStructure(states, platform, id); + return new SpongeStructure(states, id); } catch(IOException e) { throw new IllegalArgumentException("Failed to parse Sponge schematic: ", e); } diff --git a/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeStructure.java b/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeStructure.java index 6aa90e10c..27873022b 100644 --- a/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeStructure.java +++ b/common/addons/structure-sponge-loader/src/main/java/com/dfsek/terra/addons/sponge/SpongeStructure.java @@ -7,75 +7,28 @@ package com.dfsek.terra.addons.sponge; -import net.jafama.FastMath; - import java.util.Random; import com.dfsek.terra.api.Platform; import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.structure.Structure; -import com.dfsek.terra.api.structure.buffer.Buffer; -import com.dfsek.terra.api.structure.buffer.items.BufferedBlock; import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.vector.Vector3; import com.dfsek.terra.api.util.vector.integer.Vector2Int; import com.dfsek.terra.api.world.WritableWorld; -import com.dfsek.terra.api.world.chunk.Chunk; public class SpongeStructure implements Structure { private final BlockState[][][] blocks; - private final Platform platform; private final String id; - public SpongeStructure(BlockState[][][] blocks, Platform platform, String id) { + public SpongeStructure(BlockState[][][] blocks, String id) { this.blocks = blocks; - this.platform = platform; this.id = id; } - @Override - public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) { - int bX = location.getBlockX(); - int bY = location.getBlockY(); - int bZ = location.getBlockZ(); - for(int x = 0; x < blocks.length; x++) { - for(int z = 0; z < blocks[x].length; z++) { - Vector2Int r = Vector2Int.of(x, z).rotate(rotation); - int rX = r.getX(); - int rZ = r.getZ(); - if(FastMath.floorDiv(bX + rX, 16) != chunk.getX() || FastMath.floorDiv(bZ + rZ, 16) != chunk.getZ()) { - continue; - } - for(int y = 0; y < blocks[z].length; y++) { - BlockState state = blocks[x][z][y]; - if(state == null) continue; - world.setBlockState(bX + rX, bY + y, bZ + rZ, state); - } - } - } - return true; - } - - @Override - public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) { - for(int x = 0; x < blocks.length; x++) { - for(int z = 0; z < blocks[x].length; z++) { - Vector2Int r = Vector2Int.of(x, z).rotate(rotation); - int rX = r.getX(); - int rZ = r.getZ(); - for(int y = 0; y < blocks[z].length; y++) { - BlockState state = blocks[x][z][y]; - if(state == null) continue; - buffer.addItem(new BufferedBlock(state, true, platform, false), new Vector3(rX, y, rZ)); - } - } - } - return true; - } - @Override public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { int bX = location.getBlockX(); diff --git a/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/StructureScript.java b/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/StructureScript.java index 86cdfc51d..03b770032 100644 --- a/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/StructureScript.java +++ b/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/StructureScript.java @@ -46,14 +46,11 @@ import com.dfsek.terra.api.profiler.ProfileFrame; import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.structure.LootTable; import com.dfsek.terra.api.structure.Structure; -import com.dfsek.terra.api.structure.buffer.Buffer; import com.dfsek.terra.api.structure.buffer.buffers.DirectBuffer; import com.dfsek.terra.api.structure.buffer.buffers.StructureBuffer; import com.dfsek.terra.api.util.Rotation; import com.dfsek.terra.api.util.vector.Vector3; -import com.dfsek.terra.api.world.ServerWorld; import com.dfsek.terra.api.world.WritableWorld; -import com.dfsek.terra.api.world.chunk.Chunk; public class StructureScript implements Structure { @@ -132,24 +129,6 @@ public class StructureScript implements Structure { this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build(); } - @Override - @SuppressWarnings("try") - public boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation) { - try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_chunk:" + id)) { - StructureBuffer buffer = computeBuffer(location, world, random, rotation); - buffer.paste(location, chunk); - return buffer.succeeded(); - } - } - - @Override - @SuppressWarnings("try") - public boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions) { - try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_recursive:" + id)) { - return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, recursions)); - } - } - @Override @SuppressWarnings("try") public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) { @@ -159,11 +138,10 @@ public class StructureScript implements Structure { } } - @SuppressWarnings("try") - public boolean test(Vector3 location, ServerWorld world, Random random, Rotation rotation) { - try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_test:" + id)) { - StructureBuffer buffer = computeBuffer(location, world, random, rotation); - return buffer.succeeded(); + public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation, int recursions) { + try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) { + DirectBuffer buffer = new DirectBuffer(location, world); + return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, recursions)); } } diff --git a/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/functions/StructureFunction.java b/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/functions/StructureFunction.java index cc397b03b..4421849b5 100644 --- a/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/functions/StructureFunction.java +++ b/common/addons/structure-terrascript-loader/src/main/java/com/dfsek/terra/addons/terrascript/script/functions/StructureFunction.java @@ -7,6 +7,8 @@ package com.dfsek.terra.addons.terrascript.script.functions; +import com.dfsek.terra.addons.terrascript.script.StructureScript; + import net.jafama.FastMath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +40,7 @@ public class StructureFunction implements Function { private final Position position; private final Platform platform; private final List> rotations; - + public StructureFunction(Returnable x, Returnable y, Returnable z, Returnable id, List> rotations, Registry registry, Position position, Platform platform) { this.registry = registry; @@ -76,14 +78,25 @@ public class StructureFunction implements Function { rotation1 = Rotation.valueOf(rotString); } catch(IllegalArgumentException e) { LOGGER.warn("Invalid rotation {}", rotString); - return null; + return false; } - - Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(), - FastMath.roundToInt(xz.getZ())); - - return script.generate(new IntermediateBuffer(arguments.getBuffer(), offset), arguments.getWorld(), arguments.getRandom(), - arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1); + + if(script instanceof StructureScript structureScript) { + return structureScript.generate(arguments.getBuffer().getOrigin(), + arguments.getWorld() + .buffer(FastMath.roundToInt(xz.getX()), + y.apply(implementationArguments, variableMap).intValue(), + FastMath.roundToInt(xz.getZ())), + arguments.getRandom(), + arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1); + } + return script.generate(arguments.getBuffer().getOrigin(), + arguments.getWorld() + .buffer(FastMath.roundToInt(xz.getX()), + y.apply(implementationArguments, variableMap).intValue(), + FastMath.roundToInt(xz.getZ())), + arguments.getRandom(), + arguments.getRotation().rotate(rotation1)); }).orElseGet(() -> { LOGGER.error("No such structure {}", app); return false; diff --git a/common/api/core/src/main/java/com/dfsek/terra/api/structure/Structure.java b/common/api/core/src/main/java/com/dfsek/terra/api/structure/Structure.java index 95f102ed3..b0aec87fe 100644 --- a/common/api/core/src/main/java/com/dfsek/terra/api/structure/Structure.java +++ b/common/api/core/src/main/java/com/dfsek/terra/api/structure/Structure.java @@ -18,9 +18,5 @@ import com.dfsek.terra.api.world.chunk.Chunk; public interface Structure extends StringIdentifiable { - boolean generate(Vector3 location, WritableWorld world, Chunk chunk, Random random, Rotation rotation); - - boolean generate(Buffer buffer, WritableWorld world, Random random, Rotation rotation, int recursions); - boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation); } diff --git a/common/api/core/src/main/java/com/dfsek/terra/api/world/BufferedWorld.java b/common/api/core/src/main/java/com/dfsek/terra/api/world/BufferedWorld.java new file mode 100644 index 000000000..79e7f5184 --- /dev/null +++ b/common/api/core/src/main/java/com/dfsek/terra/api/world/BufferedWorld.java @@ -0,0 +1,82 @@ +package com.dfsek.terra.api.world; + +import com.dfsek.terra.api.block.entity.BlockEntity; +import com.dfsek.terra.api.block.state.BlockState; +import com.dfsek.terra.api.config.ConfigPack; +import com.dfsek.terra.api.entity.Entity; +import com.dfsek.terra.api.entity.EntityType; +import com.dfsek.terra.api.world.biome.generation.BiomeProvider; +import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator; + + +public class BufferedWorld implements WritableWorld { + private final WritableWorld delegate; + private final int offsetX, offsetY, offsetZ; + + protected BufferedWorld(WritableWorld delegate, int offsetX, int offsetY, int offsetZ) { + this.delegate = delegate; + this.offsetX = offsetX; + + this.offsetY = offsetY; + this.offsetZ = offsetZ; + } + + @Override + public Object getHandle() { + return delegate.getHandle(); + } + + @Override + public BlockState getBlockState(int x, int y, int z) { + return delegate.getBlockState(x + offsetX, y + offsetY, z + offsetZ); + } + + @Override + public BlockEntity getBlockEntity(int x, int y, int z) { + return delegate.getBlockEntity(x + offsetX, y + offsetY, z + offsetZ); + } + + @Override + public long getSeed() { + return delegate.getSeed(); + } + + @Override + public int getMaxHeight() { + return delegate.getMaxHeight(); + } + + @Override + public int getMinHeight() { + return delegate.getMinHeight(); + } + + @Override + public ChunkGenerator getGenerator() { + return delegate.getGenerator(); + } + + @Override + public BiomeProvider getBiomeProvider() { + return delegate.getBiomeProvider(); + } + + @Override + public ConfigPack getPack() { + return delegate.getPack(); + } + + @Override + public void setBlockState(int x, int y, int z, BlockState data, boolean physics) { + delegate.setBlockState(x + offsetX, y + offsetY, z + offsetZ, data, physics); + } + + @Override + public Entity spawnEntity(double x, double y, double z, EntityType entityType) { + return delegate.spawnEntity(x + offsetX, y + offsetY, z + offsetZ, entityType); + } + + public WritableWorld getDelegate() { + return delegate; + } +} diff --git a/common/api/core/src/main/java/com/dfsek/terra/api/world/WritableWorld.java b/common/api/core/src/main/java/com/dfsek/terra/api/world/WritableWorld.java index b385e64ba..676270c3f 100644 --- a/common/api/core/src/main/java/com/dfsek/terra/api/world/WritableWorld.java +++ b/common/api/core/src/main/java/com/dfsek/terra/api/world/WritableWorld.java @@ -28,4 +28,7 @@ public interface WritableWorld extends ReadableWorld { Entity spawnEntity(double x, double y, double z, EntityType entityType); + default BufferedWorld buffer(int offsetX, int offsetY, int offsetZ) { + return new BufferedWorld(this, offsetX, offsetY, offsetZ); + } }