Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • (1.16.2) Making a new capability (3)
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 2
e2rifia

(1.16.2) Making a new capability (3)

By e2rifia, January 17 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17 (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 January 17 by e2rifia
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    118

poopoodice

poopoodice    118

  • Dragon Slayer
  • poopoodice
  • Members
  • 118
  • 928 posts
Posted January 17

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

  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17
17 minutes ago, poopoodice said:

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

I think so.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56414 posts
Posted January 17
  • Do not create a new LazyOptional every time. This defeats the purpose of LazyOptional.
  • 1 hour ago, e2rifia said:

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

    "true"

    More context is needed. Where is this code located?

  • 36 minutes ago, e2rifia said:

    I think so.

    Thinking is not helpful here. Verify using the debugger.

  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17
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.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56414 posts
Posted January 17
1 minute ago, e2rifia said:

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

How did you register it to the event bus?

  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17 (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 January 17 by e2rifia
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56414 posts
Posted January 17

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

 

 

Please use the code format when posting code.

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17

 

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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56414 posts
Posted January 17
1 minute ago, e2rifia said:

I should separate @Mod and @EventBusSubscriber?

You don't need to, no. Why?

  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17 (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 January 17 by e2rifia
  • Quote

Share this post


Link to post
Share on other sites

loordgek    176

loordgek

loordgek    176

  • World Shaper
  • loordgek
  • Members
  • 176
  • 1826 posts
Posted January 17
24 minutes ago, e2rifia said:

It didn't work...

what dint work, show your new code

  • Quote

Share this post


Link to post
Share on other sites

e2rifia    0

e2rifia

e2rifia    0

  • Tree Puncher
  • e2rifia
  • Members
  • 0
  • 23 posts
Posted January 17
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();
    }

}

 

  • Quote

Share this post


Link to post
Share on other sites

Linky132    1

Linky132

Linky132    1

  • Tree Puncher
  • Linky132
  • Members
  • 1
  • 33 posts
Posted January 17 (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 January 17 by Linky132
  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7697

diesieben07

diesieben07    7697

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7697
  • 56414 posts
Posted January 17
4 hours ago, e2rifia said:

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

}

 

So now you just removed the @EventBusSubscriber annotation.

I did not tell you to do this.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 2
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • troublemaker_47
      Teleport player in same direction as where he looks

      By troublemaker_47 · Posted 4 minutes ago

      Could you rewrite the function please because i am fairly new and i really wanted to add this functionality to my mod
    • Extrupt
      Forge won't fully download

      By Extrupt · Posted 7 minutes ago

      i tried to download forge but every time i got the same message after a few seconds of downloading Failed to run processor: java.io.FileNotFoundException:C:\Users\mande\AppData\Roaming\.minecraft\libraries\de\oceanlabs\mcp\mcp_config\1.16.5-20210115.111550\mcp_config-1.16.5-20210115.111550-mappings.txt (Access is denied) See log for more details. i tried to look up what i could do but no one else had the problem. Could someone please help me with this. thanks-  
    • GenElectrovise
      When I try to run my mod after I just added Deferred Registries it crashes

      By GenElectrovise · Posted 9 minutes ago

      All of it. If you don't have a repo, please make one. Everything will be easier.
    • SwiftNinjaPro
      Bug report: files.minecraftforge.net ssl

      By SwiftNinjaPro · Posted 10 minutes ago

      If I go to files.minecraftforge.net the http request does not redirect to https. This can effect security. The non ssl request does not redirect to ssl. Just letting you know that http:// does not redirect to https:// on files.minecraftforge.net   Just figured you should know. Thanks.
    • ThisIsNotOriginal
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal · Posted 13 minutes ago

      all of it? as in my registries, my main file and is there anything else you need to see?
  • Topics

    • troublemaker_47
      6
      Teleport player in same direction as where he looks

      By troublemaker_47
      Started 1 hour ago

    • Extrupt
      0
      Forge won't fully download

      By Extrupt
      Started 7 minutes ago

    • ThisIsNotOriginal
      14
      When I try to run my mod after I just added Deferred Registries it crashes

      By ThisIsNotOriginal
      Started 15 hours ago

    • SwiftNinjaPro
      0
      Bug report: files.minecraftforge.net ssl

      By SwiftNinjaPro
      Started 10 minutes ago

    • Retsal
      6
      [1.16.x] Custom block as a shop

      By Retsal
      Started Yesterday at 06:04 AM

  • Who's Online (See full list)

    • RaySanketsu
    • J_F1Nl3Y_
    • GenElectrovise
    • 3.1459
    • diesieben07
    • Extrupt
    • ThisIsNotOriginal
    • arna
    • WindRunner7
    • troublemaker_47
    • CAS_ual_TY
    • Tessa
    • SwiftNinjaPro
    • thepumpkinbg
    • llllllll
    • brok4d
    • Spacerulerwill
    • Draco18s
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • (1.16.2) Making a new capability (3)
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community