mirror of
https://code.chipmunk.land/Parker2991/NitoriBot/
synced 2026-12-01 22:55:40 -05:00
60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
const BOOLEAN_TYPES = [
|
|
"interpret",
|
|
"bold",
|
|
"italic",
|
|
"underlined",
|
|
"strikethrough",
|
|
"obfuscated",
|
|
"float"
|
|
];
|
|
function convertNbtComponentToJson(key, tag) {
|
|
let chatTypes;
|
|
if (!tag) {
|
|
return null;
|
|
} else if (tag.type === "compound") {
|
|
const object = {};
|
|
|
|
for (const key of Object.keys(tag.value)) {
|
|
object[key === "" ? "text" : key] = convertNbtComponentToJson(
|
|
key,
|
|
tag.value[key],
|
|
);
|
|
}
|
|
|
|
return object;
|
|
} else if (tag.type === "list") {
|
|
const array = [];
|
|
|
|
for (const item of tag.value.value) {
|
|
array.push(
|
|
convertNbtComponentToJson(null, { type: tag.value.type, value: item }),
|
|
);
|
|
}
|
|
|
|
return array;
|
|
} else if (tag.type === "byte") {
|
|
if (key && BOOLEAN_TYPES.includes(key)) return tag.value !== 0;
|
|
|
|
return tag.value;
|
|
} else if (tag.type === "int") {
|
|
return tag.value;
|
|
} else if (tag.type === "string") {
|
|
return tag.value;
|
|
} else if (tag.type === "float") {
|
|
return tag.value
|
|
} else if (tag.type === "intArray") {
|
|
const array = [];
|
|
|
|
for (const value of tag.value) {
|
|
array.push(value);
|
|
}
|
|
|
|
return array;
|
|
} else {
|
|
console.log("Failed to parse NBT component type " + tag.type);
|
|
|
|
return { text: "" };
|
|
}
|
|
}
|
|
module.exports = convertNbtComponentToJson;
|