Untitled

Anonymous
plain_text
04/30/2026 9:34 PM
1.9 KB
9
Indexable
/**
 * Processes a give command string
 * @param {string} input - e.g., "/give Alex diamond 64"
 * @param {Array} players - List of active player names
 * @param {Object} inventories - Object containing player inventories
 */
function handleGiveCommand(input, players, inventories) {
    // 1. Split the string by spaces
    const args = input.split(' ');

    // 2. Check if it's the right command
    if (args[0] !== '/give') return "Error: Command must start with /give";

    // 3. Extract parameters
    const [_, targetPlayer, item, amountStr] = args;
    const amount = parseInt(amountStr);

    // 4. Validate inputs
    if (!targetPlayer || !item || isNaN(amount)) {
        return "Usage: /give [player] [item] [amount]";
    }

    if (!players.includes(targetPlayer)) {
        return `Error: Player '${targetPlayer}' not found.`;
    }

    // 5. Update Inventory logic
    if (!inventories[targetPlayer]) inventories[targetPlayer] = {};
    
    // Add the amount to existing item count or set to amount
    inventories[targetPlayer][item] = (inventories[targetPlayer][item] || 0) + amount;

    return `Success: Gave ${amount} ${item}(s) to ${targetPlayer}.`;
}

// --- Implementation Example ---
const currentPlayers = ["Steve", "Alex"];
const gameData = { "Steve": { "wood": 10 } };

console.log(handleGiveCommand("/give Steve iron_ingot 5", currentPlayers, gameData));
console.log(gameData.Steve); // { wood: 10, iron_ingot: 5 }
Editor is loading...
Leave a Comment