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

Hello guys,

 

i have a problem with my network that send Data from Server to Client.

 

This is my Event, where is sending the Packet:

	 @SubscribeEvent
	 public void onPlayerTick(PlayerTickEvent event) 
	 {
		if(event.side == Side.SERVER) 
		{
			EntityPlayer player = event.player;
			ICharacterData cd = player.getCapability(CD_CAPABILITY, null);
			
			if(cd != null)
			{
				PacketHandler.INSTANCE.sendTo(new PlayerCapabilitiesMessage(cd), (EntityPlayerMP) player);				
			}		
		}
	}

 

And this is my PlayerCapabilitiesMessage:

public class PlayerCapabilitiesMessage implements IMessage {

	@CapabilityInject(ICharacterData.class)
	public static Capability<ICharacterData> CD_CAPABILITY = null;	
	
	private static ICharacterData value;

	public PlayerCapabilitiesMessage() {}
	
	public PlayerCapabilitiesMessage(ICharacterData value) {
		this.value = value;
	}	

	@Override
	public void toBytes(ByteBuf buf) 
	{
		if(this.value != null) {
			buf.writeInt(this.value.GetReiPlayer());
			buf.writeInt(this.value.GetReiPlayerMax());
			buf.writeInt(this.value.PlayerisGhost(0));
		}
	}	
	
	@Override
	public void fromBytes(ByteBuf buf) 
	{
		if(this.value != null) {
			this.value.SetReiPlayer(buf.readInt());
			this.value.SetReiPlayerMax(buf.readInt());
			this.value.PlayerisGhost(buf.readInt());
		}
	}
	
	public ICharacterData getValue(ICharacterData value) {
		return this.value;
	}	
	
	public static class Handler implements IMessageHandler<PlayerCapabilitiesMessage, IMessage>{

		@Override
		public IMessage onMessage(PlayerCapabilitiesMessage message, MessageContext ctx) 
		{
			final ICharacterData newvalue = message.getValue(value);
			final EntityPlayerSP p = Minecraft.getMinecraft().player;
			final ICharacterData oldvalue = p.getCapability(CD_CAPABILITY, null);
			IThreadListener mainThread = Minecraft.getMinecraft();
			
			mainThread.addScheduledTask(() -> {
				
				if(oldvalue != null && newvalue != null) 
				{
					oldvalue.SetReiPlayer(newvalue.GetReiPlayer());
					oldvalue.SetReiPlayerMax((newvalue.GetReiPlayerMax()));
					oldvalue.PlayerisGhost(newvalue.PlayerisGhost(0));
				}
				
			});
			return null;
		}
		
	}
}

 

Now my problem is:

 

The Server and the Client are sync in Singleplayer, there is no problem.

 

In Mulitplayer it isnt working, because the value is null, but sometimes it works with the first Player/Client who join the Server and the second one got null.

 

Someone any idea? Thanks in advance :)

Edited by PJack
Got solved

6 hours ago, PJack said:

private static ICharacterData value;

You can't do this in your packet. You must reconstruct the data in your handler, you can't store it like that. 

  • Author
4 hours ago, V0idWa1k3r said:

You can't do this in your packet. You must reconstruct the data in your handler, you can't store it like that.  

You mean, it should be like this?:
 

private int GetReiPlayer

private int GetReiPlayerMax

....

 

instead

 

private static ICharacterData value;

 

1 hour ago, PJack said:

You mean, it should be like this?:
 


private int GetReiPlayer

private int GetReiPlayerMax

....

 

instead

 


private static ICharacterData value;

 

The point is the values cannot be static, and should contain the data to be send across the sides each time.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

  • Author

Okay, i removed the static.

private ICharacterData value;

 

Edit:

 

I found out that my this.value getting null in here, but i still dont know why.

	@Override
	public void fromBytes(ByteBuf buf) 
	{
		System.out.println("Check:" + this.value);
		if(this.value != null) {
			value.SetReiPlayer(buf.readInt());
			value.SetReiPlayerMax(buf.readInt());
			value.PlayerisGhost(buf.readInt());
		}
	}

 

Edited by PJack

  • Author

I got the solution. Thanks to a post from diesieben07.

 

Instead of sending the capability, i send the raw data from it and it works now fine in Single and Multiplayer.

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.