Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.10.2][Capabilities]How do I create my own capability?


Starless
 Share

Recommended Posts

Note: While I use UUIDs in the code bellow, this is just a test. The truth is I'll have to hash a file in the player's client machine and store that hash in a field.

 

This is what I have so far. It's kind of a IEEP.

 

//the class below is to register the capabilities and to store their unique instances.
public final class ModCapabilities
{
@CapabilityInject(IUUIDCapability.class)
public static Capability<IUUIDCapability> UUIDCapability = null;

private ModCapabilities(){}

public static void preInit()
{
	CapabilityManager.INSTANCE.register(IUUIDCapability.class, new IUUIDCapability.Storage(), new IUUIDCapability.Factory());
}
}

 

// the class bellow needs to add the capability to each player. In it I have identified a problem: I don't know how to handle the PlayerEvent.Clone
public final class EventHandler
{	
private EventHandler(){}

        @SubscribeEvent
public void onPlayerClone(PlayerEvent.Clone event)
{
	//TODO: don't know what to do here.
}

        @SubscribeEvent
public void onAttachCapabilityEntity(AttachCapabilitiesEvent.Entity event)
{
	if(event.getEntity() instanceof EntityPlayer)
	{
		event.addCapability(new ResourceLocation(TheMod.MODID, "UUIDCapability"), UUIDCapabilityProvider);
	}
}

public static void init(FMLInitializationEvent event)
{
	MinecraftForge.EVENT_BUS.register(new EventHandler());
}
}

 

// bellow is the rest. I don't know what is right and what is wrong here. My main problem is the Provider. I don't know how to implement it.
public interface IUUIDCapability
{
UUID getUUID();
void setUUID(UUID uuid);

public static final class Storage implements Capability.IStorage<IUUIDCapability>
{
	@Override
	public NBTTagCompound writeNBT(Capability<IUUIDCapability> capability, IUUIDCapability instance, EnumFacing side)
	{
		NBTTagCompound tag = new NBTTagCompound();
		tag.setLong("HI", instance.getUUID().getMostSignificantBits());
		tag.setLong("LO", instance.getUUID().getLeastSignificantBits());
		return tag;
	}

	@Override
	public void readNBT(Capability<IUUIDCapability> capability, IUUIDCapability instance, EnumFacing side, NBTBase nbt)
	{
		NBTTagCompound tag = (NBTTagCompound)nbt;
		long hi = tag.getLong("HI");
		long low = tag.getLong("LO");
		instance.setUUID(new UUID(hi, low));
	}
}

public static final class Factory implements Callable<IUUIDCapability>
{
	  @Override
	  public IUUIDCapability call() throws Exception
	  {
	    return new Implementation();
	  }
}

public static final class Implementation implements IUUIDCapability
{
	private UUID uuid;

	public Implementation()
	{
		this.uuid = UUID.randomUUID();
	}

	public Implementation(UUID uuid)
	{
		this.uuid = uuid;
	}
	@Override
	public UUID getUUID()
	{
		return uuid;
	}

	@Override
	public void setUUID(UUID uuid)
	{
		this.uuid = uuid;
	}
}

public static final class Provider implements ICapabilitySerializable<NBTTagCompound> 
{
	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing)
	{
		return capability == ModCapabilities.UUIDCapability;
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing)
	{
		if(capability == ModCapabilities.UUIDCapability)
		{
			//don't know how to get it.
		}
	}

	@Override
	public NBTTagCompound serializeNBT()
	{
		NBTTagCompound tag = new NBTTagCompound();
		UUID uuid = null;//don't know where to get the UUID from
		tag.setLong("HI", uuid.getMostSignificantBits());
		tag.setLong("LO", uuid.getLeastSignificantBits());

		return tag;
	}

	@Override
	public void deserializeNBT(NBTTagCompound tag)
	{
		long hi = tag.getLong("HI");
		long low = tag.getLong("LO");
		UUID uuid = new UUID(hi, low);//should be setting something else, not a local variable
	}
}
}

 

I can only instantiate an

Implementation

using the parameterless constructor once. Even though my immediate problem wouldn't break if I was to instantiate my hash more than once, it is an expensive operation and I would like if it would run just once (and only run again if I explicitly send a package for the client to do so). But I will need

