Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

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

  • Author

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

  • Author

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.