I am confused on how to make this code work
const CuriosApi = java('top.theillusivec4.curios.api.CuriosApi');
// Function to generate a random ID
function random_id() {
return Math.floor(Math.random() * 10000000);
}
// Function to determine item quality based on smithing skill
function determine_type(event) {
const types = ['terrible', 'poor', 'bad', 'normal', 'good', 'amazing', 'legendary'];
const v = [1, 3, 5, 7, 9, 12, 15];
const vals = [-3, -2, 1, 0, 1, 2, 3];
const smithing_skill = global.get_level(event.player, 'smithing');
var goal = Math.floor(Math.random() * smithing_skill);
var closest = v.reduce((prev, curr) => {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});
for (let i = 0; i < v.length; i++) {
if (v[i] == closest) {
return [types[i], vals[i]];
}
}
}
// Event when a player equips a curio
onEvent('player.inventory.changed', (event) => {
const player = event.player;
const equipped = CuriosApi.getCuriosHelper().getEquippedCurios(player.minecraftPlayer).resolve().get();
for (let i = 0; i < equipped.getSlots(); i++) {
let itemStack = equipped.getStackInSlot(i);
if (!itemStack.isEmpty()) {
let item = Item.of(itemStack);
let typeData = determine_type(event);
let type = typeData[0];
let value = typeData[1];
// Apply attribute as NBT data to the item
let nbt = item.nbt || {};
nbt.customQuality = {
id: random_id(),
type: type,
value: value
};
item.nbt = nbt;
player.tell(`Your ${item.getDisplayName()} has been assigned a quality: ${type} (${value})`);
}
}
});