refactor createBlockState to use new error API

This commit is contained in:
dfsek
2025-12-29 22:33:07 -07:00
parent cb08401536
commit c4a366112e
6 changed files with 28 additions and 7 deletions

View File

@@ -8,6 +8,9 @@
package com.dfsek.terra.addons.sponge;
import com.dfsek.seismic.type.vector.Vector3Int;
import com.dfsek.terra.api.error.Invalid;
import net.querz.nbt.io.NBTDeserializer;
import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag;
@@ -129,7 +132,7 @@ public class SpongeSchematicAddon implements AddonInitializer {
for(int y = 0; y < hei; y++) {
String block = data.get((int) arr[x + z * wid + y * wid * len]);
if(block.startsWith("minecraft:structure_void")) continue;
states[x][z][y] = platform.getWorldHandle().createBlockState(block);
states[x][z][y] = platform.getWorldHandle().createBlockState(block).collectThrow(Invalid::toIllegal);
}
}
}

View File

@@ -10,6 +10,10 @@ package com.dfsek.terra.addons.terrascript.script.functions;
import com.dfsek.seismic.math.floatingpoint.FloatingPointFunctions;
import com.dfsek.seismic.type.vector.Vector2;
import com.dfsek.seismic.type.vector.Vector3;
import com.dfsek.terra.addons.terrascript.parser.exceptions.ParseException;
import com.dfsek.terra.api.handle.WorldHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -85,7 +89,11 @@ public class BlockFunction implements Function<Void> {
}
protected BlockState getBlockState(ImplementationArguments arguments, Scope scope) {
return data.computeIfAbsent(blockData.apply(arguments, scope), platform.getWorldHandle()::createBlockState);
WorldHandle handle = platform.getWorldHandle();
return data.computeIfAbsent(blockData.apply(arguments, scope), s -> handle.createBlockState(s).collect(left -> {
logger.error("Invalid block state \"" + s + "\": " + left.message());
return handle.air();
}, java.util.function.Function.identity()));
}
@@ -95,7 +103,8 @@ public class BlockFunction implements Function<Void> {
public Constant(Returnable<Number> x, Returnable<Number> y, Returnable<Number> z, StringConstant blockData,
Returnable<Boolean> overwrite, Returnable<Boolean> physics, Platform platform, Position position) {
super(x, y, z, blockData, overwrite, physics, platform, position);
this.state = platform.getWorldHandle().createBlockState(blockData.getConstant());
this.state = platform.getWorldHandle().createBlockState(blockData.getConstant()).collectThrow(
left -> new ParseException("Invalid block state: \"" + blockData.getConstant() + "\": " + left.message(), position));
}
@Override

View File

@@ -33,7 +33,9 @@ public class PullFunction implements Function<Void> {
this.position = position;
if(!(data instanceof ConstantExpression)) throw new ParseException("Block data must be constant", data.getPosition());
this.data = platform.getWorldHandle().createBlockState(((ConstantExpression<String>) data).getConstant());
String constant = ((ConstantExpression<String>) data).getConstant();
this.data = platform.getWorldHandle().createBlockState(constant).collectThrow(
left -> new ParseException("Invalid block state: \"" + constant + "\": " + left.message(), position));
this.x = x;
this.y = y;
this.z = z;

View File

@@ -9,4 +9,8 @@ public interface Invalid {
default <T> Either<Invalid, T> left() {
return Either.left(this);
}
default IllegalArgumentException toIllegal() {
return new IllegalArgumentException(message());
}
}

View File

@@ -24,6 +24,8 @@ import com.dfsek.tectonic.api.TypeRegistry;
import java.util.LinkedHashMap;
import com.dfsek.tectonic.api.exception.LoadException;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.BaseAddon;
import com.dfsek.terra.api.block.BlockType;
@@ -61,9 +63,9 @@ public class GenericLoaders implements LoaderRegistrar {
if(platform != null) {
registry.registerLoader(BaseAddon.class, platform.getAddons())
.registerLoader(BlockType.class, (type, object, configLoader, depthTracker) -> platform
.getWorldHandle().createBlockState((String) object).blockType())
.getWorldHandle().createBlockState((String) object).collectThrow(left -> new LoadException(left.message(), depthTracker)).blockType())
.registerLoader(BlockState.class, (type, object, configLoader, depthTracker) -> platform
.getWorldHandle().createBlockState((String) object));
.getWorldHandle().createBlockState((String) object).collectThrow(left -> new LoadException("Invalid BlockState \"" + object + "\": " + left.message(), depthTracker)));
}
}
}