Files
behavior_packs
resource_packs
Code
10 315
behavior_packs/StarDestroyer_BP/scripts/main.js
import { system, world } from "@minecraft/server";
const CANNON_ID = "star_destroyer:orbital_cannon";
const COOLDOWN_TICKS = 45 * 20;
const CHARGE_TICKS = 3 * 20;
const MAX_RANGE = 256;
const CRATER_RADIUS = 10;
const CRATER_DEPTH = 15;
const BLOCKS_PER_TICK = 120;
const chargingPlayers = new Set();
const coolingPlayers = new Set();
world.afterEvents.itemUse.subscribe((event) => {
if (event.itemStack.typeId !== CANNON_ID) return;
const player = event.source;
if (player.typeId !== "minecraft:player") return;
if (coolingPlayers.has(player.id)) {
player.sendMessage("§dSistema em recarga…");
return;
}
if (chargingPlayers.has(player.id)) return;
let hit;
try {
hit = player.getBlockFromViewDirection({
maxDistance: MAX_RANGE,
includeLiquidBlocks: false,
includePassableBlocks: false
});
} catch {
hit = undefined;
}
if (!hit?.block) {
player.sendMessage("§cNenhum alvo encontrado a ate 256 blocos.");
return;
}
const dimension = player.dimension;
const target = { ...hit.block.location };
const center = { x: target.x + 0.5, y: target.y + 1, z: target.z + 0.5 };
chargingPlayers.add(player.id);
player.sendMessage("§bCanhao carregando: 3 segundos...");
try {
dimension.playSound("beacon.activate", center);
} catch {}
for (let pulse = 0; pulse < 6; pulse++) {
system.runTimeout(() => {
try {
for (let point = 0; point < 8; point++) {
const angle = (Math.PI * 2 * point) / 8 + pulse * 0.18;
dimension.spawnParticle("minecraft:basic_flame_particle", {
x: center.x + Math.cos(angle) * 3,
y: center.y + 0.2,
z: center.z + Math.sin(angle) * 3
});
}
} catch {}
}, pulse * 10);
}
system.runTimeout(() => {
chargingPlayers.delete(player.id);
coolingPlayers.add(player.id);
system.runTimeout(() => coolingPlayers.delete(player.id), COOLDOWN_TICKS);
player.sendMessage("§f§lESTRELA DESTRUIDORA! §r§bSistema em recarga por 45 segundos.");
try {
dimension.playSound("random.explode", center, { volume: 2, pitch: 0.6 });
dimension.spawnParticle("minecraft:large_explosion", center);
for (let y = 8; y <= 24; y += 4) {
dimension.spawnParticle("minecraft:basic_flame_particle", {
x: center.x,
y: center.y + y,
z: center.z
});
}
} catch {}
eliminateNearbyEntities(dimension, center);
destroyCrater(dimension, target);
}, CHARGE_TICKS);
});
function eliminateNearbyEntities(dimension, center) {
try {
const entities = dimension.getEntities({ location: center, maxDistance: CRATER_RADIUS });
for (const entity of entities) {
if (entity.typeId === "minecraft:player") continue;
try {
entity.kill();
} catch {}
}
} catch {}
}
function destroyCrater(dimension, target) {
const blocks = [];
for (let dx = -CRATER_RADIUS; dx <= CRATER_RADIUS; dx++) {
for (let dz = -CRATER_RADIUS; dz <= CRATER_RADIUS; dz++) {
const distance = Math.sqrt(dx * dx + dz * dz);
if (distance > CRATER_RADIUS) continue;
const depth = Math.max(1, Math.round(CRATER_DEPTH * (1 - distance / CRATER_RADIUS)));
for (let dy = 0; dy < depth; dy++) {
blocks.push({ x: target.x + dx, y: target.y - dy, z: target.z + dz });
}
}
}
let index = 0;
const removeBatch = () => {
const end = Math.min(index + BLOCKS_PER_TICK, blocks.length);
for (; index < end; index++) {
try {
const block = dimension.getBlock(blocks[index]);
if (block && block.typeId !== "minecraft:air") block.setType("minecraft:air");
} catch {}
}
if (index < blocks.length) system.run(removeBatch);
};
system.run(removeBatch);
}