bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
96 lines (95 loc) • 3.31 kB
JavaScript
import { Directories } from "../../file_manager.js";
import { NameData, currentFormatVersion } from "../../utils.js";
import { MinecraftDataType } from "../minecraft.js";
export class ServerItem extends MinecraftDataType {
static get DirectoryPath() {
return Directories.BEHAVIOR_PATH + 'items/';
}
constructor(filepath, template) {
super(filepath, template);
this.format_version = template.format_version;
this["minecraft:item"] = template["minecraft:item"];
}
static createFromTemplate(nameData) {
return new ServerItem(this.createFilePath(nameData), {
format_version: currentFormatVersion,
"minecraft:item": {
description: {
identifier: nameData.fullname
},
components: {}
},
});
}
setDisplayData(name) {
this["minecraft:item"].description.identifier = name.fullname;
this["minecraft:item"].components["minecraft:display_name"] = {
value: `item.${name.fullname}.name`
};
this["minecraft:item"].components["minecraft:icon"] = {
texture: name.fullname
};
}
setStackSize(stack) {
this["minecraft:item"].components["minecraft:max_stack_size"] = {
value: stack
};
}
setWearable(slot, protectionPoints) {
this["minecraft:item"].components["minecraft:wearable"] = {
slot: slot,
dispensable: true,
protection: Math.trunc(protectionPoints),
};
this["minecraft:item"].components["minecraft:repairable"] = {
repair_items: [
{
items: [
this["minecraft:item"].description.identifier
],
repair_amount: "query.remaining_durability + 0.05 * query.max_durability",
}
]
};
this["minecraft:item"].components["minecraft:enchantable"] = {
value: 10,
slot: enchantSlot(slot),
};
}
setFood() {
this["minecraft:item"].components["minecraft:food"] = {
can_always_eat: true,
nutrition: 10,
saturation_modifier: 10,
};
}
setCooldown(duration, category) {
this["minecraft:item"].components["minecraft:cooldown"] = {
duration,
category: category ? category : new NameData(this["minecraft:item"].description.identifier).shortname,
};
}
setModifiers(use_duration = 30000, movement_modifier = 1) {
this["minecraft:item"].components["minecraft:use_modifiers"] = {
use_duration: use_duration,
movement_modifier: movement_modifier,
};
}
setInteractButton(name) {
this["minecraft:item"].components["minecraft:interact_button"] = `action.hint.interact.${name.fullname}`;
}
}
function enchantSlot(slot) {
switch (slot) {
case "slot.armor.feet":
return 'armor_feet';
case "slot.armor.legs":
return "armor_legs";
case "slot.armor.chest":
return "armor_torso";
case "slot.armor.head":
return "armor_head";
default:
return '';
}
}