Inventory Copier

Code Block

Tags:

Command

Creator:

Jolly_EIf

Code:

// List of allowed users
const allowed = ["Player1", "Player2", "Player3"];

// Get the players username
const username = api.getEntityName(myId);

// Check permission
if (!allowed.includes(username)) {
    api.sendMessage(myId, "You do not have permission to use this.", { color: "red" });
} else {
// Replace this with the target player's name:
const targetName = "0mnivyx";
const targetId = api.getPlayerId(targetName);

if (!targetId || !api.playerIsInGame(targetId)) {
    api.sendMessage(myId, "Target not found or not in game!", { color: "red" });
} else {

    // Clear your slots 050
    for (let i = 0; i < 51; i++) {
        api.setItemSlot(myId, i, "Air", 0);
    }

    // Copy target slots
    for (let i = 0; i < 51; i++) {
        const slot = api.getItemSlot(targetId, i);

        if (!slot) continue;

        // If item has attributes
        let safeAttrs = {};

        if (slot.attributes) {

            // preserve safe display fields
            if (slot.attributes.customDisplayName) {
                safeAttrs.customDisplayName = slot.attributes.customDisplayName;
            }
            if (slot.attributes.customDescription) {
                safeAttrs.customDescription = slot.attributes.customDescription;
            }

            // preserve enchantments only if they exist
            if (slot.attributes.customAttributes && slot.attributes.customAttributes.enchantments) {
                safeAttrs.customAttributes = {
                    enchantments: slot.attributes.customAttributes.enchantments,
                    enchantmentTier: slot.attributes.customAttributes.enchantmentTier
                };
            }
        }

        // Set the item to your inventory with safe attributes
        api.setItemSlot(
            myId,
            i,
            slot.name,
            slot.amount,
            safeAttrs
        );
    }

    api.sendMessage(myId, `Mirrored inventory of ${targetName}!`, { color: "lime" });
}
}

Instructions:

copy a players inventory!!