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.3 Trouble with capabilities
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
link1213

1.16.3 Trouble with capabilities

By link1213, October 20, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020

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

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020
45 minutes ago, link1213 said:

IDrugUse drugUse = player.getCapability(DrugUseProvider.DRUG_USE_CAPABILITY, null);

This should not even compile, considering getCapability returns a LazyOptional, not an IDrugUse.

  • Quote

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020
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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020
Just now, link1213 said:

Would I just want to cast the LazyOptional to an IDrugUse?

This shows a fundamental lack of knowledge about casting and inheritance in Java. Please learn the basics before modding.

  • Quote

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020 (edited)

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, 2020 by link1213
  • Quote

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020

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

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020

A LazyOptional is a class.

With great Javadocs. Read it and you shall learn what it is, that's why Javadocs exist.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15919 posts
Posted October 20, 2020

 

  • Like 1
  • Quote

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.

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020

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?

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020
Just now, link1213 said:

So I would somehow need to get that info, from the LazyOptional if I wanted to check it for anything?

Correct, there are various methods that let you do that.

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020 (edited)

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, 2020 by link1213
  • Quote

Share this post


Link to post
Share on other sites

poopoodice    109

poopoodice

poopoodice    109

  • Dragon Slayer
  • poopoodice
  • Members
  • 109
  • 879 posts
Posted October 20, 2020 (edited)

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, 2020 by poopoodice
  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020
5 minutes ago, link1213 said:

IDrugUse pDrugUse = drugUse.orElse(new DrugUse());

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?). 

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

link1213    1

link1213

link1213    1

  • Tree Puncher
  • link1213
  • Members
  • 1
  • 20 posts
Posted October 20, 2020 (edited)
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, 2020 by link1213
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 20, 2020
Just now, link1213 said:

So if somehow the player doesn't have the capability, wouldn't the orElse method give it to them?

No, it would not. It would simply create a temporary object.

 

1 minute ago, link1213 said:

Shouldn't every player have the capability regardless by doing 

Correct.

 

1 minute ago, link1213 said:

I wouldn't want there to be a situation where the player does not have the capability

Correct. So what do you do in Java when you are in a wrong situation? You throw an exception. In this case, you can use orElseThrow for this.

  • Like 1
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15919 posts
Posted October 20, 2020

or use ifPresent

  • Quote

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.

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 21, 2020
11 hours ago, Draco18s said:

or use ifPresent

Which goes in the same direction of sweeping the problem (capability not present) under the rug.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15919 posts
Posted October 21, 2020
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

  • Quote

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.

Share this post


Link to post
Share on other sites

diesieben07    7583

diesieben07

diesieben07    7583

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7583
  • 54911 posts
Posted October 21, 2020
2 minutes ago, Draco18s said:

It depends on the use-case

Correct. Read the rest of the thread :P

  • 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

    • PotatoEz1
      Any good exploration mods for 1.16.3?

      By PotatoEz1 · Posted 3 minutes ago

      Does anybody know any cool mods for 1.16.3 that adds biomes and dungeons? Thanks!
    • XenoPyax
      GUI Button has weird image

      By XenoPyax · Posted 23 minutes ago

      SO I have the following code I've been looking through the gui of the Minecraft menus and replicated them but somehow my buttons don't seem to take the correct texture. Code: https://paste.md-5.net/figovuzufa.java Image:
    • Draco18s
      [1.16.4] setBlockState with out send update event.

      By Draco18s · Posted 27 minutes ago

      Still don't know why you added the 1 if you didn't want block updates.
    • DrakenXI
      [1.16.4] setBlockState with out send update event.

      By DrakenXI · Posted 41 minutes ago

      Thanks for your reply. You gave me another idea for a solution.   19 :  1 will cause a block update. 2 will send the change to clients. 16 will prevent neighbor reactions (e.g. fences connecting, observers pulsing). That what I found in Minecraft source for "setBlockState" Function. 🤨    
    • Somonestolemyusername
      [1.15.2] How Would I make a custom bow?

      By Somonestolemyusername · Posted 45 minutes ago

      all i has in it is  package com.vicken.mod3.entities.projectiles; import com.vicken.mod3.util.RegistryHandler; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.WallTorchBlock; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.projectile.ArrowEntity; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.potion.EffectInstance; import net.minecraft.potion.Effects; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; public class Ak47ArrowEntity extends ArrowEntity { public Ak47ArrowEntity(World worldIn, LivingEntity shooter) { super(worldIn, shooter); RegistryHandler.BULLET.get(); } @Override protected void arrowHit(LivingEntity living) { super.arrowHit(living); living.setFire(15); } }
  • Topics

    • PotatoEz1
      0
      Any good exploration mods for 1.16.3?

      By PotatoEz1
      Started 3 minutes ago

    • XenoPyax
      0
      GUI Button has weird image

      By XenoPyax
      Started 23 minutes ago

    • DrakenXI
      3
      [1.16.4] setBlockState with out send update event.

      By DrakenXI
      Started 20 hours ago

    • Somonestolemyusername
      16
      [1.15.2] How Would I make a custom bow?

      By Somonestolemyusername
      Started Tuesday at 09:30 PM

    • Ipsissimus418
      2
      [1.16.4] WorldTickEvent Concurrent Access Exception

      By Ipsissimus418
      Started 4 hours ago

  • Who's Online (See full list)

    • PotatoEz1
    • MineModder2000
    • lawra771
    • Forix
    • Danebi
    • Somonestolemyusername
    • XenoPyax
    • monkeysHK
    • Draco18s
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • 1.16.3 Trouble with capabilities
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community