/*******************************************************************************
 * Arekkuusu / Solar 2017
 *
 * This project is licensed under the MIT.
 * The source code is available on github:
 * https://github.com/ArekkuusuJerii/Solar#solar
 ******************************************************************************/
package arekkuusu.solar.api.helper;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.Rotation;

import static net.minecraft.util.EnumFacing.*;

/**
 * Created by <Snack> on 19/01/2018.
 * It's distributed as part of Solar.
 */
public final class FacingHelper {

	public static EnumFacing rotateXY(EnumFacing actual, EnumFacing.AxisDirection direction, EnumFacing facing) {
		boolean inverse = direction == EnumFacing.AxisDirection.POSITIVE;
		switch(facing) {
			case NORTH:
			case SOUTH:
				inverse = inverse ? facing == SOUTH : facing == NORTH;
				actual = rotateX(actual, inverse);
				break;
			case WEST:
			case EAST:
				inverse = inverse ? facing == EAST : facing == WEST;
				actual = rotateZ(actual, inverse);
				break;
		}
		return actual;
	}

	public static EnumFacing rotateX(EnumFacing facing, boolean inverse) {
		if(!inverse) {
			return facing.rotateAround(Axis.X);
		} else {
			switch(facing) {
				case NORTH:
					return UP;
				case SOUTH:
					return DOWN;
				case UP:
					return SOUTH;
				case DOWN:
					return NORTH;
				case EAST:
				case WEST:
				default:
					throw new IllegalStateException("Unable to get X-rotated facing of " + facing);
			}
		}
	}

	public static EnumFacing rotateZ(EnumFacing facing, boolean inverse) {
		if(!inverse) {
			return facing.rotateAround(Axis.Z);
		} else {
			switch(facing) {
				case EAST:
					return UP;
				case WEST:
					return DOWN;
				case UP:
					return WEST;
				case DOWN:
					return EAST;
				case SOUTH:
				default:
					throw new IllegalStateException("Unable to get Z-rotated facing of " + facing);
			}
		}
	}

	public static Rotation getHorizontalRotation(EnumFacing from, EnumFacing to) {
		if(from.getAxis().isVertical() || to.getAxis().isVertical()) return Rotation.NONE;
		if(from.getOpposite() == to) {
			return Rotation.CLOCKWISE_180;
		} else if(from != to) {
			int indexFrom = from.getHorizontalIndex();
			int indexTo = to.getHorizontalIndex();
			if(indexFrom < indexTo || (indexFrom == 3 && indexTo == 0)) {
				return Rotation.CLOCKWISE_90;
			} else {
				return Rotation.COUNTERCLOCKWISE_90;
			}
		}
		return Rotation.NONE;
	}
}

@@ -2025,24 +2025,40 @@
  %7B%0A%09%09if(from
+ != to && (!from
 .getAxis().i
@@ -2069,19 +2069,20 @@
 tical() 
-%7C%7C 
+&& !
 to.getAx
@@ -2103,33 +2103,15 @@
 l())
- return Rotation.NONE;
+) %7B
 %0A%09%09
+%09
 if(f
@@ -2134,19 +2134,18 @@
 ) == to)
- %7B
 %0A
+%09
 %09%09%09retur
@@ -2174,34 +2174,8 @@
 80;%0A
-%09%09%7D else if(from != to) %7B%0A
 %09%09%09i
@@ -2264,16 +2264,17 @@
 ;%0A%09%09%09if(
+(
 indexFro
@@ -2275,17 +2275,17 @@
 dexFrom 
-%3C
+%3E
  indexTo
@@ -2293,53 +2293,110 @@
 %7C%7C (
-indexF
+f
 rom == 
-3 && indexTo == 0)) %7B%0A%09%09%09%09return
+SOUTH && to == EAST)))%0A%09%09%09%09return from != EAST %7C%7C to != SOUTH ? Rotation.COUNTERCLOCKWISE_90 :
  Rot
@@ -2418,18 +2418,16 @@
 _90;%0A%09%09%09
-%7D 
 else %7B%0A%09
@@ -2437,39 +2437,32 @@
 return Rotation.
-COUNTER
 CLOCKWISE_90;%0A%09%09

Fix horizontal rotation logic