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 am trying to update my mod from 1.8.9 to 1.10.2. I was using IExtendedEntityProperty to give players a thirst property. I'm trying to implement this functionality using Capabilities, but I can't get it to work. I am getting an InstantiationException when my mod tries to attach the capability to the player upon login.

 

My interface class:

 

 

public interface IThirst
{

public boolean getThirstier(int amount, boolean notify);

public boolean slakeThirst(int amount, boolean notify);

public void setThirst(int amount);

public int getCurrentThirst();

public int getMaxThirst();

}

 

 

 

My implementation class:

 

 

public class Thirst implements IThirst
{

private int currentThirst, maxThirst;
private final EntityPlayer player;

public Thirst(EntityPlayer player)
{
	this.player = player;
	this.currentThirst = 20;
	this.maxThirst = 20;
}

@Override
public boolean getThirstier(int amount, boolean notify)
{
	boolean sufficient = amount <= this.currentThirst;
	this.currentThirst -= (sufficient ? amount : this.currentThirst);
	if (notify)
	{
		PrimalPacketHandler.INSTANCE.sendTo(new PrimalThirstPacket(this.player, this.currentThirst), (EntityPlayerMP) player);
	}
	return sufficient;
}

@Override
public boolean slakeThirst(int amount, boolean notify)
{
	int thirstAlreadyUsed = this.maxThirst - this.currentThirst;
	boolean sufficient = amount <= thirstAlreadyUsed;
	this.currentThirst += (sufficient ? amount : thirstAlreadyUsed);
	if (notify)
	{
		PrimalPacketHandler.INSTANCE.sendTo(new PrimalThirstPacket(this.player, this.currentThirst), (EntityPlayerMP) player);
	}
	return sufficient;
}

@Override
public void setThirst(int amount)
{
	if (amount < 0)
	{
		this.currentThirst = 0;
	} else if (amount > this.maxThirst)
	{
		this.currentThirst = this.maxThirst;
	} else
	{
		this.currentThirst = amount;
	}
}

@Override
public int getCurrentThirst()
{
	return this.currentThirst;
}

@Override
public int getMaxThirst()
{
	return this.maxThirst;
}

}

 

 

 

My provider class:

 

 

public class ThirstProvider implements ICapabilitySerializable<NBTBase>
{

@CapabilityInject(IThirst.class)
public static final Capability<IThirst> THIRST_CAPABILITY = null;

private IThirst instance = THIRST_CAPABILITY.getDefaultInstance();

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
{
	return capability == THIRST_CAPABILITY;
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
      return capability == THIRST_CAPABILITY ? THIRST_CAPABILITY.<T> cast(this.instance) : null;
}

@Override
public NBTBase serializeNBT()
{
	return THIRST_CAPABILITY.getStorage().writeNBT(THIRST_CAPABILITY, this.instance, null);
}

@Override
public void deserializeNBT(NBTBase nbt)
{
	THIRST_CAPABILITY.getStorage().readNBT(THIRST_CAPABILITY, this.instance, null, nbt);
}

}

 

 

 

My storage class:

 

 

public class ThirstStorage implements IStorage<IThirst>
{

@Override
public NBTBase writeNBT(Capability<IThirst> capability, IThirst instance, EnumFacing side)
{
	return new NBTTagInt(instance.getCurrentThirst());
}

@Override
public void readNBT(Capability<IThirst> capability, IThirst instance, EnumFacing side, NBTBase nbt)
{
	instance.setThirst(((NBTTagInt) nbt).getInt());
}

}

 

 

 

My handler class:

 

 

public class CapabilityHandler
{
public static final ResourceLocation THIRST_CAPABILITY = new ResourceLocation(Main.MODID, "thirst");

@SubscribeEvent
public void attachCapability(AttachCapabilitiesEvent.Entity event)
{
	if (!(event.getEntity() instanceof EntityPlayer)) return;
	event.addCapability(THIRST_CAPABILITY, new ThirstProvider());
}
}

 

 

 

Edited to add my CommonProxy class where I register stuff:

 

 

public class CommonProxy
{

public void preInit(FMLPreInitializationEvent e)
{
	PrimalPacketHandler.INSTANCE.registerMessage(PrimalThirstHandler.class, PrimalThirstPacket.class, 0, Side.CLIENT);
	PrimalPacketHandler.INSTANCE.registerMessage(PrimalToeStubHandler.class, PrimalToeStubPacket.class, 1, Side.SERVER);

	PrimalItemRegistry.createItems();
	PrimalBlockRegistry.createBlocks();
	PrimalRecipes.initRecipes();
	PrimalTileEntityRegistry.initTileEntities();

	CapabilityManager.INSTANCE.register(IThirst.class, new ThirstStorage(), Thirst.class);
	MinecraftForge.EVENT_BUS.register(new CapabilityHandler());
}

public void init(FMLInitializationEvent e)
{
	GameRegistry.registerWorldGenerator(new PrimalWorldGen(), 0);
	NetworkRegistry.INSTANCE.registerGuiHandler(Main.instance, new PrimalGuiHandler());
}

public void postInit(FMLPostInitializationEvent e)
{
	MinecraftForge.EVENT_BUS.register(new PrimalHarvestEvent());
	MinecraftForge.EVENT_BUS.register(new PrimalToolDestroyEvent());
	MinecraftForge.EVENT_BUS.register(new PrimalPlayerTickEvent());
	MinecraftForge.EVENT_BUS.register(new PrimalPlayerInteractEvent());
	MinecraftForge.EVENT_BUS.register(new PrimalBreakSpeedEvent());
	MinecraftForge.EVENT_BUS.register(new PrimalEntityJoinWorldEvent());
}

public void registerItemRenderer(Item item, int meta, String registryName) {
}

}

 

 

 

The error: http://pastebin.com/64bfYYkd

  • Author

I wondered about that. I didn't see any examples with a constructor. Without it, how would I get access to the player in order to send my packets? I need to notify the client when the player's thirst changes, in order to update the GUI.

I wondered about that. I didn't see any examples with a constructor. Without it, how would I get access to the player in order to send my packets? I need to notify the client when the player's thirst changes, in order to update the GUI.

Make your IThirst#getThirstier and IThirst#slakeThirst take in a player so it can send the packet.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Thank you! That did it. I knew it would be something simple.

 

Now it's crashing for a different reason. Something to do with block rendering. But I'll post on that later if I can't figure it out.

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.