Jump to content

[1.18.2] Error when attaching capabilities


Squander

Recommended Posts

Attempts to attach capabilities to the player but receives this error.

[10:27:23] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: Cannot invoke "net.minecraftforge.common.util.LazyOptional.cast()" because "this.playerJoinedHandler" is null
	Index: 1
	Listeners:
		0: NORMAL
		1: ASM: class me.squander.backpack.event.AttachCapabilities onAttachCapabilities(Lnet/minecraftforge/event/AttachCapabilitiesEvent;)V
java.lang.NullPointerException: Cannot invoke "net.minecraftforge.common.util.LazyOptional.cast()" because "this.playerJoinedHandler" is null
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.entity.player.Player.getCapability(Player.java:2228)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.common.capabilities.ICapabilityProvider.getCapability(ICapabilityProvider.java:34)
	at TRANSFORMER/backpackmod@0.0NONE/me.squander.backpack.event.AttachCapabilities.onAttachCapabilities(AttachCapabilities.java:19)
	at net.minecraftforge.eventbus.ASMEventHandler_1_AttachCapabilities_onAttachCapabilities_AttachCapabilitiesEvent.invoke(.dynamic)
	at MC-BOOTSTRAP/eventbus@5.0.7/net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
	at MC-BOOTSTRAP/eventbus@5.0.7/net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
	at MC-BOOTSTRAP/eventbus@5.0.7/net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:593)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:587)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.common.capabilities.CapabilityProvider.doGatherCapabilities(CapabilityProvider.java:73)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:68)
	at TRANSFORMER/forge@40.1.68/net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:52)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.entity.Entity.<init>(Entity.java:257)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.entity.LivingEntity.<init>(LivingEntity.java:234)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.entity.player.Player.<init>(Player.java:172)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.level.ServerPlayer.<init>(ServerPlayer.java:238)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.players.PlayerList.getPlayerForLogin(PlayerList.java:412)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.network.ServerLoginPacketListenerImpl.handleAcceptedLogin(ServerLoginPacketListenerImpl.java:124)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.network.ServerLoginPacketListenerImpl.tick(ServerLoginPacketListenerImpl.java:70)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.network.Connection.tick(Connection.java:267)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.network.ServerConnectionListener.tick(ServerConnectionListener.java:160)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:947)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:873)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.client.server.IntegratedServer.tickServer(IntegratedServer.java:90)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:704)
	at TRANSFORMER/minecraft@1.18.2/net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:262)
	at java.base/java.lang.Thread.run(Thread.java:833)

[10:27:23] [Server thread/ERROR] [minecraft/ServerLoginPacketListenerImpl]: Couldn't place player in world
java.lang.NullPointerException: Cannot invoke "net.minecraftforge.common.util.LazyOptional.cast()" because "this.playerJoinedHandler" is null
	at net.minecraft.world.entity.player.Player.getCapability(Player.java:2228) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraftforge.common.capabilities.ICapabilityProvider.getCapability(ICapabilityProvider.java:34) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at me.squander.backpack.event.AttachCapabilities.onAttachCapabilities(AttachCapabilities.java:19) ~[%2384!/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_1_AttachCapabilities_onAttachCapabilities_AttachCapabilitiesEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-5.0.7.jar%2310!/:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-5.0.7.jar%2310!/:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-5.0.7.jar%2310!/:?] {}
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:593) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:587) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.doGatherCapabilities(CapabilityProvider.java:73) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:68) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.gatherCapabilities(CapabilityProvider.java:52) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2378%2385!/:?] {re:classloading}
	at net.minecraft.world.entity.Entity.<init>(Entity.java:257) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.world.entity.LivingEntity.<init>(LivingEntity.java:234) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading}
	at net.minecraft.world.entity.player.Player.<init>(Player.java:172) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.level.ServerPlayer.<init>(ServerPlayer.java:238) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.players.PlayerList.getPlayerForLogin(PlayerList.java:412) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading}
	at net.minecraft.server.network.ServerLoginPacketListenerImpl.handleAcceptedLogin(ServerLoginPacketListenerImpl.java:124) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.network.ServerLoginPacketListenerImpl.tick(ServerLoginPacketListenerImpl.java:70) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.network.Connection.tick(Connection.java:267) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading}
	at net.minecraft.server.network.ServerConnectionListener.tick(ServerConnectionListener.java:160) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:947) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:873) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.server.IntegratedServer.tickServer(IntegratedServer.java:90) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:704) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:262) ~[forge-1.18.2-40.1.68_mapped_parchment_2022.07.17-1.18.2-recomp.jar%2379!/:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:833) [?:?] {}
