Jump to content

1.16.3 Trouble with capabilities


link1213

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

  • Like 1

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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 by poopoodice
  • Like 1
Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.