Posted October 20, 20204 yr So I have been trying to make a capability and attach it to the player. This capability would monitor how much they are using the "drugs", which are just custom potions, in my mod and have negative consequences if they use them a lot. I have been following the documentation and guides but I still haven't managed to add the capability properly and I can't figure out why. package com.mitymods.funwithdrugs.capabilities; public interface IDrugUse { public void remove(float points); public void add(float points); public void setDrugUse(float points); public float getDrugUse(); } package com.mitymods.funwithdrugs.capabilities; public class DrugUse implements IDrugUse { private float drug_use; public DrugUse() { this.drug_use = 5; } @Override public void remove(float points) { // TODO Auto-generated method stub this.drug_use -= points; } @Override public void add(float points) { // TODO Auto-generated method stub this.drug_use += points; } @Override public void setDrugUse(float points) { // TODO Auto-generated method stub this.drug_use = points; } @Override public float getDrugUse() { // TODO Auto-generated method stub return this.drug_use; } } package com.mitymods.funwithdrugs.capabilities; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.util.LazyOptional; public class DrugUseProvider implements ICapabilitySerializable<INBT> { @CapabilityInject(IDrugUse.class) public static final Capability<IDrugUse> DRUG_USE_CAPABILITY = null; private LazyOptional<IDrugUse> instance = LazyOptional.of(DRUG_USE_CAPABILITY::getDefaultInstance); @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { return cap == DRUG_USE_CAPABILITY ? instance.cast() : LazyOptional.empty(); } @Override public INBT serializeNBT() { // @formatter:off return DRUG_USE_CAPABILITY.getStorage().writeNBT(DRUG_USE_CAPABILITY, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); // @formatter:on } @Override public void deserializeNBT(INBT nbt) { // @formatter:off DRUG_USE_CAPABILITY.getStorage().readNBT(DRUG_USE_CAPABILITY, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); // @formatter:on } } package com.mitymods.funwithdrugs.capabilities; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; import net.minecraft.util.Direction; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability.IStorage; public class DrugUseStorage implements Capability.IStorage<IDrugUse> { @Override public INBT writeNBT(Capability<IDrugUse> capability, IDrugUse instance, Direction side) { // return an NBT tag CompoundNBT tag = new CompoundNBT(); tag.putFloat("drug_use", instance.getDrugUse()); return tag; } @Override public void readNBT(Capability<IDrugUse> capability, IDrugUse instance, Direction side, INBT nbt) { // load from the NBT tag CompoundNBT tag = (CompoundNBT) nbt; instance.setDrugUse(tag.getFloat("drug_use")); } } package com.mitymods.funwithdrugs.events; import com.mitymods.funwithdrugs.FunWithDrugs; import com.mitymods.funwithdrugs.capabilities.DrugUseProvider; import com.mitymods.funwithdrugs.capabilities.IDrugUse; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; @Mod.EventBusSubscriber(modid = FunWithDrugs.MOD_ID, bus = Bus.FORGE) public class AddDrugUse { @SubscribeEvent public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof PlayerEntity) { event.addCapability(new ResourceLocation(FunWithDrugs.MOD_ID, "drug_use"), new DrugUseProvider()); } } @SubscribeEvent public void onPlayerFalls(LivingFallEvent event) { if(event.getEntityLiving() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntityLiving(); IDrugUse drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); if (drugUse.getDrugUse() == 5) { player.setHealth(0); } } } } And a bit of code in my main mod file FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onCommonSetup); private void onCommonSetup(FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(IDrugUse.class, new DrugUseStorage(), DrugUse::new); } The purpose of this second event is to test if the player has the capability and if it equals the default value, however this code doesn't even work for some reason that I can not figure out. I am very stumped on what the issue is. And I DO know java, I just am having a hard time understanding this specific thing. I know my code is probably pretty messy, I do intend to clean it up once I can figure out what I'm doing wrong
October 20, 20204 yr Author 58 minutes ago, diesieben07 said: This should not even compile, considering getCapability returns a LazyOptional, not an IDrugUse. Ah It complied earlier but i just checked and it didn't this time so I must have accidentally changed something before I posted the code here. So how would I want to go about this then? Would I just want to cast the LazyOptional to an IDrugUse? Or is the issue a lot more complex?
October 20, 20204 yr Author I do know the basics. I have taken 3 college courses on java. What I do not understand is what a LazyOptional is which is why I am so confused. I know exactly how inheritance and casting works, but since I dont get what a LazyOptional is, I do not know if it works in this situation. Hence why I asked the question if that Is what I am to do. I also asked that question as that is the suggestion that Eclipse gave me to solve it so it wasnt a question that I asked out of nowhere. Edited October 20, 20204 yr by link1213
October 20, 20204 yr Author I am grateful for your help so sorry if that last statement sounded rude, I just don't get why you assume that me having trouble = me not knowing java
October 20, 20204 yr Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
October 20, 20204 yr Author So then if i am understanding this correctly, LazyOptional is holding a value, and in this case that value is an instance of my DrugUse capability. So when I am calling the getCapability function, I am getting the LazyOptional that contains that players DrugUse Capability? So I would somehow need to get that info, from the LazyOptional if I wanted to check it for anything?
October 20, 20204 yr Author So I think I have figured it out, i tested it with a couple events and while It is having no issue getting the data, it seems to have a problem checking it. @SubscribeEvent public static void onPlayerLogsIn(PlayerLoggedInEvent event) { PlayerEntity player = event.getPlayer(); LazyOptional<IDrugUse> drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); IDrugUse pDrugUse = drugUse.orElse(new DrugUse()); player.sendMessage(new StringTextComponent("Your drug use variable is " + (pDrugUse.getDrugUse())), null); } This does display the correct number @SubscribeEvent public void onPlayerDamage(LivingDamageEvent event) { if(event.getEntityLiving() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntityLiving(); LazyOptional<IDrugUse> drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null); IDrugUse pDrugUse = drugUse.orElse(new DrugUse()); if (pDrugUse.getDrugUse() == 5) { player.setHealth(0); } } } This does nothing however. Am I accessing the data incorrectly? Edited October 20, 20204 yr by link1213
October 20, 20204 yr You should throw an exception if the player does not have the capability because what you do here drugUse.orElse(new DrugUse()) is pointless because if the player does not have the capability you are always accessing data of a new instance of your capability. Also is the event correctly registered? Edited October 20, 20204 yr by poopoodice
October 20, 20204 yr Author 4 minutes ago, diesieben07 said: Is this really what you want? Please think about what it means if the capability is not there and what you want to do in that case (can that even happen?). Shouldn't every player have the capability regardless by doing @SubscribeEvent public static void onAttachCapabilities(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof PlayerEntity) { event.addCapability(new ResourceLocation(FunWithDrugs.MOD_ID, "drug_use"), new DrugUseProvider()); } } So if somehow the player doesn't have the capability, wouldn't the orElse method give it to them? I wouldn't want there to be a situation where the player does not have the capability Edited October 20, 20204 yr by link1213
October 20, 20204 yr or use ifPresent Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
October 21, 20204 yr 2 hours ago, diesieben07 said: Which goes in the same direction of sweeping the problem (capability not present) under the rug. It depends on the use-case Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
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.