Jump to content

[SOLVED] [1.15.2] Capability#getDefaultInstance is returning null


Nozzomi

Recommended Posts

It's as the title says; I'm having trouble with my capabilities :( Here are the classes that are relevant:

 

Main class

Spoiler

import com.github.soravoid.exceed.capabilities.ChargesCap;
import com.github.soravoid.exceed.capabilities.SmithQualityCap;
import com.github.soravoid.exceed.init.ExceedBlocks;
import com.github.soravoid.exceed.init.ExceedItemGroups;
import com.github.soravoid.exceed.init.ExceedItems;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(Exceed.MODID)
public class Exceed
{
    public static final String MODID = "exceed";
    public static final Logger LOG = LogManager.getLogger(MODID);

    public static final ItemGroup EXCEED_TAB = new ExceedItemGroups.EXCEED_GROUP(MODID, () -> new ItemStack(ExceedItems.TEST_ITEM.get()));

    public Exceed()
    {
        LOG.info("\"Hello World\" -Exceed");
        final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        ExceedItems.ITEMS.register(modEventBus);
        ExceedBlocks.BLOCKS.register(modEventBus);
        modEventBus.addListener(this::onCommonSetup);
    }

    private void onCommonSetup(FMLCommonSetupEvent e)
    {
        CapabilityManager.INSTANCE.register(SmithQualityCap.class, new SmithQualityCap(), SmithQualityCap::new);
        CapabilityManager.INSTANCE.register(ChargesCap.class, new ChargesCap(), ChargesCap::new);
    }
}
 

Capability class

Spoiler

package com.github.soravoid.exceed.capabilities;

import com.github.soravoid.exceed.util.SmithQuality;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.IntNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Random;

public class SmithQualityCap implements Capability.IStorage<SmithQualityCap>, ICapabilitySerializable<IntNBT>
{
    @CapabilityInject(SmithQualityCap.class)
    public static final Capability<SmithQualityCap> SMITH_QUALITY_CAP = null;

    private LazyOptional<SmithQualityCap> instance = LazyOptional.of(SMITH_QUALITY_CAP::getDefaultInstance);

    private SmithQuality quality;

    //Random Quality
    public SmithQualityCap()
    {
        //TODO Weighted Randomness
        SmithQuality[] vals = SmithQuality.values();
        this.setQuality(vals[new Random().nextInt(vals.length)]);
    }

    public SmithQualityCap(SmithQuality quality) { this.setQuality(quality); }

    public SmithQuality getQuality() { return quality; }

    public void setQuality(SmithQuality quality) { this.quality = quality; }

    @Nullable
    @Override
    public INBT writeNBT(Capability<SmithQualityCap> capability, SmithQualityCap instance, Direction side) { return IntNBT.valueOf(instance.getQuality().ordinal()); }

    @Override
    public void readNBT(Capability<SmithQualityCap> capability, SmithQualityCap instance, Direction side, INBT nbt) { instance.setQuality(SmithQuality.values()[((IntNBT) nbt).getInt()]); }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { return SMITH_QUALITY_CAP.orEmpty(cap, instance); }

    @Override
    public IntNBT serializeNBT() { return (IntNBT) SMITH_QUALITY_CAP.getStorage().writeNBT(SMITH_QUALITY_CAP, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); }

    @Override
    public void deserializeNBT(IntNBT nbt) { SMITH_QUALITY_CAP.getStorage().readNBT(SMITH_QUALITY_CAP, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); }
}
 

log:

Spoiler
[06Feb2020 16:03:23.418] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmluserdevclient, --fml.mcpVersion, 20200122.131323, --fml.mcVersion, 1.15.2, --fml.forgeGroup, net.minecraftforge, --fml.forgeVersion, 31.0.13, --version, MOD_DEV, --assetIndex, 1.15, --assetsDir, C:\Users\jnola\.gradle\caches\forge_gradle\assets, --username, Dev, --accessToken, ❄❄❄❄❄❄❄❄, --userProperties, {}]
[06Feb2020 16:03:23.421] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 5.0.0-milestone.4+67+b1a340b starting: java version 1.8.0_242 by AdoptOpenJDK
[06Feb2020 16:03:23.623] [main/INFO] [net.minecraftforge.fml.loading.FixSSL/CORE]: Added Lets Encrypt root certificates as additional trust
[06Feb2020 16:03:24.506] [main/INFO] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Launching target 'fmluserdevclient' with arguments [--version, MOD_DEV, --gameDir, ., --assetsDir, C:\Users\jnola\.gradle\caches\forge_gradle\assets, --assetIndex, 1.15, --username, Dev, --accessToken, ❄❄❄❄❄❄❄❄, --userProperties, {}]
[06Feb2020 16:03:26.626] [Render thread/INFO] [net.minecraft.client.Minecraft/]: Setting user: Dev
[06Feb2020 16:03:36.896] [Render thread/INFO] [net.minecraft.client.Minecraft/]: Backend library: LWJGL version 3.2.2 build 10
[06Feb2020 16:03:38.361] [modloading-worker-3/INFO] [exceed/]: "Hello World" -Exceed
[06Feb2020 16:03:38.364] [modloading-worker-1/INFO] [net.minecraftforge.common.ForgeMod/FORGEMOD]: Forge mod loading, version 31.0.13, for MC 1.15.2 with MCP 20200122.131323
[06Feb2020 16:03:38.364] [modloading-worker-1/INFO] [net.minecraftforge.common.MinecraftForge/FORGE]: MinecraftForge v31.0.13 Initialized
[06Feb2020 16:03:41.751] [Render thread/INFO] [com.mojang.text2speech.NarratorWindows/]: Narrator library for x64 successfully loaded
[06Feb2020 16:03:41.832] [Render thread/INFO] [net.minecraft.resources.SimpleReloadableResourceManager/]: Reloading ResourceManager: Default, Mod Resources
[06Feb2020 16:03:41.860] [modloading-worker-4/ERROR] [net.minecraftforge.fml.javafmlmod.FMLModContainer/]: Exception caught during firing event: null
    Index: 1
    Listeners:
        0: NORMAL
        1: net.minecraftforge.eventbus.EventBus$$Lambda$2095/597049482@c1bc99
