//Version: 1150R20241107TS
import { BlockPermutation, BlockType, Direction, EntityEquippableComponent, EntityInventoryComponent, EntityType, EquipmentSlot, GameMode, ItemDurabilityComponent, ItemStack, ItemType, world } from "@minecraft/server";
export const item = {
    decreseItem(entity, slot, amount = 1, ignoreGameMode = false) {
        if (!entity.isValid())
            return false;
        if (!ignoreGameMode &&
            entity.typeId == 'minecraft:player' &&
            entity.getGameMode() == GameMode.creative)
            return true;
        const invComp = entity.getComponent(EntityInventoryComponent.componentId);
        if (!invComp)
            return false;
        const { container } = invComp;
        if (!container)
            return false;
        let itemStack = container.getItem(slot);
        if (!itemStack)
            return false;
        if (itemStack.amount > amount) {
            itemStack.amount -= amount;
        }
        else if (itemStack.amount == amount) {
            itemStack = undefined;
        }
        else
            return false;
        container.setItem(slot, itemStack);
    },
    damageEquipment(entity, damage = 1, slot = EquipmentSlot.Mainhand, ignoreGameMode = false) {
        if (!entity.isValid())
            return false;
        if (!ignoreGameMode &&
            entity.typeId == 'minecraft:player' &&
            entity.getGameMode() == GameMode.creative)
            return true;
        const equipComp = entity.getComponent(EntityEquippableComponent.componentId);
        if (!equipComp)
            return false;
        const itemStack = equipComp.getEquipment(slot);
        if (!itemStack)
            return false;
        const duraComp = itemStack.getComponent(ItemDurabilityComponent.componentId);
        if (!duraComp)
            return false;
        const currentDamage = duraComp.damage;
        if (currentDamage + damage <= duraComp.maxDurability) {
            duraComp.damage += damage;
            equipComp.setEquipment(EquipmentSlot.Mainhand, itemStack);
        }
        else {
            equipComp.setEquipment(EquipmentSlot.Mainhand, undefined);
            level.playSound(entity.dimension, 'random.break', entity.location);
        }
        return duraComp.damage;
    },
    addItemToInventory(entity, item) {
        if (!entity.isValid())
            return false;
        const invComp = entity.getComponent(EntityInventoryComponent.componentId);
        if (!invComp)
            return false;
        const { container, inventorySize: size } = invComp;
        if (!container)
            return false;
        let slot = 0;
        while (slot < size) {
            let slotItem = container.getItem(slot);
            if (slotItem === undefined) {
                container.setItem(slot, item);
                return true;
            }
            else if (slotItem.typeId == item.typeId) {
                if (slotItem.amount + item.amount <= slotItem.maxAmount) {
                    slotItem.amount += item.amount;
                    container.setItem(slot, slotItem);
                    return true;
                }
                else {
                    item.amount -= slotItem.maxAmount - slotItem.amount;
                    slotItem.amount = slotItem.maxAmount;
                    container.setItem(slot, slotItem);
                    slot++;
                }
            }
            else
                slot++;
        }
        /*if (!set)*/ return item;
    },
    hasItem(entity, item) {
        if (item == undefined)
            return false;
        if (!entity.isValid())
            return false;
        const invComp = entity.getComponent(EntityInventoryComponent.componentId);
        if (!invComp)
            return false;
        const container = invComp.container;
        if (!container)
            return false;
        let slot = 0;
        let amount = item.amount;
        let invItem;
        while (slot < 27) {
            invItem = container.getItem(slot);
            if (invItem) {
                if (invItem.typeId == item.typeId) {
                    if (invItem.amount > amount) {
                        invItem.amount -= amount;
                        amount = 0;
                    }
                    else if (invItem.amount == amount) {
                        amount = 0;
                    }
                    else {
                        amount -= invItem.amount;
                    }
                }
            }
            slot++;
        }
        if (amount == 0) {
            return true;
        }
        else {
            return false;
        }
    },
    consumeItem(entity, item) {
        if (!entity.isValid())
            return false;
        const invComp = entity.getComponent(EntityInventoryComponent.componentId);
        if (!invComp)
            return false;
        const container = invComp.container;
        if (!container)
            return false;
        let slot = 0;
        let amount = item.amount;
        let invItem;
        let obj = [];
        while (slot < container.size) {
            invItem = container.getItem(slot);
            if (invItem) {
                if (invItem.typeId == item.typeId) {
                    if (invItem.amount > amount) {
                        invItem.amount -= amount;
                        obj[slot] = amount;
                        amount = 0;
                    }
                    else if (invItem.amount == amount) {
                        obj[slot] = undefined;
                        amount = 0;
                    }
                    else {
                        amount -= invItem.amount;
                        obj[slot] = undefined;
                    }
                }
            }
            slot++;
        }
        if (amount == 0) {
            for (let i in obj) {
                const amount = obj[i];
                if (amount) {
                    const item1 = container.getItem(Number(i));
                    if (item1) {
                        item1.amount -= amount;
                        container.setItem(Number(i), item1);
                    }
                }
            }
            return true;
        }
        else {
            return false;
        }
    },
    getEquipment(entity, slot = EquipmentSlot.Mainhand) {
        if (!entity.isValid())
            return undefined;
        const equipComp = entity.getComponent(EntityEquippableComponent.componentId);
        if (!equipComp)
            return undefined;
        return equipComp.getEquipment(slot);
    },
    hasTagComponent(item, name) {
        const tags = item.getTags();
        for (const tag of tags) {
            const tagName = tag.slice(0, name.length);
            if (tagName == name) {
                return true;
            }
        }
        return false;
    },
    getTagComponent(item, name) {
        const tags = item.getTags();
        for (const tag of tags) {
            const tagName = tag.slice(0, name.length);
            if (tagName == name) {
                const args = tag.split(';').slice(1);
                const comp = {
                    id: name,
                    args
                };
                return comp;
            }
        }
        return undefined;
    },
    setItem(player, toItem, slot = player.selectedSlotIndex) {
        if (typeof (toItem) == 'string') {
            toItem = new ItemStack(toItem);
        }
        return player.getComponent(EntityInventoryComponent.componentId)?.container?.setItem(slot, toItem);
    }
};
export const entity = {};
export const block = {
    setBlockState(block, stateName, value) {
        const permutation = block.permutation;
        block.setPermutation(permutation.withState(stateName, value));
    },
    checkBlockDirection(block, face) {
        const permutation = block.permutation;
        if (permutation.getState('minecraft:facing_direction') == face.toLowerCase())
            return true;
        if (permutation.getState('minecraft:cardinal_direction') == face.toLowerCase())
            return true;
        else
            return false;
    },
    getTargetBlock(blockPerm, targetObj) {
        for (const name in targetObj) {
            if (!targetObj[name]) {
                if (blockPerm.matches(name)) {
                    return name;
                }
            }
            else {
                if (blockPerm.matches(name, targetObj[name])) {
                    return name;
                }
            }
        }
        return false;
    }
};
export const system = {
    logger(data, mode = 'log') {
        let prefix = mode == 'error' ? '§c' : mode == 'warning' ? '§e' : '';
        world.sendMessage(`${prefix}[${mode}]${JSON.stringify(data)}`);
    },
    getRelativeDirection(loc, locRel) {
        if (loc.z = locRel.z - 1) {
            return 'north';
        }
        else if (loc.z = locRel.z + 1) {
            return 'south';
        }
        else if (loc.x = locRel.x - 1) {
            return 'west';
        }
        else if (loc.x = locRel.x + 1) {
            return 'east';
        }
        else if (loc.y = locRel.y - 1) {
            return 'down';
        }
        else if (loc.y = locRel.y + 1) {
            return 'up';
        }
    },
    getRelativeLocation(baseLoc, face) {
        let loc = baseLoc;
        face = face.toLowerCase();
        switch (face) {
            case 'north':
                loc.z -= 1;
                break;
            case 'south':
                loc.z += 1;
                break;
            case 'east':
                loc.x += 1;
                break;
            case 'west':
                loc.x -= 1;
                break;
            case 'up':
                loc.y += 1;
                break;
            case 'down':
                loc.y -= 1;
                break;
            default:
                return false;
        }
        return loc;
    },
    randomInt(min, max) {
        return Math.floor(min + Math.random() * (max - min));
    },
    amountExchange(plusNumber, minusNumber, changeAmount, max) {
        if (minusNumber >= changeAmount) {
            if (plusNumber <= max - changeAmount)
                plusNumber += changeAmount,
                    minusNumber -= changeAmount;
            else
                minusNumber -= max - plusNumber, plusNumber = max;
        }
        else {
            if (plusNumber <= max - minusNumber)
                plusNumber += minusNumber,
                    minusNumber = 0;
            else
                minusNumber -= max - plusNumber, plusNumber = max;
        }
        return [plusNumber, minusNumber];
    }
};
export const level = {
    playSound(dimension, soundId, location) {
        const players = dimension.getPlayers();
        for (const player of players) {
            player.playSound(soundId, { location });
        }
    }
};
export const vector = {
    equals(v1, v2) {
        if (v1.x == v2.x &&
            v1.y == v2.y &&
            v1.z == v2.z)
            return true;
        else
            return false;
    }
};
export const lang = {
    getTranslationKey(type) {
        if (type) {
            if (type instanceof ItemType) {
                try {
                    BlockPermutation.resolve(type.id);
                }
                catch {
                    if (type.id.split(':')[0] == 'minecraft') {
                        return `item.${type.id.split(':')[1]}.name`;
                    }
                    else {
                        return `item.${type.id}`;
                    }
                }
                if (type.id.split(':')[0] == 'minecraft') {
                    return `tile.${type.id.split(':')[1]}.name`;
                }
                else {
                    return `tile.${type.id}.name`;
                }
            }
            else if (type instanceof BlockType) {
                if (type.id.split(':')[0] == 'minecraft') {
                    return `tile.${type.id.split(':')[1]}.name`;
                }
                else {
                    return `tile.${type.id}.name`;
                }
            }
            else if (type instanceof EntityType) {
                if (type.id.split(':')[0] == 'minecraft') {
                    return `entity.${type.id.split(':')[1]}.name`;
                }
                else {
                    return `entity.${type.id}.name`;
                }
            }
        }
    }
};
const dirs = {
    [Direction.Up.toLowerCase()]: Direction.Down,
    [Direction.Down.toLowerCase()]: Direction.Up,
    [Direction.East.toLowerCase()]: Direction.West,
    [Direction.West.toLowerCase()]: Direction.East,
    [Direction.North.toLowerCase()]: Direction.South,
    [Direction.South.toLowerCase()]: Direction.North,
    [Direction.Up]: Direction.Down,
    [Direction.Down]: Direction.Up,
    [Direction.East]: Direction.West,
    [Direction.West]: Direction.East,
    [Direction.North]: Direction.South,
    [Direction.South]: Direction.North
};
export const direction = {
    reverse(dir) {
        return dirs[dir];
    }
};
