Jump to content

getCapability from itemStack, that has it returns null


TEXHIK

Recommended Posts

I have an ItemStack, that has only one capability - SocketCapability. When i'm trying to check/get it, i'm getting false/null.

But if I call methods right on it's provider, all is fine

image.png.3473ec3ff073a01780bdd2ea16af4d0f.png

 

	public void preInit() {
		CapabilityManager.INSTANCE.register(ISocketCapability.class, new SocketCapabilityStorage(), new SocketCapabilityFactory());
		GameRegistry.registerTileEntity(TileInlayTable.class, ModBlocks.INLAY_TABLE.getRegistryName());//TODO universal tileEntity registing
	}
public class SocketCapabilityFactory implements Callable<SocketCapability> {
	@Override
	public SocketCapability call() throws Exception {
		return new SocketCapability();
	}
}

 

public class SocketCapabilityProvider implements ICapabilitySerializable<NBTBase> {

	@CapabilityInject(ISocketCapability.class)
	public static final Capability<ISocketCapability> SOCKET_CAPABILITY = null;

	private ISocketCapability instance = SOCKET_CAPABILITY.getDefaultInstance();

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

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

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

	@Override
	public void deserializeNBT(NBTBase nbt) {
		SOCKET_CAPABILITY.getStorage().readNBT(SOCKET_CAPABILITY, instance, null, nbt);
	}
}
public class SocketCapabilityStorage implements Capability.IStorage<ISocketCapability> {
	@Nullable
	@Override
	public NBTBase writeNBT(Capability<ISocketCapability> capability, ISocketCapability instance, EnumFacing side) {
		NBTTagCompound compound = new NBTTagCompound();
		NBTTagList list = new NBTTagList();
		if (instance.getSocketsCount() > 0) {
			for (Socket socket : instance.getSockets()) {
				list.appendTag(socket.serializeNBT());
			}
		}
		compound.setTag("sockets", list);
		return compound;
	}

	@Override
	public void readNBT(Capability<ISocketCapability> capability, ISocketCapability instance, EnumFacing side, NBTBase nbt) {
		NBTTagList list = ((NBTTagCompound) nbt).getTagList("sockets", 10);
		if (list.isEmpty()) {
			instance.setSockets(new Socket[0]);
		} else {
			Socket[] sockets = new Socket[list.tagCount()];
			for (int i = 0; i < list.tagCount(); i++) {
				sockets[i] = new Socket(list.getCompoundTagAt(i));
			}
			instance.setSockets(sockets);
		}
	}

 

Edited by TEXHIK
Link to comment
Share on other sites

On 8/17/2018 at 2:08 PM, diesieben07 said:

This is broken. You need to use a ResourceLocation that represents your capability provider, not the stack's item's name.

Fixed it, but it didn't fix the bug. And bug only when I shift-click, when taking item in regular way, all is ok.

Figured out, that the problem is that on successfull getting item by shift-click, itemStack passed into onTake is empty. What is the right way to change item, before taking it?

Edited by TEXHIK
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.