Need Help with hopper like block

Started by SleepyTurtle on

Topic category: Help with modding (Java Edition)

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Need Help with hopper like block

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

https://imgur.com/a/dTie5nT

 

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Your code looks good; the…
Fri, 02/09/2024 - 12:12

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!

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I honestly have no idea how…
Fri, 02/09/2024 - 12:39

I honestly have no idea how to add it to my current procedure I'm still pretty new but thank u for helping:)

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You would want to add a…
Fri, 02/09/2024 - 12:43

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!

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thank u so much!!
Fri, 02/09/2024 - 12:47

Thank u so much!!

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Ok so I have it working now…
Fri, 02/09/2024 - 22:28

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 

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It might be worth making a…
Sat, 02/10/2024 - 00:02

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!

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Ok sounds complicated but I…
Sat, 02/10/2024 - 00:40

Ok sounds complicated but I think I get it thanks and sorry for asking so many questions 

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
alright so i have another…
Sat, 02/10/2024 - 07:14

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

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm not sure if the number…
Sat, 02/10/2024 - 15:17

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!

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Ok so It works sort of..so…
Sun, 02/11/2024 - 06:16

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? 

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
forgot to add the picture of…
Sun, 02/11/2024 - 06:18

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 :)

Last seen on 03:48, 16. Feb 2024
Joined Feb 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
ok so new problem for items…
Sun, 02/11/2024 - 06:48

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?

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I meant more like use a…
Mon, 02/12/2024 - 12:07

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:

  • For Each Entity in square cube of size 1 at x+0.5 y+1.5 z+0.5 as entity iterator...
    • Set local variable 'count' to 0
    • Set local variable 'saved_item' to the entity iterator converted to an itemstack.
    • Set local variable 'saved_size' to the # of items in the entity iterator converted to an itemstack.
    • While entity iterator is alive and count is less than or equal to 26... (Slot 27 uses id number 26 because it starts from 0)...
      • If the number of items in slot 'count' of the block equals the max stack size of the item in slot 'count', or the item in slot 'count' does not equal 'saved_item,' set count to itself plus one.
      • Else, subtract one from 'saved_size,' and add set the item in slot 'count' to 'saved items' with the current number of items in slot 'count' plus one. If 'saved_size' is less than or equal to one, despawn the entity iterator.

...That should be pretty much all you need. And has the added benefit of dealing with pesky non-stackable items like swords.