Jump to content

[SOLVED] [1.17.1] AttachCapabilitiesEvent firing twice.


DaqEm

Recommended Posts

Hey everyone,

I've been testing the new 1.17.1 way to create capabilities and for some reason the AttachCapabilitiesEvent is firing twice when I join a singleplayer world.

The code I used is here: GitHub - TheIllusiveC4/Curios at 726bfd9589d49174520379de8bfdb53b5208ed13 (this is not my code, I just it for testing purposes only)

@SubscribeEvent
public void onAttachEntityCaps(AttachCapabilitiesEvent<Entity> event) {
    if (event.getObject() instanceof Player) {
        event.addCapability(CuriosCapability.ID_INVENTORY,
                CurioInventoryCapability.createProvider((Player) event.getObject()));
        LOGGER.info("Attached Capability!");
    }
}

 

Also to do some more testing I've added a PlayerTickEvent (I know it's bad but its for testing xD):

@Mod.EventBusSubscriber(bus= Mod.EventBusSubscriber.Bus.MOD)
public class EventPlayerTick {

    int count = 0;

    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (count == 100) {
            event.player.getCapability(CuriosCapability.INVENTORY).ifPresent((jobHandler) -> {
                int currentValue = jobHandler.getSlots();
                JobsPlus.LOGGER.info(currentValue + " | | | " + jobHandler + " " + event.player.getScoreboardName());
                jobHandler.setSlots(currentValue + 1);
            });
            count = 0;
        }
        count += 1;
    }
}

and that outputs this:

[20:45:13] [Server thread/INFO]: 0 | | |  me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3524f28 Dev
[20:45:14] [Render thread/INFO]: 0 | | |  me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@1014c825 Dev
[20:45:15] [Server thread/INFO]: 1 | | |  me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3524f28 Dev
[20:45:16] [Server thread/INFO]: 2 | | |  me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3524f28 Dev

So this definitely are 2 capabilities attached to the Player.

 

Any help is appreciated! Thank you.

Edited by DaqEm
Topic has been solved.
Link to comment
Share on other sites

no there is only one Capability,
you don't notice that TickEvents fired twice (tick START and END),
also the TickEvents are fired on server and client

so if you don't check the phase and the side the Event is fired 4 times

  1. client START
  2. server START
  3. client END
  4. server END

also you can't use static values in Events, since they are not synced to the client and they exists for all Players (in this case)

Link to comment
Share on other sites

Okay, so completely forgot it fired client and server side. I made some adjustments to the PlayerTickEvent and found the problem.

@SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (event.side == LogicalSide.SERVER && event.phase == TickEvent.Phase.START) {
            event.player.getCapability(CuriosCapability.INVENTORY).ifPresent((jobHandler) -> {
                int currentValue = jobHandler.getSlots();
                JobsPlus.LOGGER.info(currentValue + " | | | " + jobHandler + " " + event.player.getScoreboardName() + " server");
                jobHandler.setSlots(currentValue + 1);
            });
        }
        if (event.side == LogicalSide.CLIENT && event.phase == TickEvent.Phase.START) {
            event.player.getCapability(CuriosCapability.INVENTORY).ifPresent((jobHandler) -> {
                int currentValue = jobHandler.getSlots();
                JobsPlus.LOGGER.info(currentValue + " | | | " + jobHandler + " " + event.player.getScoreboardName() + " client");
                jobHandler.setSlots(currentValue + 1);
            });
        }
    }
[21:20:45] [Server thread/INFO]: 23 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@8899b18 Dev server
[21:20:45] [Render thread/INFO]: 31 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3db84bfa Dev client
[21:20:45] [Server thread/INFO]: 24 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@8899b18 Dev server
[21:20:45] [Render thread/INFO]: 32 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3db84bfa Dev client
[21:20:45] [Server thread/INFO]: 25 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@8899b18 Dev server
[21:20:45] [Render thread/INFO]: 33 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@3db84bfa Dev client
[21:20:45] [Server thread/INFO]: 26 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@8899b18 Dev server
[21:20:45] [Server thread/INFO]: 27 | | | me.daqem.jobsplus.testcap.CurioInventoryCapability$CurioInventoryWrapper@8899b18 Dev server

Marking this as solved.

Link to comment
Share on other sites

  • DaqEm changed the title to [SOLVED] [1.17.1] AttachCapabilitiesEvent firing twice.

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.