-
Notifications
You must be signed in to change notification settings - Fork 53
/
BlockColoredRotatable.java
112 lines (95 loc) · 3.72 KB
/
BlockColoredRotatable.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package choonster.testmod3.block;
import choonster.testmod3.TestMod3;
import choonster.testmod3.tileentity.TileEntityColoredRotatable;
import choonster.testmod3.util.OreDictUtils;
import net.minecraft.block.BlockColored;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.util.Optional;
/**
* A block with 16 colours (stored in the metadata) and 6 facings (stored in the TileEntity).
*
* @author Choonster
*/
public class BlockColoredRotatable extends BlockColored {
public static final IProperty<EnumFacing> FACING = PropertyDirection.create("facing");
public BlockColoredRotatable(Material materialIn, String blockName) {
super(materialIn);
BlockTestMod3.setBlockName(this, blockName);
setCreativeTab(TestMod3.creativeTab);
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, COLOR, FACING);
}
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new TileEntityColoredRotatable();
}
@Nullable
public TileEntityColoredRotatable getTileEntity(IBlockAccess world, BlockPos pos) {
return (TileEntityColoredRotatable) world.getTileEntity(pos);
}
public EnumFacing getFacing(IBlockAccess world, BlockPos pos) {
final TileEntityColoredRotatable tileEntity = getTileEntity(world, pos);
return tileEntity != null ? tileEntity.getFacing() : EnumFacing.NORTH;
}
public void setFacing(IBlockAccess world, BlockPos pos, EnumFacing facing) {
final TileEntityColoredRotatable tileEntity = getTileEntity(world, pos);
if (tileEntity != null) {
tileEntity.setFacing(facing);
}
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
setFacing(worldIn, pos, BlockPistonBase.getFacingFromEntity(pos, placer));
}
@SuppressWarnings("deprecation")
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
return state.withProperty(FACING, getFacing(worldIn, pos));
}
@Override
public boolean rotateBlock(World world, BlockPos pos, EnumFacing axis) {
final IBlockState oldState = world.getBlockState(pos);
final EnumFacing facing = getFacing(world, pos);
setFacing(world, pos, facing.rotateAround(axis.getAxis()));
world.notifyBlockUpdate(pos, oldState, world.getBlockState(pos), 3);
return true;
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
if (heldItem != null) { // If the player is holding dye, change the colour
final Optional<EnumDyeColor> dyeColour = OreDictUtils.INSTANCE.getDyeColour(heldItem);
if (dyeColour.isPresent()) {
final boolean success = recolorBlock(worldIn, pos, side, dyeColour.get());
if (success) {
heldItem.stackSize--;
return true;
}
}
return false;
} else { // Else rotate the block
return rotateBlock(worldIn, pos, side);
}
}
}