#detecting player moving item from their inventory into a chest

1 messages · Page 1 of 1 (latest)

wooden abyss
#

So I currently have a function that prevents someone from dropping an item. But what I was wondering if there's a way to detect when someone moves an item out of their inventory into another inventory like a chest, container, etc.

rancid isleBOT
#

<@&1201956957406109788>

Someone will come and help soon!

💬 While you wait, take this time to provide more context and details.

🙇 If nobody has answered you by <t:1750252060:t>, feel free to use the Summon Helpers button to ping our helper team.

✅ Once your question has been resolved (or you no longer need it), please click Resolve Question or run /resolve

thorn zinc
#

Well there is an inventory_changed advancement

#

I don't remember if it can detect an item leaving your inventory vs moving within it

wooden abyss
thorn zinc
#

You could also detect whether the item is in their inv at all using /clear

#

If there isn't a specific method of dropping the item that you want

wooden abyss
#

wouldn't that remove the item from the inventory if it was there though?

thorn zinc
#

/clear @s item 0 returns success if there was an item and fail if there wasnt, but clears 0 of the item

wooden abyss
#

ahhhhhhhh

#

that makes sense

#

I can specify NBT data of the item right?

lime falcon
#

what is your ultimate goal

#

is it one item only you are trying to detect, does it matter where that item is put or do you just wanna know if it was throw out of the player's inventory

#

thrown*

#

thrown as in moved since you said the player cant throw stuff

wooden abyss
# lime falcon is it one item only you are trying to detect, does it matter where that item is ...

It's an item that I find using nbt data because I need to detect whether or not it has an encantment. currently I have

execute as @e[type=item, nbt={Item:{components:{"minecraft:enchantments":{levels:{"enchf:gluttony":1}}}}}] at @s run data modify entity @s Owner set from entity @p UUID
execute as @e[type=item, nbt={Item:{components:{"minecraft:enchantments":{levels:{"enchf:gluttony":1}}}}}] run data modify entity @s PickupDelay set value 0

to prevent someone from dropping the item but well, right now they can just move it into a chest and bypass it.

lime falcon
#

you dont need nbt data to detect it by the way but sure if it works

#

let me use my brain cells

#

oh

crystal zodiac
#

I can only think of the following:

Advancement that triggers when opening chests and other inventory containers using the minecraft:default_block_use

have it "save" inventory somehow and each tick reset the inventory.

not a neat or nice method but I guess it could work. this is assuming you have more than 1 item to restrict, the /clear can work for some items.

lime falcon
#

yea kind of the easiest option, but do you want the item to get deleted in case it was put in a chest or just returned to the inventory

#

i'd probably make it so if i have a chest gui open, the item is immovable, as in you can't pick it up with your cursor or move it with shift right click by doing checks similar to the ones done in custom guis

#

i think there is a scoreboard that detects whether a chest is open or not

crystal zodiac
#

yeah and this is the easy summary, but you would have to check if the player is still in the chest or not to revoke it at some point.

lime falcon
#

or something in your nbt actually

lime falcon
crystal zodiac
#

scoreboard would have the same issue, since when do you stop checking (there is no close chest gui statistic or event is there?)

lime falcon
#

nbt/score that outputs 1 as long as you have the chest gui open

wooden abyss
lime falcon
#

i know there is one for furnace and other workshops

lime falcon
#

maybe someone could give a smarter solution in the meantime

wooden abyss
lime falcon
#

good luck 🍀

crystal zodiac
#

Unfortunately we do not have the bedrock component that would make this super easy

lime falcon
#

btw i forgot to say this, if your goal is to make the item soulbound, in other words, only one certain player can have it in his inventory, that'd make stuff far simpler

#

but you'd have to compromise by allowing the player to put it in a chest

#

in this case you'd detect two things: if the item is on ground, you will teleport it to it's owner, if the item is in a wrong player's inventory, you will give it back to it's owner

#

but for chests and barrels, yea it'll just stay there

wooden abyss
#

I don't know if soulbound is what I was looking for unfortunately

lime falcon
#

rip

wooden abyss
#

