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

    • I was just trying to play my modded world when i randomly got this crash for no reason. I sorted through like every mod and eventually I realized it was LLibrary but I can't seem to find a solution to fix the crashing. I can't lose the world that I have that uses this mod please help me. Here's the report: https://pastebin.com/0D00B79i If anyone has a solution please let me know.  
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Ligawin88 adalah bocoran slot rekomendasi gacor dari Ligawin88 yang bisa anda temukan di SLOT Ligawin88. Situs SLOT Ligawin88 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Ligawin88 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Ligawin88 merupakan SLOT Ligawin88 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Ligawin88. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Ligawin88 hari ini yang telah disediakan SLOT Ligawin88. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Ligawin88 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Ligawin88 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Ligawin88 di link SLOT Ligawin88.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑   Daftar Slot Asusslot adalah bocoran slot rekomendasi gacor dari Asusslot yang bisa anda temukan di SLOT Asusslot. Situs SLOT Asusslot hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Asusslot terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Asusslot merupakan SLOT Asusslot hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Asusslot. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Asusslot hari ini yang telah disediakan SLOT Asusslot. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Asusslot terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Asusslot terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Asusslot di link SLOT Asusslot.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Galeri555 adalah bocoran slot rekomendasi gacor dari Galeri555 yang bisa anda temukan di SLOT Galeri555. Situs SLOT Galeri555 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Galeri555 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Galeri555 merupakan SLOT Galeri555 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Galeri555. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Galeri555 hari ini yang telah disediakan SLOT Galeri555. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Galeri555 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Galeri555 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Galeri555 di link SLOT Galeri555.
    • 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 🤑DAFTAR & LOGIN🤑 Daftar Slot Kocok303 adalah bocoran slot rekomendasi gacor dari Kocok303 yang bisa anda temukan di SLOT Kocok303. Situs SLOT Kocok303 hari ini yang kami bagikan di sini adalah yang terbaik dan bersiaplah untuk mengalami sensasi tak terlupakan dalam permainan slot online. Temukan game SLOT Kocok303 terbaik dengan 100 pilihan provider ternama yang dipercaya akan memberikan kepuasan dan kemenangan hari ini untuk meraih x500. RTP SLOT Kocok303 merupakan SLOT Kocok303 hari ini yang telah menjadi pilihan utama bagi pemain judi online di seluruh Indonesia. Setiap harinya jutaan pemain memasuki dunia maya untuk memperoleh hiburan seru dan kemenangan besar dalam bermain slot dengan adanya bocoran RTP SLOT Kocok303. Tidak ada yang lebih menyenangkan daripada mengungguli mesin slot dan meraih jackpot x500 yang menggiurkan di situs SLOT Kocok303 hari ini yang telah disediakan SLOT Kocok303. Menangkan jackpot besar x500 rajanya maxwin dari segala slot dan raih kemenangan spektakuler di situs Kocok303 terbaik 2024 adalah tempat yang menyediakan mesin slot dengan peluang kemenangan lebih tinggi daripada situs slot lainnya. Bagi anda yang mencari pengalaman judi slot paling seru dan mendebarkan, situs bo SLOT Kocok303 terbaik 2024 adalah pilihan yang tepat. Jelajahi dunia slot online melalui situs SLOT Kocok303 di link SLOT Kocok303.
  • Topics

×
×
  • Create New...

Important Information

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