UUID

s for certain

TileEntities

in my mod, so it is crucial that I only generate an

UUID

if something doesn't already have one, otherwise the whole thing will break.

Link to comment
Share on other sites

Why are you recreating a UUID system?  The game already has UUIDs for entities.

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.

Link to comment
Share on other sites

Normally the

ICapabilityProvider

(or

ICapabilitySerializable

) you attach to an external object stores at least one instance of your handler (

IUUIDCapability

) and returns that from

ICapabilityProvider#getCapability

.

 

If the hashing operation is expensive, consider lazy-loading it; i.e. only run it when something requests the data and the data doesn't already exist.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Why are you recreating a UUID system?  The game already has UUIDs for entities.

 

I know, this is just an example. I will use

UUIDs

for a certain type of

TileEntity

in the future though.

 

Normally the

ICapabilityProvider

(or

ICapabilitySerializable

) you attach to an external object stores at least one instance of your handler (

IUUIDCapability

) and returns that from

ICapabilityProvider#getCapability

.

 

If the hashing operation is expensive, consider lazy-loading it; i.e. only run it when something requests the data and the data doesn't already exist.

 

This is a replacement for IEEP. How do I give players a capability?

Link to comment
Share on other sites

Ok, this is what I have so far. Is it correct? Only thing missing is the implementation of a handler to PlayerEvent.Clone that will let the capability persist after player death.

 

public final class EventHandler
{
private EventHandler()
{

}

@SubscribeEvent
public void onAttachCapabilityEntity(AttachCapabilitiesEvent.Entity event)
{
	if(event.getEntity() instanceof EntityPlayer)
	{
		event.addCapability(new ResourceLocation(TheMod.MODID, "UUIDCapability"), new IUUIDCapability.Provider((EntityPlayer)event.getEntity())));
	}
}

@SubscribeEvent
public void onPlayerClone(PlayerEvent.Clone e)
{
	//TODO: STILL DON'T KNOW HOW TO PERSIST THE CAPABILITY.
}

public static void init(FMLInitializationEvent event)
{
	MinecraftForge.EVENT_BUS.register(new EventHandler());
}
}

 

Most of the file bellow remained unchanged. I just added a

clone

method to

IUUIDCapability.Implementation

(That I still don't use, but I think will be useful when implementing the handler to

PlayerEvent.Clone

). I also refactored

IUUIDCapability.Provider

to keep a reference to its

EntityPlayer

owner and to use that reference to read and write to the actual

Capability

(via

EntityPlayer#getCapability

)

public interface IUUIDCapability
{
UUID getUUID();
void setUUID(UUID uuid);

public static final class Storage implements Capability.IStorage<IUUIDCapability>
{
	@Override
	public NBTTagCompound writeNBT(Capability<IUUIDCapability> capability, IUUIDCapability instance, EnumFacing side)
	{
		NBTTagCompound tag = new NBTTagCompound();
		tag.setLong("HI", instance.getUUID().getMostSignificantBits());
		tag.setLong("LO", instance.getUUID().getLeastSignificantBits());
		return tag;
	}

	@Override
	public void readNBT(Capability<IUUIDCapability> capability, IUUIDCapability instance, EnumFacing side, NBTBase nbt)
	{
		NBTTagCompound tag = (NBTTagCompound)nbt;
		long hi = tag.getLong("HI");
		long low = tag.getLong("LO");
		instance.setUUID(new UUID(hi, low));
	}
}

public static final class Factory implements Callable<IUUIDCapability>
{
	  @Override
	  public IUUIDCapability call() throws Exception
	  {
	    return new Implementation();
	  }
}

public static final class Implementation implements IUUIDCapability
{
	private UUID uuid;

	public Implementation()
	{
		//this.uuid = UUID.randomUUID();
	}

	public Implementation(UUID uuid)
	{
		this.uuid = uuid;
	}

	@Override
	public UUID getUUID()
	{
		if(uuid != null)
		{
			return uuid;
		}
		else
		{
			uuid = UUID.randomUUID();
			return uuid;
		}
	}

	@Override
	public void setUUID(UUID uuid)
	{
		this.uuid = uuid;
	}

	@Override
	protected Implementation clone()
	{
		return new Implementation(uuid);
	}
}

public static final class Provider implements ICapabilitySerializable<NBTTagCompound> 
{
	private EntityPlayer owner;

