1
1
package com .tomboshoven .minecraft .magicmirror .blocks ;
2
2
3
+ import com .tomboshoven .minecraft .magicmirror .blocks .modifiers .MagicMirrorModifier ;
4
+ import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorBase ;
3
5
import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorCore ;
4
6
import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorPart ;
5
7
import mcp .MethodsReturnNonnullByDefault ;
14
16
import net .minecraft .block .state .BlockStateContainer ;
15
17
import net .minecraft .block .state .IBlockState ;
16
18
import net .minecraft .entity .EntityLivingBase ;
19
+ import net .minecraft .entity .player .EntityPlayer ;
20
+ import net .minecraft .init .SoundEvents ;
17
21
import net .minecraft .item .ItemStack ;
18
22
import net .minecraft .tileentity .TileEntity ;
19
23
import net .minecraft .util .EnumBlockRenderType ;
20
24
import net .minecraft .util .EnumFacing ;
25
+ import net .minecraft .util .EnumHand ;
21
26
import net .minecraft .util .IStringSerializable ;
22
27
import net .minecraft .util .Rotation ;
28
+ import net .minecraft .util .SoundCategory ;
23
29
import net .minecraft .util .math .AxisAlignedBB ;
24
30
import net .minecraft .util .math .BlockPos ;
25
31
import net .minecraft .world .IBlockAccess ;
28
34
import javax .annotation .Nullable ;
29
35
import javax .annotation .ParametersAreNonnullByDefault ;
30
36
37
+ @ SuppressWarnings ("deprecation" )
31
38
@ ParametersAreNonnullByDefault
32
39
@ MethodsReturnNonnullByDefault
33
40
public class BlockMagicMirror extends BlockHorizontal {
34
41
/**
35
42
* Property describing whether the mirror is completely constructed.
36
43
*/
37
- private static final PropertyBool COMPLETE = PropertyBool .create ("complete" );
44
+ public static final PropertyBool COMPLETE = PropertyBool .create ("complete" );
38
45
39
46
/**
40
47
* Property describing which part of the mirror is being represented by this block.
41
48
*/
42
- private static final PropertyEnum <EnumPartType > PART = PropertyEnum .create ("part" , EnumPartType .class );
49
+ public static final PropertyEnum <EnumPartType > PART = PropertyEnum .create ("part" , EnumPartType .class );
43
50
44
51
/**
45
52
* The bounding boxes of the various orientations of this block; should be indexed by facing.horizontalIndex()
@@ -55,12 +62,16 @@ public class BlockMagicMirror extends BlockHorizontal {
55
62
new AxisAlignedBB (0 , 0 , 0 , 0.125 , 1 , 1 ),
56
63
};
57
64
58
- public BlockMagicMirror () {
65
+ /**
66
+ * Create a new Magic Mirror block.
67
+ * This is typically not necessary. Use Blocks.blockMagicMirror instead.
68
+ */
69
+ BlockMagicMirror () {
59
70
super (new Material (MapColor .GRAY ));
60
71
61
72
// By default, we're the bottom part of a broken mirror
62
73
setDefaultState (
63
- this . blockState .getBaseState ()
74
+ blockState .getBaseState ()
64
75
.withProperty (COMPLETE , Boolean .FALSE )
65
76
.withProperty (PART , EnumPartType .BOTTOM )
66
77
);
@@ -92,8 +103,8 @@ public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Bloc
92
103
// Break the mirror if the other part is broken.
93
104
if (state .getValue (COMPLETE )) {
94
105
if (
95
- ( state .getValue (PART ) == EnumPartType .TOP && worldIn .getBlockState (pos .down ()).getBlock () != this ) ||
96
- ( state .getValue (PART ) == EnumPartType .BOTTOM && worldIn .getBlockState (pos .up ()).getBlock () != this )
106
+ state .getValue (PART ) == EnumPartType .TOP && worldIn .getBlockState (pos .down ()).getBlock () != this ||
107
+ state .getValue (PART ) == EnumPartType .BOTTOM && worldIn .getBlockState (pos .up ()).getBlock () != this
97
108
) {
98
109
worldIn .setBlockState (pos , state .withProperty (COMPLETE , false ));
99
110
}
@@ -114,13 +125,13 @@ public int getMetaFromState(IBlockState state) {
114
125
115
126
@ Override
116
127
public IBlockState getStateFromMeta (int meta ) {
117
- return this . getDefaultState ()
128
+ return getDefaultState ()
118
129
.withProperty (FACING , EnumFacing .byHorizontalIndex (meta & 3 ))
119
- .withProperty (COMPLETE , (meta & ( 1 << 2 ) ) != 0 )
120
- .withProperty (PART , (meta & ( 1 << 3 )) != 0 ? EnumPartType .TOP : EnumPartType .BOTTOM );
130
+ .withProperty (COMPLETE , (meta & 1 << 2 ) != 0 )
131
+ .withProperty (PART , (meta & 1 << 3 ) == 0 ? EnumPartType .BOTTOM : EnumPartType .TOP );
121
132
}
122
133
123
-
134
+ @ Override
124
135
public BlockFaceShape getBlockFaceShape (IBlockAccess worldIn , IBlockState state , BlockPos pos , EnumFacing face ) {
125
136
// Only the opposite face is default
126
137
return state .getValue (FACING ).getOpposite () == face ? BlockFaceShape .SOLID : BlockFaceShape .UNDEFINED ;
@@ -152,8 +163,8 @@ public boolean hasTileEntity() {
152
163
}
153
164
154
165
@ Override
155
- public boolean hasTileEntity (IBlockState blockState ) {
156
- return blockState .getValue (COMPLETE );
166
+ public boolean hasTileEntity (IBlockState state ) {
167
+ return state .getValue (COMPLETE );
157
168
}
158
169
159
170
@ Nullable
@@ -166,42 +177,51 @@ public TileEntity createTileEntity(World world, IBlockState state) {
166
177
return new TileEntityMagicMirrorPart ();
167
178
}
168
179
180
+ @ Override
169
181
public IBlockState withRotation (IBlockState state , Rotation rot ) {
170
182
return state .withProperty (FACING , rot .rotate (state .getValue (FACING )));
171
183
}
172
184
185
+ @ Override
173
186
public IBlockState getStateForPlacement (World worldIn , BlockPos pos , EnumFacing facing , float hitX , float hitY , float hitZ , int meta , EntityLivingBase placer ) {
174
187
// Make sure the mirror is facing the right way when placed
175
188
return getDefaultState ().withProperty (FACING , placer .getHorizontalFacing ().getOpposite ());
176
189
}
177
190
178
191
@ Override
179
- public String getTranslationKey () {
180
- return super .getTranslationKey ();
181
- }
192
+ public boolean onBlockActivated (World worldIn , BlockPos pos , IBlockState state , EntityPlayer playerIn , EnumHand hand , EnumFacing facing , float hitX , float hitY , float hitZ ) {
193
+ // First, see if we can add a modifier
194
+ ItemStack heldItem = playerIn .getHeldItem (hand );
195
+ if (!heldItem .isEmpty ()) {
196
+ for (MagicMirrorModifier modifier : MagicMirrorModifier .getModifiers ()) {
197
+ if (modifier .canModify (worldIn , pos , heldItem )) {
198
+ modifier .apply (worldIn , pos , heldItem );
199
+ worldIn .playSound (pos .getX () + .5 , pos .getY () + .5 , pos .getZ () + .5 , SoundEvents .ITEM_ARMOR_EQUIP_GENERIC , SoundCategory .BLOCKS , .6f , .6f , true );
200
+ return true ;
201
+ }
202
+ }
203
+ }
182
204
183
- /**
184
- * @param blockState: The blockstate to evaluate.
185
- * @return Which direction this mirror block is facing in.
186
- */
187
- public EnumFacing getFacing ( IBlockState blockState ) {
188
- return blockState . getValue ( FACING );
189
- }
205
+ // Then, see if any existing modifier can do something.
206
+ TileEntity tileEntity = worldIn . getTileEntity ( pos );
207
+ if ( tileEntity instanceof TileEntityMagicMirrorBase ) {
208
+ if ((( TileEntityMagicMirrorBase ) tileEntity ). tryActivate ( playerIn , hand )) {
209
+ return true ;
210
+ }
211
+ }
190
212
191
- /**
192
- * @param blockState: The blockstate to evaluate.
193
- * @return Which part is represented by this block.
194
- */
195
- public EnumPartType getPart (IBlockState blockState ) {
196
- return blockState .getValue (PART );
213
+ return super .onBlockActivated (worldIn , pos , state , playerIn , hand , facing , hitX , hitY , hitZ );
197
214
}
198
215
199
- /**
200
- * @param blockState: The blockstate to evaluate.
201
- * @return Whether the mirror is completely constructed.
202
- */
203
- public boolean isComplete (IBlockState blockState ) {
204
- return blockState .getValue (COMPLETE );
216
+ @ Override
217
+ public void breakBlock (World worldIn , BlockPos pos , IBlockState state ) {
218
+ if (state .getValue (COMPLETE )) {
219
+ TileEntity tileEntity = worldIn .getTileEntity (pos );
220
+ if (tileEntity instanceof TileEntityMagicMirrorBase ) {
221
+ ((TileEntityMagicMirrorBase ) tileEntity ).removeModifiers (worldIn , pos );
222
+ }
223
+ }
224
+ super .breakBlock (worldIn , pos , state );
205
225
}
206
226
207
227
/**
@@ -212,9 +232,13 @@ public enum EnumPartType implements IStringSerializable {
212
232
BOTTOM ("bottom" , 1 ),
213
233
;
214
234
215
- String name ;
216
- int value ;
235
+ private final String name ;
236
+ private final int value ;
217
237
238
+ /**
239
+ * @param name The name of the part.
240
+ * @param value The integer value of the part; used for setting block metadata.
241
+ */
218
242
EnumPartType (String name , int value ) {
219
243
this .name = name ;
220
244
this .value = value ;
@@ -225,8 +249,11 @@ public String getName() {
225
249
return name ;
226
250
}
227
251
252
+ /**
253
+ * @return The integer value of the part; used for setting block metadata.
254
+ */
228
255
int getValue () {
229
- return this . value ;
256
+ return value ;
230
257
}
231
258
}
232
259
}
0 commit comments