Jump to content

[1.16.1] Difficulty Understanding Capabilities


Karutoh

Recommended Posts

I'm having a hard time understanding capabilities as there seems to be a few ways of adding them making it really confusing to me. I also seem to be missing something when adding a capability to an ItemStack. Could someone nudge me in the right direction? I've tried looking at the Forge Docs and Github code and nothing seems to help.

 

package com.event_horizon.capabilities;

public interface IFission
{
	public float getDuration();
	
	public float getTemperature();
	
	public void addNeutrons(final long neutrons);
	
	public long getNeutrons();
}
package com.event_horizon.capabilities;

import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.common.util.INBTSerializable;

public class Fission implements IFission, INBTSerializable<CompoundNBT>
{
	private static final float duration = 0.0001f;
	private float temperature = 293;
	private long neutrons = 0;
	
	@Override
	public float getDuration()
	{
		return duration;
	}
	
	@Override
	public float getTemperature()
	{
		return temperature;
	}

	@Override
	public void addNeutrons(long neutrons)
	{
		this.neutrons += neutrons;
	}

	@Override
	public long getNeutrons()
	{
		return neutrons;
	}

	@Override
	public CompoundNBT serializeNBT()
	{
		CompoundNBT nbt = new CompoundNBT();
		
		nbt.putFloat("temperature", temperature);
		nbt.putLong("neutrons", neutrons);
		
		return nbt;
	}

	@Override
	public void deserializeNBT(CompoundNBT nbt)
	{
		temperature = nbt.getFloat("temperature");
		neutrons = nbt.getLong("neutrons");
	}
}
package com.event_horizon.events;

import com.event_horizon.EventHorizon;
import com.event_horizon.capabilities.FissionProvider;
import com.event_horizon.items.FuelRod;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@Mod.EventBusSubscriber(modid = EventHorizon.MOD_ID, bus = Bus.FORGE)
public class CapabilitiesEvent
{
	public static final ResourceLocation FISSION = new ResourceLocation(EventHorizon.MOD_ID, "fission");
	
	@SubscribeEvent
	public static void itemStackCapabilities(AttachCapabilitiesEvent<ItemStack> event)
	{
		Item item = event.getObject().getItem();
		if (item instanceof FuelRod)
		{
			event.addCapability(FISSION, new Fission());
		}
	}
}

 

Edited by Karutoh
Link to comment
Share on other sites

Oh okay. I was unaware I could override a function like that in a Item class. Also would this work if I changed the

public class Fission implements IFission, INBTSerializable<CompoundNBT>

to

public class Fission implements IFission, ICapabilitySerializable<CompoundNBT>

?

Edited by Karutoh
Link to comment
Share on other sites

Ah okay so here's what I have on a custom class implementing ICapabilityProvider. The serializers confuse me and I'm not sure what they are for. Is it this when the capability is used in other mods, between server/client, or save/load?

public class FissionProvider implements ICapabilitySerializable<CompoundNBT>
{
	private LazyOptional<IFission> instance = LazyOptional.of(CapabilityRegister.FISSION::getDefaultInstance);
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side)
	{
		return CapabilityRegister.FISSION.orEmpty(cap, instance);
	}

	@Override
	public CompoundNBT serializeNBT()
	{
		IFission fission = instance.orElseThrow(() -> new IllegalArgumentException("at serialize"));
		
		CompoundNBT nbt = new CompoundNBT();
		nbt.putFloat("temperature", fission.getTemperature());
		nbt.putLong("neutrons", fission.getNeutrons());
		return nbt;
	}

	@Override
	public void deserializeNBT(CompoundNBT nbt)
	{
		//IFission fission = instance.orElseThrow(() -> new IllegalArgumentException("at serialize"));
	}

}

I'm confused on the serializers on the register as well.
 

public class CapabilityRegister
{
	@CapabilityInject(IFission.class)
	public static Capability<IFission> FISSION = null;
	
	public static void init()
	{
			CapabilityManager.INSTANCE.register(IFission.class, new IStorage<IFission>()
			{
				@Override
				public INBT writeNBT(Capability<IFission> capability, IFission instance, Direction side)
				{
					return null;
				}
	
				@Override
				public void readNBT(Capability<IFission> capability, IFission instance, Direction side, INBT nbt)
				{
				}
			}, Fission::new);
	}
}

 

Edited by Karutoh
Link to comment
Share on other sites

AH okay so that makes much more sense. For the FissionProvider#instance can I use the Fission class instead of the IFission interface class? That would make the serialization and deserialization easier by just calling those methods directly and returning them and would allow me to set the variables. Also for the FissionProvider.FISSION variable what is the second arguement in the new

ResourceLocation(EventHorizon.MOD_ID, "fission");

 referencing?

Edited by Karutoh
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



×
×
  • Create New...

Important Information

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