Jump to content

[SOLVED 1.12.2] Server to Client Sync Problem


PJack

Recommended Posts

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
Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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