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.x] Unable to Set Capability
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
urbanxx001

[1.16.x] Unable to Set Capability

By urbanxx001, October 22, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 22, 2020 (edited)

Below are my capability classes for attaching to vanilla living entities (besides player). I'd like to change the entity's capability (to a value of 1.0F) when right clicking with a mod item. I thought this would be accomplished with:

target.getCapability(ModCap.MOD_CAP).ifPresent(IModCap::set);

But checking the values before and after with:

System.out.println(target.getCapability(ModCap.MOD_CAP).orElse(new ModCapMethods()).get());

Still yields 0.0F (the default value). Since it returns the default, the capability should be registered (so ifPresent should pass?), but I could be wrong. 

 

ModCap Class

ModCapStorage Class

ModCapMethods Class

IModCap Class

ModItem Class

Edited October 22, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 23, 2020
.orElse(new ModCapMethods()

This won't assign the living entity a new instance of your capability. This means 

System.out.println(target.getCapability(ModCap.MOD_CAP).orElse(new ModCapMethods()).get());

If the capability does not exist on the entity, it prints the default value (of the new instance).

By using orElseThrow you can get notified whenever you are accessing an entity that does not have the capability (which should not happen). At the moment it seems like the entities does not have your capability, is the capability correctly registered, and capability correctly attached?

  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 23, 2020 (edited)
2 hours ago, poopoodice said:

.orElse(new ModCapMethods()

This won't assign the living entity a new instance of your capability.

The orElse line isn't the one I'm using to set the capability, it's the .ifPresent(IModCap::set)

 

2 hours ago, poopoodice said:

If the capability does not exist on the entity, it prints the default value (of the new instance).

Ah ok. I checked and fixed the register method and the AttachCapabilityEvent, but still receive the default value. Should all entities automatically receive the capability through that event? If not, does it need to initially be attached to them with something like EntityJoinWorldEvent?

Edited October 23, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

loordgek    176

loordgek

loordgek    176

  • World Shaper
  • loordgek
  • Members
  • 176
  • 1826 posts
Posted October 23, 2020
2 hours ago, urbanxx001 said:

The orElse line isn't the one I'm using to set the capability, it's the .ifPresent(IModCap::set)

that is not how it works, you set nothing

 

does your event method get called?

  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 23, 2020
2 hours ago, urbanxx001 said:

The orElse line isn't the one I'm using to set the capability, it's the .ifPresent(IModCap::set)

What I was trying to say is, apparently the ifPresent() check failed, therefore you are printing a default value (got from orElse()). Here you should use orElseThrow so there will be an exception when the entity does not have the capability (to notify you that somethings wrong with your capability).

2 hours ago, urbanxx001 said:

Should all entities automatically receive the capability through that event? If not, does it need to initially be attached to them with something like EntityJoinWorldEvent?

No, the capability is added during the attaching event.

And if I understand it correctly, 

LazyOptional.of(MOD_CAP::getDefaultInstance);

this will provide an instance from the factory that you have passed in the third parameter when you register the capability, and you are currently splitting up your capability into ModCap and ModCapMethods, which for me it seems a bit redundant (a class can have multiple interface implementations), and might be the cause of the problem.

  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 23, 2020 (edited)

Apologies I understand what you mean now, orElse was giving a false positive with a temporary object that it was registered, which means I should inspect the register again. Will use

.orElseThrow(() -> new IllegalArgumentException("Illegal argument"))

instead. Thank you for your help.

Edited October 23, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 24, 2020 (edited)

After checking the capability register is in FMLCommonSetupEvent:

CapabilityManager.INSTANCE.register(IModCap.class, new ModCapStorage(), ModCapMethods::new);

And the event in:

@Mod.EventBusSubscriber(modid = Main.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModSubscribeEvents {
    @SubscribeEvent
    public void attachCapabilitiesEntity(final AttachCapabilitiesEvent<Entity> event) {
        if ((event.getObject() instanceof LivingEntity) && !(event.getObject() instanceof PlayerEntity))
            event.addCapability(new ResourceLocation(Main.MOD_ID, "mod_cap"), new ModCap());
    }
}

orElseThrow() still throws an error. I'm in the same boat as this guy, I don't think he resolved it. Out of curiosity, does anything need to be created in the data folder?

Edited October 24, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 24, 2020 (edited)

Iirc attach capability event is fired on forge bus.

Edited October 24, 2020 by poopoodice
  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 24, 2020 (edited)

Ok. Sorry for how tedious this has been. I removed the @Mod.EventBusSubscriber line and tried

MinecraftForge.EVENT_BUS.register(ModSubscribeEvents.class);

and 

FMLJavaModLoadingContext.get().getModEventBus().register(ModSubscribeEvents.class);

But throws the same error in-game.

 

Edited October 24, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 24, 2020 (edited)

That is still on the mod event bus, the default bus value of annotation @Mod.EventBusSubscriber is forge bus already, so what you need to do is remove bus = Bus.MOD here.

2 hours ago, urbanxx001 said:

@Mod.EventBusSubscriber(modid = Main.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)

 

And maybe add some loggings to make sure it is triggered. 

 

Edit: by using @Mod.EventBusSubscriber or

MinecraftForge.EVENT_BUS.register

the listener must be static

Edited October 24, 2020 by poopoodice
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 24, 2020 (edited)

Alright I tried:

@Mod.EventBusSubscriber(modid = Main.MOD_ID)

And added a log inside the event:

Main.LOGGER.info("Logged capability event");

But the info wasn't generated in latest.log or debug.log.

Edited October 24, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 24, 2020

Can you show all the updated code?

  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 24, 2020 (edited)

Of course:

IModTag

ModTagItem

ModTagProvider

ModTagStorage

ModEvents

 

CommonProxy

Main

Edited October 24, 2020 by urbanxx001
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    117

poopoodice

poopoodice    117

  • Dragon Slayer
  • poopoodice
  • Members
  • 117
  • 927 posts
Posted October 24, 2020 (edited)
1 hour ago, poopoodice said:

Edit: by using @Mod.EventBusSubscriber or


MinecraftForge.EVENT_BUS.register

the listener must be static

Make sure the listener is static (L5 https://pastebin.com/rHWc6H6k)

Edited October 24, 2020 by poopoodice
  • Thanks 1
  • Quote

Share this post


Link to post
Share on other sites

urbanxx001    8

urbanxx001

urbanxx001    8

  • Creeper Killer
  • urbanxx001
  • Members
  • 8
  • 139 posts
Posted October 24, 2020
6 minutes ago, poopoodice said:

Make sure the listener is static

That did the trick! Thank you so much

  • 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 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • brok4d
      OBJ MODELS

      By brok4d · Posted 17 minutes ago

      Hello, this mod is the source, you have to get boredhttps://gitlab.com/Lycanite/LycanitesMobs
    • JayNeedsHelp
      Logger not working

      By JayNeedsHelp · Posted 46 minutes ago

      So I'm currently creating a forge mod and I'm having an issue where the console stops logging after some errors. It seems to be connected to the access transformers that I'm using as before I added at's my console was working fine.   Here is my at file:  public-f net.minecraft.client.Minecraft session public net.minecraft.client.Minecraft timer public net.minecraft.client.gui.GuiScreen buttonList public net.minecraft.util.Timer tickLength public net.minecraft.network.play.client.CPacketPlayer onGround public net.minecraft.network.play.server.SPacketEntityVelocity motionX public net.minecraft.network.play.server.SPacketEntityVelocity motionY public net.minecraft.network.play.server.SPacketEntityVelocity motionZ public net.minecraft.network.play.server.SPacketExplosion motionX public net.minecraft.network.play.server.SPacketExplosion motionY public net.minecraft.network.play.server.SPacketExplosion motionZ public net.minecraft.client.renderer.entity.RenderManager renderPosX public net.minecraft.client.renderer.entity.RenderManager renderPosY public net.minecraft.client.renderer.entity.RenderManager renderPosZ   Any help is greatly appreciated thank you!
    • cadbane86140
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140 · Posted 2 hours ago

      Hello There! Today we are back on Hunger Games after a little break but we are finally back! In this episode we are on the good ol' map Survival Games 4 and it ACTUALLY went well for once. Also we have so many great battles on rooftops, small rooms and just out in the open! We also use shears to fight at one point and that was pretty crazy! There are so many hilarious moments in this episode that I know you guys are gonna love! I hope you all enjoy this video and if you did don't forget to like and sub for more Hunger Games in the future!  
    • Sad Whale
      Game crashes whenever I try to increase the RAM

      By Sad Whale · Posted 2 hours ago

      latest.log
    • diesieben07
      Game crashes whenever I try to increase the RAM

      By diesieben07 · Posted 2 hours ago

      In the logs folder of your game directory.
  • Topics

    • Milk_Shak3s
      1
      OBJ MODELS

      By Milk_Shak3s
      Started 15 hours ago

    • JayNeedsHelp
      0
      Logger not working

      By JayNeedsHelp
      Started 42 minutes ago

    • cadbane86140
      0
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140
      Started 2 hours ago

    • Sad Whale
      6
      Game crashes whenever I try to increase the RAM

      By Sad Whale
      Started 3 hours ago

    • Unusualty
      0
      GUI'S and player editing

      By Unusualty
      Started 2 hours ago

  • Who's Online (See full list)

    • PyRoTheLifeLess
    • Woosh
    • HexaGoat49
    • rhinitis
    • JayNeedsHelp
    • brok4d
    • Chumbanotz
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.16.x] Unable to Set Capability
  • Theme

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