-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathStateTexturedBlock.java
More file actions
74 lines (60 loc) · 2.86 KB
/
StateTexturedBlock.java
File metadata and controls
74 lines (60 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package mcjty.modtut.blocks;
import mcjty.modtut.CreativeTab;
import mcjty.modtut.ModTut;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class StateTexturedBlock extends Block {
public static final PropertyDirection FACING = PropertyDirection.create("facing");
public static final PropertyBool ENABLED = PropertyBool.create("enabled");
public StateTexturedBlock() {
super(Material.ROCK);
setUnlocalizedName(ModTut.MODID + ".statetexturedblock");
setRegistryName("statetexturedblock");
setCreativeTab(CreativeTab.tabMODTAB);
setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
@SideOnly(Side.CLIENT)
public void initModel() {
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
}
@Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block blockIn, BlockPos p_189540_5_) {
int powered = world.isBlockIndirectlyGettingPowered(pos);
world.setBlockState(pos, state.withProperty(ENABLED, powered > 0), 3);
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
world.setBlockState(pos, state.withProperty(FACING, getFacingFromEntity(pos, placer)), 2);
}
public static EnumFacing getFacingFromEntity(BlockPos clickedBlock, EntityLivingBase entity) {
return EnumFacing.getFacingFromVector((float) (entity.posX - clickedBlock.getX()), (float) (entity.posY - clickedBlock.getY()), (float) (entity.posZ - clickedBlock.getZ()));
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState()
.withProperty(FACING, EnumFacing.getFront(meta & 7))
.withProperty(ENABLED, (meta & 8) != 0);
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(FACING).getIndex() + (state.getValue(ENABLED) ? 8 : 0);
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING, ENABLED);
}
}