mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
refactor createBlockState to use new error API
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -9,4 +9,8 @@ public interface Invalid {
|
||||
default <T> Either<Invalid, T> left() {
|
||||
return Either.left(this);
|
||||
}
|
||||
|
||||
default IllegalArgumentException toIllegal() {
|
||||
return new IllegalArgumentException(message());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.dfsek.terra.minestom;
|
||||
|
||||
import com.dfsek.tectonic.api.TypeRegistry;
|
||||
import com.dfsek.tectonic.api.exception.LoadException;
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.util.RGBLike;
|
||||
@@ -81,7 +82,7 @@ public final class TerraMinestomPlatform extends AbstractPlatform {
|
||||
.registerLoader(EntityType.class,
|
||||
(TypeLoader<EntityType>) (annotatedType, o, configLoader, depthTracker) -> new MinestomEntityType((String) o))
|
||||
.registerLoader(BlockState.class,
|
||||
(TypeLoader<BlockState>) (annotatedType, o, configLoader, depthTracker) -> worldHandle.createBlockState((String) o))
|
||||
(TypeLoader<BlockState>) (annotatedType, o, configLoader, depthTracker) -> worldHandle.createBlockState((String) o).collectThrow(left -> new LoadException("Invalid BlockState \"" + o + "\": " + left.message(), depthTracker)))
|
||||
.registerLoader(BiomeEffects.Particle.class, BiomeParticleConfigTemplate::new)
|
||||
.registerLoader(BiomeEffects.MoodSound.class, BiomeMoodSoundTemplate::new)
|
||||
.registerLoader(BiomeEffects.AdditionsSound.class, BiomeAdditionsSoundTemplate::new)
|
||||
|
||||
Reference in New Issue
Block a user