[10:27:23] [Server thread/INFO] [minecraft/ServerLoginPacketListenerImpl]: com.mojang.authlib.GameProfile@5e18a6a7[id=380df991-f603-344c-a090-369bad2a924a,name=Dev,properties={},legacy=false] (local:E:d56832b6) lost connection: Invalid player data
[10:27:23] [Server thread/WARN] [minecraft/Connection]: handleDisconnection() called twice
@SubscribeEvent
    public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event){
        if(event.getObject() instanceof Player){
            if(!event.getObject().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent()){
                event.addCapability(new ResourceLocation(BackpackMod.MODID, "extra_storage"), new ExtraStorageInventory(9));
            }
        }
    }

 

Link to comment
Share on other sites

public class ExtraStorageInventory extends ItemStackHandler implements ICapabilityProvider {
    private final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> this);

    public ExtraStorageInventory(int size){
        this.setSize(size);
    }

    @NotNull
    @Override
    public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? optional.cast() : LazyOptional.empty();
    }
}

 

Link to comment
Share on other sites

I did a new capability but still have items from the hotbar.

public static final Capability<IItemHandler> EXTRA_STORAGE = CapabilityManager.get(new CapabilityToken<>(){});
    private final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> this);

    public ExtraStorageInventory(int size){
        this.setSize(size);
    }

    @NotNull
    @Override
    public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        return cap == EXTRA_STORAGE ? optional.cast() : LazyOptional.empty();
    }
player.getCapability(ExtraStorageInventory.EXTRA_STORAGE).ifPresent(handler -> {
                    NetworkHooks.openGui((ServerPlayer) player, new SimpleMenuProvider((id, inv, plr) ->
                            new ExtraStorageMenu(id, inv, handler), new TextComponent("EXTRA STORAGE")), buffer -> buffer.writeVarInt(9));
                });

 

Link to comment
Share on other sites

public ExtraStorageMenu(int pContainerId, Inventory inventory, FriendlyByteBuf buffer) {
        this(pContainerId, inventory, new ItemStackHandler(buffer.readVarInt()));
    }

    public ExtraStorageMenu(int pContainerId, Inventory inventory, IItemHandler handler) {
        super(MenuTypeInit.EXTRA_STORAGE.get(), pContainerId);

        for(int i = 0; i < 9; ++i) {
            this.addSlot(new SlotItemHandler(handler, i, 1 + i * 18, 1));
        }

        this.addPlayerInventory(inventory);
        this.addPlayerHotbar(inventory);
    }

    private void addPlayerInventory(Inventory inventory){
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 9; ++j) {
                this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }
    }

    private void addPlayerHotbar(Inventory inventory){
        for(int k = 0; k < 9; ++k) {
            this.addSlot(new Slot(inventory, k, 8 + k * 18, 142));
        }
    }

 

Link to comment
Share on other sites

You should get the IItemHandler on server and client from the Player whihc you can get from the given Inventory.

22 minutes ago, Squander said:

I did a new capability but still have items from the hotbar.

You have in the Slots of your IItemHandler the Items from the Hotbar right?

Please post a git repo of your Mod.

Link to comment
Share on other sites

Can't finish GitHub sharing process
            Successfully created project 'Backpack1.18.2' on GitHub, but initial commit failed:
            gpg: skipped "Squander12 <devcodedred@gmail.com>": No secret key
            gpg: signing failed: No secret key
            gpg failed to sign the data
            failed to write commit object

I get this error all the time.

Link to comment
Share on other sites

As far as i can find out this has something to do with commit signing.
You can disable this via the following command or your edit the config file of your git repo:
command:

git config commit.gpgsign false

config file (git repo/.git/config), Note the .git folder is hidden by default.

[commit]
  gpgsign = false

Source: https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

Link to comment
Share on other sites

My config in my mod project

 

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = git@github.com:/Squander12/Backpack1.18.2.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "github"]
    url = git@github.com:/Squander12/Backpack_1.18.2.git
    fetch = +refs/heads/*:refs/remotes/github/*
[lfs]
    repositoryformatversion = 0
[remote "githubh"]
    url = git@github.com:/Squander12/Backpack_1.18.2.git
    fetch = +refs/heads/*:refs/remotes/githubh/*
[remote "githubasd"]
    url = git@github.com:/Squander12/Backpack1.18.2123.git
    fetch = +refs/heads/*:refs/remotes/githubasd/*
[remote "originn"]
    url = git@github.com:/Squander12/Backpack-1.18.2.git
    fetch = +refs/heads/*:refs/remotes/originn/*
[remote "githubheunyrty"]
    url = git@github.com:/Squander12/aaaa.git
    fetch = +refs/heads/*:refs/remotes/githubheunyrty/*
[commit]
    gpgsign = false

Link to comment
Share on other sites

28 minutes ago, Squander said:

okay i run github desktop then file tab then add local repository i gave the path of my mod, all files are like loaded but i don't really know what to do now.

You need to create a public repo not a local, if you done this you need to publish the repo (Note make sure you uncheck "keep this repo private").
The publish it and post the like here.

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.