diff --git a/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockCounter.java b/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockCounter.java index 0a8be46a3..858ca1546 100644 --- a/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockCounter.java +++ b/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockCounter.java @@ -6,6 +6,7 @@ import net.blay09.mods.cookingforblockheads.GuiHandler; import net.blay09.mods.cookingforblockheads.client.render.block.CounterBlockRenderer; import net.blay09.mods.cookingforblockheads.tile.TileCounter; +import net.blay09.mods.cookingforblockheads.utils.DirectionUtils; import net.blay09.mods.cookingforblockheads.utils.DyeUtils; import net.minecraft.block.Block; import net.minecraft.block.material.Material; @@ -16,7 +17,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; -import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -99,9 +99,8 @@ public void onBlockAdded(World world, int x, int y, int z) { @Override public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase placer, ItemStack itemStack) { - double blockRotation = (double) (placer.rotationYaw * 4.0F / 360.0F) + 0.5D; - boolean flipped = Math.abs(blockRotation - (int) blockRotation) < 0.5; - int orientation = MathHelper.floor_double(blockRotation) & 3; + boolean flipped = DirectionUtils.isHalfDirection(placer.rotationYaw); + int orientation = DirectionUtils.getDirection(placer.rotationYaw); if (orientation == 0) { world.setBlockMetadataWithNotify(x, y, z, 2, 2); } diff --git a/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockFridge.java b/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockFridge.java index ab9867df7..23f7ebdb0 100644 --- a/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockFridge.java +++ b/src/main/java/net/blay09/mods/cookingforblockheads/block/BlockFridge.java @@ -7,6 +7,7 @@ import net.blay09.mods.cookingforblockheads.client.render.block.FridgeBlockRenderer; import net.blay09.mods.cookingforblockheads.item.ItemBlockFridge; import net.blay09.mods.cookingforblockheads.tile.TileFridge; +import net.blay09.mods.cookingforblockheads.utils.DirectionUtils; import net.blay09.mods.cookingforblockheads.utils.DyeUtils; import net.minecraft.block.Block; import net.minecraft.block.material.Material; @@ -17,7 +18,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; -import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -142,9 +142,8 @@ public void onBlockAdded(World world, int x, int y, int z) { @Override public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase placer, ItemStack itemStack) { - double blockRotation = (double) (placer.rotationYaw * 4.0F / 360.0F) + 0.5D; - boolean flipped = Math.abs(blockRotation - (int) blockRotation) < 0.5; - int orientation = MathHelper.floor_double(blockRotation) & 3; + boolean flipped = DirectionUtils.isHalfDirection(placer.rotationYaw); + int orientation = DirectionUtils.getDirection(placer.rotationYaw); if (orientation == 0) { world.setBlockMetadataWithNotify(x, y, z, 2, 2); } diff --git a/src/main/java/net/blay09/mods/cookingforblockheads/utils/DirectionUtils.java b/src/main/java/net/blay09/mods/cookingforblockheads/utils/DirectionUtils.java new file mode 100644 index 000000000..be67f2b62 --- /dev/null +++ b/src/main/java/net/blay09/mods/cookingforblockheads/utils/DirectionUtils.java @@ -0,0 +1,46 @@ +package net.blay09.mods.cookingforblockheads.utils; + +public class DirectionUtils { + + /** + * Returns the result of the mathematical-operation "modulus" of any pair of a number and integer. This is distinct + * from Java's mod-operator (%), which returns the remainder of any two numbers; the mathematics-operation, + * "modulus" returns the remainder, in whole multiples. + * + * @param number operand + * @param modulus zero-congruent definition (maximum space for modular arithmetic) + * @return + */ + private static double modulus(final double number, final int modulus) { + if (number < 0) { + return number - (modulus * Math.floor(number / modulus)); + } else { + return number % modulus; + } + } + + /** + * Maps direction, calculated from the nearest cardinal-directions, as {South, West, North, East} to {0, 1, 2, 3} + * + * @param yaw lateral-direction of gaze + * @return cardinal-direction index + */ + public static int getDirection(final double yaw) { + double modified_yaw = (yaw * (4.0 / 360.0)) + 0.5; + modified_yaw = modulus(modified_yaw, 4); + return (int) Math.floor(modified_yaw); + } + + /** + * Returns the "handedness" of a current gaze, relative to the nearest cardinal-direction, represented as (binary) + * being before or after (clockwise) the nearest cardinal-direction + * + * @param yaw lateral-direction of gaze + * @return truth of being before the center of the nearest cardinal-direction + */ + public static boolean isHalfDirection(final double yaw) { + double modified_yaw = yaw * (8.0 / 360.0); + modified_yaw = modulus(modified_yaw, 2); + return Math.floor(modified_yaw) == 0; + } +}