diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java b/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java index f89787fa1..e0bac9608 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java @@ -4,6 +4,8 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState; import net.momirealms.craftengine.core.item.context.BlockPlaceContext; import net.momirealms.craftengine.core.util.Direction; import net.momirealms.craftengine.core.util.HorizontalDirection; +import net.momirealms.craftengine.core.util.MiscUtils; +import net.momirealms.craftengine.core.util.SegmentedAngle; import net.momirealms.sparrow.nbt.Tag; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,7 +35,7 @@ public abstract class Property> { Property directionProperty = (Property) property; return (context, state) -> state.with(directionProperty, context.getNearestLookingDirection().opposite()); } else { - throw new IllegalArgumentException("Unsupported property type used in hard-coded `facing` property: " + property.valueClass()); + return (context, state) -> state; } })); HARD_CODED_PLACEMENTS.put("facing_clockwise", (property -> { @@ -41,13 +43,26 @@ public abstract class Property> { Property directionProperty = (Property) property; return (context, state) -> state.with(directionProperty, context.getHorizontalDirection().clockWise().toHorizontalDirection()); } else { - throw new IllegalArgumentException("Unsupported property type used in hard-coded `facing_clockwise` property: " + property.valueClass()); + return (context, state) -> state; } })); HARD_CODED_PLACEMENTS.put("waterlogged", (property -> { Property waterloggedProperty = (Property) property; return (context, state) -> state.with(waterloggedProperty, context.isWaterSource()); })); + HARD_CODED_PLACEMENTS.put("rotation", property -> { + if (property.valueClass() == Integer.class) { + IntegerProperty rotationProperty = (IntegerProperty) property; + if (rotationProperty.min == 0 && rotationProperty.max > 0) { + return (context, state) -> { + float rotation = context.getRotation(); + SegmentedAngle segmentedAngle = new SegmentedAngle(rotationProperty.max + 1); + return state.with(rotationProperty, segmentedAngle.fromDegrees(rotation + 180)); + }; + } + } + return (context, state) -> state; + }); } private final Class clazz; diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/SegmentedAngle.java b/core/src/main/java/net/momirealms/craftengine/core/util/SegmentedAngle.java new file mode 100644 index 000000000..9d1aea132 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/util/SegmentedAngle.java @@ -0,0 +1,58 @@ +package net.momirealms.craftengine.core.util; + +public class SegmentedAngle { + private final int segments; + private final float degreeToAngle; + private final float angleToDegree; + + public SegmentedAngle(int segments) { + if (segments < 2) { + throw new IllegalArgumentException("Segments cannot be less than 2"); + } else if (segments > 360) { + throw new IllegalArgumentException("Segments cannot be greater than 360"); + } else { + this.segments = segments; + this.degreeToAngle = (float) segments / 360.0F; + this.angleToDegree = 360.0F / (float) segments; + } + } + + public boolean isSameAxis(int alpha, int beta) { + int halfSegments = this.segments / 2; + return (alpha % halfSegments) == (beta % halfSegments); + } + + public int fromDirection(Direction direction) { + if (direction.axis().isVertical()) { + return 0; + } else { + int i = direction.data2d(); + return i * (this.segments / 4); + } + } + + public int fromDegreesWithTurns(float degrees) { + return Math.round(degrees * this.degreeToAngle); + } + + public int fromDegrees(float degrees) { + return this.normalize(this.fromDegreesWithTurns(degrees)); + } + + public float toDegreesWithTurns(int rotation) { + return (float) rotation * this.angleToDegree; + } + + public float toDegrees(int rotation) { + float f = this.toDegreesWithTurns(this.normalize(rotation)); + return f >= 180.0F ? f - 360.0F : f; + } + + public int normalize(int rotationBits) { + return ((rotationBits % this.segments) + this.segments) % this.segments; + } + + public int getSegments() { + return this.segments; + } +} \ No newline at end of file