store structures in Buffer

This commit is contained in:
dfsek
2020-12-24 02:40:28 -07:00
parent 7127943298
commit 058ec9f24d
29 changed files with 173 additions and 70 deletions

View File

@@ -1,7 +1,7 @@
package com.dfsek.terra.api.structures.parser.lang;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import java.util.List;
@@ -20,9 +20,9 @@ public class Block implements Item<Block.ReturnLevel> {
}
@Override
public synchronized ReturnLevel apply(Location location, Rotation rotation, int recursions) {
public synchronized ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
for(Item<?> item : items) {
Object result = item.apply(location, rotation, recursions);
Object result = item.apply(buffer, rotation, recursions);
if(result instanceof ReturnLevel) {
ReturnLevel level = (ReturnLevel) result;
if(!level.equals(ReturnLevel.NONE)) return level;

View File

@@ -1,11 +1,11 @@
package com.dfsek.terra.api.structures.parser.lang;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public interface Item<T> {
T apply(Location location, Rotation rotation, int recursions);
T apply(Buffer buffer, Rotation rotation, int recursions);
Position getPosition();
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.constants;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class ConstantExpression<T> implements Returnable<T> {
@@ -15,7 +15,7 @@ public abstract class ConstantExpression<T> implements Returnable<T> {
}
@Override
public T apply(Location location, Rotation rotation, int recursions) {
public T apply(Buffer buffer, Rotation rotation, int recursions) {
return constant;
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath;
@@ -20,7 +20,7 @@ public class AbsFunction extends MathFunction {
}
@Override
public Number apply(Location location, Rotation rotation, int recursions) {
return FastMath.abs(returnable.apply(location, rotation, recursions).doubleValue());
public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.abs(returnable.apply(buffer, rotation, recursions).doubleValue());
}
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath;
@@ -22,7 +22,7 @@ public class PowFunction extends MathFunction {
}
@Override
public Number apply(Location location, Rotation rotation, int recursions) {
return FastMath.pow(base.apply(location, rotation, recursions).doubleValue(), power.apply(location, rotation, recursions).doubleValue());
public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.pow(base.apply(buffer, rotation, recursions).doubleValue(), power.apply(buffer, rotation, recursions).doubleValue());
}
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.functions.builtin;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath;
@@ -20,7 +20,7 @@ public class SqrtFunction extends MathFunction {
}
@Override
public Number apply(Location location, Rotation rotation, int recursions) {
return FastMath.sqrt(returnable.apply(location, rotation, recursions).doubleValue());
public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return FastMath.sqrt(returnable.apply(buffer, rotation, recursions).doubleValue());
}
}

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class BreakKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class BreakKeyword implements Keyword<Block.ReturnLevel> {
}
@Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.BREAK;
}

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class ContinueKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class ContinueKeyword implements Keyword<Block.ReturnLevel> {
}
@Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.CONTINUE;
}

View File

@@ -1,10 +1,10 @@
package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class IfKeyword implements Keyword<Block.ReturnLevel> {
@@ -19,8 +19,8 @@ public class IfKeyword implements Keyword<Block.ReturnLevel> {
}
@Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
if(statement.apply(location, rotation, recursions)) return conditional.apply(location, rotation, recursions);
public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
if(statement.apply(buffer, rotation, recursions)) return conditional.apply(buffer, rotation, recursions);
return Block.ReturnLevel.NONE;
}

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class ReturnKeyword implements Keyword<Block.ReturnLevel> {
@@ -14,7 +14,7 @@ public class ReturnKeyword implements Keyword<Block.ReturnLevel> {
}
@Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
return Block.ReturnLevel.RETURN;
}

View File

@@ -1,10 +1,10 @@
package com.dfsek.terra.api.structures.parser.lang.keywords;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Block;
import com.dfsek.terra.api.structures.parser.lang.Keyword;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class WhileKeyword implements Keyword<Block.ReturnLevel> {
@@ -19,9 +19,9 @@ public class WhileKeyword implements Keyword<Block.ReturnLevel> {
}
@Override
public Block.ReturnLevel apply(Location location, Rotation rotation, int recursions) {
while(statement.apply(location, rotation, recursions)) {
Block.ReturnLevel level = conditional.apply(location, rotation, recursions);
public Block.ReturnLevel apply(Buffer buffer, Rotation rotation, int recursions) {
while(statement.apply(buffer, rotation, recursions)) {
Block.ReturnLevel level = conditional.apply(buffer, rotation, recursions);
if(level.equals(Block.ReturnLevel.BREAK)) break;
if(level.equals(Block.ReturnLevel.RETURN)) return Block.ReturnLevel.RETURN;
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.operations;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class BinaryOperation<I, O> implements Returnable<O> {
@@ -24,7 +24,7 @@ public abstract class BinaryOperation<I, O> implements Returnable<O> {
}
@Override
public O apply(Location location, Rotation rotation, int recursions) {
return apply(left.apply(location, rotation, recursions), right.apply(location, rotation, recursions));
public O apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(left.apply(buffer, rotation, recursions), right.apply(buffer, rotation, recursions));
}
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.operations;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public abstract class UnaryOperation<T> implements Returnable<T> {
@@ -17,8 +17,8 @@ public abstract class UnaryOperation<T> implements Returnable<T> {
public abstract T apply(T input);
@Override
public T apply(Location location, Rotation rotation, int recursions) {
return apply(input.apply(location, rotation, recursions));
public T apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(input.apply(buffer, rotation, recursions));
}
@Override

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.parser.lang.variables;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Item;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class Assignment<T> implements Item<T> {
@@ -18,8 +18,8 @@ public class Assignment<T> implements Item<T> {
}
@Override
public synchronized T apply(Location location, Rotation rotation, int recursions) {
T val = value.apply(location, rotation, recursions);
public synchronized T apply(Buffer buffer, Rotation rotation, int recursions) {
T val = value.apply(buffer, rotation, recursions);
delegate.setValue(val);
return val;
}

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.parser.lang.variables;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class Getter implements Returnable<Object> {
@@ -18,7 +18,7 @@ public class Getter implements Returnable<Object> {
}
@Override
public synchronized Object apply(Location location, Rotation rotation, int recursions) {
public synchronized Object apply(Buffer buffer, Rotation rotation, int recursions) {
return delegate.getValue();
}

View File

@@ -11,6 +11,8 @@ import com.dfsek.terra.api.structures.script.builders.RandomFunctionBuilder;
import com.dfsek.terra.api.structures.script.builders.RecursionsFunctionBuilder;
import com.dfsek.terra.api.structures.script.builders.StructureFunctionBuilder;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.StructureBuffer;
import com.dfsek.terra.registry.ScriptRegistry;
import org.apache.commons.io.IOUtils;
@@ -42,8 +44,14 @@ public class StructureScript {
this.id = parser.getID();
}
public void execute(Location location, Rotation rotation, int recursions) {
block.apply(location, rotation, recursions);
public void execute(Location location, Rotation rotation) {
StructureBuffer buffer = new StructureBuffer(location);
block.apply(buffer, rotation, 0);
buffer.paste();
}
public void executeInBuffer(Buffer buffer, Rotation rotation, int recursions) {
block.apply(buffer, rotation, recursions);
}
public String getId() {

View File

@@ -1,7 +1,7 @@
package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
@@ -10,6 +10,8 @@ import com.dfsek.terra.api.structures.parser.lang.constants.ConstantExpression;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.BufferedBlock;
import com.dfsek.terra.api.structures.tokenizer.Position;
import net.jafama.FastMath;
@@ -34,12 +36,12 @@ public class BlockFunction implements Function<Void> {
}
@Override
public Void apply(Location location, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue());
public Void apply(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation);
location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())).getBlock().setBlockData(data, false);
buffer.addItem(new BufferedBlock(data), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
return null;
}

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin;
@@ -9,6 +8,7 @@ import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.api.structures.world.LandCheck;
import com.dfsek.terra.api.structures.world.OceanCheck;
@@ -32,17 +32,17 @@ public class CheckFunction implements Function<String> {
return "check";
}
private Vector3 getVector(Location location, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue());
private Vector3 getVector(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation);
return new Vector3(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
return new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ())).add(buffer.getOrigin().toVector());
}
@Override
public String apply(Location location, Rotation rotation, int recursions) {
return apply(getVector(location, rotation, recursions), location.getWorld());
public String apply(Buffer buffer, Rotation rotation, int recursions) {
return apply(getVector(buffer, rotation, recursions), buffer.getOrigin().getWorld());
}
private String apply(Vector3 vector, World world) {

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import java.util.concurrent.ThreadLocalRandom;
@@ -29,8 +29,8 @@ public class RandomFunction implements Function<Integer> {
}
@Override
public Integer apply(Location location, Rotation rotation, int recursions) {
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(location, rotation, recursions).intValue()); // TODO: deterministic random
public Integer apply(Buffer buffer, Rotation rotation, int recursions) {
return ThreadLocalRandom.current().nextInt(numberReturnable.apply(buffer, rotation, recursions).intValue()); // TODO: deterministic random
}
@Override

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
public class RecursionsFunction implements Function<Number> {
@@ -23,7 +23,7 @@ public class RecursionsFunction implements Function<Number> {
}
@Override
public Number apply(Location location, Rotation rotation, int recursions) {
public Number apply(Buffer buffer, Rotation rotation, int recursions) {
return recursions;
}

View File

@@ -1,13 +1,15 @@
package com.dfsek.terra.api.structures.script.functions;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.script.StructureScript;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.RotationUtil;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.structure.buffer.IntermediateBuffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import com.dfsek.terra.registry.ScriptRegistry;
import net.jafama.FastMath;
@@ -45,20 +47,20 @@ public class StructureFunction implements Function<Void> {
}
@Override
public Void apply(Location location, Rotation rotation, int recursions) {
public Void apply(Buffer buffer, Rotation rotation, int recursions) {
Vector2 xz = new Vector2(x.apply(location, rotation, recursions).doubleValue(), z.apply(location, rotation, recursions).doubleValue());
Vector2 xz = new Vector2(x.apply(buffer, rotation, recursions).doubleValue(), z.apply(buffer, rotation, recursions).doubleValue());
RotationUtil.rotateVector(xz, rotation);
StructureScript script = registry.get(id.apply(location, rotation, recursions));
StructureScript script = registry.get(id.apply(buffer, rotation, recursions));
if(script == null) {
main.getLogger().severe("No such structure " + id.apply(location, rotation, recursions));
main.getLogger().severe("No such structure " + id.apply(buffer, rotation, recursions));
return null;
}
Rotation rotation1;
String rotString = rotations.get(ThreadLocalRandom.current().nextInt(rotations.size())).apply(location, rotation, recursions);
String rotString = rotations.get(ThreadLocalRandom.current().nextInt(rotations.size())).apply(buffer, rotation, recursions);
try {
rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) {
@@ -66,9 +68,9 @@ public class StructureFunction implements Function<Void> {
return null;
}
Location location1 = location.clone().add(FastMath.roundToInt(xz.getX()), y.apply(location, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
Vector3 offset = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, recursions).intValue(), FastMath.roundToInt(xz.getZ()));
script.execute(location1, rotation.rotate(rotation1), recursions + 1);
script.executeInBuffer(new IntermediateBuffer(buffer, offset), rotation.rotate(rotation1), recursions + 1);
return null;
}

View File

@@ -0,0 +1,10 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
public interface Buffer {
Buffer addItem(BufferedItem item, Vector3 location);
Location getOrigin();
}

View File

@@ -0,0 +1,17 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.platform.block.BlockData;
public class BufferedBlock implements BufferedItem {
private final BlockData data;
public BufferedBlock(BlockData data) {
this.data = data;
}
@Override
public void paste(Location origin) {
origin.getBlock().setBlockData(data, false);
}
}

View File

@@ -0,0 +1,7 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
public interface BufferedItem {
void paste(Location origin);
}

View File

@@ -0,0 +1,24 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
public class IntermediateBuffer implements Buffer {
private final Buffer original;
private final Vector3 offset;
public IntermediateBuffer(Buffer original, Vector3 offset) {
this.original = original;
this.offset = offset;
}
@Override
public Buffer addItem(BufferedItem item, Vector3 location) {
return original.addItem(item, location.add(offset));
}
@Override
public Location getOrigin() {
return original.getOrigin().add(offset);
}
}

View File

@@ -0,0 +1,33 @@
package com.dfsek.terra.api.structures.structure.buffer;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.math.vector.Vector3;
import java.util.HashMap;
import java.util.Map;
public class StructureBuffer implements Buffer {
private final Map<Vector3, BufferedItem> bufferedItemMap = new HashMap<>();
private final Location origin;
public StructureBuffer(Location origin) {
this.origin = origin;
}
public void paste() {
bufferedItemMap.forEach(((vector3, item) -> {
item.paste(origin.clone().add(vector3));
}));
}
@Override
public Buffer addItem(BufferedItem item, Vector3 location) {
bufferedItemMap.put(location, item);
return this;
}
@Override
public Location getOrigin() {
return origin;
}
}

View File

@@ -21,7 +21,7 @@ public class TerraTree implements Tree {
@Override
public synchronized boolean plant(Location location, Random random) {
structure.execute(location.clone().add(0, yOffset, 0), Rotation.fromDegrees(90 * random.nextInt(4)), 0);
structure.execute(location.clone().add(0, yOffset, 0), Rotation.fromDegrees(90 * random.nextInt(4)));
return true;
}

View File

@@ -1,6 +1,5 @@
package structure;
import com.dfsek.terra.api.math.vector.Location;
import com.dfsek.terra.api.structures.parser.Parser;
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
import com.dfsek.terra.api.structures.parser.lang.Block;
@@ -8,6 +7,7 @@ import com.dfsek.terra.api.structures.parser.lang.Returnable;
import com.dfsek.terra.api.structures.parser.lang.functions.Function;
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
import com.dfsek.terra.api.structures.structure.Rotation;
import com.dfsek.terra.api.structures.structure.buffer.Buffer;
import com.dfsek.terra.api.structures.tokenizer.Position;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
@@ -67,8 +67,8 @@ public class ParserTest {
}
@Override
public Void apply(Location location, Rotation rotation, int recursions) {
System.out.println("string: " + a.apply(location, rotation, recursions) + ", double: " + b.apply(location, rotation, recursions));
public Void apply(Buffer buffer, Rotation rotation, int recursions) {
System.out.println("string: " + a.apply(buffer, rotation, recursions) + ", double: " + b.apply(buffer, rotation, recursions));
return null;
}

View File

@@ -36,7 +36,7 @@ public class LoadRawCommand extends LoadCommand implements DebugCommand {
TerraWorld terraWorld = getMain().getWorld(new BukkitWorld(sender.getWorld()));
long t = System.nanoTime();
terraWorld.getConfig().getScriptRegistry().get(args[0]).execute(new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()), Rotation.fromDegrees(90 * ThreadLocalRandom.current().nextInt(4)), 0);
terraWorld.getConfig().getScriptRegistry().get(args[0]).execute(new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()), Rotation.fromDegrees(90 * ThreadLocalRandom.current().nextInt(4)));
long l = System.nanoTime() - t;
sender.sendMessage("Took " + ((double) l) / 1000000 + "ms");