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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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    174

loordgek

loordgek    174

  • World Shaper
  • loordgek
  • Members
  • 174
  • 1797 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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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    113

poopoodice

poopoodice    113

  • Dragon Slayer
  • poopoodice
  • Members
  • 113
  • 896 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

    • BinAufGoogle
      Server doesnt start

      By BinAufGoogle · Posted 7 minutes ago

      1.8.9 Java 8
    • monkeysHK
      Register a Custom Command

      By monkeysHK · Posted 13 minutes ago

      Oh that's what I need. Now it worked & thanks again!
    • Ben Newman
      [1.16.1] [SOLVED] Block drops not respecting harvest level and tooltype

      By Ben Newman · Posted 21 minutes ago

      Where do I add the setRequiresTools i really need to know as I cannot find where to code it. Many thanks. 
    • GenElectrovise
      What is the method to left click?

      By GenElectrovise · Posted 54 minutes ago

      There's probably something in or nearby to PlayerEntity (as a movement controller or something similar?) I'd start with searching my workspace for something along the lines of KeystrokeHandler or PlayerMovementController
    • Luis_ST
      [1.16.5] GameOverlay

      By Luis_ST · Posted 54 minutes ago

      I just want to render a overlay (i have creat a spyglass likt that from 1.17) and now i want to render the Overlay this is the code of the event i used: @SubscribeEvent(priority = EventPriority.HIGHEST) public static void RenderSpyglassOverlay(RenderGameOverlayEvent event) { PlayerEntity player = Minecraft.getInstance().player; int posX = event.getWindow().getScaledWidth() / 2; int posY = event.getWindow().getScaledHeight() / 2; if (player.getActiveItemStack().getItem() == CaveItems.SPYGLASS.get()) { RenderSystem.disableDepthTest(); RenderSystem.depthMask(false); RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); RenderSystem.disableAlphaTest(); Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("cave:textures/misc/spyglass_scope.png")); Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), posX - 128, posY - 128, 0, 0, posX * 2, posY * 2, 256, 256); RenderSystem.depthMask(true); RenderSystem.enableDepthTest(); RenderSystem.enableAlphaTest(); RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); } } but the overlay looks like this: https://drive.google.com/file/d/15llZaiIqNWK7WRqcihIJY7oaAszkFKnn/view?usp=sharing so my question: 1 .how to render the game overlay translucent 2. how to set the outside of the overlay to black
  • Topics

    • BinAufGoogle
      4
      Server doesnt start

      By BinAufGoogle
      Started 19 hours ago

    • monkeysHK
      4
      Register a Custom Command

      By monkeysHK
      Started 19 hours ago

    • StefanDeSterke
      6
      [1.16.1] [SOLVED] Block drops not respecting harvest level and tooltype

      By StefanDeSterke
      Started July 20, 2020

    • Gubipe
      5
      What is the method to left click?

      By Gubipe
      Started 14 hours ago

    • Luis_ST
      0
      [1.16.5] GameOverlay

      By Luis_ST
      Started 54 minutes ago

  • Who's Online (See full list)

    • BinAufGoogle
    • diesieben07
    • Blizosko
    • RpxdYTX
    • Novârch
    • monkeysHK
    • GenElectrovise
    • MHT121
    • MHT
  • 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