Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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)

  • Author

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.

  • 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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.