java.lang.NullPointerException
    at com.github.soravoid.exceed.capabilities.SmithQualityCap.<init>(SmithQualityCap.java:21)
    at com.github.soravoid.exceed.Exceed.onCommonSetup(Exceed.java:37)
    at net.minecraftforge.eventbus.EventBus.doCastFilter(EventBus.java:212)
    at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:204)
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258)
    at net.minecraftforge.fml.javafmlmod.FMLModContainer.fireEvent(FMLModContainer.java:106)
    at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65)
    at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65)
    at net.minecraftforge.fml.ModContainer.transitionState(ModContainer.java:112)
    at net.minecraftforge.fml.ModList.lambda$null$10(ModList.java:134)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
    at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
[06Feb2020 16:03:41.861] [modloading-worker-4/ERROR] [net.minecraftforge.fml.javafmlmod.FMLModContainer/LOADING]: Caught exception during event FMLCommonSetupEvent dispatch for modid exceed
java.lang.NullPointerException: null
    at com.github.soravoid.exceed.capabilities.SmithQualityCap.<init>(SmithQualityCap.java:21) ~[classes/:?]
    at com.github.soravoid.exceed.Exceed.onCommonSetup(Exceed.java:37) ~[classes/:?]
    at net.minecraftforge.eventbus.EventBus.doCastFilter(EventBus.java:212) ~[eventbus-2.0.0-milestone.1-service.jar:?]
    at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:204) ~[eventbus-2.0.0-milestone.1-service.jar:?]
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258) ~[eventbus-2.0.0-milestone.1-service.jar:?]
    at net.minecraftforge.fml.javafmlmod.FMLModContainer.fireEvent(FMLModContainer.java:106) ~[?:31.0]
    at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65) ~[?:1.8.0_242]
    at java.util.function.Consumer.lambda$andThen$0(Consumer.java:65) ~[?:1.8.0_242]
    at net.minecraftforge.fml.ModContainer.transitionState(ModContainer.java:112) ~[?:?]
    at net.minecraftforge.fml.ModList.lambda$null$10(ModList.java:134) ~[?:?]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) [?:1.8.0_242]
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) [?:1.8.0_242]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) [?:1.8.0_242]
    at java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290) [?:1.8.0_242]
    at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) [?:1.8.0_242]
[06Feb2020 16:03:42.003] [Server-Worker-3/FATAL] [net.minecraftforge.fml.ModLoader/LOADING]: Failed to complete lifecycle event SETUP, 1 errors found
[06Feb2020 16:03:42.003] [Server-Worker-3/FATAL] [net.minecraftforge.eventbus.EventBus/EVENTBUS]: EventBus 0 shutting down - future events will not be posted.
java.lang.Exception: stacktrace
    at net.minecraftforge.eventbus.EventBus.shutdown(EventBus.java:278) ~[eventbus-2.0.0-milestone.1-service.jar:?]
    at net.minecraftforge.fml.client.ClientModLoader.lambda$createRunnableWithCatch$5(ClientModLoader.java:115) ~[?:?]
    at net.minecraftforge.fml.client.ClientModLoader.startModLoading(ClientModLoader.java:123) ~[?:?]
    at net.minecraftforge.fml.client.ClientModLoader.lambda$onreload$3(ClientModLoader.java:105) ~[?:?]
    at net.minecraftforge.fml.client.ClientModLoader.lambda$createRunnableWithCatch$5(ClientModLoader.java:113) ~[?:?]
    at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1640) [?:1.8.0_242]
    at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1632) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) [?:1.8.0_242]
