remove buffer api

This commit is contained in:
dfsek
2021-12-20 00:01:04 -07:00
parent f088928483
commit 62d0f109b4
24 changed files with 217 additions and 185 deletions

View File

@@ -1,58 +0,0 @@
/*
* Copyright (c) 2020-2021 Polyhedral Development
*
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in this module's root directory.
*/
package com.dfsek.terra.addons.terrascript.buffer.items;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.script.StructureScript;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.event.events.world.generation.LootPopulateEvent;
import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.buffer.BufferedItem;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.WritableWorld;
public class BufferedLootApplication implements BufferedItem {
private static final Logger LOGGER = LoggerFactory.getLogger(BufferedLootApplication.class);
private final LootTable table;
private final Platform platform;
private final StructureScript structure;
public BufferedLootApplication(LootTable table, Platform platform, StructureScript structure) {
this.table = table;
this.platform = platform;
this.structure = structure;
}
@Override
public void paste(Vector3 origin, WritableWorld world) {
try {
BlockEntity data = world.getBlockEntity(origin);
if(!(data instanceof Container container)) {
LOGGER.error("Failed to place loot at {}; block {} is not a container", origin, data);
return;
}
LootPopulateEvent event = new LootPopulateEvent(container, table, world.getPack(), structure);
platform.getEventManager().callEvent(event);
if(event.isCancelled()) return;
event.getTable().fillInventory(container.getInventory(), new Random(origin.hashCode()));
data.update(false);
} catch(Exception e) {
LOGGER.error("Could not apply loot at {}", origin, e);
e.printStackTrace();
}
}
}

View File

@@ -7,8 +7,8 @@
package com.dfsek.terra.addons.terrascript.script;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import net.jafama.FastMath;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
@@ -18,7 +18,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import com.dfsek.terra.addons.terrascript.parser.Parser;
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
@@ -46,10 +45,7 @@ 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.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.WritableWorld;
@@ -57,7 +53,6 @@ public class StructureScript implements Structure {
private static final Logger LOGGER = LoggerFactory.getLogger(StructureScript.class);
private final Block block;
private final String id;
private final Cache<Vector3, StructureBuffer> cache;
private final Platform platform;
@SuppressWarnings("rawtypes")
@@ -89,11 +84,11 @@ public class StructureScript implements Structure {
.registerFunction("getBlock", new CheckBlockFunctionBuilder())
.registerFunction("state", new StateFunctionBuilder(platform))
.registerFunction("setWaterlog", new UnaryBooleanFunctionBuilder((waterlog, args) -> args.setWaterlog(waterlog)))
.registerFunction("originX", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getX(),
.registerFunction("originX", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getX(),
Returnable.ReturnType.NUMBER))
.registerFunction("originY", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getY(),
.registerFunction("originY", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getY(),
Returnable.ReturnType.NUMBER))
.registerFunction("originZ", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getBuffer().getOrigin().getZ(),
.registerFunction("originZ", new ZeroArgFunctionBuilder<Number>(arguments -> arguments.getOrigin().getZ(),
Returnable.ReturnType.NUMBER))
.registerFunction("rotation", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().toString(),
Returnable.ReturnType.STRING))
@@ -126,34 +121,19 @@ public class StructureScript implements Structure {
block = parser.parse();
this.platform = platform;
this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build();
}
@Override
@SuppressWarnings("try")
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
try(ProfileFrame ignore = platform.getProfiler().profile("terrascript_direct:" + id)) {
DirectBuffer buffer = new DirectBuffer(location, world);
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, 0));
return applyBlock(new TerraImplementationArguments(location, rotation, random, world, 0));
}
}
public boolean generate(Vector3 location, WritableWorld world, Random random, Rotation rotation, int recursions) {
public boolean generate(Vector3Int 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));
}
}
private StructureBuffer computeBuffer(Vector3 location, WritableWorld world, Random random, Rotation rotation) {
try {
return cache.get(location, () -> {
StructureBuffer buf = new StructureBuffer(location);
buf.setSucceeded(applyBlock(new TerraImplementationArguments(buf, rotation, random, world, 0)));
return buf;
});
} catch(ExecutionException e) {
throw new RuntimeException(e);
return applyBlock(new TerraImplementationArguments(location, rotation, random, world, recursions));
}
}
@@ -161,7 +141,7 @@ public class StructureScript implements Structure {
try {
return block.apply(arguments).getLevel() != Block.ReturnLevel.FAIL;
} catch(RuntimeException e) {
LOGGER.error("Failed to generate structure at {}", arguments.getBuffer().getOrigin(), e);
LOGGER.error("Failed to generate structure at {}", arguments.getOrigin(), e);
return false;
}
}

View File

