    001package net.minecraftforge.common;
    002
    003import net.minecraft.block.Block;
    004import net.minecraft.block.BlockChest;
    005import net.minecraft.block.BlockDispenser;
    006import net.minecraft.block.BlockDropper;
    007import net.minecraft.block.BlockPistonBase;
    008import net.minecraft.world.World;
    009import static net.minecraftforge.common.ForgeDirection.*;
    010
    011public class RotationHelper {
    012
    013    private static final ForgeDirection[] UP_DOWN_AXES = new ForgeDirection[] { UP, DOWN };
    014    public static ForgeDirection[] getValidVanillaBlockRotations(Block block)
    015    {
    016        return block instanceof BlockChest ? UP_DOWN_AXES : VALID_DIRECTIONS;
    017    }
    018
    019    public static boolean rotateVanillaBlock(Block block, World worldObj, int x, int y, int z, ForgeDirection axis)
    020    {
    021        if (worldObj.isRemote)
    022        {
    023            return false;
    024        }
    025
    026        if (block instanceof BlockChest && (axis == UP || axis == DOWN))
    027        {
    028            return rotateBlock(worldObj, x, y, z, axis, 0x7);
    029        }
    030        if (block instanceof BlockPistonBase || block instanceof BlockDropper || block instanceof BlockDispenser)
    031        {
    032            return rotateBlock(worldObj, x, y, z, axis, 0x7);
    033        }
    034        return false;
    035    }
    036
    037    private static boolean rotateBlock(World worldObj, int x, int y, int z, ForgeDirection axis, int mask)
    038    {
    039        int rotMeta = worldObj.getBlockMetadata(x, y, z);
    040        int masked = rotMeta & ~mask;
    041        ForgeDirection orientation = ForgeDirection.getOrientation(rotMeta & mask);
    042        ForgeDirection rotated = orientation.getRotation(axis);
    043        worldObj.setBlockMetadataWithNotify(x,y,z,rotated.ordinal() & mask | masked,3);
    044        return true;
    045    }
    046
    047}