/
RedstoneMachine.java
199 lines (160 loc) · 6.73 KB
/
RedstoneMachine.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package slimeknights.tmechworks.blocks;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.properties.PropertyEnum;
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.inventory.IInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import slimeknights.mantle.block.EnumBlock;
import slimeknights.mantle.tileentity.TileInventory;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.blocks.logic.RedstoneMachineLogicBase;
import javax.annotation.Nonnull;
public abstract class RedstoneMachine<E extends Enum<E> & EnumBlock.IEnumMeta & IStringSerializable> extends EnumBlock<E> implements ITileEntityProvider
{
public static final PropertyEnum<DefaultTypes> DEF_TYPE = PropertyEnum.create("type", DefaultTypes.class);
public static final PropertyDirection FACING = PropertyDirection.create("facing");
protected RedstoneMachine(Material material, PropertyEnum<E> prop, Class<E> clazz)
{
super(material, prop, clazz);
this.isBlockContainer = true;
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
protected boolean openGui(EntityPlayer player, World world, BlockPos pos)
{
player.openGui(TMechworks.instance, 0, world, pos.getX(), pos.getY(), pos.getZ());
return true;
}
@Override @Nonnull protected BlockStateContainer createBlockState ()
{
return new BlockStateContainer(this, prop, FACING);
}
@Override public IBlockState getActualState (@Nonnull IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
RedstoneMachineLogicBase baseLogic = (RedstoneMachineLogicBase) worldIn.getTileEntity(pos);
EnumFacing face = EnumFacing.NORTH;
if (baseLogic != null)
{
face = baseLogic.getFacingDirection();
}
return state.withProperty(FACING, face);
}
@Override public void neighborChanged (IBlockState state, World worldIn, BlockPos pos, Block blockIn)
{
RedstoneMachineLogicBase logicBase = (RedstoneMachineLogicBase) worldIn.getTileEntity(pos);
if (logicBase != null)
{
logicBase.updateRedstone();
}
}
public boolean hasFacingDirection ()
{
return true;
}
@Override public void onBlockPlacedBy (World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
// set custom name from named stack
if (stack.hasDisplayName()) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileInventory) {
((TileInventory) tileentity).setCustomName(stack.getDisplayName());
}
}
if (!hasFacingDirection())
return;
RedstoneMachineLogicBase baseLogic = (RedstoneMachineLogicBase) worldIn.getTileEntity(pos);
if (baseLogic == null)
{
return;
}
baseLogic.setFacingDirection(BlockPistonBase.getFacingFromEntity(pos, placer));
}
@Override public int getMetaFromState (IBlockState state)
{
return 0;
}
@Override public IBlockState getStateFromMeta (int meta)
{
return getDefaultState();
}
/////////////////////////
// BlockContainer Code //
/////////////////////////
protected boolean isInvalidNeighbor(World worldIn, BlockPos pos, EnumFacing facing) {
return worldIn.getBlockState(pos.offset(facing)).getMaterial() == Material.CACTUS;
}
protected boolean hasInvalidNeighbor(World worldIn, BlockPos pos) {
return this.isInvalidNeighbor(worldIn, pos, EnumFacing.NORTH) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.SOUTH) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.WEST) || this.isInvalidNeighbor(worldIn, pos, EnumFacing.EAST);
}
/**
* Called on both Client and Server when World#addBlockEvent is called. On the Server, this may perform additional
* changes to the world, like pistons replacing the block with an extended base. On the client, the update may
* involve replacing tile entities, playing sounds, or performing other visual actions to reflect the server side
* changes.
*/
public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int id, int param) {
super.eventReceived(state, worldIn, pos, id, param);
TileEntity tileentity = worldIn.getTileEntity(pos);
return tileentity != null && tileentity.receiveClientEvent(id, param);
}
/////////////////////////
// BlockInventory Code //
/////////////////////////
// inventories usually need a tileEntity
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@Nonnull
@Override
public abstract TileEntity createNewTileEntity(@Nonnull World worldIn, int meta);
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack stack, EnumFacing side, float clickX, float clickY, float clickZ) {
if (player.isSneaking()) {
return false;
}
if (!world.isRemote) {
return this.openGui(player, world, pos);
}
return true;
}
@Override
public void breakBlock(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileInventory) {
InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
}
public enum DefaultTypes implements IEnumMeta, IStringSerializable {
NORMAL;
@Override
public String getName() {
return toString().toLowerCase();
}
@Override
public int getMeta() {
return ordinal();
}
}
}