Posted October 22, 20205 yr 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, 20205 yr by urbanxx001
October 23, 20205 yr .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?
October 23, 20205 yr Author 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, 20205 yr by urbanxx001
October 23, 20205 yr 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?
October 23, 20205 yr 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.
October 23, 20205 yr Author 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, 20205 yr by urbanxx001
October 24, 20205 yr Author 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, 20205 yr by urbanxx001
October 24, 20205 yr Iirc attach capability event is fired on forge bus. Edited October 24, 20205 yr by poopoodice
October 24, 20205 yr Author 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, 20205 yr by urbanxx001
October 24, 20205 yr 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, 20205 yr by poopoodice
October 24, 20205 yr Author 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, 20205 yr by urbanxx001
October 24, 20205 yr Author Of course: IModTag ModTagItem ModTagProvider ModTagStorage ModEvents CommonProxy Main Edited October 24, 20205 yr by urbanxx001
October 24, 20205 yr 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, 20205 yr by poopoodice
October 24, 20205 yr Author 6 minutes ago, poopoodice said: Make sure the listener is static That did the trick! Thank you so much
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.