Jump to content

[1.16.x] Unable to Set Capability


urbanxx001

Recommended Posts

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 by urbanxx001
Link to comment
Share on other sites

.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
Link to comment
Share on other sites

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 by urbanxx001
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by urbanxx001
Link to comment
Share on other sites

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 by urbanxx001
Link to comment
Share on other sites

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 by urbanxx001
Link to comment
Share on other sites

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 by poopoodice
Link to comment
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.
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.