Posted March 22, 201510 yr I've made a test package to learn IMessage and IMessage handler and it works fine if i play singleplayer and multiplayer but when I try to connect with a second player LAN or server the second player will get a NullPointerException when i try to find the player Here's the package and handler public class TestPacket implements IMessage{ private int test; public TestPacket() {} public TestPacket(int test) { this.test = test; } @Override public void toBytes(ByteBuf buf) { buf.writeInt(test); } @Override public void fromBytes(ByteBuf buf) { test = buf.readInt(); } public static class Handler implements IMessageHandler<TestPacket,IMessage>{ @Override public IMessage onMessage(TestPacket message, MessageContext ctx) { LogHelper.info(message.test); if(ctx.side.isClient()){ EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().theWorld.getEntityByID(message.test); player.addChatComponentMessage(new ChatComponentText("Awesome!")); } return null; } } } how i make the network channel @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { ConfigurationHandler.init(event.getSuggestedConfigurationFile()); FMLCommonHandler.instance().bus().register(new ConfigurationHandler()); MinecraftForge.EVENT_BUS.register(new RegisterEvent()); Network = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.NETWORKCHANNEL); Network.registerMessage(TestPacket.Handler.class,TestPacket.class, 0,Side.CLIENT); } The package gets send on PlayerLoggedInEvent @SubscribeEvent public void onEntityJoinWorld(PlayerEvent.PlayerLoggedInEvent event) { BloodCore.get(event.player).SyncWithServer(); } What it calls in my IEEP public void SyncWithServer(){ VampireMain.Network.sendTo(new TestPacket(player.getEntityId()),(EntityPlayerMP) player); } Here's the Log from the second client https://gist.github.com/anonymous/674535f7e4ca01055389
March 22, 201510 yr Author As of 1.8 packets are handled asynchronously. That means: Not on the main minecraft thread. If you want to interact with the world or players or anything like that from a packet handler you need to pass a Runnable that does the actual work to IThreadListener#addScheduledTask. The IThreadListener implementation is Minecraft.getMinecraft() on the client and the world on the server. thanks it works now but would that not mean that the first player also should return NullPointerException? also where can I find some doc on the IThreadListener? can't hurt to read up on it
March 22, 201510 yr Author Which "first player"? I don't think there's any docs on this, because not many people even realize it. The NullPointerException where only happening for the second client connected to the server.
March 23, 201510 yr FYI some links which talk a bit more about this http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html plus some example network code in this project https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe60_network_messages/Notes.txt -TGG
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.