Jump to content

[1.12.2] extractItem and insertItem problems for IItemHandler


Recommended Posts

Posted

I have been working on trying to get inventory items to transfer between things using the new "system" and it just doesn't work no matter what I try. I use the extractItem and nothing changes in the inventory when the simulated variable is set to false. Same with insertItem. If it's an ItemStackHandler instance I can use setStackInSlot in some cases, but not all. For example, this works (inside a block update using scheduled update):

 

if(isdraining && tile.getCurrentFuel() > 0) {
			tile.consumeFuel();
			if(tile.getCurrentFuel() == 0)
				for(EnumFacing face2 : EnumFacing.values())  
					this.notifyChange(world, pos, face2);
		}
		
		ItemStackHandler inventory = (ItemStackHandler)tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
		if(inventory != null) {
			ItemStack item = inventory.getStackInSlot(0);
			Item thing = item.getItem();
			int burn = 0;
			if(thing != null) burn = thing.getItemBurnTime(item);
			if(!item.isEmpty() && burn != 0) {
				if(burn < 0) {
					if(thing instanceof ItemBlock) {
						Block block = ((ItemBlock)thing).getBlock();
						if((block instanceof BlockLog) || (block instanceof BlockPlanks)) burn = 300;
						else if(block == Blocks.COAL_BLOCK) burn = 16000;
					} else if(thing == Items.LAVA_BUCKET) burn = 20000;
					else if(thing == Items.COAL) burn = 1600;
					else if(thing == Items.BLAZE_ROD) burn = 2400;
				}
				if(burn > 0) {
					if(tile.canConsume(burn)) {
						tile.increaseFuel(burn);
						if(item.getItem() == Items.LAVA_BUCKET) {
							ItemStack nitem = new ItemStack(Items.BUCKET, 1);
							inventory.setStackInSlot(0, nitem);
						} else inventory.extractItem(0, 1, false);
						for(EnumFacing face2 : EnumFacing.values())  
							this.notifyChange(world, pos, face2);
					}
				}
			}
		}

 

However in the same update setup, this doesn't work if trying to push a stack into the inventory of a neighboring block. Here's an example with one technique commented out, the other not. Neither technique works:

 

public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {
		if(world.isRemote) return;
		EnumFacing face = state.getValue(FACING);
		TileEntity tile = world.getTileEntity(pos);
		TileEntity entity = world.getTileEntity(pos.offset(face));
		if((tile instanceof MagitechTileEntityPipe) && entity != null && entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, face.getOpposite())) {
			System.out.println("Begin Transfer");
			ItemStackHandler inventory = (ItemStackHandler)tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
			IItemHandler target = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
			boolean done = !(target instanceof ItemStackHandler);
			for(int i = 0; i < inventory.getSlots() && !done; ++i) {
				ItemStack test = inventory.extractItem(i, this._speed, true);
				if(test != null && !test.isEmpty()) {
					int start = test.getCount();
					System.out.println("Got an item " + Integer.toString(start) + " @ " + Integer.toString(i));
					test = this.addToInventory(test, (ItemStackHandler)target);
					int end = test.getCount();
					if(end != start) {
						test = inventory.getStackInSlot(i);
						if(end > 0) {
							test = test.copy();
							test.setCount(end);
							inventory.setStackInSlot(i, test);
						} else inventory.setStackInSlot(i, ItemStack.EMPTY);
//						inventory.extractItem(i, start - end, false);
						System.out.println("Item Transfered. " + Integer.toString(end));
						tile.markDirty();
						entity.markDirty();
						done = true;
					}
					System.out.println("End Transfer");
				}
			}
		}
		world.scheduleBlockUpdate(pos, this, 10, 0);
	}
              
    public ItemStack addToInventory(ItemStack stack, ItemStackHandler target) {
		int max = target.getSlots();
		ItemStack nstack = stack.copy();
		int meta = stack.getMetadata();
		boolean done = false;
		for(int i = 0; i < max && !done; ++i) {
//			nstack = target.insertItem(i, nstack, false);
//			done = nstack == null || nstack.isEmpty();
			ItemStack test = target.getStackInSlot(i);
			if(test != null && !test.isEmpty()) {
				Item titem = test.getItem();
				int tmeta = test.getMetadata();
				if(test.getMaxStackSize() > test.getCount() && titem == nstack.getItem() && meta == tmeta) {
					int count = Math.min(test.getCount() + nstack.getCount(), test.getMaxStackSize()) - test.getCount();
					ItemStack place = test.copy();
					place.grow(count);
					target.setStackInSlot(i, place);
					System.out.println("Put item into place. " + Integer.toString(i) + " " + place.getItem());
					if(count >= nstack.getCount()) {
						nstack = ItemStack.EMPTY;
						done = true;
					} else nstack.shrink(count);
				}
			} else {
				target.setStackInSlot(i, nstack);
				nstack = ItemStack.EMPTY;
				done = true;
			}
		}
		return nstack;
	}

 

In what way would one accomplish transferring items from one block to another using this system?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
    • Maybe you need to create file in assets/<modid>/items/<itemname>.json with content like this:   { "model": { "type": "minecraft:model", "model": "modname:item/itemname" } }  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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