Jump to content

[1.8] [Solved] NullPointerException network problem


dark2222

Recommended Posts

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

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.