#custom command (!sellall)

1 messages · Page 1 of 1 (latest)

silent agate
#
import { world, system } from "@minecraft/server";

const sellPrices = {
    "minecraft:diamond": 5,
    "minecraft:emerald": 3,
    "minecraft:raw_gold": 2,
    "minecraft:raw_iron": 1,
    "minecraft:raw_copper": 1,
    "minecraft:coal": 1,
    "minecraft:lapis_lazuli": 1,
    "minecraft:redstone": 1,
    "minecraft:gold_nugget": 1
};

world.beforeEvents.chatSend.subscribe((event) => {
    const message = event.message.trim().toLowerCase();
    const sender = event.sender;
    if (message === "!sellall") {
        event.cancel = true;
        const invComp = sender.getComponent("inventory");
        if (!invComp) return;

        const container = invComp.container;
        let totalMoney = 0;

        for (let i = 0; i < container.size; i++) {
            const item = container.getItem(i);
            if (!item) continue;
            const price = sellPrices[item.typeId];
            if (price) {
                totalMoney += price * item.amount;
                container.setItem(i, undefined);
            }
        }

        if (totalMoney > 0) {
            system.run(() => {
                sender.runCommandAsync(`scoreboard players add "${sender.name}" money ${totalMoney}`);
                sender.sendMessage(`§ayou sell all your ores for${totalMoney} money !`);
            });
        } else {
            sender.sendMessage("§cNothing to sell !");
        }
    }
})
#

i have this errors

#

im using @minecraft/sever 1.18.0 / 1.19.0

#
    {
      "module_name": "@minecraft/server",
      "version": "1.19.0"
    }
  ],```
glossy arch
#

chatSend is beta. You need to enable Beta API and use 2.0.0-beta for @minecraft/server.

iron stone
#

and also if you use 2.0.0-beta you might as well use a custom command

glossy arch
#

Personal preference.