Jump to content

Problem with Capabilities [1.15.2]


moyas1009

Recommended Posts

Hi, don't know what I'm doing wrong I'm trying to attach a capability to a player 

IChakra

package com.moyas1009.mnarutomod.capabilities;

public interface IChakra
{
    public void consume(float points);
    public void fill(float points);
    public void set(float points);
    public float getChakra();
}

Chakra

package com.moyas1009.mnarutomod.capabilities;

public class Chakra implements IChakra
{
    private float chakra = 250F;
    @Override
    public void consume(float points)
    {
        this.chakra -= points;
        if (this.chakra < 0.0f)
        {
            this.chakra = 0.0f;
        }
    }

    @Override
    public void fill(float points)
    {
        this.chakra += points;
    }

    @Override
    public void set(float points)
    {
        this.chakra = points;
    }

    @Override
    public float getChakra() {
        return chakra;
    }
}

ChakraCapability

package com.moyas1009.mnarutomod.capabilities;

import net.minecraft.nbt.FloatNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class ChakraCapability implements ICapabilitySerializable<FloatNBT>
{
    @CapabilityInject(IChakra.class)
    public static final Capability<Chakra> CHAKRA_CAPABILITY = null;
    private LazyOptional<Chakra> instance = LazyOptional.of(CHAKRA_CAPABILITY::getDefaultInstance);

    public static void register()
    {
        CapabilityManager.INSTANCE.register(IChakra.class, new ChakraStorage(), Chakra::new);
    }

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

    @Override
    public FloatNBT serializeNBT()
    {
        return (FloatNBT) CHAKRA_CAPABILITY.getStorage().writeNBT(CHAKRA_CAPABILITY, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty")), null);
    }

    @Override
    public void deserializeNBT(FloatNBT nbt)
    {
        CHAKRA_CAPABILITY.getStorage().readNBT(CHAKRA_CAPABILITY, instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty")), null, nbt);
    }
}

ChakraStorage

package com.moyas1009.mnarutomod.capabilities;

import net.minecraft.command.arguments.NBTCompoundTagArgument;
import net.minecraft.command.arguments.NBTTagArgument;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.FloatNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;

import javax.annotation.Nullable;

public class ChakraStorage implements Capability.IStorage<IChakra>
{

    @Nullable
    @Override
    public INBT writeNBT(Capability<IChakra> capability, IChakra instance, Direction side) {
        return FloatNBT.valueOf(instance.getChakra());
    }

    @Override
    public void readNBT(Capability<IChakra> capability, IChakra instance, Direction side, INBT nbt)
    {
        if (!(instance instanceof Chakra))
            throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
        instance.set(((FloatNBT)nbt).getFloat());
    }
}

CapabilityHandler

@Mod.EventBusSubscriber(modid = NarutoMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class CapabilityHandler {
    public static final ResourceLocation PLAYER_CHAKRA = new ResourceLocation(NarutoMod.MOD_ID, "chakra");

    @SubscribeEvent
    public void attachCapability(AttachCapabilitiesEvent<Entity> event)
    {
        if (event.getObject() instanceof PlayerEntity)
            event.addCapability(PLAYER_CHAKRA, new ChakraCapability());

        Logger logger = LogManager.getLogger();
        logger.log(Level.DEBUG, "It fires");
    }
}

My problem is whenever I create a world to test there doesn't seem to be a capability attatched to my player, I check the nbt tags with the /data command and with an nbt editor and the capability doesn't show up

 

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.