Jump to content

Recommended Posts

Posted (edited)

When I asked console:

System.out.println(player.getCapability(MagicCapability.MAGIC).orElse(null) == null);

"true"

 

What did I do or not do, that made this capability not loaded?

Please point it out. I have no clue.

 

public static void setup (final FMLCommonSetupEvent event){
        MinecraftForge.EVENT_BUS.register(new Fool());
        MagicCapability.register();
    }

 

public class MagicCapability
{
    @CapabilityInject(IMaxMP.class)
    public static Capability<IMaxMP> MAGIC = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(IMaxMP.class, new IStorage<IMaxMP>()
        {
            @Override
            public INBT writeNBT(Capability<IMaxMP> capability, IMaxMP instance, Direction side)
            {
                return IntNBT.valueOf(instance.getManaStored());
            }

            @Override
            public void readNBT(Capability<IMaxMP> capability, IMaxMP instance, Direction side, INBT nbt)
            {
                ((MaxMP)instance).addMana(((IntNBT)nbt).getInt());
            }
        },
        () -> new MaxMP(null));
    }
}
public interface IMaxMP
{
	public int getManaStored();
	public void addMana(int amount);
	public void setMana(int amount);
	public boolean useMana(int amount);
	public int getMaxMana();
	public void addMaxMana(int amount);
	public void setMaxMana(int amount);
}
public class Fool {

	 @SubscribeEvent
	 public void attatchCapability(AttachCapabilitiesEvent <Entity> event)
	 {
		 ServerPlayerEntity player;
		 if (event.getObject() instanceof ServerPlayerEntity) {
			 player = (ServerPlayerEntity) event.getObject();
		 } else {
			 return;
		 }
		 LazyOptional<IMaxMP> optPlayer = player.getCapability(MagicCape.MAGIC);
		 if (!optPlayer.isPresent()) {
			 event.addCapability(new ResourceLocation(Chants.MODID, "magic_capability"), new Maou(player));
		 }
	 }
}
public class Maou implements ICapabilityProvider, ICapabilitySerializable<CompoundNBT>
{
	IMaxMP storage;

	public Maou (ServerPlayerEntity serverPlayerEntity) {
		storage = new MaxMP(serverPlayerEntity);
	}
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
		if (cap == MagicCape.MAGIC)
			return (LazyOptional<T>) LazyOptional.of(() -> storage);
		return LazyOptional.empty();
	}

	@Override
	public CompoundNBT serializeNBT() {
		CompoundNBT ret = new CompoundNBT();
		ret.putInt("magicStored", storage.getManaStored());
		return ret;
	}

	@Override
	public void deserializeNBT(CompoundNBT nbt) {
		int magic = nbt.getInt("magicStored");
		storage.addMana(magic);
	}
}

 

Edited by e2rifia
Posted
31 minutes ago, diesieben07 said:
  • More context is needed. Where is this code located?

@Mod(Main.MODID)
@Mod.EventBusSubscriber(modid = Main.MODID)
public class Main {
    public static final String MODID = "main";

@SubscribeEvent
    public static void onServerStarting(FMLServerStartingEvent event) {
        CommandDispatcher<CommandSource> d = event.getServer().getCommandManager().getDispatcher();

d.register(Commands.literal("tellme").requires(source -> {
            try {
                return source.asPlayer() != null;
            } catch(CommandSyntaxException e) {
                return false;
            }
        }).executes(command -> {
            ServerPlayerEntity player = command.getSource().asPlayer();
            System.out.println(player.getCapability(MagicCape.MAGIC).orElse(null) == null);
            return 1;
        }));

42 minutes ago, diesieben07 said:
  • Thinking is not helpful here. Verify using the debugger.

I see that @SubscribeEvent setup() is not called at all.

Posted (edited)
@Mod(Main.MODID)
@Mod.EventBusSubscriber(modid = Main.MODID)
public class Main{
    public static final String MODID = "main";

@SubscribeEvent 
    public static void setup(final FMLCommonSetupEvent event) {
        System.out.println("I'm setting up!");
        MinecraftForge.EVENT_BUS.register(new CapabilityAttatcher());
        MagicCapability.register();
    }

}

 

Edited by e2rifia
Posted

 

5 minutes ago, diesieben07 said:

@EventBusSubscriber subscribes to the forge bus by default, but FMLCommonSetupEvent is fired on the mod bus.

I should separate @Mod and @EventBusSubscriber?

Posted (edited)
9 minutes ago, diesieben07 said:

You don't need to, no. Why?

Is @EventBusSubscriber what's getting in the way of setup?

It didn't work...

Edited by e2rifia
Posted
40 minutes ago, diesieben07 said:

@EventBusSubscriber subscribes to the forge bus by default, but FMLCommonSetupEvent is fired on the mod bus.

@Mod(Main.MODID)
//@Mod.EventBusSubscriber(modid = Main.MODID)
public class Main{
    public static final String MODID = "main";

@SubscribeEvent 
    public static void setup(final FMLCommonSetupEvent event) {
        System.out.println("I'm setting up!");
        MinecraftForge.EVENT_BUS.register(new CapabilityAttatcher());
        MagicCapability.register();
    }

}

 

Posted (edited)

I could be wrong, but I think you might need to add this code in the constructor:

FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);

And you may need to change the setup method to non-static.

Edited by Linky132
  • Like 1

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.