Skip to content

Commit ee9159d

Browse files
committed
Finally got around to making networking working.
1 parent 4a71502 commit ee9159d

File tree

7 files changed

+522
-73
lines changed

7 files changed

+522
-73
lines changed

build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ dependencies {
3131
}
3232

3333
processResources {
34-
// this will ensure that this task is redone when the versions change.
34+
// This will ensure that this task is redone when the versions change.
3535
inputs.property "version", project.version
3636
inputs.property "mc_version", project.minecraft.version
3737

38-
// replace stuff in mcmod.info, nothing else
38+
// Replace stuff in mcmod.info, nothing else
3939
from(sourceSets.main.resources.srcDirs) {
4040
include 'mcmod.info'
4141

42-
// replace version and mcversion
42+
// Replace version and mcversion
4343
expand 'version': project.version, 'mc_version': project.minecraft.version
4444
}
4545

46-
// copy everything else, thats not the mcmod.info
46+
// Copy everything else that's not the mcmod.info
4747
from(sourceSets.main.resources.srcDirs) {
4848
exclude 'mcmod.info'
4949
}

src/main/java/com/tomboshoven/minecraft/magicmirror/ModMagicMirror.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorCore;
77
import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorPart;
88
import com.tomboshoven.minecraft.magicmirror.commands.Commands;
9-
import com.tomboshoven.minecraft.magicmirror.commands.MagicMirrorCommand;
109
import com.tomboshoven.minecraft.magicmirror.items.Items;
10+
import com.tomboshoven.minecraft.magicmirror.packets.Network;
1111
import com.tomboshoven.minecraft.magicmirror.renderers.Renderers;
1212
import mcp.MethodsReturnNonnullByDefault;
1313
import net.minecraft.util.ResourceLocation;
14-
import net.minecraftforge.client.ClientCommandHandler;
1514
import net.minecraftforge.common.MinecraftForge;
1615
import net.minecraftforge.fml.common.Mod;
1716
import net.minecraftforge.fml.common.Mod.EventHandler;
@@ -26,7 +25,6 @@
2625
@Mod(modid = ModMagicMirror.MOD_ID, name = ModMagicMirror.NAME, useMetadata = true)
2726
public class ModMagicMirror {
2827
public static final String MOD_ID = "magic_mirror";
29-
@SuppressWarnings("WeakerAccess")
3028
static final String NAME = "Magic Mirror";
3129

3230
@SuppressWarnings("PublicField")
@@ -40,6 +38,9 @@ public void init(FMLPreInitializationEvent event) {
4038
MinecraftForge.EVENT_BUS.register(Items.class);
4139
MinecraftForge.EVENT_BUS.register(Renderers.class);
4240

41+
// Register packets
42+
Network.registerMessages();
43+
4344
// Register tile entities
4445
GameRegistry.registerTileEntity(TileEntityMagicMirrorCore.class, new ResourceLocation(MOD_ID, "magic_mirror_core"));
4546
GameRegistry.registerTileEntity(TileEntityMagicMirrorPart.class, new ResourceLocation(MOD_ID, "magic_mirror"));

src/main/java/com/tomboshoven/minecraft/magicmirror/blocks/BlockMagicMirror.java

+144-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.tomboshoven.minecraft.magicmirror.blocks;
22

3+
import com.tomboshoven.minecraft.magicmirror.ModMagicMirror;
34
import com.tomboshoven.minecraft.magicmirror.blocks.modifiers.MagicMirrorModifier;
45
import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorBase;
56
import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorCore;
67
import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorPart;
8+
import com.tomboshoven.minecraft.magicmirror.packets.Network;
9+
import io.netty.buffer.ByteBuf;
710
import mcp.MethodsReturnNonnullByDefault;
811
import net.minecraft.block.Block;
912
import net.minecraft.block.BlockHorizontal;
@@ -15,6 +18,8 @@
1518
import net.minecraft.block.state.BlockFaceShape;
1619
import net.minecraft.block.state.BlockStateContainer;
1720
import net.minecraft.block.state.IBlockState;
21+
import net.minecraft.client.Minecraft;
22+
import net.minecraft.client.multiplayer.WorldClient;
1823
import net.minecraft.entity.EntityLivingBase;
1924
import net.minecraft.entity.player.EntityPlayer;
2025
import net.minecraft.init.SoundEvents;
@@ -30,6 +35,13 @@
3035
import net.minecraft.util.math.BlockPos;
3136
import net.minecraft.world.IBlockAccess;
3237
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;
3345

3446
import javax.annotation.Nullable;
3547
import javax.annotation.ParametersAreNonnullByDefault;
@@ -62,6 +74,16 @@ public class BlockMagicMirror extends BlockHorizontal {
6274
new AxisAlignedBB(0, 0, 0, 0.125, 1, 1),
6375
};
6476

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+
6587
/**
6688
* Create a new Magic Mirror block.
6789
* This is typically not necessary. Use Blocks.blockMagicMirror instead.
@@ -80,6 +102,24 @@ public class BlockMagicMirror extends BlockHorizontal {
80102
setSoundType(SoundType.GLASS);
81103
}
82104

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+
83123
@Override
84124
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
85125
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
@@ -190,27 +230,32 @@ public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing
190230

191231
@Override
192232
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+
}
201245
}
202-
}
203-
}
204246

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+
}
210254
}
255+
return true;
211256
}
212257

213-
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
258+
return false;
214259
}
215260

216261
@Override
@@ -256,4 +301,87 @@ int getValue() {
256301
return value;
257302
}
258303
}
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+
}
259387
}

0 commit comments

Comments
 (0)