I assume I need to make another advancement based on movement, however the issue I think I see happening is if it's just based on a keypress wouldn't that mean they could press a key to remove the tag before they exit out of the block?

lime falcon
#

those dont trigger if you are in gui i believe

wooden abyss
#

ah ok, I can test it. what is the command to check for player movement?

lime falcon
#

subpredicate to check whether a key is pressed or not

#

you can check for all, if any is pressed you can do the rest

wooden abyss
#

I assume I'd need to use minecraft:tick for this advnamcenet then right?

lime falcon
#

yup

lime falcon
# wooden abyss I assume I'd need to use minecraft:tick for this advnamcenet then right?

you can base off something like this:

{
  "criteria": {
    "shmovin": {
      "trigger": "minecraft:tick",
      "conditions": {
        "player": {
          "type_specific": {
            "type": "minecraft:player",
            "input": {
              "forward": true
            }
          }
        }
      }
    },
    "shmovin1": {
      "trigger": "minecraft:tick",
      "conditions": {
        "player": {
          "type_specific": {
            "type": "minecraft:player",
            "input": {
              "backward": true
            }
          }
        }
      }
    }
  },
"shmovin3": {
     // .. 
  },
  "requirements": [
    [
      "shmovin",
      "shmovin1",
      ".... etc", 
    ]
  ]
}
#

the requirements part says "if any of those criterias are triggered grant the advancement"

#

and each criteria checks for a different key

#

there should be like 7 or 8 idk how many

wooden abyss
lime falcon
#

also tick

#

tick, conditions, "player", "type_specific": type player, and from there there is an advancements field i believe

