Skip to content

Commit aa04ed1

Browse files
committed
Add Plane block
-- http://www.minecraftforge.net/forum/index.php/topic,36713.0.html -- Model rotations aren't working properly -- The block still has a full bounding box at the moment, I'm trying to figure out a good way to give it a stairs-like bounding box without having to hardcode every rotation
1 parent d46882b commit aa04ed1

File tree

12 files changed

+289
-0
lines changed

12 files changed

+289
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
}

src/main/java/com/choonster/testmod3/client/model/ModModelManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ private void registerBlockModels() {
8686
registerBlockItemModelForMeta(ModBlocks.variants, enumType.getMeta(), "variant=" + enumType.getName());
8787
}
8888

89+
registerBlockItemModel(ModBlocks.mirrorPlane, new ModelResourceLocation(ModBlocks.mirrorPlane.getRegistryName(), "horizontal_rotation=north,vertical_rotation=up"));
90+
8991
ModBlocks.blocks.stream().filter(block -> !itemsRegistered.contains(Item.getItemFromBlock(block))).forEach(this::registerBlockItemModel);
9092
}
9193

@@ -100,6 +102,10 @@ private void registerBlockItemModel(Block block, String modelLocation) {
100102
registerItemModel(Item.getItemFromBlock(block), modelLocation);
101103
}
102104

105+
private void registerBlockItemModel(Block block, ModelResourceLocation fullModelLocation){
106+
registerItemModel(Item.getItemFromBlock(block), fullModelLocation);
107+
}
108+
103109
private void registerBlockItemModelForMeta(Block block, int metadata, String variant) {
104110
registerItemModelForMeta(Item.getItemFromBlock(block), metadata, variant);
105111
}

src/main/java/com/choonster/testmod3/init/ModBlocks.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ModBlocks {
4242
public static BlockVariants variants;
4343
public static BlockClientPlayerRotation clientPlayerRotation;
4444
public static BlockPigSpawnerRefiller pigSpawnerRefiller;
45+
public static BlockPlane mirrorPlane;
4546

4647
public static BlockPipeBasic pipeBasic;
4748
public static BlockPipeFluid pipeFluid;
@@ -65,6 +66,7 @@ public static void registerBlocks() {
6566
variants = registerBlock(new BlockVariants(Material.iron), ItemMultiTextureMod.class, BlockVariants.EnumType.getNames(), true);
6667
clientPlayerRotation = registerBlock(new BlockClientPlayerRotation());
6768
pigSpawnerRefiller = registerBlock(new BlockPigSpawnerRefiller());
69+
mirrorPlane = registerBlock(new BlockPlane(Material.iron, "mirrorPlane"));
6870

6971
pipeBasic = registerBlock(new BlockPipeBasic("basicPipe"));
7072
pipeFluid = registerBlock(new BlockPipeFluid());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"forge_marker": 1,
3+
"defaults": {
4+
"model": "testmod3:plane",
5+
"textures": {
6+
"side": "testmod3:blocks/mirror_plane_side",
7+
"base": "testmod3:blocks/mirror_plane_base",
8+
"plane": "testmod3:blocks/mirror_plane_plane"
9+
},
10+
"transform": "forge:default-block"
11+
},
12+
"variants": {
13+
"horizontal_rotation=north,vertical_rotation=up": { "x": 0, "y": 0 },
14+
"horizontal_rotation=north,vertical_rotation=down": { "x": 180, "y": 180 },
15+
"horizontal_rotation=north,vertical_rotation=side": { "x": 270, "y": 0 },
16+
17+
"horizontal_rotation=south,vertical_rotation=up": { "x": 0, "y": 180 },
18+
"horizontal_rotation=south,vertical_rotation=down": { "x": 180, "y": 0 },
19+
"horizontal_rotation=south,vertical_rotation=side": { "x": 270, "y": 180 },
20+
21+
"horizontal_rotation=east,vertical_rotation=up": { "x": 0, "y": 90 },
22+
"horizontal_rotation=east,vertical_rotation=down": { "x": 180, "y": 270 },
23+
"horizontal_rotation=east,vertical_rotation=side": { "x": 270, "y": 90 },
24+
25+
"horizontal_rotation=west,vertical_rotation=up": { "x": 0, "y": 270 },
26+
"horizontal_rotation=west,vertical_rotation=down": { "x": 180, "y": 90 },
27+
"horizontal_rotation=west,vertical_rotation=side": { "x": 270, "y": 270 }
28+
}
29+
}

src/main/resources/assets/testmod3/lang/en_US.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tile.testmod3:endPortalFrameFull.name=End Portal Frame Full
1919
tile.testmod3:potionEffect.name=Potion Effect Block
2020
tile.testmod3:clientPlayerRotation.name=Client Player Rotation Block
2121
tile.testmod3:pigSpawnerRefiller.name=Pig Spawner Refiller
22+
tile.testmod3:mirrorPlane.name=Mirror Plane
2223

2324
tile.testmod3:basicPipe.name=Basic Pipe
2425
tile.testmod3:fluidPipe.name=Fluid Pipe
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"__comment": "Model generated using MrCrayfish's Model Creator (http://mrcrayfish.com/modelcreator/)",
3+
"textures": {
4+
"particle": "#base"
5+
},
6+
"elements": [
7+
{
8+
"name": "Base",
9+
"from": [ 0.0, 0.0, 0.0 ],
10+
"to": [ 16.0, 16.0, 16.0 ],
11+
"faces": {
12+
"east": { "texture": "#side", "uv": [ 0.0, 0.0, 16.0, 16.0 ] },
13+
"south": { "texture": "#base", "uv": [ 0.0, 0.0, 16.0, 16.0 ] },
14+
"west": { "texture": "#side", "uv": [ 16.0, 0.0, 0.0, 16.0 ] },
15+
"down": { "texture": "#base", "uv": [ 0.0, 0.0, 16.0, 16.0 ] }
16+
}
17+
},
18+
{
19+
"name": "Plane",
20+
"from": [ 0.0, 0.0, 0.0 ],
21+
"to": [ 16.0, 16.0, 0.0 ],
22+
"rotation": { "origin": [ 0.0, 0.0, 0.0 ], "axis": "x", "angle": 45.0, "rescale": "true" },
23+
"faces": {
24+
"north": { "texture": "#plane", "uv": [ 0.0, 0.0, 16.0, 16.0 ] }
25+
}
26+
}
27+
]
28+
}
317 Bytes
Loading
378 Bytes
Loading
300 Bytes
Loading

textures/mirror_plane_base.xcf

2.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)