Files
behavior_packs
resource_packs
README.md
Code
18 798
behavior_packs/MirrorBlade_BP/scripts/main.js
import { system, world } from "@minecraft/server";
import { CONFIG } from "./mirror_blade/config.js";
import { playerStates, getState, clearTemporaryState } from "./mirror_blade/state.js";
import { hasMirrorBlade } from "./mirror_blade/utils.js";
import { renderShards, addShards } from "./mirror_blade/shards.js";
import { tickProjectiles } from "./mirror_blade/projectiles.js";
import { handleMeleeHit, performMirrorSweep } from "./mirror_blade/combat.js";
import { activateGuard, handleIncomingDamage } from "./mirror_blade/shield.js";
import { tickNightwither } from "./mirror_blade/nightwither.js";
let tick = 0;
world.afterEvents.entityHitEntity.subscribe(handleMeleeHit);
world.afterEvents.itemUse.subscribe(event => {
const player = event.source;
if (player?.typeId !== "minecraft:player" || event.itemStack?.typeId !== "mirror:mirror_blade") return;
const state = getState(player);
if (player.isSneaking) {
activateGuard(player, state);
} else {
performMirrorSweep(player);
}
});
world.beforeEvents.entityHurt.subscribe(handleIncomingDamage);
world.afterEvents.playerSpawn.subscribe(event => {
const state = getState(event.player);
clearTemporaryState(state);
state.dimensionId = event.player.dimension.id;
});
world.afterEvents.playerLeave.subscribe(event => {
playerStates.delete(event.playerId);
});
world.afterEvents.entityDie.subscribe(event => {
const entity = event.deadEntity;
if (entity?.typeId !== "minecraft:player") return;
const state = playerStates.get(entity.id);
if (state) clearTemporaryState(state);
});
world.afterEvents.scriptEventReceive.subscribe(event => {
const player = event.sourceEntity;
if (player?.typeId !== "minecraft:player") return;
const state = getState(player);
if (event.id === "mirror:add_shards") {
const amount = Math.min(CONFIG.MAX_SHARDS, Math.max(0, Number.parseInt(event.message || "1", 10) || 1));
addShards(player, state, amount);
} else if (event.id === "mirror:clear_shards") {
clearTemporaryState(state);
} else if (event.id === "mirror:debug") {
player.sendMessage(`§dMirror Blade §7| shards ${state.shards}/${CONFIG.MAX_SHARDS} | guard ${state.guarding ? "on" : "off"} | projectiles ${state.launchers.length}`);
}
});
system.runInterval(() => {
tick++;
for (const [playerId, state] of playerStates) {
const player = state.player;
try {
const dimensionId = player.dimension.id;
if (dimensionId !== state.dimensionId) {
clearTemporaryState(state);
state.dimensionId = dimensionId;
}
if (!hasMirrorBlade(player)) {
clearTemporaryState(state);
continue;
}
state.tick = tick;
if (state.guardCooldown > 0) state.guardCooldown--;
if (state.breakCooldown > 0) state.breakCooldown--;
if (state.attackCooldown > 0) state.attackCooldown--;
if (state.counterTicks > 0) state.counterTicks--;
if (state.guarding && state.shards <= 0) state.guarding = false;
if (tick % CONFIG.ORBIT_INTERVAL_TICKS === 0) renderShards(player, state, tick);
tickProjectiles(state);
if (tick >= state.hudTick) {
player.onScreenDisplay.setActionBar(`§dMirror Shards: ${state.shards}/${CONFIG.MAX_SHARDS}${state.guarding ? " §b| Mirror Guard" : ""}`);
state.hudTick = tick + CONFIG.HUD_INTERVAL_TICKS;
}
} catch {
clearTemporaryState(state);
playerStates.delete(playerId);
}
}
tickNightwither(tick);
}, 1);