#
{
  "criteria": {
    "e": {
      "trigger": "minecraft:tick",
      "conditions": {
        "player": {
          "type_specific": {
            "type": "minecraft:player",
            "advancements": {
              "adv path": true
            }
          }
        }
      }
    }
  }
wooden abyss
#

hell yeah i got it to work

#

now i just gotta get the functions working

wooden abyss
crystal zodiac
#

there is no command to prevent moving items in java

#

in your case, you would need a combination of the clear and give commands.

clear for the chest/hopper etc. that receives the item(s)
give to ensure the player still has it.

technically what you want is not easy to support in java.

wooden abyss
#

I'm probably doing this very wrong lol

#

rn since this happens every tick it just glitches out the game until the item dissapears

wooden abyss
#

I think i could use data because the item would be a block however I havent used data very frequently yet so I'm not sure how to do that

valid wyvern
#

what are you trying to do?

wooden abyss
# valid wyvern what are you trying to do?

make it so that if someone puts a specific item in the chest/shulker... etc... from their inventory it deletes the item from the chest and gives it back to the player

valid wyvern
#

interesting

#

so what have u got so far?

wooden abyss
valid wyvern
#

you're on 1.21.5?

wooden abyss
#

.4

#

though i'll be putting it on 1.21.5 too after

valid wyvern
#

wait

wooden abyss
#

oh I also have two functions which control whether or not the loop is running and to control resetting the enchantments

valid wyvern
#

youre the summon-allies-with-a-goat-horn girlie

wooden abyss
#

ye

#

I finished that, just waiting for modrinth to review it, I started workin on a new thing

valid wyvern
#

resetting the enchantments?

#

and what loop? q-q

wooden abyss
#

rn I'm making an enchantment called gluttony

#

that u put on food

#

uh, it's a bit evil lol

valid wyvern
#

okay well what do you mean by resetting the enchantments?

#

and what does your loop do?

wooden abyss
#

oh sorry i mistpoke I meant achievements

#

advancements*

#

So for reference these are the advancements and functions that control whether or not the loop should toggle on/off based on whether or not someone is opening a chest

#

The loop is supposed to check to see if the food item that is enchanted with "gluttony" is put in a nearby chest or other item that accepts storage, and then delete that item, and put it in their inventory for as long as "gluttony_punish2true" achievement is granted to the player

#

I think I can use data get I'm just not very familiar with the data function yet

valid wyvern
#

are you sure your loops work as intended?

wooden abyss
#

everything else does tho from what I can test

#

it triggers/disables the loop fine it seems

valid wyvern
#

but like- i'm not sure if a loop is what you need here

#

a loop runs a function multiple times in the same tick for as long as a condition is met

wooden abyss
#

ye

valid wyvern
#

are you intending to run your function every tick for as long as the player has that container open?

wooden abyss
#

from what I was talkin about with some of the other ppl here I think that's the only way to get around some of the limitation of commands

valid wyvern
#

okay could you show me punish2loop

valid wyvern
wooden abyss
valid wyvern
#

yeah so basically

#

this will run it like a lot of times

#

in one tick

#

you want it just once every tick as someone who has a container open

wooden abyss
#

ohhhh

valid wyvern
#

what i recommend is giving players who have a container open a tag and running your detect function as every player with that tag

#

so wait are u using that one advancement with inputs to reset the loop?

wooden abyss
valid wyvern
#

no i mean like a normal tag you give with tag @s add ...

wooden abyss
#

oh ok

wooden abyss
valid wyvern
#

so in the true function just run tag @s add gluttony_punishing or something

#

and in the false just tag @s remove gluttony_punishing

wooden abyss
#

and then I add the clear/give commands in the tick function right?

valid wyvern
#

yeah hold on tho im thinking how you would detect it

wooden abyss
#

yeah thats the main thing that stumped me

valid wyvern
#

okay sooo basically the inventory_changed advancement does actually detect removing of items

valid wyvern
#

maybe we could use that

valid wyvern
#

but not when you specify it apparently 😭

wooden abyss
#

yeah the items condition I think on the wiki says it only talks about items that were added

#

I think you can use the data command to get info about the chest since the chest is placed on the ground I'm just not familiar enough with the data command yet to make it work

valid wyvern
#

yeah thats a good idea i just wouldnt know how to select the chest

#

i feel like raycasting your way to the chest is kinda overkill here so im tinkering with something else lol

wooden abyss
#

fair fair

valid wyvern
#

okay f it lets raycast

wooden abyss
#

i will be learning something new then 😄

#

is exciting

valid wyvern
#

yeah same here i've only thought about this never done it before

#

but its not that hard, so i think first you'll need a simple inventory_changed advancement that triggers another function without condition

#

actually i have no clue how you could possibly do this without it breaking

#

bc you can't remove one item from a chest

#

i think

#

only a specific type of item

#

acutally

#

its not possible to get an item with gluttony into a container right?

#

like at all?

wooden abyss
valid wyvern
#

yes yes yes yes but i mean the thought behind it

#

like

#

its not intended to happen

wooden abyss
#

well either you can't put it into the container or you put it in the container and then that's detected and it's removed from the container and you get given the item

valid wyvern
#

yeah okay thats what we're gonna do

#

so actually you dont need that inventory_changed advancement

wooden abyss
#

ok

valid wyvern
#

just make a function called raycast

#

and another one called raycast_success or something

#

oh actually first you should make a block tag for all your containers

#

so in namespace/tags/block

#
    "values": [
        "chest",
        "barrel"
    ]
}```

in there just place this json file name it like `container.json` or something and add all your containers
wooden abyss
#

yup, did that

valid wyvern
#

okay great!

#

now back to the raycast function

#

the raycast function should just be

execute unless block ~ ~ ~ #(your container tag) positioned ^ ^ ^1 run return run function (raycast)
function (raycast success)```
#

should be it

#

this will find the container and run the raycast success function at its location

#

thats basically all the raycast does

wooden abyss
#

pog

valid wyvern
#

looks about right

#

yes okay i got it

#

so in your raycast_success function
execute unless data block ~ ~ ~ Items[].components."minecraft:enchantments".levels."enchf:gluttony" run return fail

#

thats your first line and basically it should only continue the function if it detects an item with that enchantment in the container

wooden abyss
#

that makes sense ye

valid wyvern
#

so afterwards i thought this would be easy, cause i thought you could simply remove it from the chest and give it to the player, but this doesnt take the item count into account yk?

#

so if they placed 2 of that something then they would only get one back

wooden abyss
#

