Files
behavior_packs
resource_packs
Code
7 234
behavior_packs/kodari_staff/scripts/main.js
import { EquipmentSlot, system, world } from "@minecraft/server";
const STAFF_ID = "kodari:teleport_staff";
const MAX_TELEPORT_DISTANCE = 64;
function isStaff(itemStack) {
return itemStack?.typeId === STAFF_ID;
}
function getMainHandItem(entity) {
const equippable = entity.getComponent("minecraft:equippable");
return equippable?.getEquipment(EquipmentSlot.Mainhand);
}
function notify(player, message) {
player.sendMessage(`§d[Teleport Staff]§r ${message}`);
}
function useTeleportStaff(player) {
const hit = player.getBlockFromViewDirection({
maxDistance: MAX_TELEPORT_DISTANCE,
includeLiquidBlocks: false,
includePassableBlocks: false
});
if (!hit) {
notify(player, "Look at a block within 64 blocks.");
return;
}
const destination = {
x: hit.block.location.x + 0.5,
y: hit.block.location.y + 1,
z: hit.block.location.z + 0.5
};
const blockX = Math.floor(destination.x);
const blockY = Math.floor(destination.y);
const blockZ = Math.floor(destination.z);
const feetBlock = player.dimension.getBlock({ x: blockX, y: blockY, z: blockZ });
const headBlock = player.dimension.getBlock({ x: blockX, y: blockY + 1, z: blockZ });
if (!feetBlock?.isAir || !headBlock?.isAir) {
notify(player, "There is not enough room to teleport there.");
return;
}
try {
player.teleport(destination, {
dimension: player.dimension,
keepVelocity: false,
rotation: player.getRotation()
});
player.dimension.playSound("mob.endermen.portal", destination);
notify(player, "Teleported.");
} catch {
notify(player, "That location cannot be reached.");
}
}
world.beforeEvents.itemUse.subscribe((event) => {
if (!isStaff(event.itemStack) || event.source.typeId !== "minecraft:player") {
return;
}
event.cancel = true;
system.run(() => useTeleportStaff(event.source));
});
world.afterEvents.entityHitEntity.subscribe((event) => {
const attacker = event.damagingEntity;
const target = event.hitEntity;
if (attacker.typeId !== "minecraft:player" || !isStaff(getMainHandItem(attacker))) {
return;
}
if (target.id === attacker.id) {
return;
}
const strikeLocation = {
x: target.location.x,
y: target.location.y,
z: target.location.z
};
system.run(() => {
try {
target.kill();
} catch {
try {
target.applyDamage(10000);
} catch {
return;
}
}
try {
attacker.addEffect("resistance", 10, {
amplifier: 255,
showParticles: false
});
attacker.dimension.spawnEntity("minecraft:lightning_bolt", strikeLocation);
notify(attacker, "Target eliminated by lightning.");
} catch {
notify(attacker, "Target eliminated.");
}
});
});