Jump to content

"getCapability" calling other capability


LuccaPossamai

Recommended Posts

I created a new capability, but when I call player.getCapability it returns me the wrong capability. It returns me a capability that I created before.

This event is giving an error:

Spoiler
@SubscribeEvent
    public static void tick(TickEvent.ClientTickEvent event){
        if (Minecraft.getInstance().level != null) {
            for (Player p : Minecraft.getInstance().level.players()) {
                p.getCapability(ModCapabilities.MANA_CAPABILITY, null).ifPresent(mana -> {
                    if (mana.canRegenerate()) {
                        mana.regenMana(1);
                    }
                });
            }
        }
    }

 

And this error appears:

Spoiler
[17:37:47] [Render thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: class com.lucca.mohard.block.altar.capability.AltarCapability cannot be cast to class com.lucca.mohard.capabilities.mana.ManaCapability (com.lucca.mohard.block.altar.capability.AltarCapability and com.lucca.mohard.capabilities.mana.ManaCapability are in module mohard@0.0NONE of loader 'TRANSFORMER' @7c8d5312)
	Index: 2
	Listeners:
		0: NORMAL
		1: ASM: net.minecraftforge.common.ForgeInternalHandler@621e241a checkSettings(Lnet/minecraftforge/event/TickEvent$ClientTickEvent;)V
		2: ASM: class com.lucca.mohard.capabilities.mana.ManaCapabilitySetup tick(Lnet/minecraftforge/event/TickEvent$ClientTickEvent;)V
		3: ASM: class com.lucca.mohard.evolution.PlayerEvolution tick(Lnet/minecraftforge/event/TickEvent;)V
java.lang.ClassCastException: class com.lucca.mohard.block.altar.capability.AltarCapability cannot be cast to class com.lucca.mohard.capabilities.mana.ManaCapability (com.lucca.mohard.block.altar.capability.AltarCapability and com.lucca.mohard.capabilities.mana.ManaCapability are in module mohard@0.0NONE of loader 'TRANSFORMER' @7c8d5312)
	at TRANSFORMER/forge@38.0.14/net.minecraftforge.common.util.LazyOptional.ifPresent(LazyOptional.java:164)
	at TRANSFORMER/mohard@0.0NONE/com.lucca.mohard.capabilities.mana.ManaCapabilitySetup.tick(ManaCapabilitySetup.java:28)
	at net.minecraftforge.eventbus.ASMEventHandler_24_ManaCapabilitySetup_tick_ClientTickEvent.invoke(.dynamic)
	at MC-BOOTSTRAP/eventbus@5.0.3/net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
	at MC-BOOTSTRAP/eventbus@5.0.3/net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
	at MC-BOOTSTRAP/eventbus@5.0.3/net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
	at TRANSFORMER/forge@38.0.14/net.minecraftforge.event.ForgeEventFactory.onPreClientTick(ForgeEventFactory.java:871)
	at TRANSFORMER/minecraft@1.18/net.minecraft.client.Minecraft.tick(Minecraft.java:1602)
	at TRANSFORMER/minecraft@1.18/net.minecraft.client.Minecraft.runTick(Minecraft.java:1015)
	at TRANSFORMER/minecraft@1.18/net.minecraft.client.Minecraft.run(Minecraft.java:660)
	at TRANSFORMER/minecraft@1.18/net.minecraft.client.main.Main.main(Main.java:205)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at MC-BOOTSTRAP/fmlloader@1.18-38.0.14/net.minecraftforge.fml.loading.targets.ForgeClientUserdevLaunchHandler.lambda$launchService$0(ForgeClientUserdevLaunchHandler.java:38)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.Launcher.run(Launcher.java:106)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.Launcher.main(Launcher.java:77)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26)
	at MC-BOOTSTRAP/cpw.mods.modlauncher@9.0.7/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23)
	at cpw.mods.bootstraplauncher@0.1.17/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:90)

 

 

Link to comment
Share on other sites

Spoiler
public class ManaCapabilityProvider implements ICapabilitySerializable<CompoundTag> {

    private ManaCapability MANA_CAPABILITY;

    @Nonnull
    private ManaCapability getManaCap() {
        if (MANA_CAPABILITY == null) {
            MANA_CAPABILITY = new ManaCapability(600);
        }
        return MANA_CAPABILITY;
    }

    private final LazyOptional<ManaCapability> lazyMana = LazyOptional.of(this::getManaCap);

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        cap.
        if (cap == ModCapabilities.MANA_CAPABILITY) {
            return (LazyOptional<T>)(lazyMana);
        }
        return this.getCapability(cap, side);
    }


    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag = new CompoundTag();
        int mana = this.getManaCap().getMana();
        tag.putInt("Mana", mana);
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        this.getManaCap().setMana(nbt.contains("Mana") ? nbt.getInt("Mana") : 0);
    }

}

 

 

Link to comment
Share on other sites

16 hours ago, LuccaPossamai said:
    private ManaCapability MANA_CAPABILITY;

    @Nonnull
    private ManaCapability getManaCap() {
        if (MANA_CAPABILITY == null) {
            MANA_CAPABILITY = new ManaCapability(600);
        }
        return MANA_CAPABILITY;
    }

    private final LazyOptional<ManaCapability> lazyMana = LazyOptional.of(this::getManaCap);

try to create the Capability directly in the Field, and not via the method (it should also be final then)

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.



×
×
  • Create New...

Important Information

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