function() {
let target = getPlayerWith(args[0]);
let inv = target.getComponent("inventory").container;
if (inv.size == inv.emptySlotsCount) return sendMsg(player, `§4${removeColorCodes(target.name)}§c's Inventory Is Empty!`);
let msg = ` §2${removeColorCodes(target.name)}§a's Inventory: \n\n`;
msg += `§7§l================`;
for (let i = 0; i < inv.size; i++) {
let item = inv.getItem(i);
let lore = item.getLore();
if (item != undefined) {
msg += `\n§e§lSLOT ${i} §r§8|| §r${removeColorCodes(item.typeId)}${item.nameTag == undefined ? "§r" : " " + item.nameTag + "§r"} §7x${item.amount}`;
if (showEnchants == true && item.hasComponent("minecraft:enchantments")) {
let enchants = item.getComponent("minecraft:enchantments").enchantments.getEnchantments();
let allEnchants = enchants;
for (let i = 0; i < allEnchants.length; i++) {
let enchant = allEnchants[i];
let name = enchant.type;
let level = enchant.level;
msg += `\n §r§5${name} §3${level}`;
}
}
if (showLore == true && lore[0]) {
for (let i = 0; i < lore.length; i++) {
msg += `\n §5§o${lore[i]}`;
}
}
}
}
msg += `§7§l================`;
sendMsg(player, msg);
}
#[Resloved] TypeError: not a function
1 messages · Page 1 of 1 (latest)
Let me tell you something rq
sendMsg, getPlayerWith, showEnchants, showLore are defined but not here..
and all of them are being used in the rest of my code so if there was anything it should've flagged the other stuff first
And why im using a function expression is because im doing a cmds system.
And AGAIN, it has no errors
hasComponent isn't a function
What did you change?
I changed item.hasComponent to item.getComponent
item.hasComponent is true
What
Nawww
I use that for all my commands tho
Why does it not throw an error in them if its the function()
Function declarations are function funcName(params) { ... }
Ik
Im using an obj for each cmd
And a property named data and it has a function expression
One of the debugging practices we use at my workplace if the line number isn't given to you is just comment out large portions of code to identify where the error is coming from - then work in that narrow space once the commented out code helps identify where the error kept coming from (since it won't appear anymore).
Because, right now, it's a lot of us just shooting in the dark without more debugging done to help narrow it down on your end. Again, that's if the error isn't providing a line number.
Yours is an immediately invoked function, should be alright actually
use console.warn in multiple lines in the function to narrow the error down
It literally only says "TypeError: not a function"
Yeah, in that case you need to narrow down where it's coming from more.
Okay
Wait i forgot how do to that-
try { ... } catch (error) { console.warn(error, error.stack) }
console.warn("1")
Yea now i remember
that will give same error
Stack trace shows error line
ah
didn't know that
Uh
At the line i defined the inventory of the target
The ```js
let inv = target.getComponent("inventory").container;
I'm only going to assume its my getPlayerWith function but it doesn't throw any errors?
maybe it didn't return any player
Console warn target, see what pops up
What do you get in your output when you put this before that line
console.warn(typeof target);
And
console.warn(typeof target.getComponent);
The outputs of those will give everyone better insight into what that is and what's happening further.
Yooo it works... kinda
Now it picks up the same error but at the line where i defined all enchants
Wait, it just magically started working and you didn't change any code...?
I did change the code
I realized something off
I didn't input any if (!args[0]) at the start so target was undefined
getEnchantments isn't a property of itemEnchants, just the base enchant component https://jaylydev.github.io/scriptapi-docs/1.20.50/classes/_minecraft_server_1_8_0_beta.EnchantmentList.html
And it didn't send me any msg and stop the function
Err, it's not a thing there either
Well
I dont know to interact with item enchants tbh
Last time i touched it was in 1.19.83
And never again
I just wanna get a list of enchants on the item
[...this.getComponent('enchantments').enchantments].map(v => ({ name: v.type.id.capitalize(), level: v.level, maxLevel: v.type.maxLevel })); is what I do to map enchants & levels
Uh
"this" being itemstack
In a for loop terms...
Because i am having lots of problems with .map and .forEach too...........
Ill try to use this?
Yeah no i don't know how to use this
You can use the code he provided to iterate over the enchantments as a for loop, as you'd like. It's a very clean and modern ECMAScript solution.
const enchantments = [...this.getComponent('enchantments').enchantments]
.map(v => ({
name: v.type.id.capitalize(),
level: v.level,
maxLevel: v.type.maxLevel
}));
for (const enchantment of enchantments){
const enchantmentName = enchantment.name;
const enchantmentLevel = enchantment.level;
}
Though, for you, you'd replace this with item
Nice thank you very much you guys for helping me out
Actually i don't know the first part
Nvm
Enchants components changed completely in preview - much easier to use.