Started by
SleepyTurtle
on
Topic category: Help with Minecraft modding (Java Edition)
Just as the tittle says i need help with a hopper like block it works fine when its one item at a time but as soon as i drop a full stack it deletes it and adds only one any help would be appreciated :)
in case the link doesn't work here's another one
Your code looks good; the problem is that getting a copy of an itemstack doesn't preserve the number of items in that stack. You gotta add an additional number variable that gets the number of items in the stack, and add that many to the stack inside the block. (With an additional edge case for if only part of the stack will fit inside.)
Also might be easier to use an entity iterator to get the items, instead of the nearest itemstack. With an entity iterator, you can target every entity in a specified box. (So you could only target stuff directly above the block.) Anything that runs on the 'entity iterator' as opposed to an event/target entity will then individually run on every entity inside that box. But yeah, otherwise looks good!
I honestly have no idea how to add it to my current procedure I'm still pretty new but thank u for helping:)
You would want to add a local number variable, (I'll call it 'stack_size'.) When you get the copy of the itemstack, also set 'stack_size' to the number of items in the itemstack. (The entity, converted into an itemstack.) Then when you increase the number of items in the block, instead of increasing it by one, increase it by 'stack_size.'
But yeah, for just starting out, that's a decently polished procedure. Best of luck!
Thank u so much!!
Ok so I have it working now but I have no clue how to do the else case for when only half of it fits in the slot if u could point me in the right direction that would be very appreciated
It might be worth making a separate procedure to sort of simplify this- something like 'add itemstack to hopper,' so you can deal with the item transfer and item collection behaviors separately. But it could work either way.
If you're already checking which slot to put the items in, you should be able to get the current number of items from that slot, and check if the number of items in the entity-converted-to-itemstack plus the current number is greater than 64. (...or if you wanted to be more general, greater than the max stack size of whatever's in that slot.) If it is, you would subtract the number of items in the slot from sixty four, and subtract the result from the entity-converted-to-itemstack, set a full stack in the current slot, then either run the procedure again for the next slot, or stop if there's no more room available. (You could also, say, run a repeat or a while function that subtracts one from the entity-converted-to-itemstack and adds 1 to the block, so you know exactly when to switch, and repeat it until the block is full or the itemstack is gone.)
...But anyways, there's lots of good ways to do this. Best of luck, hope that helps!
Ok sounds complicated but I think I get it thanks and sorry for asking so many questions
alright so i have another problem whenever i subtract the slot items with 64 then adding that to the dropped item when i add it to the block its not a full stack rather its missing one
for example i have 1 item in the block and a stack on the floor instead of being 64 and 1 item its only 63 when adding it to the block
i know i did something wrong somewhere but its been 5 hours and im still stuck maybe u can help if not thats ok you've helped enough already
code: https://imgur.com/a/tKCHE6U
oops send the wrong picture here's the new right one
https://imgur.com/a/Ya5NZBj
I'm not sure if the number of items math is right, order of operations is important to determine the extra items it should be. Should be something like:
items being targeted - (64 - items already in slot)
which will give you the extra items you need to move to the next slot, not the number of items you need to put in the current slot. You need to put 64 items in the current slot, and the result of that equation in the next slot if space is available.
It's gonna be kind of tricky to save items for the next slot the way the procedure's written. It might be easier to use a while loop. The idea is that the while loop would subtract one from the itemstack, add one to the correct slot of the block, advance to the next slot if the block is full, and stop once the targeted itemstack is gone or the block is full. (It would do the same thing, but maybe be a bit easier to follow.)
Anyways, good luck!
Ok so It works sort of..so let's say there's 1 item in the first slot and 64 on the ground instead of adding that to the existing item it adds it to the second slot but if I drop a stack that its not over 64 it adds it to the first slot that has 1 item in it so kinda works but in a weird way im definitely not complaining but im curious did i do something backwards at some point?
forgot to add the picture of the code
https://imgur.com/a/10gfExc
if anyone wants to improve on it or add it to their mod go for it :)
ok so new problem for items like swords that are not stackable what would i do for that cause currently it just adds them up like any other item and for example a sword that has an enchantment looses it because its added to the sword stack any way to fix that?
I meant more like use a single while loop to both add items and move to the next slot, in single item increments. That way you can specifically take each item into account instead of doing complicated math and nested loops. It would be something like:
...That should be pretty much all you need. And has the added benefit of dealing with pesky non-stackable items like swords.