Jump to content

[SOLVED] Some questions about capabilities


IamMaxim

Recommended Posts

How should I send data from server to client on player login? I am trying to do this:

public void onEntityLoad(AttachCapabilitiesEvent.Entity event) {
        if (event.getEntity() instanceof EntityPlayer) {
            event.addCapability(new ResourceLocation(TESItems.attributesTagName), new PlayerAttributesCapabilityProvider());
            IPlayerAttributesCapability cap = event.getEntity().getCapability(TESItems.attributesCapability, null);
            networkWrapper.sendTo(new AttributesMessage(cap.getAttributes()), (EntityPlayerMP) event.getEntity());
            System.out.println("onEntityLoad(" + event.getEntity().getDisplayName() + ")");
        }

, but I get cap == null. Where should I send my packet? Capability works fine on server side, I already used it.

Link to comment
Share on other sites

Capabilities are only attached and read from NBT after

AttachCapabilityEvent

has been fired. You should send the packet from

EntityJoinWorldEvent

.

 

To make the capability persist through respawning, subscribe to

PlayerEvent.Clone

and copy the capability's data from the old player to the new one.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

No, it's still null. Here's my code:

@SubscribeEvent
    public void onEntityJoinWorld(EntityJoinWorldEvent event) {
        IPlayerAttributesCapability cap = event.getEntity().getCapability(TESItems.attributesCapability, null);
        if (cap == null) System.out.println("cap == null");
        networkWrapper.sendTo(new AttributesMessage(cap.getAttributes()), (EntityPlayerMP) event.getEntity());
    }

Link to comment
Share on other sites

EntityJoinWorldEvent

is fired for all entities, not just players. Check if the entity is a player before trying to retrieve the capability.

 

If it still doesn't work, post your code.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

There's another problem now. Minecraft#thePlayer here is null:

@Override
    public IMessage onMessage(AttributesMessage message, MessageContext ctx) {
        System.out.println("got message");
        EntityPlayer player = Minecraft.getMinecraft().thePlayer;
        if (player == null) System.out.println("player == null");
        IPlayerAttributesCapability cap = player.getCapability(TESItems.attributesCapability, null);

        for (String s : TESItems.ATTRIBUTES) {
            System.out.println("Adding " + s + " to player");
            cap.setAttribute(s, message.getAttribute(s));
        }

        return null;
    }

Link to comment
Share on other sites

Packets are handled on a separate thread in 1.8+, so you need to schedule a task on the main thread before you can safely interact with game objects. This page explains more.

 

If scheduling a task on the main thread doesn't fix this, post your new code.

 

The client player is created and spawned when the client receives

SPacketJoinGame

from the server. The server sends this before spawning the player, so it should usually be received before a packet sent from

EntityJoinWorldEvent

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.