@@ -7,32 +7,32 @@
package com.dfsek.terra.addons.terrascript.script;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.api.structure.buffer.Buffer;
import com.dfsek.terra.api.util.Rotation;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.util.vector.integer.Vector3Int;
import com.dfsek.terra.api.world.WritableWorld;
public class TerraImplementationArguments implements ImplementationArguments {
private final Buffer buffer;
private final Rotation rotation;
private final Random random;
private final WritableWorld world;
private final Map<Vector3, String> marks = new HashMap<>();
private final int recursions;
private boolean waterlog = false;
private final Vector3Int origin;
public TerraImplementationArguments(Buffer buffer, Rotation rotation, Random random, WritableWorld world, int recursions) {
this.buffer = buffer;
public TerraImplementationArguments(Vector3Int origin, Rotation rotation, Random random, WritableWorld world, int recursions) {
this.rotation = rotation;
this.random = random;
this.world = world;
this.recursions = recursions;
}
public Buffer getBuffer() {
return buffer;
this.origin = origin;
}
public int getRecursions() {
@@ -58,4 +58,16 @@ public class TerraImplementationArguments implements ImplementationArguments {
public WritableWorld getWorld() {
return world;
}
public Vector3Int getOrigin() {
return origin;
}
public void setMark(Vector3 pos, String mark) {
marks.put(pos, mark);
}
public String getMark(Vector3 pos) {
return marks.get(pos);
}
}

View File

@@ -29,7 +29,7 @@ public class StateFunctionBuilder implements FunctionBuilder<StateFunction> {
public StateFunction build(List<Returnable<?>> argumentList, Position position) {
if(argumentList.size() < 4) throw new ParseException("Expected data", position);
return new StateFunction((Returnable<Number>) argumentList.get(0), (Returnable<Number>) argumentList.get(1),
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), platform, position);
(Returnable<Number>) argumentList.get(2), (Returnable<String>) argumentList.get(3), position);
}
@Override

View File

@@ -50,10 +50,9 @@ public class BiomeFunction implements Function<String> {
BiomeProvider grid = arguments.getWorld().getBiomeProvider();
return grid.getBiome(arguments.getBuffer()
.getOrigin()
.clone()
.add(new Vector3(FastMath.roundToInt(xz.getX()),
return grid.getBiome(arguments.getOrigin()
.toVector3()
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ()))), arguments.getWorld().getSeed()).getID();
}

View File

@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.state.properties.base.Properties;
import net.jafama.FastMath;
import java.util.HashMap;
@@ -26,8 +28,12 @@ import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BlockFunction implements Function<Void> {
private static final Logger logger = LoggerFactory.getLogger(BlockFunction.class);
protected final Returnable<Number> x, y, z;
protected final Returnable<String> blockData;
protected final Platform platform;
@@ -72,10 +78,20 @@ public class BlockFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation());
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
arguments.getBuffer().addItem(
new BufferedBlock(rot, overwrite.apply(implementationArguments, variableMap), platform, arguments.isWaterlog()),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ())));
try {
Vector3 set = Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).doubleValue(),
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
BlockState current = arguments.getWorld().getBlockState(set);
if(overwrite.apply(implementationArguments, variableMap) || current.isAir()) {
if(arguments.isWaterlog() && current.has(Properties.WATERLOGGED) && current.getBlockType().isWater()) {
current.set(Properties.WATERLOGGED, true);
}
arguments.getWorld().setBlockState(set, rot);
}
} catch(RuntimeException e) {
logger.error("Failed to place block at location {}", arguments.getOrigin(), e);
}
}
protected BlockState getBlockState(ImplementationArguments arguments, Map<String, Variable<?>> variableMap) {

View File

@@ -44,12 +44,11 @@ public class CheckBlockFunction implements Function<String> {
RotationUtil.rotateVector(xz, arguments.getRotation());
String data = arguments.getWorld()
.getBlockState(arguments.getBuffer()
.getOrigin()
.clone()
.add(new Vector3(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
.getBlockState(arguments.getOrigin()
.toVector3()
.add(Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
.doubleValue(), FastMath.roundToInt(xz.getZ()))))
.getAsString();
if(data.contains("[")) return data.substring(0, data.indexOf('[')); // Strip properties
else return data;

View File

@@ -19,7 +19,9 @@ import com.dfsek.terra.addons.terrascript.parser.lang.variables.Variable;
import com.dfsek.terra.addons.terrascript.script.TerraImplementationArguments;
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.entity.Entity;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.event.events.world.generation.EntitySpawnEvent;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
@@ -50,9 +52,8 @@ public class EntityFunction implements Function<Void> {
z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().addItem(new BufferedEntity(data, platform),
new Vector3(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()));
Entity entity = arguments.getWorld().spawnEntity(Vector3.of(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).add(arguments.getOrigin()).add(0.5, 0, 0.5), data);
platform.getEventManager().callEvent(new EntitySpawnEvent(entity.world().getPack(), entity));
return null;
}

View File

@@ -40,8 +40,8 @@ public class GetMarkFunction implements Function<String> {
z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
String mark = arguments.getBuffer().getMark(new Vector3(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())));
String mark = arguments.getMark(Vector3.of(FastMath.floorToInt(xz.getX()), FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()), FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()));
return mark == null ? "" : mark;
}

View File

@@ -7,13 +7,19 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Container;
import com.dfsek.terra.api.event.events.world.generation.LootPopulateEvent;
import com.dfsek.terra.api.util.vector.Vector3;
import net.jafama.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Random;
import com.dfsek.terra.addons.terrascript.buffer.items.BufferedLootApplication;
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
@@ -26,7 +32,6 @@ import com.dfsek.terra.api.registry.Registry;
import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
public class LootFunction implements Function<Void> {
@@ -59,12 +64,37 @@ public class LootFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation());
String id = data.apply(implementationArguments, variableMap);
registry.get(id).ifPresentOrElse(table -> arguments.getBuffer().addItem(new BufferedLootApplication(table, platform, script),
new Vector3(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
.intValue(),
FastMath.roundToInt(xz.getZ()))),
() -> LOGGER.error("No such loot table {}", id));
registry.get(id)
.ifPresentOrElse(table -> {
Vector3 apply = Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap)
.intValue(),
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
try {
BlockEntity data = arguments.getWorld().getBlockEntity(apply);
if(!(data instanceof Container container)) {
LOGGER.error("Failed to place loot at {}; block {} is not a container",
apply, data);
return;
}
LootPopulateEvent event = new LootPopulateEvent(container, table,
arguments.getWorld().getPack(), script);
platform.getEventManager().callEvent(event);
if(event.isCancelled()) return;
event.getTable().fillInventory(container.getInventory(),
new Random(apply.hashCode()));
data.update(false);
} catch(Exception e) {
LOGGER.error("Could not apply loot at {}", apply, e);
e.printStackTrace();
}
},
() -> LOGGER.error("No such loot table {}", id));
return null;
}

