Files
behavior_pack
resource_pack
README.md
Code
8 311
behavior_pack/scripts/main.js
import {
world,
system,
ItemStack,
EquipmentSlot,
EntityComponentTypes,
} from "@minecraft/server";
const CONTROL_ITEM = "smartpvp:control";
const ELYTRA = "minecraft:elytra";
const CHESTPLATES = [
"minecraft:leather_chestplate",
"minecraft:chainmail_chestplate",
"minecraft:iron_chestplate",
"minecraft:golden_chestplate",
"minecraft:diamond_chestplate",
"minecraft:netherite_chestplate",
];
const EXACT_BUILDING_ITEMS = new Set([
"minecraft:andesite",
"minecraft:basalt",
"minecraft:blackstone",
"minecraft:clay",
"minecraft:cobblestone",
"minecraft:dirt",
"minecraft:end_stone",
"minecraft:gravel",
"minecraft:ice",
"minecraft:mossy_cobblestone",
"minecraft:netherrack",
"minecraft:obsidian",
"minecraft:packed_ice",
"minecraft:sand",
"minecraft:snow_block",
"minecraft:stone",
]);
const BUILDING_SUFFIXES = [
"_bricks",
"_block",
"_concrete",
"_concrete_powder",
"_fence",
"_glass",
"_leaves",
"_log",
"_ore",
"_planks",
"_slab",
"_stairs",
"_stone",
"_terracotta",
"_wall",
"_wool",
"_wood",
];
world.afterEvents.itemUse.subscribe(({ itemStack, source: player }) => {
if (itemStack.typeId !== CONTROL_ITEM) return;
const controlSlot = player.selectedSlotIndex;
performContextAction(player);
system.runTimeout(() => {
if (!player.isValid) return;
const inventory = player.getComponent(EntityComponentTypes.Inventory);
const container = inventory?.container;
if (!container) return;
const current = container.getItem(controlSlot);
if (!current || current.typeId === CONTROL_ITEM) {
container.setItem(controlSlot, new ItemStack(CONTROL_ITEM));
}
}, 1);
});
function performContextAction(player) {
const inventory = player.getComponent(EntityComponentTypes.Inventory);
const container = inventory?.container;
if (!container) return;
const equipment = player.getComponent(EntityComponentTypes.Equippable);
if (!equipment) return;
const chestItem = equipment.getEquipment(EquipmentSlot.Chest);
if (player.isGliding) {
if (chestItem?.typeId === ELYTRA) {
equipFromInventory(player, container, equipment, CHESTPLATES, "No chestplate found in your inventory.", "Chestplate equipped.");
} else {
player.sendMessage("You are gliding, but no chestplate is equipped to swap.");
}
return;
}
if (player.isJumping) {
if (chestItem?.typeId === ELYTRA) {
player.sendMessage("Elytra is already equipped. Use the normal jump controls to start gliding.");
} else {
equipFromInventory(player, container, equipment, [ELYTRA], "No Elytra found in your inventory.", "Elytra equipped. Use the normal jump controls to start gliding.");
}
return;
}
if (player.isSneaking) {
const slot = findHotbarSlot(container, isBuildingBlock);
if (slot < 0) {
player.sendMessage("No recognized building block is in your hotbar.");
return;
}
player.selectedSlotIndex = slot;
player.sendMessage("Building block selected. Place it with the normal touch controls.");
return;
}
const pearlSlot = findHotbarSlot(container, (stack) => stack.typeId === "minecraft:ender_pearl");
if (pearlSlot < 0) {
player.sendMessage("No Ender Pearl is in your hotbar.");
return;
}
player.selectedSlotIndex = pearlSlot;
player.sendMessage("Ender Pearl selected. Throw it with the normal use control.");
}
function equipFromInventory(player, container, equipment, targetTypes, missingMessage, successMessage) {
const slot = findInventorySlot(container, (stack) => targetTypes.includes(stack.typeId));
if (slot < 0) {
player.sendMessage(missingMessage);
return;
}
const incoming = container.getItem(slot);
const outgoing = equipment.getEquipment(EquipmentSlot.Chest);
if (!incoming || !equipment.setEquipment(EquipmentSlot.Chest, incoming)) {
player.sendMessage("Could not change the chest equipment slot.");
return;
}
container.setItem(slot, outgoing);
player.sendMessage(successMessage);
}
function findInventorySlot(container, predicate) {
for (let slot = 0; slot < container.size; slot++) {
const stack = container.getItem(slot);
if (stack && predicate(stack)) return slot;
}
return -1;
}
function findHotbarSlot(container, predicate) {
for (let slot = 0; slot < 9; slot++) {
const stack = container.getItem(slot);
if (stack && predicate(stack)) return slot;
}
return -1;
}
function isBuildingBlock(stack) {
return EXACT_BUILDING_ITEMS.has(stack.typeId) || BUILDING_SUFFIXES.some((suffix) => stack.typeId.endsWith(suffix));
}