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

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

  • Author
17 minutes ago, poopoodice said:

Is the event called? Have you attach the capability to the players?

I think so.

  • Author
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.

  • Author
@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

  • Author

 

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?

  • Author
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

  • Author
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();
    }

}

 

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

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.