#Custom Shop with Player UI

1 messages · Page 1 of 1 (latest)

nova widget
#

I made several shops that have buttons and the shops work. I want to make a shop that can tell what is in a players inventory and generate options based on the items/blocks available. For instance a sell shop could detect how many stacks of cobblestone are in a players inventory.
I have some code but it has errors.

#

const soundOption = {
pitch: 0.9,
volume: 1.0
};

// Subscribe to the playerInteract event
world.afterEvents.PlayerInteractEntity.subscribe(data => {
const { player, entity } = data;

if (entity?.hasTag('testSell')) {
    openCustomShop(player);
    //This variable pulls the tags from the entity that is being interacted with
    const entityTags = entity.getTags();

    //If the target entity does not have a tag from the shopTypes set, return/cancel
    if (entity?.has(entityTags)) {
        console.warn(`${entityTags}`);
        return;

    };

    function openCustomShop(player) {
        const inventory = Player.getComponent('minecraft:inventory').container;
        const blocks = {};

        for (let i = 0; i < inventory.size; i++) {
            const item = inventory.getItem(i);
            if (item) {
                const itemType = item.id;
                const itemCount = item.amount;
                if (!blocks[itemType]) {
                    blocks[itemType] = 0;
                }
                blocks[itemType] += itemCount;
            }
        }

        const customShopForm = new ActionFormData()
            .title('Custom Shop')
            .body('Select an item to purchase based on your inventory');

        for (const blockType in blocks) {
            if (blocks[blockType] > 0) {
                customShopForm.button(`${blockType}\nQuantity: ${blocks[blockType]}`);
            }
        }

        customShopForm.show(player).then((response) => {
            if (response.canceled) {
                return;
            }

            const selectedBlock = Object.keys(blocks)[response.selection];
            Player.runCommandAsync(`say You selected ${selectedBlock} with quantity ${blocks[selectedBlock]}`);
        });
    }
}

});

#

these are the resources that I imported at the beginning of the file import { world, Player, ItemStack } from '@minecraft/server';
import { ActionFormData, ActionFormResponse } from '@minecraft/server-ui';

shut brambleBOT
#
Formatting Your JSON Or JS Code

Please format your code as code-blocks! This makes the text monospaced, and has support for syntax highlighting.

JSON
```json
{
"example": 123
}
```

JavaScript
```js
console.log("Hello World");
```

The character used here is the backtick. This symbol is usually at the top left of your keyboard, occupying the tilde key (~). On mobile, it will be on the second or third page of symbols.

gleaming sail
#

Pretty decent for chat gpt

#

Suprised it would know what a after event is unless the person has some knowledge and gave some documentation to work off of

#

It does?

#

Oh damn

#

I’ve used it a little when Im stuck sometimes and if you lead it towards the right direction it can be helpful

#

Debugging n such. Good to have another opinion on something