Add base stuff for a block with connected textures

Only adds "full", no horizontal or alike. All the models are done using a Forge blockstate for simplicity, so copying the example Blockstate and changing the textures is the easiest way to add a model

This is based off the tutorial by Darkhax linked below, though with a few things modified to make it easier for extending and support adding via EnumBlocks. http://tutorials.darkhax.net/connected-block-textures.html
This commit is contained in:
KnightMiner committed 2016-06-05 16:50:20 -05:00
1 parent 7925eb0665
commit 7e2b6b0464
15 files changed
+420 -16

No files matched your search

@@ -0,0 +1,94 @@
package slimeknights.mantle.block;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
/**
* Creates a block with textures that connect to other blocks
* <p>
* Based off a tutorial by Darkhax, used under the Creative Commons Zero 1.0 Universal license
*/
public class BlockConnectedTexture extends Block {
// These are the properties used for determining whether or not a side is connected. They
// do NOT take up block IDs, they are unlisted properties
public static final PropertyBool CONNECTED_DOWN = PropertyBool.create("connected_down");
public static final PropertyBool CONNECTED_UP = PropertyBool.create("connected_up");
public static final PropertyBool CONNECTED_NORTH = PropertyBool.create("connected_north");
public static final PropertyBool CONNECTED_SOUTH = PropertyBool.create("connected_south");
public static final PropertyBool CONNECTED_WEST = PropertyBool.create("connected_west");
public static final PropertyBool CONNECTED_EAST = PropertyBool.create("connected_east");
public BlockConnectedTexture(Material material) {
super(material);
// By default none of the sides are connected
this.setDefaultState(this.blockState.getBaseState()
.withProperty(CONNECTED_DOWN, Boolean.FALSE)
.withProperty(CONNECTED_EAST, Boolean.FALSE)
.withProperty(CONNECTED_NORTH, Boolean.FALSE)
.withProperty(CONNECTED_SOUTH, Boolean.FALSE)
.withProperty(CONNECTED_UP, Boolean.FALSE)
.withProperty(CONNECTED_WEST, Boolean.FALSE));
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos position) {
// Creates the state to use for the block. This is where we check if every side is
// connectable or not.
return state.withProperty(CONNECTED_DOWN, this.isSideConnectable(world, position, EnumFacing.DOWN))
.withProperty(CONNECTED_EAST, this.isSideConnectable(world, position, EnumFacing.EAST))
.withProperty(CONNECTED_NORTH, this.isSideConnectable(world, position, EnumFacing.NORTH))
.withProperty(CONNECTED_SOUTH, this.isSideConnectable(world, position, EnumFacing.SOUTH))
.withProperty(CONNECTED_UP, this.isSideConnectable(world, position, EnumFacing.UP))
.withProperty(CONNECTED_WEST, this.isSideConnectable(world, position, EnumFacing.WEST));
}
@Nonnull
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, new IProperty[] { CONNECTED_DOWN, CONNECTED_UP, CONNECTED_NORTH, CONNECTED_SOUTH, CONNECTED_WEST, CONNECTED_EAST });
}
// Since the block has state information but we are not switching the meta value, we have
// to override this method to return 0
@Override
public int getMetaFromState(IBlockState state) {
return 0;
}
/**
* Checks if a specific side of a block can connect to this block. For this example, a side
* is connectable if the block is the same block as this one.
*
* @param world The world to run the check in.
* @param pos The position of the block to check for.
* @param side The side of the block to check.
* @return Whether or not the side is connectable.
*/
private boolean isSideConnectable(IBlockAccess world, BlockPos pos, EnumFacing side) {
final IBlockState original = world.getBlockState(pos);
final IBlockState connected = world.getBlockState(pos.offset(side));
return original != null && connected != null && canConnect(original, connected);
}
/**
* Checks if this block should connect to another block
* @param state BlockState to check
* @return True if the block is valid to connect
*/
protected boolean canConnect(@Nonnull IBlockState original, @Nonnull IBlockState connected) {
return original.getBlock() == connected.getBlock();
}
}
@@ -21,7 +21,7 @@ public class EnumBlock<E extends Enum<E> & EnumBlock.IEnumMeta & IStringSerializ
public final PropertyEnum<E> prop;
private final E[] values;
private static PropertyEnum<?> tmp;
protected static PropertyEnum<?> tmp;
public EnumBlock(Material material, PropertyEnum<E> prop, Class<E> clazz) {
super(preInit(material, prop));
@@ -29,7 +29,6 @@ public class EnumBlock<E extends Enum<E> & EnumBlock.IEnumMeta & IStringSerializ
values = clazz.getEnumConstants();
}
@SuppressWarnings("unchecked")
private static Material preInit(Material material, PropertyEnum<?> property) {
tmp = property;
return material;
@@ -0,0 +1,80 @@
package slimeknights.mantle.block;
import javax.annotation.Nonnull;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
/**
* Creates a block with textures that connect to other blocks
* <p>
* Based off a tutorial by Darkhax, used under the Creative Commons Zero 1.0 Universal license
*/
public class EnumBlockConnectedTexture<E extends Enum<E> & EnumBlock.IEnumMeta & IStringSerializable> extends EnumBlock<E> {
public EnumBlockConnectedTexture(Material material, PropertyEnum<E> prop, Class<E> clazz) {
super(material, prop, clazz);
// By default none of the sides are connected
this.setDefaultState(this.blockState.getBaseState()
.withProperty(BlockConnectedTexture.CONNECTED_DOWN, Boolean.FALSE)
.withProperty(BlockConnectedTexture.CONNECTED_EAST, Boolean.FALSE)
.withProperty(BlockConnectedTexture.CONNECTED_NORTH, Boolean.FALSE)
.withProperty(BlockConnectedTexture.CONNECTED_SOUTH, Boolean.FALSE)
.withProperty(BlockConnectedTexture.CONNECTED_UP, Boolean.FALSE)
.withProperty(BlockConnectedTexture.CONNECTED_WEST, Boolean.FALSE));
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos position) {
// Creates the state to use for the block. This is where we check if every side is
// connectable or not.
return state.withProperty(BlockConnectedTexture.CONNECTED_DOWN, this.isSideConnectable(world, position, EnumFacing.DOWN))
.withProperty(BlockConnectedTexture.CONNECTED_EAST, this.isSideConnectable(world, position, EnumFacing.EAST))
.withProperty(BlockConnectedTexture.CONNECTED_NORTH, this.isSideConnectable(world, position, EnumFacing.NORTH))
.withProperty(BlockConnectedTexture.CONNECTED_SOUTH, this.isSideConnectable(world, position, EnumFacing.SOUTH))
.withProperty(BlockConnectedTexture.CONNECTED_UP, this.isSideConnectable(world, position, EnumFacing.UP))
.withProperty(BlockConnectedTexture.CONNECTED_WEST, this.isSideConnectable(world, position, EnumFacing.WEST));
}
@Nonnull
@Override
protected BlockStateContainer createBlockState() {
if(prop == null) {
return new BlockStateContainer(this, new IProperty[] { tmp, BlockConnectedTexture.CONNECTED_DOWN, BlockConnectedTexture.CONNECTED_UP, BlockConnectedTexture.CONNECTED_NORTH, BlockConnectedTexture.CONNECTED_SOUTH, BlockConnectedTexture.CONNECTED_WEST, BlockConnectedTexture.CONNECTED_EAST });
}
return new BlockStateContainer(this, new IProperty[] { prop, BlockConnectedTexture.CONNECTED_DOWN, BlockConnectedTexture.CONNECTED_UP, BlockConnectedTexture.CONNECTED_NORTH, BlockConnectedTexture.CONNECTED_SOUTH, BlockConnectedTexture.CONNECTED_WEST, BlockConnectedTexture.CONNECTED_EAST });
}
/**
* Checks if a specific side of a block can connect to this block. For this example, a side
* is connectable if the block is the same block as this one.
*
* @param world The world to run the check in.
* @param pos The position of the block to check for.
* @param side The side of the block to check.
* @return Whether or not the side is connectable.
*/
private boolean isSideConnectable(IBlockAccess world, BlockPos pos, EnumFacing side) {
final IBlockState original = world.getBlockState(pos);
final IBlockState connected = world.getBlockState(pos.offset(side));
return original != null && connected != null && canConnect(original, connected);
}
/**
* Checks if this block should connect to another block
* @param state BlockState to check
* @return True if the block is valid to connect
*/
protected boolean canConnect(IBlockState original, IBlockState connected) {
return connected.getBlock() == original.getBlock() && connected.getValue(prop) == original.getValue(prop);
}
}
@@ -41,22 +41,24 @@ public abstract class BookElement extends Gui {
}
public void renderToolTip(FontRenderer fontRenderer, ItemStack stack, int x, int y) {
List<String> list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);
for(int i = 0; i < list.size(); ++i) {
if(i == 0) {
list.set(i, stack.getRarity().rarityColor + list.get(i));
} else {
list.set(i, TextFormatting.GRAY + list.get(i));
if(stack != null) {
List<String> list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);
for(int i = 0; i < list.size(); ++i) {
if(i == 0) {
list.set(i, stack.getRarity().rarityColor + list.get(i));
} else {
list.set(i, TextFormatting.GRAY + list.get(i));
}
}
FontRenderer font = stack.getItem().getFontRenderer(stack);
if(font == null) {
font = fontRenderer;
}
GuiUtils.drawHoveringText(list, x, y, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, -1, font);
RenderHelper.disableStandardItemLighting();
}
FontRenderer font = stack.getItem().getFontRenderer(stack);
if(font == null) {
font = fontRenderer;
}
GuiUtils.drawHoveringText(list, x, y, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, -1, font);
RenderHelper.disableStandardItemLighting();
}
@Deprecated