mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
implement block & check functions
This commit is contained in:
@@ -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.BlockFunction;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BlockFunctionBuilder implements FunctionBuilder<BlockFunction> {
|
||||
private final TerraPlugin main;
|
||||
|
||||
public BlockFunctionBuilder(TerraPlugin main) {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public BlockFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||
return new BlockFunction((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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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.CheckFunction;
|
||||
import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CheckFunctionBuilder implements FunctionBuilder<CheckFunction> {
|
||||
private final TerraPlugin main;
|
||||
|
||||
public CheckFunctionBuilder(TerraPlugin main) {
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public CheckFunction build(List<Returnable<?>> argumentList, Position position) throws ParseException {
|
||||
return new CheckFunction(main, (Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1), (Returnable<Number>) argumentList.get(2), position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int argNumber() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Returnable.ReturnType getArgument(int position) {
|
||||
switch(position) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
return Returnable.ReturnType.NUMBER;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.dfsek.terra.api.math.vector.Location;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.world.Chunk;
|
||||
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;
|
||||
@@ -11,12 +12,12 @@ import com.dfsek.terra.api.structures.tokenizer.Position;
|
||||
|
||||
public class BlockFunction implements Function<Void> {
|
||||
private final BlockData data;
|
||||
private final Returnable<Integer> x, y, z;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
|
||||
public BlockFunction(Returnable<Integer> x, Returnable<Integer> y, Returnable<Integer> z, Returnable<String> data, TerraPlugin main, Position position) {
|
||||
public BlockFunction(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 IllegalArgumentException("Block data must be constant.");
|
||||
if(!(data instanceof ConstantExpression)) throw new ParseException("Block data must be constant.");
|
||||
|
||||
this.data = main.getWorldHandle().createBlockData(((ConstantExpression<String>) data).getConstant());
|
||||
this.x = x;
|
||||
@@ -31,7 +32,7 @@ public class BlockFunction implements Function<Void> {
|
||||
|
||||
@Override
|
||||
public Void apply(Location location) {
|
||||
location.clone().add(x.apply(location), y.apply(location), z.apply(location)).getBlock().setBlockData(data, false);
|
||||
location.clone().add(x.apply(location).intValue(), y.apply(location).intValue(), z.apply(location).intValue()).getBlock().setBlockData(data, false);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,10 @@ import com.dfsek.terra.api.structures.world.OceanCheck;
|
||||
|
||||
public class CheckFunction implements Function<String> {
|
||||
private final TerraPlugin main;
|
||||
private final Returnable<Integer> x, y, z;
|
||||
private final Returnable<Number> x, y, z;
|
||||
private final Position position;
|
||||
|
||||
public CheckFunction(TerraPlugin main, Returnable<Integer> x, Returnable<Integer> y, Returnable<Integer> z, Position position) {
|
||||
public CheckFunction(TerraPlugin main, Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Position position) {
|
||||
this.main = main;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
@@ -30,8 +30,8 @@ public class CheckFunction implements Function<String> {
|
||||
}
|
||||
|
||||
private Vector3 getVector(Location location, Chunk chunk) {
|
||||
return chunk == null ? new Vector3(x.apply(location) + location.getBlockX(), y.apply(location) + location.getBlockY(), z.apply(location) + location.getBlockZ())
|
||||
: new Vector3(x.apply(location, chunk) + location.getBlockX(), y.apply(location, chunk) + location.getBlockY(), z.apply(location, chunk) + location.getBlockZ());
|
||||
return location.clone().add(chunk == null ? new Vector3(x.apply(location).intValue(), y.apply(location).intValue(), z.apply(location).intValue())
|
||||
: new Vector3(x.apply(location, chunk).intValue(), y.apply(location, chunk).intValue(), z.apply(location, chunk).intValue())).toVector();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
test("hello" + 3 + "gdfg", (2 * (3+1) * (2 * (1+1))));
|
||||
test("minecraft:green_w" + "ool", (2 * (3+1) * (2 * (1+1))));
|
||||
//
|
||||
|
||||
num testVar = 3.4;
|
||||
@@ -18,6 +18,7 @@ while(iterator < 5) {
|
||||
|
||||
|
||||
if(true && !(boolean && false) && true) {
|
||||
test(0,-1,0, "minecraft:green_w" + "ool");
|
||||
num scopedVar = 2;
|
||||
test("if statement" + 2 + stringVar, 1 + testVar + scopedVar);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ 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;
|
||||
import com.dfsek.terra.api.structures.script.builders.BlockFunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.script.builders.CheckFunctionBuilder;
|
||||
import com.dfsek.terra.bukkit.BukkitWorld;
|
||||
import com.dfsek.terra.bukkit.command.DebugCommand;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@@ -37,7 +39,15 @@ public class LoadRawCommand extends LoadCommand implements DebugCommand {
|
||||
public boolean execute(@NotNull Player sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
|
||||
try {
|
||||
Parser parser = new Parser(IOUtils.toString(new FileInputStream(new File(getMain().getDataFolder(), "test.tesf"))));
|
||||
parser.addFunction("block", new BlockFunctionBuilder(getMain()))
|
||||
.addFunction("check", new CheckFunctionBuilder(getMain()));
|
||||
|
||||
System.out.println("Parsing...");
|
||||
|
||||
Block main = parser.parse();
|
||||
|
||||
System.out.println("Done parsing");
|
||||
|
||||
main.apply(new Location(new BukkitWorld(sender.getWorld()), sender.getLocation().getX(), sender.getLocation().getY(), sender.getLocation().getZ()));
|
||||
|
||||
} catch(IOException | ParseException e) {
|
||||
|
||||
Reference in New Issue
Block a user