Jump to content

[1.12.2] Player Capability null pointer exception


SaltStation

Recommended Posts

Hello everyone.

The problem I have is that every time I try accessing my capability I get a null pointer exception.

Do I have to send packets to the client at the start of the game to prevent this?

Because I thought you only need packets to update information.

 

Default Implementation:

Spoiler


public class Fatigue implements IFatigue {

    private double fatigue = 0;
    private double limit = 20;
    private double regen = 0;

    @Override
    public void setFatigue(double points) {
        this.fatigue=points;

    }

    public void setLimit(double points) {
        this.limit=points;

    }

    public void setRegen(double points) {
        this.regen=points;

    }

    @Override
    public void add(double points) {
        this.fatigue+=points;

    }

    @Override
    public void remove(double points) {
        this.fatigue-=points;

    }

    @Override
    public double getFatigue() {
        return this.fatigue ;
    }

    @Override
    public double getLimit() {
        return this.limit ;
    }

    @Override
    public double getRegen() {
        return this.regen ;
    }
}

 

Storage:

Spoiler


public class FatigueStorage implements Capability.IStorage<IFatigue> {

    @Override
    public NBTBase writeNBT(Capability<IFatigue> capability, IFatigue instance, EnumFacing side) {

        NBTTagCompound tag = new NBTTagCompound();
        tag.setDouble("fatigue", instance.getFatigue());
        tag.setDouble("fatigue_limit", instance.getLimit());
        tag.setDouble("cosmo_regen", instance.getRegen());

        System.out.println("written to NBT(Fatigue): "+instance.getFatigue());

        return tag;
    }

    @Override
    public void readNBT(Capability<IFatigue> capability, IFatigue instance, EnumFacing side, NBTBase nbt) {

        NBTTagCompound tag = (NBTTagCompound) nbt;
        instance.setFatigue(tag.getDouble("fatigue"));
        instance.setLimit(tag.getDouble("fatigue_limit"));
        instance.setRegen(tag.getDouble("fatigue_regen"));

        System.out.println("read from NBT(Fatigue): "+instance.getFatigue());

    }
}

 

Provider:

Spoiler


public class FatigueProvider implements ICapabilitySerializable<NBTBase> {
    @CapabilityInject(IFatigue.class)
    public static final Capability<IFatigue> FATIGUE_CAPABILITY = null;

    private IFatigue instance = FATIGUE_CAPABILITY.getDefaultInstance();

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing){
        return capability == FATIGUE_CAPABILITY;
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing)
    {
        return capability == FATIGUE_CAPABILITY ? FATIGUE_CAPABILITY.<T> cast(this.instance) : null;
    }

    @Override
    public NBTBase serializeNBT(){
        return FATIGUE_CAPABILITY.getStorage().writeNBT(FATIGUE_CAPABILITY, this.instance, null);
    }

    @Override
    public void deserializeNBT(NBTBase nbt) {
        FATIGUE_CAPABILITY.getStorage().readNBT(FATIGUE_CAPABILITY, this.instance,null,nbt);
    }

}

 

Capability Handler:

Spoiler

public class CapabilityHandeler {
    public static final ResourceLocation FATIGUE_CAPABILITY = new ResourceLocation(Reference.MOD_ID,"fatigue");

    @SubscribeEvent
    public void attachCapability(AttachCapabilitiesEvent<Entity> event){
        if (!(event.getObject() instanceof EntityPlayer)) return;
        event.addCapability(FATIGUE_CAPABILITY, new FatigueProvider());
    }
}

 

 

Other Handler:

Spoiler

public class FatiguePlayerHandeler {

    @SubscribeEvent
    public void onPlayerSleep(PlayerSleepInBedEvent event)
    {
        EntityPlayer player = event.getEntityPlayer();
        IFatigue fatigue = player.getCapability(FatigueProvider.FATIGUE_CAPABILITY, null);

        System.out.println(fatigue.getLimit());
        System.out.println(fatigue.getFatigue());
        System.out.println(fatigue.getFatigue());


    }
    @SubscribeEvent
    public void onPlayerClone(PlayerEvent.Clone event){
        EntityPlayer player = event.getEntityPlayer();
        IFatigue fatigue = player.getCapability(FatigueProvider.FATIGUE_CAPABILITY, null);
        IFatigue oldFatigue = event.getOriginal().getCapability(FatigueProvider.FATIGUE_CAPABILITY, null);

        fatigue.setFatigue(oldFatigue.getFatigue());
        fatigue.setLimit(oldFatigue.getLimit());
        fatigue.setRegen(oldFatigue.getRegen());
    }
}

 

Common Proxy:

Spoiler

public class CommonProxy {

    public void preInit() {
        CapabilityManager.INSTANCE.register(IFatigue.class,new FatigueStorage(),Fatigue.class);
        MinecraftForge.EVENT_BUS.register(new CapabilityHandeler());
    }

    public void init() {}

    public void postInit() {}


    public void registerItemRenderer(Item item, int meta, String id) {
    }

}

 

 

 

 

 

 

 

 

 

Edited by SaltStation
typo
Link to comment
Share on other sites

47 minutes ago, SaltStation said:

Common Proxy:

 

Don't use a CommonProxy, it makes no sense. Proxies exist to separate sided only code. If your code is common it goes anywhere else but your proxy. This might be the cause of your issue, but

 

51 minutes ago, SaltStation said:

CapabilityManager.INSTANCE.register(IFatigue.class,new FatigueStorage(),Fatigue.class);

Don't use this overload, it's deprecated. Use the one that takes in a Callable as it's last parameter. I think that this is the case of your issue since you don't provide a public parameterless constructor in your implementation.

 

53 minutes ago, SaltStation said:

public void registerItemRenderer(Item item, int meta, String id) {

Don't use IHasModel?

  • Thanks 1
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.