#How can I prevent players from clicking on or dropping items in their inventory?

12 messages · Page 1 of 1 (latest)

haughty spoke
#

.

pastel steepleBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

late acorn
#

May you elaborate more clearly?

urban acornBOT
#

The XY problem is when you really want to do X, and you think that Y can achieve X. However, you can't get Y to work, and so ask for help exclusively about Y.

This can lead to a lot of confusion from the people trying to help you, as Y can seem like a very random thing to want to do, and a lot of the time isn't the best way to achieve X anyway.

Its fine to ask about Y, just always include some context about X so you can be put on the right track if Y won't do X well, or there is an easier way to do X.

haughty spoke
late acorn
# haughty spoke This is inventory for mini-game server. I want to prevent players to drop or mov...
ItemEvents.dropped(event=>{
    event.itemEntity.setNoPickUpDelay()
})

This prevent from dropping

let lockedItemSlot = {0: Item.of("diamond", {notTakeAway: "notTakeAway"})}
PlayerEvents.inventoryChanged(event=>{
    let inventory = event.player.inventory
    Object.keys(lockedItemSlot).forEach(k => {
            let playerSlotItem = inventory.getItem(k)
            console.log(playerSlotItem == lockedItemSlot[k])
        })    
})

This detect if specific slot has specific item.

Maybe combine these with some other mechanism can achieve what you want.

haughty spoke
#

Thx! May I set all numbers (0~35) If I want to lock every slot?

late acorn
#

Maybe you can add punishing mechanism for players who move the slots to make them obey the rules of the mini-game. IDK

#

Here's another similar script I wrote. It makes you drop anything that is not the specific item or put it to another slot. You may try it and see. Maybe you'll come up with better ideas.
#1360174100001194104 message

urban acornBOT
#

[➤](#1360174100001194104 message)

const slotLocked = 10
const allowedInLockedSlot = "minecraft:diamond"

PlayerEvents.inventoryChanged(event=>{
    let player = event.player
    let inventory = player.inventory
    let inventorySize = inventory.slots
    let freeSlots = []
    for(let i=0; i<inventorySize; i++){
        if(inventory.getItem(i).id == "minecraft:air" && i != slotLocked) freeSlots.push(i)
    }
    console.log(freeSlots)
    let itemstackToMove
    if(inventory.getItem(slotLocked).id != allowedInLockedSlot){
        itemstackToMove = inventory.getItem(slotLocked)
        while(freeSlots.length) {
            let freeSlot = freeSlots.shift()
            inventory.setItem(slotLocked, "minecraft:air")
            inventory.setItem(freeSlot, itemstackToMove)
            if(!inventory.armor.contains(itemstackToMove)) break
        }
        if(!freeSlots.length){
            player.drop(itemstackToMove, false)
            inventory.setItem(slotLocked, "minecraft:air")
        }
    }
})

I think you don't have to mess with pick up event. Just inventoryChanged should be enough.

late acorn
# haughty spoke Thx! May I set all numbers (0~35) If I want to lock every slot?

I don't know if you still need this. But I figured out how to actually lock their inventory. Need to be used with the script above that prevents dropping item.

const slotsLocked = {10: "minecraft:diamond", 12: "minecraft:emerald"}

PlayerEvents.tick(event=>{
    let player = event.player
    let inventory = player.inventory
    let inventorySize = inventory.getSlots()
    
    Object.keys(slotsLocked).forEach(slot => {
        let itemstackToMove = inventory.getItem(slot)
        let itemToPut = Item.of(slotsLocked[slot])
        itemToPut.nbt = itemToPut.nbt??{}
        itemToPut.nbt.DoNotTakeAway = 1
        if(itemstackToMove != itemToPut){
            player.block.popItem(itemstackToMove)
        }
        inventory.setItem(slot, itemToPut)
    })

    for(let i = 0; i < inventorySize; i++){
        if(Object.keys(slotsLocked).find(k => k==i)) continue
        if(inventory.getItem(i).nbt?.DoNotTakeAway == 1) inventory.setItem(i, "minecraft:air")
    }
})