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

[1.16.5] save/transfer of capability


Recommended Posts

35 minutes ago, diesieben07 said:

You can use the Direction parameter of getCapability. If the direction is not null, return only your handler instead of the combined one.

okay this  is my capability class now

when I try to get the capability on the way:

		original.getCapability(EnderChestCapability.ENDERCHEST, Direction.WEST).ifPresent(oldEnderChest -> {
			
			CompoundNBT nbt = ((ItemStackHandler) oldEnderChest).serializeNBT();
					
			player.getCapability(EnderChestCapability.ENDERCHEST, Direction.WEST).ifPresent(newEnderChest -> {
				
				((ItemStackHandler) newEnderChest).deserializeNBT(nbt);
				
			});
			
		});

 

eclipse gives me the following error:

"Cannot cast from CombinedInvWrapper to ItemStackHandler"

Link to post
Share on other sites

Update: I have changed a few things / tried but in most cases I get a ClassCastException,

because I cannot cast the CombinedInvWrapper to an ItemStackHandler,

although my capability should return an ItemStackHandler if I specify a direction.

 

that is the important part of my capability provider:

		private EnderChestItemStackHandler inventory = new EnderChestItemStackHandler(27);
		private PlayerEntity player;
		private LazyOptional<EnderChestItemStackHandler> lazyOptional = LazyOptional.of(() -> inventory);
		private LazyOptional<CombinedInvWrapper> optional = LazyOptional.of(() -> {
			
			EnderChestInventory enderChestInventory = player.getInventoryEnderChest();
			InvWrapper invWrapper = new InvWrapper(enderChestInventory);
			CombinedInvWrapper combinedInvWrapper = new CombinedInvWrapper(invWrapper, inventory);
			
			return combinedInvWrapper;
			
		});
		
		@Override
		@SuppressWarnings({ "unchecked" })
		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			
			LazyOptional<?> ret = side == null ? optional : lazyOptional;
			return cap == ENDERCHEST && cap != null ? (LazyOptional<T>) ret : LazyOptional.empty();
			
		}

 

and how I am currently trying to get the capability (I have to use IItemHandlerModifiable first because, as already said,

I cannot cast the CombinedInvWrapper to an ItemStackHandler and it should actually work anyway because getCapability should return an extension of the ItemStackHandler in this case)

		original.getCapability(EnderChestCapability.ENDERCHEST, Direction.WEST).ifPresent(oldEnderChest -> {
			
			IItemHandlerModifiable oldItemModifiable = oldEnderChest;
			ItemStackHandler oldItemHandler = (ItemStackHandler) oldItemModifiable;
			CompoundNBT nbt = oldItemHandler.serializeNBT();
					
			player.getCapability(EnderChestCapability.ENDERCHEST, Direction.WEST).ifPresent(newEnderChest -> {
				
				IItemHandlerModifiable newItemModifiable = newEnderChest;
				ItemStackHandler newItemHandler = (ItemStackHandler) newItemModifiable;
				newItemHandler.deserializeNBT(nbt);
				
			});
			
		});

 

what do i have to change?

 

Link to post
Share on other sites
42 minutes ago, Luis_ST said:

private LazyOptional<EnderChestItemStackHandler> lazyOptional = LazyOptional.of(() -> inventory);
private LazyOptional<CombinedInvWrapper> optional

 

These variable names are terrible.

 

The code in your Git repository does not use a Direction to query the capability in your PlayerEvent.Clone handler. Please update your Git repo.

Link to post
Share on other sites
1 hour ago, Luis_ST said:

cap == ENDERCHEST && cap != null

if cap IS the enderchest, then it CAN'T be null and vice versa.

 

  • Thanks 1

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 post
Share on other sites
38 minutes ago, Draco18s said:

if cap IS the enderchest, then it CAN'T be null and vice versa.

1 hour ago, diesieben07 said:

These variable names are terrible.

The code in your Git repository does not use a Direction to query the capability in your PlayerEvent.Clone handler. Please update your Git repo.

updated and changed the things you mentioned

Link to post
Share on other sites
14 hours ago, diesieben07 said:

Here you define that your capability always returns a CombinedInvWrapper. Which is not true now.

okay thanks i changed the capability to IItemHandlerModifiable and now it works

do I have to clone the capability in another event?

Link to post
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.

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.



×
×
  • Create New...

Important Information

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