Files
behavior_packs
resource_packs
build_mcaddon.sh
Code
37 781
behavior_packs/power_spheres/scripts/main.js
import { EquipmentSlot, system, world } from "@minecraft/server";
import { getSphere } from "./spheres.js";
const EFFECT_DURATION = 40;
const activeSpheres = new Map();
const appliedEffects = new Map();
const appliedHealthPenalties = new Map();
const damageGuards = new Set();
function getOffhand(entity) {
try {
return entity.getComponent("minecraft:equippable")?.getEquipment(EquipmentSlot.Offhand);
} catch {
return undefined;
}
}
function getPlayerDamager(entity) {
if (!entity) return undefined;
if (entity.typeId === "minecraft:player") return entity;
try {
return entity.getComponent("minecraft:projectile")?.owner?.typeId === "minecraft:player"
? entity.getComponent("minecraft:projectile").owner
: undefined;
} catch {
return undefined;
}
}
function applyEffect(entity, effect) {
try {
entity.addEffect(effect.id, EFFECT_DURATION, {
amplifier: effect.amplifier,
showParticles: false
});
} catch {
}
}
function removeSphereEffects(player) {
const effectIds = appliedEffects.get(player.id) ?? [];
for (const effectId of effectIds) {
try {
player.removeEffect(effectId);
} catch {
}
}
appliedEffects.delete(player.id);
const healthPenalty = appliedHealthPenalties.get(player.id) ?? 0;
if (healthPenalty > 0) {
try {
const health = player.getComponent("minecraft:health");
if (health) health.setCurrentValue(Math.min(health.effectiveMax, health.currentValue + healthPenalty));
} catch {
}
}
appliedHealthPenalties.delete(player.id);
}
function refreshSphere(player, sphere) {
const health = player.getComponent("minecraft:health");
const isLowHealth = health !== undefined && health.currentValue <= health.effectiveMax * 0.35;
const effects = [...(sphere.effects ?? [])];
if (sphere.healthBonus !== undefined) {
effects.push({id: "health_boost", amplifier: Math.max(0, Math.ceil(sphere.healthBonus / 2) - 1)});
}
const effectIds = [...new Set(effects.map((effect) => effect.id))];
for (const oldEffectId of appliedEffects.get(player.id) ?? []) {
if (!effectIds.includes(oldEffectId)) {
try {
player.removeEffect(oldEffectId);
} catch {
}
}
}
for (const effect of effects) applyEffect(player, effect);
appliedEffects.set(player.id, effectIds);
if (health && sphere.healthPenalty !== undefined) {
const allowedHealth = Math.max(1, health.effectiveMax - sphere.healthPenalty);
if (!appliedHealthPenalties.has(player.id)) {
const amount = Math.min(sphere.healthPenalty, Math.max(0, health.currentValue - allowedHealth));
appliedHealthPenalties.set(player.id, amount);
if (amount > 0) health.setCurrentValue(health.currentValue - amount);
}
}
}
function syncPlayer(player) {
const item = getOffhand(player);
const typeId = item?.typeId;
const sphere = typeId ? getSphere(typeId) : undefined;
const previousTypeId = activeSpheres.get(player.id);
if (typeId !== previousTypeId) {
removeSphereEffects(player);
if (sphere) {
activeSpheres.set(player.id, typeId);
} else {
activeSpheres.delete(player.id);
}
}
if (sphere) {
refreshSphere(player, sphere);
}
}
function sphereForPlayer(player) {
const item = getOffhand(player);
return item ? getSphere(item.typeId) : undefined;
}
system.runInterval(() => {
for (const player of world.getAllPlayers()) {
syncPlayer(player);
}
}, 10);
world.afterEvents.entityHitEntity.subscribe((event) => {
const attacker = getPlayerDamager(event.damagingEntity);
if (!attacker) {
return;
}
const sphere = sphereForPlayer(attacker);
if (!sphere) {
return;
}
if (sphere.lightningChance !== undefined && Math.random() < sphere.lightningChance) {
try {
attacker.dimension.spawnEntity("minecraft:lightning_bolt", event.hitEntity.location);
} catch {
}
}
if (sphere.fireSeconds !== undefined) {
try {
event.hitEntity.setOnFire(sphere.fireSeconds);
} catch {
}
}
if (sphere.slowSeconds !== undefined) {
try {
event.hitEntity.addEffect("slowness", sphere.slowSeconds * 20, {amplifier: 1, showParticles: true});
} catch {
}
}
});
world.afterEvents.entityHurt.subscribe((event) => {
const hurtEntity = event.hurtEntity;
const attacker = getPlayerDamager(event.damageSource?.damagingEntity);
const attackerSphere = attacker ? sphereForPlayer(attacker) : undefined;
const hurtSphere = hurtEntity?.typeId === "minecraft:player" ? sphereForPlayer(hurtEntity) : undefined;
const guardKey = `${attacker?.id ?? "none"}:${hurtEntity?.id ?? "none"}`;
if (damageGuards.has(guardKey)) return;
damageGuards.add(guardKey);
try {
if (attackerSphere && hurtEntity) {
const isProjectile = event.damageSource?.cause === "projectile";
let attackBonus = isProjectile ? attackerSphere.rangedAttackBonus ?? 0 : attackerSphere.attackBonus ?? 0;
const attackerHealth = attacker.getComponent("minecraft:health");
if (attackerSphere.lowHealthAttackBonus !== undefined && attackerHealth && attackerHealth.currentValue <= attackerHealth.effectiveMax * 0.35) {
attackBonus += attackerSphere.lowHealthAttackBonus;
}
if (attackBonus > 0) {
hurtEntity.applyDamage(attackBonus);
} else if (attackBonus < 0) {
const health = hurtEntity.getComponent("minecraft:health");
if (health && health.currentValue > 0) health.setCurrentValue(Math.min(health.effectiveMax, health.currentValue - attackBonus));
}
if (attackerSphere.lifesteal !== undefined) {
const health = attacker.getComponent("minecraft:health");
if (health) health.setCurrentValue(Math.min(health.effectiveMax, health.currentValue + (event.damage + Math.max(0, attackBonus)) * attackerSphere.lifesteal));
}
}
if (hurtSphere?.defense && hurtEntity) {
const defenseAdjustment = event.damage * hurtSphere.defense * 0.04;
const health = hurtEntity.getComponent("minecraft:health");
if (health && defenseAdjustment > 0 && health.currentValue > 0) {
health.setCurrentValue(Math.min(health.effectiveMax, health.currentValue + defenseAdjustment));
} else if (defenseAdjustment < 0) {
hurtEntity.applyDamage(-defenseAdjustment);
}
}
} catch {
} finally {
damageGuards.delete(guardKey);
}
});
world.afterEvents.playerLeave.subscribe((event) => {