mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-08 16:56:07 +00:00
implement EntityFunction
This commit is contained in:
@@ -3,5 +3,4 @@ package com.dfsek.terra.api.structures.parser.lang.functions;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Returnable;
|
||||
|
||||
public interface Function<T> extends Returnable<T> {
|
||||
String name();
|
||||
}
|
||||
|
||||
@@ -16,11 +16,6 @@ public class AbsFunction extends MathFunction {
|
||||
this.returnable = returnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "abs";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return FastMath.abs(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
|
||||
@@ -18,11 +18,6 @@ public class PowFunction extends MathFunction {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "pow";
|
||||
}
|
||||
|
||||
@Override
|
||||
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());
|
||||
|
||||
@@ -16,11 +16,6 @@ public class SqrtFunction extends MathFunction {
|
||||
this.returnable = returnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "sqrt";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
return FastMath.sqrt(returnable.apply(buffer, rotation, random, recursions).doubleValue());
|
||||
|
||||
@@ -18,11 +18,6 @@ public abstract class DefinedFunction<T> implements Function<T> {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T apply(Buffer buffer, Rotation rotation, Random random, int 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.script.builders.BlockFunctionBuilder;
|
||||
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.LootFunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.script.builders.MarkFunctionBuilder;
|
||||
@@ -49,7 +50,8 @@ public class StructureScript {
|
||||
.addFunction("setMark", new MarkFunctionBuilder())
|
||||
.addFunction("getMark", new GetMarkFunctionBuilder())
|
||||
.addFunction("pull", new PullFunctionBuilder(main))
|
||||
.addFunction("loot", new LootFunctionBuilder(main, lootRegistry));
|
||||
.addFunction("loot", new LootFunctionBuilder(main, lootRegistry))
|
||||
.addFunction("entity", new EntityFunctionBuilder(main));
|
||||
|
||||
try {
|
||||
block = parser.parse();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,11 +32,6 @@ public class BlockFunction implements Function<Void> {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "block";
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
@@ -36,11 +36,6 @@ public class CheckFunction implements Function<String> {
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "check";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String apply(Buffer buffer, Rotation rotation, Random random, int recursions) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,6 @@ public class GetMarkFunction implements Function<String> {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "block";
|
||||
}
|
||||
|
||||
@Override
|
||||
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());
|
||||
|
||||
@@ -33,11 +33,6 @@ public class LootFunction implements Function<Void> {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "block";
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
@@ -27,11 +27,6 @@ public class MarkFunction implements Function<Void> {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "block";
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
@@ -32,11 +32,6 @@ public class PullFunction implements Function<Void> {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "pull";
|
||||
}
|
||||
|
||||
@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());
|
||||
|
||||
@@ -18,11 +18,6 @@ public class RandomFunction implements Function<Integer> {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "randomInt";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -14,11 +14,6 @@ public class RecursionsFunction implements Function<Number> {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "recursions";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.NUMBER;
|
||||
|
||||
@@ -36,11 +36,6 @@ public class StructureFunction implements Function<Boolean> {
|
||||
this.rotations = rotations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "structure";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnType returnType() {
|
||||
return ReturnType.BOOLEAN;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user