Initial work on standalone cables.
This commit is contained in:
parent
50d1db2695
commit
d474870977
7 changed files with 518 additions and 0 deletions
169
src/main/java/techreborn/blocks/cable/BlockCable.java
Normal file
169
src/main/java/techreborn/blocks/cable/BlockCable.java
Normal file
|
@ -0,0 +1,169 @@
|
|||
package techreborn.blocks.cable;
|
||||
|
||||
import net.minecraft.block.BlockContainer;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
import net.minecraft.block.properties.PropertyBool;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockStateContainer;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumBlockRenderType;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraft.world.ChunkCache;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import techreborn.client.TechRebornCreativeTab;
|
||||
import techreborn.parts.powerCables.EnumCableType;
|
||||
import techreborn.tiles.cable.TileCable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Created by modmuss50 on 19/05/2017.
|
||||
*/
|
||||
public class BlockCable extends BlockContainer {
|
||||
|
||||
public static final PropertyBool EAST = PropertyBool.create("east");
|
||||
public static final PropertyBool WEST = PropertyBool.create("west");
|
||||
public static final PropertyBool NORTH = PropertyBool.create("north");
|
||||
public static final PropertyBool SOUTH = PropertyBool.create("south");
|
||||
public static final PropertyBool UP = PropertyBool.create("up");
|
||||
public static final PropertyBool DOWN = PropertyBool.create("down");
|
||||
public static final IProperty<EnumCableType> TYPE = PropertyEnum.create("type", EnumCableType.class);
|
||||
|
||||
|
||||
public BlockCable() {
|
||||
super(Material.ROCK);
|
||||
setCreativeTab(TechRebornCreativeTab.instance);
|
||||
this.setUnlocalizedName("techreborn.cable");
|
||||
setDefaultState(getDefaultState().withProperty(EAST, false).withProperty(WEST, false).withProperty(NORTH, false).withProperty(SOUTH, false).withProperty(UP, false).withProperty(DOWN, false).withProperty(TYPE, EnumCableType.COPPER));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
|
||||
return new ItemStack(this, 1, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int damageDropped(IBlockState state) {
|
||||
return getMetaFromState(getDefaultState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpaqueCube(IBlockState state) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullBlock(IBlockState state) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BlockStateContainer createBlockState() {
|
||||
return new BlockStateContainer(this, EAST, WEST, NORTH, SOUTH, UP, DOWN, TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
|
||||
return getStateFromMeta(placer.getHeldItem(hand).getItemDamage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubBlocks(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> list) {
|
||||
for(EnumCableType cableType : EnumCableType.values()){
|
||||
list.add(new ItemStack(this, 1, cableType.ordinal()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetaFromState(IBlockState state) {
|
||||
return state.getValue(TYPE).ordinal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState getStateFromMeta(int meta) {
|
||||
return getDefaultState().withProperty(TYPE, EnumCableType.values()[meta]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullCube(IBlockState state) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
|
||||
state = state.getActualState(source, pos);
|
||||
float minX = state.getValue(WEST) ? 0.0F : 0.3125F;
|
||||
float minY = state.getValue(DOWN) ? 0.0F : 0.3125F;
|
||||
float minZ = state.getValue(NORTH) ? 0.0F : 0.3125F;
|
||||
float maxX = state.getValue(EAST) ? 1.0F : 0.6875F;
|
||||
float maxY = state.getValue(UP) ? 1.0F : 0.6875F;
|
||||
float maxZ = state.getValue(SOUTH) ? 1.0F : 0.6875F;
|
||||
return new AxisAlignedBB((double) minX, (double) minY, (double) minZ, (double) maxX, (double) maxY, (double) maxZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
|
||||
IBlockState actualState = state;
|
||||
for (EnumFacing facing : EnumFacing.values()) {
|
||||
TileEntity tileEntity = getTileEntitySafely(worldIn, pos.offset(facing));
|
||||
if(tileEntity != null){
|
||||
actualState = actualState.withProperty(getProperty(facing), tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing.getOpposite()));
|
||||
}
|
||||
}
|
||||
return actualState;
|
||||
}
|
||||
|
||||
//see for more info https://www.reddit.com/r/feedthebeast/comments/5mxwq9/psa_mod_devs_do_you_call_worldgettileentity_from/
|
||||
public TileEntity getTileEntitySafely(IBlockAccess blockAccess, BlockPos pos) {
|
||||
if (blockAccess instanceof ChunkCache) {
|
||||
return ((ChunkCache) blockAccess).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK);
|
||||
} else {
|
||||
return blockAccess.getTileEntity(pos);
|
||||
}
|
||||
}
|
||||
|
||||
public IProperty<Boolean> getProperty(EnumFacing facing) {
|
||||
switch (facing) {
|
||||
case EAST:
|
||||
return EAST;
|
||||
case WEST:
|
||||
return WEST;
|
||||
case NORTH:
|
||||
return NORTH;
|
||||
case SOUTH:
|
||||
return SOUTH;
|
||||
case UP:
|
||||
return UP;
|
||||
case DOWN:
|
||||
return DOWN;
|
||||
default:
|
||||
return EAST;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TileEntity createNewTileEntity(World worldIn, int meta) {
|
||||
return new TileCable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumBlockRenderType getRenderType(IBlockState state) {
|
||||
return EnumBlockRenderType.MODEL;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue