Jump to content

[1.14.4] NullPointer When Loading Capability Data


unassigned

Recommended Posts

Hello,

 

I've recently started modding again so I'm still fairly new to the 1.13 changes.

As capabilities have changed somewhat, I'm getting stuck trying to read/write the cap data when the player first joins: I get a NullPointer when I first login after the capability gets attached. Meaning, the first login works, but any following attempts fail will crash:

net.minecraft.crash.ReportedException: Loading entity NBT
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:154) ~[?:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:882) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:800) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:646) [?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_231] {}
Caused by: java.lang.NullPointerException
	at net.minecraftforge.fml.network.PacketDistributor.lambda$playerConsumer$1(PacketDistributor.java:216) ~[?:?] {re:classloading}
	at net.minecraftforge.fml.network.PacketDistributor$PacketTarget.send(PacketDistributor.java:178) ~[?:?] {re:classloading}
	at net.minecraftforge.fml.network.simple.SimpleChannel.send(SimpleChannel.java:118) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.impl.PlayerVoid.setVoidStored(PlayerVoid.java:61) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.PlayerVoidStorage.readNBT(PlayerVoidStorage.java:37) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.PlayerVoidStorage.readNBT(PlayerVoidStorage.java:10) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.PlayerVoidProvider.deserializeNBT(PlayerVoidProvider.java:39) ~[?:?] {re:classloading,pl:capability_inject_definalize:A}
	at com.unassigned.voidmagic.common.capability.playervoid.PlayerVoidProvider.deserializeNBT(PlayerVoidProvider.java:15) ~[?:?] {re:classloading,pl:capability_inject_definalize:A}
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.deserializeNBT(CapabilityDispatcher.java:139) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.deserializeCaps(CapabilityProvider.java:96) ~[?:?] {re:classloading}
	at net.minecraft.entity.Entity.read(Entity.java:1656) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.management.PlayerList.readPlayerDataFromFile(PlayerList.java:269) ~[?:?] {re:classloading}
	at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:112) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:119) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:63) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:241) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:148) ~[?:?] {re:classloading}
	... 5 more

 

I should also add that it does not crash when I send the packet through other means. I do have some idea to what is causing this crash, however: the packet to update the server w/ the client is being sent while the client is not actually connected yet. I found this result through checking if the client is connected before syncing the data, however this leads to the values not being synced once the NBT is set. I just have no idea on how to fix this problem.

 

Here is the code that is within the stacktrace, and here is my github if you need to see anything else.

 

PlayerVoidStorage (Capability Storage Class):

Spoiler

package com.unassigned.voidmagic.common.capability.playervoid;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;

import javax.annotation.Nullable;

public class PlayerVoidStorage implements Capability.IStorage<IPlayerVoid> {
    @Nullable
    @Override
    public INBT writeNBT(Capability<IPlayerVoid> capability, IPlayerVoid instance, Direction side) {
        CompoundNBT compound = new CompoundNBT();
        if(capability != null && instance != null) {
            if(instance.getAttachedPlayer() != null) {
                compound.putInt("VoidStored", instance.getVoidStored());

                CompoundNBT skillCompound = new CompoundNBT();
                for(IVoidSkill skill : instance.getVoidSkills()) {
                    if(skill != null){ //fallback check
                        skillCompound.putInt("Skill#"+skill.skillID(), skill.skillID());
                    }
                }
                compound.put("VoidSkills", skillCompound);
            }
        }
        return compound;
    }

    @Override
    public void readNBT(Capability<IPlayerVoid> capability, IPlayerVoid instance, Direction side, INBT nbt) {
        if(capability != null && instance != null && nbt != null) {
            if(nbt instanceof CompoundNBT) {
                CompoundNBT compound = (CompoundNBT)nbt;
                if(compound.contains("VoidStored")) {
                    instance.setVoidStored(compound.getInt("VoidStored"));
                }
                if(compound.contains("VoidSkills")){
                    // -- TODO -- \\
                }
            }
        }
    }
}

 

 

PlayerVoidProvider (Capability Provider Class):

Spoiler

package com.unassigned.voidmagic.common.capability.playervoid;

import com.unassigned.voidmagic.common.capability.playervoid.impl.PlayerVoid;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
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;

public class PlayerVoidProvider implements ICapabilitySerializable<CompoundNBT> {

    @CapabilityInject(IPlayerVoid.class)
    public static final Capability<IPlayerVoid> CAPABILITY_PLAYER_VOID = null;

    private final LazyOptional<IPlayerVoid> instance;

    public PlayerVoidProvider(PlayerEntity player) { instance = LazyOptional.of(() -> new PlayerVoid(player)); }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        if(cap != CAPABILITY_PLAYER_VOID) return LazyOptional.empty();
        return this.instance.cast();
    }

    @Override
    public CompoundNBT serializeNBT() {
        return (CompoundNBT)(CAPABILITY_PLAYER_VOID.getStorage()
                .writeNBT(CAPABILITY_PLAYER_VOID,
                        instance.orElseThrow(()->new IllegalArgumentException("Invalid LazyOptional!")),
                        null));
    }

    @Override
    public void deserializeNBT(CompoundNBT nbt) {
        CAPABILITY_PLAYER_VOID.getStorage().readNBT(CAPABILITY_PLAYER_VOID,
                instance.orElseThrow(()->new IllegalArgumentException("Invalid LazyOptional!")),
                null, nbt);
    }

    public static IPlayerVoid getPlayerVoid(PlayerEntity playerEntity) {
        return playerEntity.getCapability(CAPABILITY_PLAYER_VOID, null).orElseThrow(() -> new IllegalArgumentException("Invalid Target!"));
    }
}

 

 

PlayerVoid (impl of IPlayerVoid):

Spoiler

package com.unassigned.voidmagic.common.capability.playervoid.impl;

import com.unassigned.voidmagic.VoidMagic;
import com.unassigned.voidmagic.common.capability.playervoid.IVoidSkill;
import com.unassigned.voidmagic.common.capability.playervoid.IPlayerVoid;
import com.unassigned.voidmagic.network.messages.MessagePlayerVoid;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.fml.network.PacketDistributor;

import javax.annotation.Nonnull;
import java.util.ArrayList;

public class PlayerVoid implements IPlayerVoid {

    protected final PlayerEntity player;

    protected ArrayList<IVoidSkill> voidSkills;
    protected int voidStored;

    public PlayerVoid(PlayerEntity player) {
        this.player = player;
        this.voidStored = 0;
        this.voidSkills = new ArrayList<>();
    }

    @Override
    public PlayerEntity getAttachedPlayer() {
        return this.player;
    }

    @Override
    public ArrayList<IVoidSkill> getVoidSkills() {
        return this.voidSkills;
    }

    @Override
    public void setVoidSkills(ArrayList<IVoidSkill> set) {
        this.voidSkills = set;
    }

    @Override
    public void addSkill(@Nonnull IVoidSkill skill) {
        if(!this.voidSkills.contains(skill)) this.voidSkills.add(skill);
    }

    @Override
    public void removeSkill(IVoidSkill skill) {
        this.voidSkills.remove(skill);
    }

    @Override
    public int getVoidStored() {
        return this.voidStored;
    }

    @Override
    public void setVoidStored(int set) {
        this.voidStored = set;
        if(player != null && !player.world.isRemote)
            VoidMagic.network.send(PacketDistributor.PLAYER.with(()-> (ServerPlayerEntity) player), new MessagePlayerVoid(this.voidStored));
    }

    @Override
    public void addVoid(int toAdd) {
        this.voidStored += toAdd;
        if(player != null && !player.world.isRemote)
            VoidMagic.network.send(PacketDistributor.PLAYER.with(()-> (ServerPlayerEntity) player), new MessagePlayerVoid(this.voidStored));
    }

    @Override
    public void removeVoid(int removeVoid) {
        if(this.voidStored - removeVoid < 0){ this.voidStored = 0; }
            else { this.voidStored -= removeVoid; }

        if(player != null && !player.world.isRemote)
            VoidMagic.network.send(PacketDistributor.PLAYER.with(()-> (ServerPlayerEntity) player), new MessagePlayerVoid(this.voidStored));
    }

    @Override
    public int getMaxVoidStored() {
        return 100000;
    }

}

 

 

Link to comment
Share on other sites

You should have a reference to your actual capability and only use/provide your LazyOptional for external operations.

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

55 minutes ago, Cadiboo said:

You should have a reference to your actual capability and only use/provide your LazyOptional for external operations.

Yeah, I just needed some clarification on the new LazyOptional system, as I'm still not too sure what/when to use them. So doing something like this:

                e.getCapability(PlayerVoidProvider.CAPABILITY_PLAYER_VOID).ifPresent(pv -> pv.setVoidStored(message.playerVoid));

isn't correct? Or am I mistaking what you are meaning.

 

Link to comment
Share on other sites

If you know that the object exists and have access to it, you shouldn’t be using an optional. An Optional should be exposed to the rest of the world with getCapability BUT you know that your capability will never be null so you should use a direct reference in your internal code.

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

17 hours ago, Cadiboo said:

If you know that the object exists and have access to it, you shouldn’t be using an optional. An Optional should be exposed to the rest of the world with getCapability BUT you know that your capability will never be null so you should use a direct reference in your internal code.

Ok. I think I see what you are meaning, however I still don't exactly see where I'm doing anything wrong. I know when I get the capability to send the packet that the cap isn't ever null, so direct reference is fine in this case I believe.

Link to comment
Share on other sites

I believe I went through and changed what needed to be changed, but I still get the same error. I did quite the rewrite to get everything in order again, and just for optimization purposes. Here is the stacktrace again:

Spoiler

[19:43:13] [Server thread/ERROR] [minecraft/MinecraftServer]: Encountered an unexpected exception
net.minecraft.crash.ReportedException: Loading entity NBT
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:154) ~[?:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:882) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:800) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:646) [?:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_231] {}
Caused by: java.lang.NullPointerException
	at net.minecraftforge.fml.network.PacketDistributor.lambda$playerConsumer$1(PacketDistributor.java:216) ~[?:?] {re:classloading}
	at net.minecraftforge.fml.network.PacketDistributor$PacketTarget.send(PacketDistributor.java:178) ~[?:?] {re:classloading}
	at net.minecraftforge.fml.network.simple.SimpleChannel.send(SimpleChannel.java:118) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.impl.PlayerVoid.onVoidChanged(PlayerVoid.java:86) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.impl.PlayerVoid.setVoidStored(PlayerVoid.java:60) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.CapabilityPlayerVoid$1.readNBT(CapabilityPlayerVoid.java:50) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.playervoid.CapabilityPlayerVoid$1.readNBT(CapabilityPlayerVoid.java:23) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.Capability.readNBT(Capability.java:104) ~[?:?] {re:classloading}
	at com.unassigned.voidmagic.common.capability.CapabilityProviderSerializable.deserializeNBT(CapabilityProviderSerializable.java:65) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityDispatcher.deserializeNBT(CapabilityDispatcher.java:139) ~[?:?] {re:classloading}
	at net.minecraftforge.common.capabilities.CapabilityProvider.deserializeCaps(CapabilityProvider.java:96) ~[?:?] {re:classloading}
	at net.minecraft.entity.Entity.read(Entity.java:1656) ~[?:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.management.PlayerList.readPlayerDataFromFile(PlayerList.java:269) ~[?:?] {re:classloading}
	at net.minecraft.server.management.PlayerList.initializeConnectionToPlayer(PlayerList.java:112) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tryAcceptPlayer(ServerLoginNetHandler.java:119) ~[?:?] {re:classloading}
	at net.minecraft.network.login.ServerLoginNetHandler.tick(ServerLoginNetHandler.java:63) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:241) ~[?:?] {re:classloading}
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:148) ~[?:?] {re:classloading}
	... 5 more

Note that this crash happens around just before the player finishes connecting after leaving with nbt data saved.

 

Here are some direct links to the proper github files, as those will house the lines where everything is happening.

CapabilityPlayerVoid

PlayerVoid

MessagePlayerVoid

 

Thank you both for your help so far.

Link to comment
Share on other sites

17 hours ago, diesieben07 said:

You are trying to send a packet while being read from NBT. That does not work, the player doesn't exist fully yet, so you can't send a packet to it.

Ah gotcha. I did the following, which is quite hacky but seems to work: I added a parameter to the setVoidStored to send the packet or not, then send the packet when the entity joins the world.

 

new readNBT within the cap:

                            instance.setVoidStored(compound.getInt("VoidStored"), false);

 

new event to send the packet after the player is done connecting:

    @SubscribeEvent
    public void onJoinWorld(EntityJoinWorldEvent event) {
        Entity e = event.getEntity();
        if(e != null) {
            if(e instanceof PlayerEntity) {
                CapabilityPlayerVoid.getPlayerVoid((PlayerEntity)e).ifPresent(IPlayerVoid::onVoidChanged);
            }
        }
    }

 

Let me know if there is a simpler way of doing it. Other than that thanks for your help!

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.