Posted April 25, 20196 yr 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 April 26, 20196 yr by PJack Got solved
April 26, 20196 yr 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.
April 26, 20196 yr 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;
April 26, 20196 yr 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.
April 26, 20196 yr 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 April 26, 20196 yr by PJack
April 26, 20196 yr 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.