|
| 1 | +package com.choonster.testmod3.block; |
| 2 | + |
| 3 | +import net.minecraft.block.material.MapColor; |
| 4 | +import net.minecraft.block.material.Material; |
| 5 | +import net.minecraft.block.properties.IProperty; |
| 6 | +import net.minecraft.block.properties.PropertyDirection; |
| 7 | +import net.minecraft.block.properties.PropertyEnum; |
| 8 | +import net.minecraft.block.state.BlockState; |
| 9 | +import net.minecraft.block.state.IBlockState; |
| 10 | +import net.minecraft.entity.EntityLivingBase; |
| 11 | +import net.minecraft.entity.player.EntityPlayer; |
| 12 | +import net.minecraft.util.*; |
| 13 | +import net.minecraft.world.IBlockAccess; |
| 14 | +import net.minecraft.world.World; |
| 15 | +import net.minecraftforge.fml.relauncher.Side; |
| 16 | +import net.minecraftforge.fml.relauncher.SideOnly; |
| 17 | + |
| 18 | +/** |
| 19 | + * A diagonal half-cube block, like the Carpenter's Slope from Carpenter's Blocks. |
| 20 | + * <p> |
| 21 | + * Test for this thread: |
| 22 | + * http://www.minecraftforge.net/forum/index.php/topic,36713.0.html |
| 23 | + * |
| 24 | + * @author Choonster |
| 25 | + */ |
| 26 | +public class BlockPlane extends BlockTestMod3 { |
| 27 | + /** |
| 28 | + * The block's rotation around the y-axis. |
| 29 | + */ |
| 30 | + public static final IProperty<EnumFacing> HORIZONTAL_ROTATION = PropertyDirection.create("horizontal_rotation", EnumFacing.Plane.HORIZONTAL); |
| 31 | + |
| 32 | + /** |
| 33 | + * The block's rotation around the x-axis. |
| 34 | + */ |
| 35 | + public static final IProperty<EnumVerticalRotation> VERTICAL_ROTATION = PropertyEnum.create("vertical_rotation", EnumVerticalRotation.class); |
| 36 | + |
| 37 | + public BlockPlane(Material material, MapColor mapColor, String blockName) { |
| 38 | + super(material, mapColor, blockName); |
| 39 | + } |
| 40 | + |
| 41 | + public BlockPlane(Material materialIn, String blockName) { |
| 42 | + super(materialIn, blockName); |
| 43 | + } |
| 44 | + |
| 45 | + { |
| 46 | + setDefaultState(getBlockState().getBaseState().withProperty(HORIZONTAL_ROTATION, EnumFacing.NORTH).withProperty(VERTICAL_ROTATION, EnumVerticalRotation.UP)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected BlockState createBlockState() { |
| 51 | + return new BlockState(this, HORIZONTAL_ROTATION, VERTICAL_ROTATION); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public IBlockState getStateFromMeta(int meta) { |
| 56 | + final EnumFacing horizontalRotation = EnumFacing.getHorizontal(meta & 3); |
| 57 | + final EnumVerticalRotation verticalRotation = EnumVerticalRotation.fromIndex(meta >> 2); |
| 58 | + |
| 59 | + return getDefaultState().withProperty(HORIZONTAL_ROTATION, horizontalRotation).withProperty(VERTICAL_ROTATION, verticalRotation); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public int getMetaFromState(IBlockState state) { |
| 64 | + final int horizontalIndex = state.getValue(HORIZONTAL_ROTATION).getHorizontalIndex(); |
| 65 | + final int verticalIndex = state.getValue(VERTICAL_ROTATION).getIndex(); |
| 66 | + |
| 67 | + return (verticalIndex << 2) | horizontalIndex; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis) { |
| 72 | + final EnumFacing.Axis axisToRotate = axis.getAxis(); |
| 73 | + |
| 74 | + IBlockState state = world.getBlockState(pos); |
| 75 | + |
| 76 | + switch (axisToRotate) { |
| 77 | + case X: |
| 78 | + case Z: |
| 79 | + state = state.cycleProperty(VERTICAL_ROTATION); |
| 80 | + break; |
| 81 | + case Y: |
| 82 | + final EnumFacing originalRotation = state.getValue(HORIZONTAL_ROTATION); |
| 83 | + final EnumFacing newRotation = axis.getAxisDirection() == EnumFacing.AxisDirection.POSITIVE ? originalRotation.rotateY() : originalRotation.rotateYCCW(); |
| 84 | + state = state.withProperty(HORIZONTAL_ROTATION, newRotation); |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + return world.setBlockState(pos, state); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) { |
| 93 | + return rotateBlock(worldIn, pos, side); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { |
| 98 | + final EnumFacing horizontalRotation = placer.getHorizontalFacing().getOpposite(); |
| 99 | + final EnumVerticalRotation verticalRotation = EnumVerticalRotation.fromFacing(facing); |
| 100 | + |
| 101 | + return super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer) |
| 102 | + .withProperty(HORIZONTAL_ROTATION, horizontalRotation) |
| 103 | + .withProperty(VERTICAL_ROTATION, verticalRotation); |
| 104 | + } |
| 105 | + |
| 106 | + @SideOnly(Side.CLIENT) |
| 107 | + @Override |
| 108 | + public EnumWorldBlockLayer getBlockLayer() { |
| 109 | + return EnumWorldBlockLayer.CUTOUT; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean isOpaqueCube() { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + @SideOnly(Side.CLIENT) |
| 118 | + @Override |
| 119 | + public boolean shouldSideBeRendered(IBlockAccess worldIn, BlockPos pos, EnumFacing side) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * A rotation around the x-axis. |
| 125 | + */ |
| 126 | + public enum EnumVerticalRotation implements IStringSerializable { |
| 127 | + DOWN(0, "down", EnumFacing.DOWN), |
| 128 | + UP(1, "up", EnumFacing.UP), |
| 129 | + SIDE(2, "side", EnumFacing.NORTH); |
| 130 | + |
| 131 | + /** |
| 132 | + * The values ordered by their index |
| 133 | + */ |
| 134 | + private static final EnumVerticalRotation[] VALUES = new EnumVerticalRotation[values().length]; |
| 135 | + |
| 136 | + static { |
| 137 | + for (EnumVerticalRotation verticalRotation : values()) { |
| 138 | + VALUES[verticalRotation.getIndex()] = verticalRotation; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get the value with the specified index. |
| 144 | + * |
| 145 | + * @param index The index |
| 146 | + * @return The value |
| 147 | + */ |
| 148 | + public static EnumVerticalRotation fromIndex(int index) { |
| 149 | + return VALUES[MathHelper.abs_int(index % VALUES.length)]; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the value corresponding to the specified facing. |
| 154 | + * |
| 155 | + * @param facing The facing |
| 156 | + * @return The value |
| 157 | + */ |
| 158 | + public static EnumVerticalRotation fromFacing(EnumFacing facing) { |
| 159 | + switch (facing) { |
| 160 | + case DOWN: |
| 161 | + return DOWN; |
| 162 | + case UP: |
| 163 | + return UP; |
| 164 | + default: |
| 165 | + return SIDE; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * The index. |
| 171 | + */ |
| 172 | + private final int index; |
| 173 | + |
| 174 | + /** |
| 175 | + * The name. |
| 176 | + */ |
| 177 | + private final String name; |
| 178 | + |
| 179 | + /** |
| 180 | + * The corresponding {@link EnumFacing}. |
| 181 | + */ |
| 182 | + private final EnumFacing facing; |
| 183 | + |
| 184 | + EnumVerticalRotation(int index, String name, EnumFacing facing) { |
| 185 | + this.index = index; |
| 186 | + this.name = name; |
| 187 | + this.facing = facing; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Get the index. |
| 192 | + * |
| 193 | + * @return The index |
| 194 | + */ |
| 195 | + public int getIndex() { |
| 196 | + return index; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Get the corresponding {@link EnumFacing}. |
| 201 | + * |
| 202 | + * @return The corresponding {@link EnumFacing} |
| 203 | + */ |
| 204 | + public EnumFacing getFacing() { |
| 205 | + return facing; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Get the name. |
| 210 | + * |
| 211 | + * @return The name |
| 212 | + */ |
| 213 | + @Override |
| 214 | + public String getName() { |
| 215 | + return name; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public String toString() { |
| 220 | + return name; |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments