1
1
package com .tomboshoven .minecraft .magicmirror .blocks ;
2
2
3
+ import com .tomboshoven .minecraft .magicmirror .ModMagicMirror ;
3
4
import com .tomboshoven .minecraft .magicmirror .blocks .modifiers .MagicMirrorModifier ;
4
5
import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorBase ;
5
6
import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorCore ;
6
7
import com .tomboshoven .minecraft .magicmirror .blocks .tileentities .TileEntityMagicMirrorPart ;
8
+ import com .tomboshoven .minecraft .magicmirror .packets .Network ;
9
+ import io .netty .buffer .ByteBuf ;
7
10
import mcp .MethodsReturnNonnullByDefault ;
8
11
import net .minecraft .block .Block ;
9
12
import net .minecraft .block .BlockHorizontal ;
15
18
import net .minecraft .block .state .BlockFaceShape ;
16
19
import net .minecraft .block .state .BlockStateContainer ;
17
20
import net .minecraft .block .state .IBlockState ;
21
+ import net .minecraft .client .Minecraft ;
22
+ import net .minecraft .client .multiplayer .WorldClient ;
18
23
import net .minecraft .entity .EntityLivingBase ;
19
24
import net .minecraft .entity .player .EntityPlayer ;
20
25
import net .minecraft .init .SoundEvents ;
30
35
import net .minecraft .util .math .BlockPos ;
31
36
import net .minecraft .world .IBlockAccess ;
32
37
import net .minecraft .world .World ;
38
+ import net .minecraftforge .fml .common .SidedProxy ;
39
+ import net .minecraftforge .fml .common .network .ByteBufUtils ;
40
+ import net .minecraftforge .fml .common .network .simpleimpl .IMessage ;
41
+ import net .minecraftforge .fml .common .network .simpleimpl .IMessageHandler ;
42
+ import net .minecraftforge .fml .common .network .simpleimpl .MessageContext ;
43
+ import net .minecraftforge .fml .relauncher .Side ;
44
+ import net .minecraftforge .fml .relauncher .SideOnly ;
33
45
34
46
import javax .annotation .Nullable ;
35
47
import javax .annotation .ParametersAreNonnullByDefault ;
@@ -62,6 +74,16 @@ public class BlockMagicMirror extends BlockHorizontal {
62
74
new AxisAlignedBB (0 , 0 , 0 , 0.125 , 1 , 1 ),
63
75
};
64
76
77
+ /**
78
+ * Handler for messages describing modifiers being attached to mirrors.
79
+ */
80
+ @ SuppressWarnings ("PublicField" )
81
+ @ SidedProxy (
82
+ clientSide = "com.tomboshoven.minecraft.magicmirror.blocks.BlockMagicMirror$MessageHandlerAttachModifierClient" ,
83
+ serverSide = "com.tomboshoven.minecraft.magicmirror.blocks.BlockMagicMirror$MessageHandlerAttachModifierServer"
84
+ )
85
+ public static IMessageHandler <MessageAttachModifier , IMessage > messageHandlerAttachModifier ;
86
+
65
87
/**
66
88
* Create a new Magic Mirror block.
67
89
* This is typically not necessary. Use Blocks.blockMagicMirror instead.
@@ -80,6 +102,24 @@ public class BlockMagicMirror extends BlockHorizontal {
80
102
setSoundType (SoundType .GLASS );
81
103
}
82
104
105
+ /**
106
+ * Attach a modifier to the mirror at the specified position.
107
+ *
108
+ * @param worldIn The world containing the mirror.
109
+ * @param pos The position of the mirror in the world.
110
+ * @param heldItem The item used to attach the modifier to the mirror.
111
+ * @param modifier The modifier to attach to the mirror.
112
+ */
113
+ private static void attachModifier (World worldIn , BlockPos pos , ItemStack heldItem , MagicMirrorModifier modifier ) {
114
+ modifier .apply (worldIn , pos , heldItem );
115
+ if (worldIn .isRemote ) {
116
+ worldIn .playSound (pos .getX () + .5 , pos .getY () + .5 , pos .getZ () + .5 , SoundEvents .ITEM_ARMOR_EQUIP_GENERIC , SoundCategory .BLOCKS , .6f , .6f , true );
117
+ } else {
118
+ IMessage mirrorMessage = new MessageAttachModifier (pos , heldItem , modifier );
119
+ Network .sendToAllTracking (mirrorMessage , worldIn , pos );
120
+ }
121
+ }
122
+
83
123
@ Override
84
124
public void onBlockPlacedBy (World worldIn , BlockPos pos , IBlockState state , EntityLivingBase placer , ItemStack stack ) {
85
125
super .onBlockPlacedBy (worldIn , pos , state , placer , stack );
@@ -190,27 +230,32 @@ public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing
190
230
191
231
@ Override
192
232
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 ;
233
+ // The mirror will only do anything if it's used from the front.
234
+ if (state .getValue (FACING ) == facing ) {
235
+ if (!worldIn .isRemote ) {
236
+ // First, see if we can add a modifier
237
+ ItemStack heldItem = playerIn .getHeldItem (hand );
238
+ if (!heldItem .isEmpty ()) {
239
+ for (MagicMirrorModifier modifier : MagicMirrorModifier .getModifiers ()) {
240
+ if (modifier .canModify (worldIn , pos , heldItem )) {
241
+ attachModifier (worldIn , pos , heldItem , modifier );
242
+ return true ;
243
+ }
244
+ }
201
245
}
202
- }
203
- }
204
246
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 ;
247
+ // Then, see if any existing modifier can do something.
248
+ TileEntity tileEntity = worldIn .getTileEntity (pos );
249
+ if (tileEntity instanceof TileEntityMagicMirrorBase ) {
250
+ if (((TileEntityMagicMirrorBase ) tileEntity ).tryActivate (playerIn , hand )) {
251
+ return true ;
252
+ }
253
+ }
210
254
}
255
+ return true ;
211
256
}
212
257
213
- return super . onBlockActivated ( worldIn , pos , state , playerIn , hand , facing , hitX , hitY , hitZ ) ;
258
+ return false ;
214
259
}
215
260
216
261
@ Override
@@ -256,4 +301,87 @@ int getValue() {
256
301
return value ;
257
302
}
258
303
}
304
+
305
+ /**
306
+ * Message describing the action of attaching a new modifier to a mirror.
307
+ */
308
+ public static class MessageAttachModifier implements IMessage {
309
+ /**
310
+ * The position of the mirror in the world.
311
+ */
312
+ BlockPos mirrorPos ;
313
+ /**
314
+ * The item used to attach the modifier to the mirror.
315
+ */
316
+ ItemStack usedItemStack ;
317
+ /**
318
+ * The name of the modifier this is being attached.
319
+ */
320
+ String modifierName ;
321
+
322
+ @ SuppressWarnings ("unused" )
323
+ public MessageAttachModifier () {
324
+ }
325
+
326
+ /**
327
+ * @param mirrorPos The position of the mirror in the world.
328
+ * @param usedItemStack The item used to attach the modifier to the mirror.
329
+ * @param modifier The modifier this is being attached.
330
+ */
331
+ MessageAttachModifier (BlockPos mirrorPos , ItemStack usedItemStack , MagicMirrorModifier modifier ) {
332
+ this .mirrorPos = mirrorPos ;
333
+ this .usedItemStack = usedItemStack ;
334
+ modifierName = modifier .getName ();
335
+ }
336
+
337
+ @ Override
338
+ public void fromBytes (ByteBuf buf ) {
339
+ mirrorPos = new BlockPos (buf .readInt (), buf .readInt (), buf .readInt ());
340
+ usedItemStack = ByteBufUtils .readItemStack (buf );
341
+ modifierName = ByteBufUtils .readUTF8String (buf );
342
+ }
343
+
344
+ @ Override
345
+ public void toBytes (ByteBuf buf ) {
346
+ buf .writeInt (mirrorPos .getX ());
347
+ buf .writeInt (mirrorPos .getY ());
348
+ buf .writeInt (mirrorPos .getZ ());
349
+ ByteBufUtils .writeItemStack (buf , usedItemStack );
350
+ ByteBufUtils .writeUTF8String (buf , modifierName );
351
+ }
352
+ }
353
+
354
+ /**
355
+ * Handler for messages describing modifiers being attached to mirrors (client side).
356
+ */
357
+ @ SideOnly (Side .CLIENT )
358
+ public static class MessageHandlerAttachModifierClient implements IMessageHandler <MessageAttachModifier , IMessage > {
359
+ @ Nullable
360
+ @ Override
361
+ public IMessage onMessage (MessageAttachModifier message , MessageContext ctx ) {
362
+ WorldClient world = Minecraft .getMinecraft ().world ;
363
+ TileEntity te = world .getTileEntity (message .mirrorPos );
364
+ if (te instanceof TileEntityMagicMirrorBase ) {
365
+ MagicMirrorModifier modifier = MagicMirrorModifier .getModifier (message .modifierName );
366
+ if (modifier == null ) {
367
+ ModMagicMirror .logger .error ("Received a request to add modifier \" {}\" which does not exist." , message .modifierName );
368
+ return null ;
369
+ }
370
+ attachModifier (world , message .mirrorPos , message .usedItemStack , modifier );
371
+ }
372
+ return null ;
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Handler for messages describing modifiers being attached to mirrors (server side).
378
+ */
379
+ @ SideOnly (Side .SERVER )
380
+ public static class MessageHandlerAttachModifierServer implements IMessageHandler <MessageAttachModifier , IMessage > {
381
+ @ Nullable
382
+ @ Override
383
+ public IMessage onMessage (MessageAttachModifier message , MessageContext ctx ) {
384
+ return null ;
385
+ }
386
+ }
259
387
}
0 commit comments