oh I didn't think about that

valid wyvern
#

this is still possible by storing the count and running a macro with the slots data thats like $give @s $(id) $(count)

#

actually no

#

you'd probs want to use item replace from

#

so that you copy the item with all its data

#

you'd still need a macro to get the slot that its in and do something line container.$(slot) but its doable

wooden abyss
valid wyvern
#

one problem tho, i'm kind of in a hurry and i kind of need to go

wooden abyss
#

Because otherwise, someone might for instance put a bunch of them in a dropper and fill the inventory with it to grief

#

ah no worries!

valid wyvern
valid wyvern
#

i'll be back later, you can try and tinker it yourself a little

wooden abyss
#

sounds good 🙂

valid wyvern
#

i'll just ping you when im back is that fine?

wooden abyss
#

sounds good!

valid wyvern
#

okay see ya

valid wyvern
#

@wooden abyss

wooden abyss
#

hello again 🙂

valid wyvern
#

hi!!1!111!!!

#

so

wooden abyss
#

I was tryina get it to work, and I think I got some of it working, rn I have this in my tick function

#

and then in success I have this

valid wyvern
#

right before run

#

very much needed for the raycast

wooden abyss
valid wyvern
#

oh and at @s is also important after as @a[...]

#

so basically normally running a command at the players position makes it run at their feet bc thats their actual position in the game

#

all we're doing is moving it to their eyes

wooden abyss
#

ahhh good to know

valid wyvern
#

so that when going forwards with ^ ^ ^1 in the raycast it will actually go to the block we're looking at and not one under it

wooden abyss
#

So it should be this?

valid wyvern
#

yeah just place at @s before anchored eyes

wooden abyss
#

sounds good!

valid wyvern
#

okay now for the raycast, i thought about it and the simplest way to do it is to summon an item at the player and copy all its data from the item in the container

#

we can't use give bc theres no way to copy data with give, it has different syntax

wooden abyss
#

ye that's fair

valid wyvern
#

and with the item command, we need to specify the exact slot that the item should go in, which we cant know

wooden abyss
#

ohh yeah so summon fixes that b y just letting the player grab it

valid wyvern
#

yup!

#

so now for your raycast_success

#

you've got

#

execute unless data block ~ ~ ~ Items[].components."minecraft:enchantments".levels."enchf:gluttony" run return fail

#

so this first line will just not even continue the code if the chest doesnt have an item with "enchf:gluttony"

#

so then we'll just want to summon the item at the player (remember the context rn is as player at chest so we need to specify at @s)

wooden abyss
valid wyvern
#

yeah bc enchanted books dont use "minecraft:enchantments", they use "minecraft:stored_enchantments" or something like that

wooden abyss
#

Ah ok ok

valid wyvern
#

so it shouldnt do anything with those

#

now

#

execute at @s run summon item ~ ~ ~ {Item:{id:"emerald"},Tags:["temporary_item"]}

#

is your second line

#

emerald is obv just a placeholder, bc ive noticed the item doesnt even spawn if you dont specify an id

wooden abyss
#

fair fair

valid wyvern
#

now this is the part where the game is kinda dumb