View File

@@ -52,9 +52,16 @@ public class PullFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation());
BlockState rot = data.clone();
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
arguments.getBuffer().addItem(new BufferedPulledBlock(rot),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())));
Vector3 mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
while(mutable.getY() > arguments.getWorld().getMinHeight()) {
if(!arguments.getWorld().getBlockState(mutable).isAir()) {
arguments.getWorld().setBlockState(mutable, rot);
break;
}
mutable.subtract(0, 1, 0);
}
return null;
}

View File

@@ -43,12 +43,10 @@ public class SetMarkFunction implements Function<Void> {
RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().setMark(mark.apply(implementationArguments, variableMap), new Vector3(FastMath.floorToInt(xz.getX()),
FastMath.floorToInt(
y.apply(implementationArguments,
variableMap)
.doubleValue()),
FastMath.floorToInt(xz.getZ())));
arguments.setMark(Vector3.of(FastMath.floorToInt(xz.getX()),
FastMath.floorToInt(
y.apply(implementationArguments, variableMap).doubleValue()),
FastMath.floorToInt(xz.getZ())).add(arguments.getOrigin()), mark.apply(implementationArguments, variableMap));
return null;
}

View File

@@ -7,6 +7,8 @@
package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.terra.api.block.entity.BlockEntity;
import net.jafama.FastMath;
import java.util.Map;
@@ -23,17 +25,19 @@ import com.dfsek.terra.api.util.RotationUtil;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StateFunction implements Function<Void> {
private static final Logger LOGGER = LoggerFactory.getLogger(StateFunction.class);
private final Returnable<String> data;
private final Returnable<Number> x, y, z;
private final Position position;
private final Platform platform;
public StateFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data, Platform platform,
public StateFunction(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, Returnable<String> data,
Position position) {
this.position = position;
this.platform = platform;
this.data = data;
this.x = x;
this.y = y;
@@ -47,9 +51,16 @@ public class StateFunction implements Function<Void> {
z.apply(implementationArguments, variableMap).doubleValue());
RotationUtil.rotateVector(xz, arguments.getRotation());
arguments.getBuffer().addItem(new BufferedStateManipulator(data.apply(implementationArguments, variableMap)),
new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())));
Vector3 origin = new Vector3(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())).add(arguments.getOrigin());
try {
BlockEntity state = arguments.getWorld().getBlockEntity(origin);
state.applyState(data.apply(implementationArguments, variableMap));
state.update(false);
} catch(Exception e) {
LOGGER.warn("Could not apply BlockState at {}", origin, e);
e.printStackTrace();
}
return null;
}

View File

@@ -82,7 +82,7 @@ public class StructureFunction implements Function<Boolean> {
}
if(script instanceof StructureScript structureScript) {
return structureScript.generate(arguments.getBuffer().getOrigin(),
return structureScript.generate(arguments.getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),
@@ -90,7 +90,7 @@ public class StructureFunction implements Function<Boolean> {
arguments.getRandom(),
arguments.getRotation().rotate(rotation1), arguments.getRecursions() + 1);
}
return script.generate(arguments.getBuffer().getOrigin(),
return script.generate(arguments.getOrigin(),
arguments.getWorld()
.buffer(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).intValue(),