#Player Interact Bug

1 messages · Page 1 of 1 (latest)

junior kayak
#

Hi, I'm currently experiencing this bug in my sign shop project. The shop works fine, it uses transferItem to transfer items via container.

The only weird thing is that it has to be interacted twice (one for buy, then second, for the item to appear in the player's inventory)

The first interaction already takes the item from the chest (and puts it in player inv), and I cannot use the slot where the item is supposed to be.

carmine kestrel
#

Oh my god it becomes louder

#

maybe your code runs more than once

junior kayak
carmine kestrel
#

then why does it become louder???

#

maybe just my imagination

junior kayak
#
if (currentSCC >= price) {
                    const sellerBalance = SCC.get(sellerName);
                    const playerContainer = player.getComponent('inventory').container;
                    for (let i = 1; i < shopContainer.size; i++) {
                        const item = shopContainer.getItem(i);

                        if (item?.typeId.slice('minecraft:'.length) === lines[2].slice(2)) {
                            const deductedSCC = currentSCC - price;
                            system.run(() => {
                                shopContainer.transferItem(i, playerContainer);
                                SCC.set(player.name, deductedSCC);
                                SCC.set(sellerName, sellerBalance + price);
                            });
                            break;
                        }

                        system.run(() => {
                            player.onScreenDisplay.setActionBar('§aPurchase Complete');
                            player.playSound('random.orb');
                        });
                    }
#

this is the code for that part

paper iris
#

ur code has issue

junior kayak
paper iris
#

what u want to do

junior kayak
#

You mentioned there was a fix for the interact spam right? I can't look it up anymore, post was deleted

carmine kestrel
#

bruh the system.run is inside the for loop

#

that explains why it's loud

junior kayak
#

as you can see here the dirt cannot be placed

#

This is the part where it actually puts the item in the inventory, just visually not there

carmine kestrel
#

just send the whole event code, It's hard to tell

junior kayak
# carmine kestrel just send the whole event code, It's hard to tell
function handleBuyShop(sign, player, blockLocation, blockFace) {
    const lines = sign.getText().split('\n');

    if (lines[1]?.includes('Buy')) {
        const sellerName = lines[0].slice(2);
        if (!buyFromOwnShop && sellerName === player.name) {
            system.run(() => {
                player.onScreenDisplay.setActionBar('§cYou cannot buy from your own shop');
                player.playSound('note.bass');
            });
        } else {
            let price = parseInt(lines[3].replace(' SCC', '').slice(2), 10);

            switch (blockFace) {
                case 'North':
                    blockLocation.z++;
                    break;
                case 'South':
                    blockLocation.z--;
                    break;
                case 'West':
                    blockLocation.x++;
                    break;
                case 'East':
                    blockLocation.x--;
                    break;
            }

            const shopBlock = world.getDimension('overworld').getBlock(blockLocation);
            const shopContainer = shopBlock?.getComponent('inventory')?.container;
            const currentSCC = SCC.get(player.name);

            if (shopContainer?.emptySlotsCount >= shopContainer?.size - 1) {
                system.run(() => {
                    player.onScreenDisplay.setActionBar('§cOut of stock');
                    player.playSound('note.bass');
                });
            } else {
                if (currentSCC >= price) {
                    const sellerBalance = SCC.get(sellerName);
                    let slotAmount = null;
                    const playerContainer = player.getComponent('inventory').container;
                    for (let i = 1; i < shopContainer.size; i++) {
                        const item = shopContainer.getItem(i);

                        if (item?.typeId.slice('minecraft:'.length) === lines[2].slice(2)) {
                            const deductedSCC = currentSCC - price;
                            // slotAmount = item.amount;
                            system.run(() => {
                                shopContainer.transferItem(i, playerContainer);
                                // shopContainer.setItem(i, undefined);
                                // player.runCommandAsync(`give @s ${lines[2].slice(2)} ${slotAmount}`);
                                SCC.set(player.name, deductedSCC);
                                SCC.set(sellerName, sellerBalance + price);
                            });
                            break;
                        }
                        system.run(() => {
                            player.onScreenDisplay.setActionBar('§aPurchase Complete');
                            player.playSound('random.orb');
                        });
                    }
                } else {
                    system.run(() => {
                        player.onScreenDisplay.setActionBar("§cYou don't have enough SCC");
                        player.playSound('note.bass');
                    });
                }
            }
        }
    }
}
#

This is the event

world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    const { block, player, itemStack, blockFace } = event;
    let blockLocation = block.location;

    if (block.typeId.includes('wall_sign')) {
        const sign = block.getComponent('minecraft:sign');
        const lines = sign.getText().split('\n');

        if (lines[0] === 'signshop' && itemStack !== undefined) {
            handleSignShopCreation(event, sign, player, itemStack, block);
        } else {
            handleBuyShop(sign, player, blockLocation, blockFace);
        }
    }
});
carmine kestrel
#

is beforeEvents necessary?

#

you don't seem to cancel anything

junior kayak
#

i tried doing afterEvents, the items only show up visually whenever the chest is out of stock

#

very strange

carmine kestrel
#

try setItem() then

#

just for a test

junior kayak
carmine kestrel
#

see if it rendered

junior kayak
#

sure i'll try

carmine kestrel
#

it could be a bug for transferItem()

#

also please put the system.run before the break statement. Since it'll spam that playsound for each empty slot the container has

junior kayak
#

vscode doesnt let me use the minecraft one

carmine kestrel
#

idk

carmine kestrel
# carmine kestrel also please put the `system.run` before the break statement. Since it'll spam th...
for (let i = 1; i < shopContainer.size; i++) {
    const item = shopContainer.getItem(i);
    
    if (item?.typeId.slice('minecraft:'.length) === lines[2].slice(2)) {
        const deductedSCC = currentSCC - price;
        // slotAmount = item.amount;
        system.run(() => {
            shopContainer.transferItem(i, playerContainer);
            // shopContainer.setItem(i, undefined);
            // player.runCommandAsync(`give @s ${lines[2].slice(2)} ${slotAmount}`);
            SCC.set(player.name, deductedSCC);
            SCC.set(sellerName, sellerBalance + price);
            player.onScreenDisplay.setActionBar('§aPurchase Complete');
            player.playSound('random.orb');
        });
        break;
    }
}
#

fix that loud ping ^

junior kayak
#

thanks

paper iris
#
let lastTick = new Map()

a.playerInteractWithBlock.subscribe(e => {
    let { player } = e
    let { id } = player

    let { currentTick } = system

    let tickDiff = currentTick - (lastTick.get(id) || 0)

    if (tickDiff < 2) return

    lastTick.set(id, currentTick)

    // code 
    world.sendMessage("interact")
})```
#

@junior kayak

unreal pond
#

Just like Wave made it here

calm flame
#

It’s just the transfer item. It doesn’t cause your inventory to update visually until you try to use an item. You can use add item and set item together instead to avoid the visual bug