implement BlockState with mixin on Fabric

This commit is contained in:
dfsek
2021-12-21 13:09:40 -07:00
parent 1a1016bdf8
commit 5dbc2c2895
25 changed files with 204 additions and 253 deletions

View File

@@ -85,22 +85,23 @@ public class TerraFlora implements Structure {
int size = layers.size();
int c = ceiling ? -1 : 1;
EnumSet<Direction> faces = doRotation ? getFaces(location.mutable().add(0, c, 0).immutable(), world) : EnumSet.noneOf(Direction.class);
EnumSet<Direction> faces = doRotation ? getFaces(location.mutable().add(0, c, 0).immutable(), world) : EnumSet.noneOf(
Direction.class);
if(doRotation && faces.size() == 0) return false; // Don't plant if no faces are valid.
for(int i = 0; FastMath.abs(i) < size; i += c) { // Down if ceiling, up if floor
int lvl = (FastMath.abs(i));
BlockState data = getStateCollection((ceiling ? lvl : size - lvl - 1)).get(distribution, location.getX(), location.getY(),
location.getZ(), world.getSeed()).clone();
location.getZ(), world.getSeed());
if(doRotation) {
Direction oneFace = new ArrayList<>(faces).get(
new Random(location.getX() ^ location.getZ()).nextInt(faces.size())); // Get random face.
data.setIfPresent(Properties.DIRECTION, oneFace.opposite())
.setIfPresent(Properties.NORTH, faces.contains(Direction.NORTH))
.setIfPresent(Properties.SOUTH, faces.contains(Direction.SOUTH))
.setIfPresent(Properties.EAST, faces.contains(Direction.EAST))
.setIfPresent(Properties.WEST, faces.contains(Direction.WEST));
data = data.setIfPresent(Properties.DIRECTION, oneFace.opposite())
.setIfPresent(Properties.NORTH, faces.contains(Direction.NORTH))
.setIfPresent(Properties.SOUTH, faces.contains(Direction.SOUTH))
.setIfPresent(Properties.EAST, faces.contains(Direction.EAST))
.setIfPresent(Properties.WEST, faces.contains(Direction.WEST));
}
world.setBlockState(location.mutable().add(0, i + c, 0).immutable(), data, physics);
}

View File

@@ -54,7 +54,7 @@ public class BlockFunction implements Function<Void> {
@Override
public Void apply(ImplementationArguments implementationArguments, Map<String, Variable<?>> variableMap) {
TerraImplementationArguments arguments = (TerraImplementationArguments) implementationArguments;
BlockState rot = getBlockState(implementationArguments, variableMap).clone();
BlockState rot = getBlockState(implementationArguments, variableMap);
setBlock(implementationArguments, variableMap, arguments, rot);
return null;
}
@@ -75,7 +75,7 @@ public class BlockFunction implements Function<Void> {
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
rot = RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
try {
Vector3.Mutable set = Vector3.of(FastMath.roundToInt(xz.getX()),
y.apply(implementationArguments, variableMap).doubleValue(),

View File

@@ -48,8 +48,7 @@ public class PullFunction implements Function<Void> {
Vector2 xz = RotationUtil.rotateVector(Vector2.of(x.apply(implementationArguments, variableMap).doubleValue(),
z.apply(implementationArguments, variableMap).doubleValue()), arguments.getRotation());
BlockState rot = data.clone();
RotationUtil.rotateBlockData(rot, arguments.getRotation().inverse());
BlockState rot = RotationUtil.rotateBlockData(data, arguments.getRotation().inverse());
Vector3.Mutable mutable = Vector3.of(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(),
FastMath.roundToInt(xz.getZ())).mutable().add(arguments.getOrigin());

View File

@@ -14,31 +14,33 @@ import com.dfsek.terra.api.block.BlockType;
import com.dfsek.terra.api.block.state.properties.Property;
public interface BlockState extends Cloneable, Handle {
public interface BlockState extends Handle {
boolean matches(BlockState other);
BlockState clone();
<T extends Comparable<T>> boolean has(Property<T> property);
<T> boolean has(Property<T> property);
<T extends Comparable<T>> T get(Property<T> property);
<T> T get(Property<T> property);
<T extends Comparable<T>> BlockState set(Property<T> property, T value);
<T> BlockState set(Property<T> property, T value);
default <T> BlockState ifProperty(Property<T> property, Consumer<BlockState> action) {
default <T extends Comparable<T>> BlockState ifProperty(Property<T> property, Consumer<BlockState> action) {
if(has(property)) action.accept(this);
return this;
}
default <T> BlockState setIfPresent(Property<T> property, T value) {
default <T extends Comparable<T>> BlockState setIfPresent(Property<T> property, T value) {
if(has(property)) set(property, value);
return this;
}
BlockType getBlockType();
String getAsString();
default String getAsString() {
return getAsString(true);
}
String getAsString(boolean properties);
boolean isAir();
}

View File

@@ -104,8 +104,8 @@ public final class RotationUtil {
};
}
public static void rotateBlockData(BlockState data, Rotation r) {
data
public static BlockState rotateBlockData(BlockState data, Rotation r) {
return data
.ifProperty(Properties.NORTH, state -> state.set(rotateCardinal(Properties.NORTH, r), state.get(Properties.NORTH)))
.ifProperty(Properties.SOUTH, state -> state.set(rotateCardinal(Properties.SOUTH, r), state.get(Properties.SOUTH)))
.ifProperty(Properties.EAST, state -> state.set(rotateCardinal(Properties.EAST, r), state.get(Properties.EAST)))