Homura Posted August 18, 2021 Posted August 18, 2021 (edited) It's the first time for me to use capability, maybe the question looks like so unreadable. In multiplayer game, the value in capability is displayed correctly on each player's own client,but when I try to get other player's value in client side, the value is not correct, is the initial value. In my client, only my player's value is correct, other player's values are wrong. In server side, the value from each player is correct. I don't know how to sync the value from other player,i want to use the value in a render event. I have already send packet in PlayerLoggedInEvent、PlayerRespawnEvent、PlayerChangedDimensionEvent event.getPlayer().getCapability(GUI_SELECT).ifPresent(t -> TackleMessage.CHANNEL.send( PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) event.getPlayer()), new GuiSelectMessage(t.getSelectParam()) ) ); please help me Edited August 18, 2021 by Homura Quote
Homura Posted August 18, 2021 Author Posted August 18, 2021 2 minutes ago, diesieben07 said: Whenever the data changes send the data to all tracking players and the player themselves. You can use PacketDistributor.TRACKING_ENTITY_AND_SELF for this, it will send packets to the player themselves as well as all players that are tracking the player (i.e. they are within visual range). In PlayerEvent.StartTracking check if event.getTarget is a player. If so, send the data of event.getTarget to event.getPlayer. I try to do like this, but when a target player changes the value, my own player's value has been changed too. It seems that it sends the value of the target to my player... I guess that the problem may be in here ... just my guess... This is a part of GuiSelectMessage.class, used to assign the received value to the value in the capability @Override public void process(Supplier<NetworkEvent.Context> context) { if (context.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) { context.get().enqueueWork(() ->Minecraft.getInstance().player.getCapability(GuiSelectCapability.GUI_SELECT).ifPresent(date -> { date.setSelectParam(selectParam); })); } } Quote
Homura Posted August 18, 2021 Author Posted August 18, 2021 20 minutes ago, diesieben07 said: To achieve this you'll have to augment your packet to include the player that the information is for. You can do this via their entity ID: Entity#getId and World#getEntity. I must get the entity ID , and assign the value to the specific client player via the entity ID ... is that right? Quote
Homura Posted August 18, 2021 Author Posted August 18, 2021 55 minutes ago, diesieben07 said: First I want to remind you that there is almost no difference between "multiplayer" and "singleplayer". If you start a single player world you are basically playing on a server, just alone. Single player uses an integrated server. Here is what you need for full syncing: In PlayerLoggedInEvent, PlayerRespawnEvent and PlayerChangedDimensionEvent send the player their own data (you already have this). Whenever the data changes send the data to all tracking players and the player themselves. You can use PacketDistributor.TRACKING_ENTITY_AND_SELF for this, it will send packets to the player themselves as well as all players that are tracking the player (i.e. they are within visual range). In PlayerEvent.StartTracking check if event.getTarget is a player. If so, send the data of event.getTarget to event.getPlayer. To achieve this you'll have to augment your packet to include the player that the information is for. You can do this via their entity ID: Entity#getId and World#getEntity. I achieved it !! I used the PlayerEvent.StartTracking event and sent the entity ID. Thank you for your help very much! Quote
Recommended Posts
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.