2021-05-17 03:02:24 +08:00
|
|
|
package com.gtnewhorizon.structurelib.alignment;
|
|
|
|
|
|
|
|
|
|
import com.gtnewhorizon.structurelib.alignment.enumerable.Direction;
|
|
|
|
|
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
|
|
|
|
|
import com.gtnewhorizon.structurelib.alignment.enumerable.Flip;
|
|
|
|
|
import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;
|
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
public interface IAlignment extends IAlignmentLimits, IAlignmentProvider {
|
|
|
|
|
int DIRECTIONS_COUNT = Direction.VALUES.length;
|
|
|
|
|
int ROTATIONS_COUNT = Rotation.VALUES.length;
|
|
|
|
|
int FLIPS_COUNT = Flip.VALUES.length;
|
2021-05-27 21:19:31 +08:00
|
|
|
int STATES_COUNT = ExtendedFacing.STATES_COUNT;
|
2021-05-17 03:02:24 +08:00
|
|
|
|
|
|
|
|
ExtendedFacing getExtendedFacing();
|
|
|
|
|
|
2021-12-22 21:17:39 +08:00
|
|
|
/**
|
|
|
|
|
* Set the facing without additional checks.
|
|
|
|
|
* Tools altering facing should use {@link #toolSetExtendedFacing(ExtendedFacing)} instead.
|
|
|
|
|
*/
|
2021-05-17 03:02:24 +08:00
|
|
|
void setExtendedFacing(ExtendedFacing alignment);
|
|
|
|
|
|
|
|
|
|
IAlignmentLimits getAlignmentLimits();
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-07-08 17:14:18 +08:00
|
|
|
default IAlignment getAlignment() {
|
2021-05-17 03:02:24 +08:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
static int getAlignmentIndex(ForgeDirection direction, Rotation rotation, Flip flip) {
|
|
|
|
|
return (direction.ordinal() * ROTATIONS_COUNT + rotation.getIndex()) * FLIPS_COUNT + flip.getIndex();
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default ForgeDirection getDirection() {
|
2021-05-17 03:02:24 +08:00
|
|
|
return getExtendedFacing().getDirection();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default void setDirection(ForgeDirection direction) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setExtendedFacing(getExtendedFacing().with(direction));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default Rotation getRotation() {
|
2021-05-17 03:02:24 +08:00
|
|
|
return getExtendedFacing().getRotation();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default void setRotation(Rotation rotation) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setExtendedFacing(getExtendedFacing().with(rotation));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default Flip getFlip() {
|
2021-05-17 03:02:24 +08:00
|
|
|
return getExtendedFacing().getFlip();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default void setFlip(Flip flip) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setExtendedFacing(getExtendedFacing().with(flip));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean toolSetDirection(ForgeDirection direction) {
|
|
|
|
|
if (direction == null || direction == ForgeDirection.UNKNOWN) {
|
2022-08-08 17:40:12 +08:00
|
|
|
for (int i = 0, j = getDirection().ordinal() + 1, valuesLength = Direction.VALUES.length;
|
|
|
|
|
i < valuesLength;
|
|
|
|
|
i++) {
|
2021-06-16 13:46:24 +08:00
|
|
|
if (toolSetDirection(Direction.VALUES[(j + i) % valuesLength].getForgeDirection())) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 17:14:18 +08:00
|
|
|
} else {
|
2021-05-17 03:02:24 +08:00
|
|
|
for (ExtendedFacing extendedFacing : ExtendedFacing.FOR_FACING.get(direction)) {
|
2022-07-08 17:14:18 +08:00
|
|
|
if (checkedSetExtendedFacing(extendedFacing)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean checkedSetDirection(ForgeDirection direction) {
|
|
|
|
|
if (isNewDirectionValid(direction)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setDirection(direction);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean canSetToDirectionAny(ForgeDirection direction) {
|
2021-05-17 03:02:24 +08:00
|
|
|
for (ExtendedFacing extendedFacing : ExtendedFacing.FOR_FACING.get(direction)) {
|
2022-07-08 17:14:18 +08:00
|
|
|
if (isNewExtendedFacingValid(extendedFacing)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default boolean toolSetRotation(Rotation rotation) {
|
2022-07-08 17:14:18 +08:00
|
|
|
if (rotation == null) {
|
2021-05-17 03:02:24 +08:00
|
|
|
int flips = Flip.VALUES.length;
|
|
|
|
|
int rotations = Rotation.VALUES.length;
|
2022-07-08 17:14:18 +08:00
|
|
|
for (int ii = 0, jj = getFlip().ordinal(); ii < flips; ii++) {
|
2021-06-16 13:46:24 +08:00
|
|
|
for (int i = 1, j = getRotation().ordinal(); i < rotations; i++) {
|
2022-08-08 17:40:12 +08:00
|
|
|
if (checkedSetExtendedFacing(ExtendedFacing.of(
|
|
|
|
|
getDirection(), Rotation.VALUES[(j + i) % rotations], Flip.VALUES[(jj + ii) % flips]))) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2022-07-08 17:14:18 +08:00
|
|
|
} else {
|
2021-05-17 03:02:24 +08:00
|
|
|
return checkedSetRotation(rotation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean checkedSetRotation(Rotation rotation) {
|
|
|
|
|
if (isNewRotationValid(rotation)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setRotation(rotation);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean toolSetFlip(Flip flip) {
|
|
|
|
|
if (flip == null) {
|
2021-06-16 13:46:24 +08:00
|
|
|
for (int i = 1, j = getFlip().ordinal(), valuesLength = Flip.VALUES.length; i < valuesLength; i++) {
|
|
|
|
|
if (checkedSetFlip(Flip.VALUES[(j + i) % valuesLength])) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2022-07-08 17:14:18 +08:00
|
|
|
} else {
|
2021-05-17 03:02:24 +08:00
|
|
|
return checkedSetFlip(flip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean checkedSetFlip(Flip flip) {
|
|
|
|
|
if (isNewFlipValid(flip)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setFlip(flip);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean toolSetExtendedFacing(ExtendedFacing extendedFacing) {
|
|
|
|
|
if (extendedFacing == null) {
|
2022-08-08 17:40:12 +08:00
|
|
|
for (int i = 0, j = getExtendedFacing().ordinal() + 1, valuesLength = ExtendedFacing.VALUES.length;
|
|
|
|
|
i < valuesLength;
|
|
|
|
|
i++) {
|
2022-07-08 17:14:18 +08:00
|
|
|
if (checkedSetExtendedFacing(ExtendedFacing.VALUES[(j + i) % valuesLength])) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2022-07-08 17:14:18 +08:00
|
|
|
} else {
|
2021-05-17 03:02:24 +08:00
|
|
|
return checkedSetExtendedFacing(extendedFacing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean checkedSetExtendedFacing(ExtendedFacing alignment) {
|
|
|
|
|
if (isNewExtendedFacingValid(alignment)) {
|
2021-05-17 03:02:24 +08:00
|
|
|
setExtendedFacing(alignment);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default boolean isNewDirectionValid(ForgeDirection direction) {
|
2022-07-08 17:14:18 +08:00
|
|
|
return isNewExtendedFacingValid(direction, getRotation(), getFlip());
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean isNewRotationValid(Rotation rotation) {
|
|
|
|
|
return isNewExtendedFacingValid(getDirection(), rotation, getFlip());
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean isNewFlipValid(Flip flip) {
|
|
|
|
|
return isNewExtendedFacingValid(getDirection(), getRotation(), flip);
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default boolean isExtendedFacingValid() {
|
2022-07-08 17:14:18 +08:00
|
|
|
return isNewExtendedFacingValid(getDirection(), getRotation(), getFlip());
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean isNewExtendedFacingValid(ForgeDirection direction, Rotation rotation, Flip flip) {
|
2021-05-17 03:02:24 +08:00
|
|
|
return getAlignmentLimits().isNewExtendedFacingValid(direction, rotation, flip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-07-08 17:14:18 +08:00
|
|
|
default boolean isNewExtendedFacingValid(ExtendedFacing alignment) {
|
2022-08-08 17:40:12 +08:00
|
|
|
return getAlignmentLimits()
|
|
|
|
|
.isNewExtendedFacingValid(alignment.getDirection(), alignment.getRotation(), alignment.getFlip());
|
2021-05-17 03:02:24 +08:00
|
|
|
}
|
2022-07-08 17:14:18 +08:00
|
|
|
}
|