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

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

 

  • Author
3 hours ago, diesieben07 said:
  • You are using the wrong event bus for AttachCapabilitiesEvent.
  • @EventBusSubscriber only works for static event handler methods.

So do I need to subscribe it in my main class or what ?

  • Author
On 2/6/2021 at 5:28 PM, diesieben07 said:

Neither of the two things I said suggest that in the slightest.

So what should i do?

 

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.