#Allow specific item only in hotbar 0-3

9 messages · Page 1 of 1 (latest)

violet cloak
#

📢 Hello,
I'm trying to only allow items from "tacz:modern_kinetic_gun" in my hotbars 0 to 3.
I don't want players to be able to have weapons elsewhere. 🥳

PlayerEvents.inventoryChanged(event => {
    const player = event.player;
    const inventory = player.inventory;
    
    // Vérifier tous les slots de l'inventaire
    // Slots 0-8 = hotbar, slots 9-35 = inventaire principal
    for (let slot = 0; slot < 36; slot++) {
        const item = inventory.getStackInSlot(slot);
        
        // Vérifier si l'item est une arme TACZ
        if (item.id === 'tacz:modern_kinetic_gun') {
            // Si l'item est dans la hotbar (slots 0-8)
            if (slot <= 8) {
                // Si l'item est dans les slots 4-8 de la hotbar (slots interdits)
                if (slot >= 4) {
                    // Retirer l'item du slot actuel
                    inventory.setStackInSlot(slot, Item.EMPTY);
                    
                    // Essayer de placer l'item dans un slot valide (0-3 de la hotbar)
                    let placed = false;
                    for (let validSlot = 0; validSlot < 4; validSlot++) {
                        const targetItem = inventory.getStackInSlot(validSlot);
                        
                        if (targetItem.isEmpty()) {
                            inventory.setStackInSlot(validSlot, item);
                            placed = true;
                            break;
                        }
                    }
                    
                    // Si aucun slot valide n'est disponible, drop l'item
                    if (!placed) {
                        player.drop(item, false);
                        player.tell(Text.red('Les armes TACZ ne peuvent être placées que dans les slots 1-4 de la hotbar !'));
                    }
                }
            }

I tried this to at least detect the weapons, but I'm not sure I'm approaching the problem the right way. 🤯

hexed ridgeBOT
#

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

violet cloak
#

No, actually lol 🥹 , it's more about knowing which event I should use.

Because I had started a few things with PlayerEvents.inventoryChanged(event)
But I'm thinking that with 40 players, if it has to check for every change, it could overload the server, right? 👀

I'm just trying to figure out which method I should use (I don't want a complete script).😇

sullen needle
drifting pantherBOT
#

[➤](#1390206886275448953 message)
I have tried to modify the item in PlayerEvents.inventoryChanged, and I realize that the event.item is actually not the item being added to players inventory. And the index of event.slot also changes when player opens other menu. This small script is meant to get the actual item and the actual slot index of the event. I think this might be useful for somedody else so I post it here.

const /**@type {Map<number, Internal.AbstractContainerMenu>} */ playerMenuMap = new Map()
PlayerEvents.inventoryOpened(event => {
    const {player, inventoryContainer} = event
    playerMenuMap.set(player.uuid.hashCode(), inventoryContainer)
})
PlayerEvents.inventoryClosed(event => {
    playerMenuMap.delete(event.player.uuid.hashCode())
})
PlayerEvents.inventoryChanged("iron_ingot", event => {
    console.log("inventory changed")
    const {player, slot, server} = event
    const inventorySlot = slot == 45? 40: slot < 9? 44 - slot: slot > 35 ? slot - 36 : slot
    const playerMenu = playerMenuMap.get(player.uuid.hashCode())
    const item = playerMenu? playerMenu.getSlot(slot).item: player.inventory.asContainer().getItem(inventorySlot)//the event.item is actually not the item in the inventory
    const getRealSlot = (item => Array.from(Array(player.inventory.slots).keys()).map(index => {if(player.inventory.getItem(index).equals(item)) return index}).filter(i => i !== undefined)[0])
    item.setHoverName(Text.of(player.name).append(Text.of("`s ")).append(Text.translatable(item.item.descriptionId).gold()).italic(false))
})
violet cloak
#

Thanks, I'll look into that.
But in concrete terms, the player will pick up the item in any case, and it will check and reject it if there's no space? We can't detect the item when the player passes over it, right?

sullen needle
#

what do you mean?

violet cloak
#

To check if the item has an available slot among the 3 hotbars, you must go through the inventory.