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    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 925 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    7695

diesieben07

diesieben07    7695

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7695
  • 56361 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    7695

diesieben07

diesieben07    7695

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7695
  • 56361 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    7695

diesieben07

diesieben07    7695

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7695
  • 56361 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    7695

diesieben07

diesieben07    7695

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7695
  • 56361 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
  • 1823 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    7695

diesieben07

diesieben07    7695

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7695
  • 56361 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

    • ESCCarp
      Forge says this file does not have an app associated with it.

      By ESCCarp · Posted 27 minutes ago

      i do  
    • LexManos
      forge a jar file not exe

      By LexManos · Posted 1 hour ago

      Some zip managers like to take control of the .jar file extension away from Java. Make sure you have Java installed and try running Jarfix once, then try the installer again.   Also, 1.12 is out of support, update.
    • Modder31
      forge a jar file not exe

      By Modder31 · Posted 1 hour ago

      i am unable to install jar files only exe files i would ask if you could change all the files to exe files, like the older versions i want the most recent version of 1.12.2 for a certain mod but cant cause its a jar file
    • diesieben07
      Forge says this file does not have an app associated with it.

      By diesieben07 · Posted 1 hour ago

      Make sure you have Java installed.
    • ESCCarp
      Forge says this file does not have an app associated with it.

      By ESCCarp · Posted 1 hour ago

      When i try to install forge it says "This file does not have a app associated with it for preforming this action". Please help me fix it i really want to play with my friends on modded servers.
  • Topics

    • ESCCarp
      2
      Forge says this file does not have an app associated with it.

      By ESCCarp
      Started 1 hour ago

    • Modder31
      1
      forge a jar file not exe

      By Modder31
      Started 1 hour ago

    • JaydenB14
      1
      Minecraft Unexpected error on 1.7.10 forge modded

      By JaydenB14
      Started 3 hours ago

    • Molotove_
      1
      Problem adding RAM to a Forge Server

      By Molotove_
      Started 4 hours ago

    • Ariel David
      2
      Do i need minecraft installed

      By Ariel David
      Started 5 hours ago

  • Who's Online (See full list)

    • rjakobs
    • MrBelieve
    • samjviana
    • Kyloren_du22
    • Talp1
  • 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