Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5]Capability cannot be synchronized to client by sending packet on PlayerEvent.Clone


LYXHAHA
 Share

Recommended Posts

public static void onPlayerCloned(PlayerEvent.Clone event) {
        if (!event.getOriginal().world.isRemote() && !event.getPlayer().world.isRemote() && event.getOriginal() != null && event.getPlayer() != null) {
            LazyOptional<IMindCapability> oldMindCap = event.getOriginal().getCapability(LibCapabilities.MIND_CAPABILITY);
            LazyOptional<IMindCapability> newMindCap = event.getPlayer().getCapability(LibCapabilities.MIND_CAPABILITY);
            if (oldMindCap.isPresent() && newMindCap.isPresent()) {
                newMindCap.ifPresent(capability -> {
                    oldMindCap.ifPresent(capabilityOld -> {
                        event.getPlayer().sendMessage(new StringTextComponent("Before: " + capability.getMindStrength()), CommonProxy.IN_GAME_UUID);
                        capability.deserializeNBT(capabilityOld.serializeNBT());
                        event.getPlayer().sendMessage(new StringTextComponent("After: " + capability.getMindStrength()), CommonProxy.IN_GAME_UUID);
                    });
                  // This don't work, the value on the client side is still the initial value
                    SCPacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) event.getPlayer()), new MSGMindCapabilitySync(capability.getMindStrength()));
                  // It works,the value on the client side is correct, but I don't know why, I think it's just a coincidence.
//                    SCPacketHandler.INSTANCE.sendToServer(new MSGMindCapabilitySync(capability.getMindStrength()));
                });
            }
        }
    }

 

This is my message code.

public class MSGMindCapabilitySync {
    private final int mindStrength;

    public MSGMindCapabilitySync(int mindStrength) {
        this.mindStrength = mindStrength;

    }

    public static void encodingMSG(MSGMindCapabilitySync msg, PacketBuffer buffer) {
        buffer.writeInt(msg.mindStrength);
    }

    public static MSGMindCapabilitySync decodingMSG(PacketBuffer buffer) {
        return new MSGMindCapabilitySync(buffer.readInt());
    }

    public static void handle(MSGMindCapabilitySync msg, Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            // Form client to server
            ServerPlayerEntity serverPlayer = ctx.get().getSender();
            if (serverPlayer != null) {
                serverPlayer.getCapability(LibCapabilities.MIND_CAPABILITY).ifPresent((capability -> {
                    capability.setMindStrength(msg.mindStrength);
                }));
                SaintChapter.LOGGER.info("Form client to server. Done");
            }
            // From server to client
            DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
                if (Minecraft.getInstance().player == null) return;
                Minecraft.getInstance().player.getCapability(LibCapabilities.MIND_CAPABILITY).ifPresent((capability -> {
                    capability.setMindStrength(msg.mindStrength);
                }));
                SaintChapter.LOGGER.info("From server to client. Done");
            });
        });
        ctx.get().setPacketHandled(true);
    }
}
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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hi, I would like to know if it's possible to render the glowing effect with depth test so it won't be visible through walls? Should I implement my own render type buffer? Thanks!
    • I think that this is easier. Thank you. I do know how to do that.
    • Thank you for your help. I think I'm on the correct page. https://mcforge.readthedocs.io/en/latest/models/color/ I only need to find out how this works.
    • Hi, I did not know where exactly to put this, so I just put it in off topic. I was under an hour ago banned from the Forge discord, the reason was "You were banned from The Forge Project | Sending Friend Requests to random users while under watch" When the user I was sending the message to was no a random person, I had a very specific reason to try to get to them, and in addition, there is no rule saying I could not send friend requests, and if there was a rule I would like to see where it is because I looked in the Code of Conduct and it was not there. And if it was there, an infraction system is used, and at the very worse it would be considered "spammy*, as it was not illegal or against TOS, and even spammy would be kind of extreme as all I did was send a friend request to someone who was the host of an even I was interested in. I was not given an amount of time which the ban would last so I guess it would be Perma, even though what I did was either within the rules, or a minor infraction. If there are some rules I do not know about, please let me know about them because I have not seen these other rules. Also, I do not understand why I am on watch, the only other rule break I had was from a few months ago for talking about old stuff, and I am generally not all that active in the Forge discord so do not see where this watch would have come from. In addition, the rules never say anything about watches and that other rules would apply. The only real conflict I had recently was a mistake in MMD  (which even then was unjust), but that is MMD thus it is separate and there was nothing that is in the rules which would say that what happens there puts anyone in any situation, and I do not know much other conflict I was in recently, especially which would have caused this. If my action were so severe I do not know why I was not  notified about them earlier, but I did not perform any bad actions, so please look into this and explain the steps I need to take to be unbanned or have my punishment reduced and please clarify some of this stuff about me being under watch as I really do not know.   My Discord is Asbestosstar#2338
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.