Posted February 21, 20169 yr Hey everyone, I am triing to start getting used to the new Capabilities system. I am triing to create my own TileEntity with an Capabilitie, instead of IInventory. And even after reading the page about it I am TOTALLY confused. Do I need to create my TileEntity and make it an ICapabilityProvider?
February 21, 20169 yr Author If I see this right I need to manually save it right? public abstract class TileEntityInventoryProvider extends TileEntity{ private ItemStackHandler handler; private static final String INVENTORY_KEY = "beeverutils.inventory"; protected abstract int getSize(); public TileEntityInventoryProvider() { handler = new ItemStackHandler(getSize()); } @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { if(capability==CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true; return false; } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { // TODO Auto-generated method stub return (T)handler; } @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setTag(INVENTORY_KEY, handler.serializeNBT()); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); handler.deserializeNBT((NBTTagCompound) compound.getTag(INVENTORY_KEY)); } }
February 22, 20169 yr public abstract class TileEntityInventoryProvider extends TileEntity{ // Why is this abstract? private ItemStackHandler handler; // IItemHandler, better to use the interface itself private static final String INVENTORY_KEY = "beeverutils.inventory"; protected abstract int getSize(); public TileEntityInventoryProvider() { handler = new ItemStackHandler(getSize()); // You should wrap this in a if (IITEM_CAP != null) } @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { if(capability==CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return true; return false; // be sure to return super else added capabilities or parents won't work } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { // TODO Auto-generated method stub return (T)handler; // be sure to return super else added capabilities or parents won't work // Also you NEED to test what the capability and side is so that you don't return your IItemHandler when it asks you for an IEnergyData or something like that. } @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); compound.setTag(INVENTORY_KEY, handler.serializeNBT()); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); handler.deserializeNBT((NBTTagCompound) compound.getTag(INVENTORY_KEY)); } } This stuff is fairly simple, and we have examples in Forge.... Not sure why people are getting so confused. I do Forge for free, however the servers to run it arn't free, so anything is appreciated. Consider supporting the team on Patreon
February 22, 20169 yr Author Well. I dont see any example of using a capability, and the tutorial from readthedocs is seriously hard to read. Im just having problems because im just like "oh yeah that piece of code looks awesome but I dont really see where I use it". But thats obviously also my problem because I kinda hate text tutorials.
February 22, 20169 yr Take these with a rain of salt code-style wise as they are patches but: Furnaces Chests BrewingStands This is my original test code which shows my intention of how modders should so it with Caps that Forge/They don't provide {Doing it this way gives soft-dependencies} https://github.com/LexManos/MinecraftForge/blob/Capabilities/patches/minecraft/net/minecraft/tileentity/TileEntityFurnace.java.patch#L58 This shows my original test code which creates, and queries a TE for it's capability: https://github.com/LexManos/MinecraftForge/blob/Capabilities/src/test/java/net/minecraftforge/test/capabilitytest/TestCapabilityMod.java#L27-L62 It's not to difficult. Someone write better docs. I do Forge for free, however the servers to run it arn't free, so anything is appreciated. Consider supporting the team on Patreon
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.