[06Feb2020 16:03:42.006] [Forge Version Check/INFO] [net.minecraftforge.fml.VersionChecker/]: [forge] Starting version check at https://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json
[06Feb2020 16:03:42.492] [Forge Version Check/INFO] [net.minecraftforge.fml.VersionChecker/]: [forge] Found status: OUTDATED Current: 31.0.13 Target: 31.1.0
[06Feb2020 16:03:45.175] [Server-Worker-8/ERROR] [net.minecraft.client.renderer.texture.AtlasTexture/]: Using missing texture, unable to load exceed:textures/item/static_sword_2.png : java.io.FileNotFoundException: exceed:textures/item/static_sword_2.png
[06Feb2020 16:03:45.387] [Server-Worker-6/ERROR] [net.minecraft.client.renderer.texture.AtlasTexture/]: Unable to parse metadata from exceed:textures/item/static_sword_3.png
java.lang.RuntimeException: invalid frameindex 9
    at net.minecraft.client.renderer.texture.TextureAtlasSprite.<init>(TextureAtlasSprite.java:60) ~[?:?]
    at net.minecraft.client.renderer.texture.AtlasTexture.loadSprite(AtlasTexture.java:206) ~[?:?]
    at net.minecraft.client.renderer.texture.AtlasTexture.lambda$null$3(AtlasTexture.java:187) ~[?:?]
    at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1640) [?:1.8.0_242]
    at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1632) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) [?:1.8.0_242]
[06Feb2020 16:03:45.451] [Server-Worker-1/ERROR] [net.minecraftforge.fml.ModLoader/LOADING]: Skipping lifecycle event ENQUEUE_IMC, 1 errors found.
[06Feb2020 16:03:45.452] [Server-Worker-1/FATAL] [net.minecraftforge.fml.ModLoader/LOADING]: Failed to complete lifecycle event ENQUEUE_IMC, 1 errors found
[06Feb2020 16:03:45.452] [Server-Worker-1/FATAL] [net.minecraftforge.eventbus.EventBus/EVENTBUS]: EventBus 0 shutting down - future events will not be posted.
java.lang.Exception: stacktrace
    at net.minecraftforge.eventbus.EventBus.shutdown(EventBus.java:278) ~[eventbus-2.0.0-milestone.1-service.jar:?]
    at net.minecraftforge.fml.client.ClientModLoader.lambda$createRunnableWithCatch$5(ClientModLoader.java:115) ~[?:?]
    at net.minecraftforge.fml.client.ClientModLoader.finishModLoading(ClientModLoader.java:137) ~[?:?]
    at net.minecraftforge.fml.client.ClientModLoader.lambda$onreload$4(ClientModLoader.java:107) ~[?:?]
    at java.util.concurrent.CompletableFuture.uniRun(CompletableFuture.java:719) [?:1.8.0_242]
    at java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:701) [?:1.8.0_242]
    at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:457) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) [?:1.8.0_242]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) [?:1.8.0_242]
[06Feb2020 16:03:45.880] [Render thread/INFO] [net.minecraft.client.audio.SoundSystem/]: OpenAL initialized.
[06Feb2020 16:03:45.881] [Render thread/INFO] [net.minecraft.client.audio.SoundEngine/SOUNDS]: Sound engine started
[06Feb2020 16:03:45.995] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 1024x512x4 minecraft:textures/atlas/blocks.png-atlas
[06Feb2020 16:03:46.058] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 128x128x4 minecraft:textures/atlas/signs.png-atlas
[06Feb2020 16:03:46.058] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 512x512x4 minecraft:textures/atlas/banner_patterns.png-atlas
[06Feb2020 16:03:46.059] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 512x512x4 minecraft:textures/atlas/shield_patterns.png-atlas
[06Feb2020 16:03:46.060] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 256x256x4 minecraft:textures/atlas/chest.png-atlas
[06Feb2020 16:03:46.060] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 512x256x4 minecraft:textures/atlas/beds.png-atlas
[06Feb2020 16:03:46.060] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 512x256x4 minecraft:textures/atlas/shulker_boxes.png-atlas
[06Feb2020 16:03:46.531] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 256x256x0 minecraft:textures/atlas/particles.png-atlas
[06Feb2020 16:03:46.532] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 256x256x0 minecraft:textures/atlas/paintings.png-atlas
[06Feb2020 16:03:46.532] [Render thread/INFO] [net.minecraft.client.renderer.texture.AtlasTexture/]: Created: 128x128x0 minecraft:textures/atlas/mob_effects.png-atlas
 

 

Edited by Nozzomi
Link to comment
Share on other sites

Can you please edit your post and click this button next time:

image.png.01ebae90f47173d47dda1095445578f8.png

 

Because without doing that your spoiler blocks are 4000 pixels tall and completely useless.

 

image.png.c50c182260becc11e7fd164cb381d4bd.png

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

18 hours ago, Draco18s said:

Can you please edit your post and click this button next time:

image.png.01ebae90f47173d47dda1095445578f8.png

 

Because without doing that your spoiler blocks are 4000 pixels tall and completely useless.

 

image.png.c50c182260becc11e7fd164cb381d4bd.png

Ahh sorry! I put a temporary fix on my phone, but I'll try re-copying-and-pasting once I get to my computer! Hopefully this is enough for now

Link to comment
Share on other sites

5 hours ago, diesieben07 said:

At this point your capabilities are not registered yet (you are in the process of doing so). Your SmithQualityCap constructor (which you call here) tries to access your @CapabilityInject field though, which cannot possibly be initialized here, because your capability is not registered yet.

You should not implement IStorage on the same class as your capability.

Thank you! That solved it

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.