mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 22:31:52 +00:00
implement EntityFunction
This commit is contained in:
-1
@@ -3,5 +3,4 @@ package com.dfsek.terra.api.structures.parser.lang.functions;
|
|||||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||||
|
|
||||||
public interface Function<T> extends Returnable<T> {
|
public interface Function<T> extends Returnable<T> {
|
||||||
String name();
|
|
||||||
}
|
}
|
||||||
|
|||||||
-5
@@ -16,11 +16,6 @@ public class AbsFunction extends MathFunction {
|
|||||||
this.returnable = returnable;
|
this.returnable = returnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "abs";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
return FastMath.abs(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
return FastMath.abs(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
-5
@@ -18,11 +18,6 @@ public class PowFunction extends MathFunction {
|
|||||||
this.power = power;
|
this.power = power;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "pow";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
return FastMath.pow(base.apply(buffer, rotation, random, recursions).doubleValue(), power.apply(buffer, rotation, random, recursions).doubleValue());
|
return FastMath.pow(base.apply(buffer, rotation, random, recursions).doubleValue(), power.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
-5
@@ -16,11 +16,6 @@ public class SqrtFunction extends MathFunction {
|
|||||||
this.returnable = returnable;
|
this.returnable = returnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "sqrt";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
return FastMath.sqrt(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
return FastMath.sqrt(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
-5
@@ -18,11 +18,6 @@ public abstract class DefinedFunction<T> implements Function<T> {
|
|||||||
this.position = position;
|
this.position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public T apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
return block.apply(buffer, rotation, random, recursions);
|
return block.apply(buffer, rotation, random, recursions);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
|||||||
import com.dfsek.terra.api.structures.parser.lang.Block;
|
import com.dfsek.terra.api.structures.parser.lang.Block;
|
||||||
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
|
||||||
|
import com.dfsek.terra.api.structures.script.builders.EntityFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.GetMarkFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.GetMarkFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.LootFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.LootFunctionBuilder;
|
||||||
import com.dfsek.terra.api.structures.script.builders.MarkFunctionBuilder;
|
import com.dfsek.terra.api.structures.script.builders.MarkFunctionBuilder;
|
||||||
@@ -49,7 +50,8 @@ public class StructureScript {
|
|||||||
.addFunction("setMark", new MarkFunctionBuilder())
|
.addFunction("setMark", new MarkFunctionBuilder())
|
||||||
.addFunction("getMark", new GetMarkFunctionBuilder())
|
.addFunction("getMark", new GetMarkFunctionBuilder())
|
||||||
.addFunction("pull", new PullFunctionBuilder(main))
|
.addFunction("pull", new PullFunctionBuilder(main))
|
||||||
.addFunction("loot", new LootFunctionBuilder(main, lootRegistry));
|
.addFunction("loot", new LootFunctionBuilder(main, lootRegistry))
|
||||||
|
.addFunction("entity", new EntityFunctionBuilder(main));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
block = parser.parse();
|
block = parser.parse();
|
||||||
|
|||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
package com.dfsek.terra.api.structures.script.builders;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||||
|
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.functions.FunctionBuilder;
|
||||||
|
import com.dfsek.terra.api.structures.script.functions.EntityFunction;
|
||||||
|
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EntityFunctionBuilder implements FunctionBuilder<EntityFunction> {
|
||||||
|
private final TerraPlugin main;
|
||||||
|
|
||||||
|
public EntityFunctionBuilder(TerraPlugin main) {
|
||||||
|
this.main = main;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public EntityFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||||
|
return new EntityFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1), (Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), main, position);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int argNumber() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Returnable.ReturnType getArgument(int position) {
|
||||||
|
switch(position) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
return Returnable.ReturnType.NUMBER;
|
||||||
|
case 3:
|
||||||
|
return Returnable.ReturnType.STRING;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-5
@@ -32,11 +32,6 @@ public class BlockFunction implements Function<Void> {
|
|||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "block";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
-5
@@ -36,11 +36,6 @@ public class CheckFunction implements Function<String> {
|
|||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "check";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
|
|||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
package com.dfsek.terra.api.structures.script.functions;
|
||||||
|
|
||||||
|
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.world.entity.EntityType;
|
||||||
|
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||||
|
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||||
|
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.items.BufferedEntity;
|
||||||
|
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||||
|
import net.jafama.FastMath;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class EntityFunction implements Function<Void> {
|
||||||
|
private final EntityType data;
|
||||||
|
private final Returnable<Number> x, y, z;
|
||||||
|
private final Position position;
|
||||||
|
|
||||||
|
public EntityFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data, TerraPlugin main, Position position) throws ParseException {
|
||||||
|
this.position = position;
|
||||||
|
if(!(data instanceof ConstantExpression)) throw new ParseException("Entity data must be constant", data.getPosition());
|
||||||
|
|
||||||
|
this.data = main.getWorldHandle().getEntity(((ConstantExpression<String>) data).getConstant());
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|
||||||
|
RotationUtil.rotateVector(xz, rotation);
|
||||||
|
|
||||||
|
buffer.addItem(new BufferedEntity(data), new Vector3(FastMath.roundToInt(xz.getX()), y.apply(buffer, rotation, random, recursions).intValue(), FastMath.roundToInt(xz.getZ())));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnType returnType() {
|
||||||
|
return ReturnType.VOID;
|
||||||
|
}
|
||||||
|
}
|
||||||
-5
@@ -25,11 +25,6 @@ public class GetMarkFunction implements Function<String> {
|
|||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "block";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
@@ -33,11 +33,6 @@ public class LootFunction implements Function<Void> {
|
|||||||
this.main = main;
|
this.main = main;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "block";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
@@ -27,11 +27,6 @@ public class MarkFunction implements Function<Void> {
|
|||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "block";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
@@ -32,11 +32,6 @@ public class PullFunction implements Function<Void> {
|
|||||||
this.z = z;
|
this.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "pull";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
public Void apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||||
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
Vector2 xz = new Vector2(x.apply(buffer, rotation, random, recursions).doubleValue(), z.apply(buffer, rotation, random, recursions).doubleValue());
|
||||||
|
|||||||
-5
@@ -18,11 +18,6 @@ public class RandomFunction implements Function<Integer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "randomInt";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnType returnType() {
|
public ReturnType returnType() {
|
||||||
return ReturnType.NUMBER;
|
return ReturnType.NUMBER;
|
||||||
|
|||||||
-5
@@ -14,11 +14,6 @@ public class RecursionsFunction implements Function<Number> {
|
|||||||
this.position = position;
|
this.position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "recursions";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnType returnType() {
|
public ReturnType returnType() {
|
||||||
return ReturnType.NUMBER;
|
return ReturnType.NUMBER;
|
||||||
|
|||||||
-5
@@ -36,11 +36,6 @@ public class StructureFunction implements Function<Boolean> {
|
|||||||
this.rotations = rotations;
|
this.rotations = rotations;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "structure";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnType returnType() {
|
public ReturnType returnType() {
|
||||||
return ReturnType.BOOLEAN;
|
return ReturnType.BOOLEAN;
|
||||||
|
|||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package com.dfsek.terra.api.structures.structure.buffer.items;
|
||||||
|
|
||||||
|
import com.dfsek.terra.api.math.vector.Location;
|
||||||
|
import com.dfsek.terra.api.platform.world.entity.EntityType;
|
||||||
|
|
||||||
|
public class BufferedEntity implements BufferedItem {
|
||||||
|
|
||||||
|
private final EntityType type;
|
||||||
|
|
||||||
|
public BufferedEntity(EntityType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paste(Location origin) {
|
||||||
|
origin.getWorld().spawnEntity(origin, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,11 +78,6 @@ public class ParserTest {
|
|||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReturnType returnType() {
|
public ReturnType returnType() {
|
||||||
return ReturnType.VOID;
|
return ReturnType.VOID;
|
||||||
|
|||||||
Reference in New Issue
Block a user