	public Provider(EntityPlayer owner)
	{
		this.owner = owner;
	}

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

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing)
	{
		if(capability == ModCapabilities.UUIDCapability)
		{
			return (T) owner.getCapability(capability, facing);
		}
		return null;
	}

	@Override
	public NBTTagCompound serializeNBT()
	{
		NBTTagCompound tag = new NBTTagCompound();

		IUUIDCapability cap = owner.getCapability(ModCapabilities.UUIDCapability, EnumFacing.DOWN);

		long hi = cap.getUUID().getMostSignificantBits();
		long low = cap.getUUID().getLeastSignificantBits();

		tag.setLong("HI", hi);
		tag.setLong("LO", low);

		return tag;
	}

	@Override
	public void deserializeNBT(NBTTagCompound tag)
	{
		IUUIDCapability cap = owner.getCapability(ModCapabilities.UUIDCapability, EnumFacing.DOWN);

		long hi = tag.getLong("HI");
		long low = tag.getLong("LO");

		UUID uuid = new UUID(hi, low);

		cap.setUUID(uuid);
	}
}
}

 

Will that work? The points that I'm worried about:

 


  • I have to generate a random UUID only once. This UUID must persist through Minecraft sessions (I'm using a lazy getter, as instructed by Choonster).
     

  • Not only the UUID must persist through sessions, to do so it has to, of course, persist through player death, and I don't know how to implement that part (The handler of PlayerEvent.Clone).
     

 

Link to comment
Share on other sites

In the

IUUIDCapability.Provider

class in

getCapability

you check if the capability queried is yours, but then delegate to the owner (the player), which will lead to an infinite loop, because asking the owner now also asks your provider, since you attached it.

Instead you need to, if you are asked for your capability, provide the instance you want to be used, which you need to store in a field in the

Provider

class.

Your

Provider

class is like the player, in a sense.

Link to comment
Share on other sites

In the

IUUIDCapability.Provider

class in

getCapability

you check if the capability queried is yours, but then delegate to the owner (the player), which will lead to an infinite loop, because asking the owner now also asks your provider, since you attached it.

Instead you need to, if you are asked for your capability, provide the instance you want to be used, which you need to store in a field in the

Provider

class.

Your

Provider

class is like the player, in a sense.

 

I still don't quite get it. Could you show me in code?

Link to comment
Share on other sites

public static final class Provider implements ICapabilitySerializable<NBTTagCompound> 
{
	private EntityPlayer owner;
	private Capability.IStorage<IUUIDCapability> theCap; //this is new
//...
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing)
	{
		if(capability == ModCapabilities.UUIDCapability)
		{
			return (T) theCap; //this is altered
		}
		return null;
	}
}

 

Was that so hard?

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.

Link to comment
Share on other sites

in

getCapability

:

 

if (cap == MY_CAP) {
   // we know about this one! give them our implementation
   return instanceOfMyCap;
} else {
   // nope, we don't have this
   return null;
}

 

but the instance is unique for each player. If I give everyone the same instance it won't work. Also, I can instantiate an

Implementation

using the parameterless construct only once for each player during the whole existence of a world (In fact, even more than that).

Link to comment
Share on other sites

private Capability.IStorage<IUUIDCapability> theCap;
This makes no sense.

 

I will admit to having BSed that line.  I made a guess at the Type to use and was wrong.

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.

Link to comment
Share on other sites

Uh, what?

 

Since the parameterless constructor of

IUUIDCapability.Implementation

generates a new random

UUID

, the only occasion when it must be called is when a player first enters the world, and never again. Also, the

ICapabilityProvider

holds one instance of the capability for each player? I don't understand. What If I needed one for each

TileEntity

(as I will need in the future)? None of this is remotely clear in the documentation. Maybe a tutorial would be in order.

Link to comment
Share on other sites

unique for each player
Indeed it is.

 

If I give everyone the same instance it won't work.
Very true.

 

I'm sorry, but I don't understand how it works. I know you are trying to help, but comments such as those are not very helpful. It is clear to me that I fundamentally don't understand how this works.

 

Could someone offer me a deeper explanation than what is presented in the documentation? If there's someone who really understands Capabilities, they should suggest a full re-writing of the relevant page in the documentation, because it is not too clear.

