Jump to content

[SOLVED] [1.10.2] Cannot get capability to work


Daeruin

Recommended Posts

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

Link to comment
Share on other sites

Your implementation should have an empty constructor.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

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.

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.