Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

 

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

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.