Link to comment
Share on other sites

Since the parameterless constructor of

IUUIDCapability.Implementation

generates a new random

UUID

, the only occasion when it must be called is when a player first enters the world, and never again. Also, the

ICapabilityProvider

holds one instance of the capability for each player? I don't understand. What If I needed one for each

TileEntity

(as I will need in the future)? None of this is remotely clear in the documentation. Maybe a tutorial would be in order.

 

Each external object you attach the capability to (whether it's an

Entity

,

TileEntity

,

ItemStack

or

World

) must have its own provider instance. Each provider instance must store its own instance of the capability (

IUUIDCapability

).

 

Your capability will be read from NBT (i.e.

INBTSerializable#deserializeNBT

will be called on your

ICapabilityProvider

) at some point after

AttachCapabilitiesEvent

is fired.

 

Like I said before, consider lazy-loading the expensive operation instead of running it in the parameterless constructor.

 

If it helps, you can look at the capabilities provided by Forge (look for usages of

CapabilityManager#register

), the capability test mod or my own mod's capabilities (API, implementation).

 

Edit: Fixed the implementation link.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Since the parameterless constructor of

IUUIDCapability.Implementation

generates a new random

UUID

, the only occasion when it must be called is when a player first enters the world, and never again. Also, the

ICapabilityProvider

holds one instance of the capability for each player? I don't understand. What If I needed one for each

TileEntity

(as I will need in the future)? None of this is remotely clear in the documentation. Maybe a tutorial would be in order.

 

Each external object you attach the capability to (whether it's an

Entity

,

TileEntity

,

ItemStack

or

World

) must have its own provider instance. Each provider instance must store its own instance of the capability (

IUUIDCapability

).

 

Your capability will be read from NBT (i.e.

INBTSerializable#deserializeNBT

will be called on your

ICapabilityProvider

) at some point after

AttachCapabilitiesEvent

is fired.

 

Like I said before, consider lazy-loading the expensive operation instead of running it in the parameterless constructor.

 

If it helps, you can look at the capabilities provided by Forge (look for usages of

CapabilityManager#register

), the capability test mod or my own mod's capabilities (API, implementation).

 

I'll try later tomorrow. Thank you.

Link to comment
Share on other sites

By the way, I use a lazy getter. Still, in case of my hash it wouldn't be so bad if somehow I'd load it twice, it would only hurt performance. In case of an

UUID

(as I'll need later for another aspect of my project: certain

TileEntity

instances will have their own

UUID

), any capability that would get a random value via a

UUID.randomUUID()

call instead of reading its saved value would utterly and completely ruin the logic of my mod.

 

I would really appreciate if someone would kind of read my classes (the second post with code, not the first) and copy paste everything that is correct while overwritting anything that's wrong. I posted the whole comprehensive code because I was hoping someone would actually take a look at it. My java code writting skills may not be superb, but I try to keep it clean and easy to read.

Link to comment
Share on other sites

In your current implementation each player has their own instance of Provider- this is the way it should be -however each player needs their own instance of the capability(IUUIDCapability), as your use case requires storage of data unique to an individual player. Your provider should have a field that stores an instance of IUUIDCapability, this field should be set to a new instance of IUUIDCapability in the constructor of the provider. In getCapability, return this field instead of calling player.getCapability(). 

Link to comment
Share on other sites

I'm having a little trouble getting the name of the player:

 

        @SubscribeEvent
public void onAttachCapabilityEntity(AttachCapabilitiesEvent.Entity event)
{
	if(event.getEntity() instanceof EntityPlayer)
	{
		System.out.println(event.getEntity().getName());//getName() throws a NullPointerException here
	}
}

why does

getName()

throws a

NullPointerException

at that point?

Link to comment
Share on other sites

AttachCapabilitiesEvent.Entity

is fired in the

Entity

constructor, before the

EntityPlayer

constructor runs and sets the

EntityPlayer#gameProfile

field used by

EntityPlayer#getName

.

 

Because

AttachCapabilitiesEvent

is fired in the constructor before the object has finished being initialised and read from NBT, there's very little data you can access from the event handler method. Pretty much the only thing you can check is whether it's an instance of a particular class.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.