Posted February 6, 20214 yr 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
February 6, 20214 yr 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 ?
February 8, 20214 yr 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.