Jump to content

Starless

Members
  • Posts

    107
  • Joined

  • Last visited

Converted

  • Gender
    Male
  • Location
    The Center of the Universe
  • Personal Text
    I am Starless and Everlasting

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Starless's Achievements

Creeper Killer

Creeper Killer (4/8)

1

Reputation

  1. I talked about implementing those interfaces because my TileEntity is a chest
  2. So Minecraft itself doesn't use the methods defined by those interfaces?
  3. So if I don't expose it, players will still be able to interact with it? And should I implement IItemHandler , IItemHandlerModifiable and INBTSerializable<NBTTagCompound> ?
  4. I have an inventory TileEntity. How do I make sure no automation mods can interact with it (like BuildCraft pipes)?
  5. 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?
  6. 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.
  7. 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.
  8. Indeed it is. 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.
  9. 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.
  10. 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).
  11. Can you show me how would I do that with the classes I have?
  12. 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).
  13. I know, this is just an example. I will use UUIDs for a certain type of TileEntity in the future though. This is a replacement for IEEP. How do I give players a capability?
×
×
  • Create New...

Important Information

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