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

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

×
×
  • Create New...

Important Information

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