#Detect who picked up an item

1 messages · Page 1 of 1 (latest)

wide cliff
#

This scripts is made to detect wich player picked up an item.

How does it work?

this uses the entityRemoved before event and checks if the entity is an item.
Right after it looks for players in a 1.5 radius area and checks their inventory before and after the item gets picked up.
When the player's inventory has a new item and matches the item that got picked it fires and warns the console that the item got picked up by that player.

https://github.com/Carchi777/detect-who-picked-up-an-item

import { world, system } from "@minecraft/server"

// GitHub:          https://github.com/Carchi777/detect-who-picked-up-an-item

world.beforeEvents.entityRemove.subscribe(e => {
    const { removedEntity: entity } = e;

    const item = entity.getComponent("item")?.itemStack

    if (!item) return

    const players = entity.dimension.getEntities(
        { maxDistance: 1.5, location: entity.location, type: "player" }
    )

    players.forEach(player => {
        const inv = player.getComponent('inventory').container
        const items = Array
            .from({ length: inv.size }, (_, i) => inv.getItem(i))
            .filter(k => k != null);

        system.run(() => {
            const valid = Array
                .from({ length: inv.size }, (_, i) => inv.getItem(i))
                .filter(k => k != null)
                .some((k, i) => k.typeId === item.typeId && k.amount != items[i]?.amount);

            if (valid) {
                world.sendMessage(
                    `§i${player.nameTag} +${item.amount} ${item.typeId.slice(10)}`
                )
            }
        })
    })
})
GitHub

This scripts detects when the player picks up an item - Carchi777/detect-who-picked-up-an-item

gleaming remnant
minor stone
#

The purpose of system.run() is he's comparing the old inventory to the new inventory.