Files
behavior_packs
resource_packs
Code
14 418
behavior_packs/kodari_energy_barrier/scripts/main.js
import { system, world } from "@minecraft/server";
const DEVICE_ID = "kodari:energy_barrier_device";
const ANCHOR_ID = "kodari:energy_barrier_anchor";
const BARRIER_HALF_WIDTH = 3.5;
const BARRIER_HEIGHT = 5;
const TOUCH_DEPTH = 0.8;
const DIMENSION_IDS = ["overworld", "nether", "the_end"];
const activeBarriers = new Map();
function vector(x, y, z) {
return { x, y, z };
}
function getFacing(rotation) {
const yaw = (rotation.y * Math.PI) / 180;
return vector(-Math.sin(yaw), 0, Math.cos(yaw));
}
function getSide(facing) {
return vector(-facing.z, 0, facing.x);
}
function add(a, b) {
return vector(a.x + b.x, a.y + b.y, a.z + b.z);
}
function scale(a, amount) {
return vector(a.x * amount, a.y * amount, a.z * amount);
}
function barrierKey(dimension, location) {
return `${dimension.id}:${location.x}:${location.y}:${location.z}`;
}
function removeAnchorsAt(dimension, location) {
const nearby = dimension.getEntities({
type: ANCHOR_ID,
location: add(location, vector(0.5, 0.5, 0.5)),
maxDistance: 0.9
});
for (const anchor of nearby) {
anchor.kill();
}
}
function isHostile(entity) {
const family = entity.getComponent("minecraft:type_family");
return family !== undefined &&
(family.hasTypeFamily("monster") || family.hasTypeFamily("hostile"));
}
function touchesBarrier(entity, origin, facing, side) {
const delta = vector(
entity.location.x - origin.x,
entity.location.y - origin.y,
entity.location.z - origin.z
);
const depth = delta.x * facing.x + delta.z * facing.z;
const width = delta.x * side.x + delta.z * side.z;
return Math.abs(depth) <= TOUCH_DEPTH &&
Math.abs(width) <= BARRIER_HALF_WIDTH &&
delta.y >= -0.75 &&
delta.y <= BARRIER_HEIGHT + 0.25;
}
function spawnParticle(dimension, particle, location) {
try {
dimension.spawnParticle(particle, location);
} catch {
}
}
function emitBarrierEffects(dimension, center, facing, side) {
const planeOffset = scale(facing, 0.04);
for (let width = -3.5; width <= 3.5; width += 1) {
for (let height = 0.5; height <= 4.5; height += 1) {
const location = add(center, add(scale(side, width), add(planeOffset, vector(0, height - 2.5, 0))));
spawnParticle(dimension, "minecraft:endrod", location);
}
}
const sweep = ((system.currentTick % 40) / 40) * 7 - 3.5;
const sweepLocation = add(center, add(scale(side, sweep), add(planeOffset, vector(0, 0, 0))));
spawnParticle(dimension, "minecraft:blue_flame_particle", sweepLocation);
}
function updateBarrier(dimension, blockLocation, rotation) {
const block = dimension.getBlock(blockLocation);
if (block === undefined || block.typeId !== DEVICE_ID) {
return false;
}
const facing = getFacing(rotation);
const side = getSide(facing);
const origin = add(blockLocation, vector(0.5, 0, 0.5));
const center = add(origin, add(scale(facing, 1.05), vector(0, 2.5, 0)));
const entities = dimension.getEntities({
location: center,
maxDistance: 6
});
for (const entity of entities) {
if (!isHostile(entity)) {
continue;
}
if (touchesBarrier(entity, center, facing, side)) {
entity.kill();
}
}
emitBarrierEffects(dimension, center, facing, side);
return true;
}
function updateAnchor(anchor) {
const blockLocation = vector(
Math.floor(anchor.location.x),
Math.floor(anchor.location.y),
Math.floor(anchor.location.z)
);
if (!updateBarrier(anchor.dimension, blockLocation, anchor.getRotation())) {
anchor.kill();
}
}
world.afterEvents.playerPlaceBlock.subscribe((event) => {
if (event.block.typeId !== DEVICE_ID) {
return;
}
const location = vector(event.block.location.x, event.block.location.y, event.block.location.z);
const rotation = { x: 0, y: event.player.getRotation().y };
activeBarriers.set(barrierKey(event.block.dimension, location), {
dimension: event.block.dimension,
location,
rotation
});
try {
const anchor = event.block.dimension.spawnEntity(ANCHOR_ID, add(location, vector(0.5, 0, 0.5)));
anchor.setRotation(rotation);
} catch {
}
});
world.afterEvents.playerBreakBlock.subscribe((event) => {
if (event.brokenBlockPermutation.type.id === DEVICE_ID) {
activeBarriers.delete(barrierKey(event.block.dimension, event.block.location));
removeAnchorsAt(event.block.dimension, event.block.location);
}
});
system.runInterval(() => {
for (const dimensionId of DIMENSION_IDS) {
const dimension = world.getDimension(dimensionId);
for (const [key, barrier] of activeBarriers) {
if (barrier.dimension.id !== dimensionId) {
continue;
}
try {
if (!updateBarrier(barrier.dimension, barrier.location, barrier.rotation)) {
activeBarriers.delete(key);
}
} catch {
activeBarriers.delete(key);
}
}
const anchors = dimension.getEntities({ type: ANCHOR_ID });
for (const anchor of anchors) {
try {
const location = vector(
Math.floor(anchor.location.x),
Math.floor(anchor.location.y),
Math.floor(anchor.location.z)
);
if (!activeBarriers.has(barrierKey(dimension, location))) {
updateAnchor(anchor);
}
} catch {
}
}
}
}, 2);