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

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)

 

 

  • Author
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);
    }

}

 

 

  • Author

I'm confused, it's right to have a provider for each capability, or only need to have one for all of them?

 

Edited by LuccaPossamai

10 hours ago, LuccaPossamai said:
        return this.getCapability(cap, side);

why? call super, and not the method again, this will throw a StackOverflowException

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)

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.