#
``` will modify the temporary item's Item value FROM the chest's item that has the enchf:gluttony lvl 1 enchantment
#

basically its just not possible to select the item that has the enchantment, the level needs to be specified

#

does your enchantment have multiple levels?

wooden abyss
valid wyvern
wooden abyss
#

that's all good lol

#

I had to do that in tick for preventing the item drop

valid wyvern
#

bc you cant specify a wildcard like "enchf:gluttony":*

wooden abyss
valid wyvern
#

ikr its weird

#

anyway

#

so now we've copied the item from the container to the spawned item

#

all we need to do is delete it from the container

#

data remove block ~ ~ ~ Items[{components:{"minecraft:enchantments":{levels:{"minecraft:sharpness":1}}}}]

#

oops i used sharpness for testing

#

just replace that with your enchantment

#

and ofc here again you'll have to have 5 lines for all 5 levels

wooden abyss
#

and I opened up the anvil because you can store items in there and I needed to include things

#

it currently gives me infinite emeralds haha

valid wyvern
#

oh thats bc removing it doesnt work

#

wait but then why is it detecting it

#

hold on

wooden abyss
#

oh wait

#

so I have one in my inventory I forgor about

#

but it's in my inventory not the chest

#

if I put it in the chest I start to get infinite cod

valid wyvern
#

uhm i think the removal line is wrong

wooden abyss
#

but yeah with the anvil it just adds infinite emerald

wooden abyss
#

yeah, unless i did it wrong

#

OH

valid wyvern
#

yeah

wooden abyss
#

oh god lol

valid wyvern
#

lmao

#

thats my bad for leaving in sharpness

#

anyway for your anvil problem i dont think its gonna work like at all

#

bc there is physically no way to detect items in anvils, crafting tables, etc

#

bc they dont actually have an inventory

#

theyre not even block entities so they dont even have data

wooden abyss
#

ah i see

#

I can remove them from the lists which is probably wahts causing the issue

valid wyvern
#

yeah

#

but what issue are you currently experiencing?

#

wait lemme go grab some strawberry cake

wooden abyss
#

all gud, actually ur stuff works just fine I just had blocks in the tag I made for storage items like the crafting table which I didn't need, it's only if I try and do stuff with those that the glitch happens

valid wyvern
#

yeah cause if you try to do data get block on like a crafting table, it just says its not a block entity

wooden abyss
#

ahhh yeah fair

valid wyvern
#

anything you can move with a piston isnt a block entity

wooden abyss
#

ok this should be fine now

#

but there's 1 other glitch

#

this one might be difficult

#

so u know how basically the advancement to remove the tag involves pressing a movmeent key right?

#

So if I don't press any movement key after removing the inventory, and look away from the block, I will get infinite emeralds

valid wyvern
# wooden abyss

-# btw you can just use "#shulker_boxes" for shulker boxes

wooden abyss
valid wyvern
#

hm i mean the input method is clever but it really sucks bc you can just close the container and not move and the code will still think that you're in the gui

wooden abyss
#

yeahhh

#

wait

valid wyvern
#

i actually think ik what to do

wooden abyss
#

man if only you could do functions in json

#

could make anotehr raycast

#

which detects if you aren't looking at the block and removes the tag

valid wyvern
#

thats exactly what i thought of!

wooden abyss
#

wait, maybe you can just do that in the function

#

ohhh yeah

valid wyvern
#

yeah we can just use our raycast

#

no need for a new one

#

so basically if you're not looking at a chest at all, the raycast will just hit the chain command block limit (which is actually a gamerule you can set) and apparently if it does at the end of the raycast when the limit is hit it ignores the "return" command

#

we know this bc its running your raycast_success

#

so basically if we just added execute unless block ~ ~ ~ #(your tag) run tag @s remove (your tag)

#

before the function raycast_success

#

it should work

valid wyvern
#

cant guarantee this will work tho bc functions get a little quirky when reaching the chain limit concern

wooden abyss
#

pog, also I need to relaunch game caused I accidnetly closed it out instead of going to title :c

wooden abyss
valid wyvern
#

oh wait im dumb

#

ofc it doesnt work

#

just try to add execute if block ~ ~ ~ #(your tag) run before the raycast_success

#

we need to actually not run the success if it didnt find a chest

#

(removing the tag from the player will not stop the function midway through)

wooden abyss
#

Unfortunately now I can just put the thing in the inventory it seems

#

Removing the tag before success I think is preventing the function in tick from calling the raycast

valid wyvern
#

huh

#

can you show me your raycast function real quick

wooden abyss
#

ah maybe i misunderstood what you wanted me to do

valid wyvern
#

no like

#

2nd line replace if with unless

wooden abyss
#

oh I understand now, the execute command to runthe function

#

or the execute line before the function

valid wyvern
#

to run the function

#

just show me again before u test

wooden abyss
valid wyvern
#

yeah looks about right

wooden abyss
#

it works 😄

valid wyvern
#

thats great!

#

i actually

#

dont think

#

you need the remove advancement and its function anymore

#

actually

#

keep it in

#

even tho it really doesnt matter that much, it will help performance

#

because we want to try to limit the time the raycast is running by as much as possible, so we want to have as many possibilities to cancel it

wooden abyss
#

Quick question, this is more of a balancing thing, do you think it would be less cancer if I prevent this from happening until a player eats it?

valid wyvern
#

PAHADHAHDAH

#

im not sure what it does

#

explain

#

the enchantment

wooden abyss
valid wyvern
#

OH MY GOD

wooden abyss
#

oh it gets worse

valid wyvern
#

oh god

wooden abyss
#

Im a bit evil lol

valid wyvern
# wooden abyss

one thing that i noticed lmao, this will currently run if the player eats ANYTHING while having an item with gluttony

#

is that intended?-

wooden abyss
valid wyvern
#

OH-

wooden abyss
#

Oh yes xd

#

Now, so long as you don't have keep inventory on, it's not a big deal because you can just /kill or whatever and get rid of it

#

however, if keepinventory is on...

valid wyvern
#

thats insane

wooden abyss
#

except for the not being able to drop it normally part to makes ure someone can't just run up and giv eother people it

#

they'd have to use something like a dropper and trick someone

#

thanks for helping me get this to work though 😄

valid wyvern
#

lol amazing!

#

happy to help!

rancid isleBOT
#
Question Closed

Your question, #1384874705332473967 (detecting player moving item from their inventory into a chest), was resolved!

Original Message

#1384874705332473967 message

Duration open

8h13m

wooden abyss
valid wyvern
#

yeah, i can imagine, i think they changed the enchantments component

#

should be an easy fix tho

wooden abyss
valid wyvern
#

or at least i thought it has

#

what exactly happens?

#

does simply nothing happen?

wooden abyss
valid wyvern
#

oh i've got it

#

yes they did change something

#

they removed the "levels" key

#

so instead of Items[].components."minecraft:enchantments".levels."enchf:gluttony its just Items[].components."minecraft:enchantments"."enchf:gluttony

#

you'll just have to remove any levels. and levels:{}

wooden abyss
#

ahhh ok

#

There's 1 more bug

#

it's like the infinite emerald bug except now with infinite cursed food

valid wyvern
#

infinite cursed food??

#

are you sure you've changed the last line of raycast_success?

wooden abyss
#

it works now 🙂

valid wyvern
#

great!

#

also, could i get ur modrinth?

wooden abyss
valid wyvern
#

thx! <3

wooden abyss
#

goat horn summoning still under review atm :c

#

oh wait did they release 1.21.6 now? its on modrinth

valid wyvern
#

lmao yeah one time one of my projects got unlisted bc they thought i didnt ask for permission before using someones assets

wooden abyss
#

oooh this means i will have to do updating ree

valid wyvern
#

like officially 2 days ago but its taking a little for all the platforms

valid wyvern
#

just the cool new dialog feature thats gonna be a blessing for my chemistry datapack

wooden abyss
valid wyvern
#

OMG WAIT WHAT I JUST READ ONE OF THE CHANGES IS LIKE BLOCK MODELS ARE NO LONGER LIMITED TO MULTIPLES OF 22.5 DEGREES

#

okay i need to calm down

#

this is big for me lol

#

but yeah you probs wont have to struggle all too much for updating to 1.21.6

wooden abyss
#

omg there's a /datapack command that's amazing

valid wyvern
#

yeah i saw thatttt

wooden abyss
#

yeah i read thru stuff it doens't look like anything ive made will break that's nice

valid wyvern
#

yeah theres this guy conure u probs know him

wooden abyss
#

oh yeah

#

his was the very first tutorial vid I watched

valid wyvern
#

he makes like "Why Your Datapack Broke in _" videos

#

i watched this and it wasnt that bad lol

valid wyvern
#

and also took some genius ideas from CloudWolf

rancid isleBOT
#
Question Closed

Your question, #1384874705332473967 (detecting player moving item from their inventory into a chest), was resolved!

Original Message

#1384874705332473967 message

Duration open

8h56m

gloomy aspen