Files
behavior_packs
resource_packs
README.md
Code
6 498
behavior_packs/swat2_bp/scripts/main.js
import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
const CONFIDENCE_OBJECTIVE = "swat_conclusoes";
const COMPANION_TAG = "swat_companion";
const SWAT_TAG = "swat_unit";
const PERMISSION_TICKS = 20 * 60;
const MISSION_TICKS = 20 * 60 * 5;
const ANIMALS = new Set([
"minecraft:armadillo",
"minecraft:axolotl",
"minecraft:bee",
"minecraft:cat",
"minecraft:chicken",
"minecraft:cow",
"minecraft:donkey",
"minecraft:fox",
"minecraft:frog",
"minecraft:goat",
"minecraft:horse",
"minecraft:llama",
"minecraft:mooshroom",
"minecraft:mule",
"minecraft:ocelot",
"minecraft:panda",
"minecraft:parrot",
"minecraft:pig",
"minecraft:polar_bear",
"minecraft:rabbit",
"minecraft:sheep",
"minecraft:sniffer",
"minecraft:strider",
"minecraft:turtle",
"minecraft:wolf"
]);
const crimeCooldowns = new Map();
let confidenceObjective;
function ownerTag(player) {
return `swat_owner_${player.id.replace(/[^a-zA-Z0-9_]/g, "_")}`;
}
function targetTag(player) {
return `swat_target_${player.id.replace(/[^a-zA-Z0-9_]/g, "_")}`;
}
function ensureObjective() {
confidenceObjective = world.scoreboard.getObjective(CONFIDENCE_OBJECTIVE);
if (!confidenceObjective) {
confidenceObjective = world.scoreboard.addObjective(CONFIDENCE_OBJECTIVE, "Conclusões SWAT");
}
}
function getConfidence(player) {
ensureObjective();
const score = confidenceObjective.getScore(player);
if (score === undefined) {
confidenceObjective.setScore(player, 100);
return 100;
}
return Math.max(0, Math.min(100, score));
}
function setConfidence(player, value) {
ensureObjective();
const score = Math.max(0, Math.min(100, Math.round(value)));
confidenceObjective.setScore(player, score);
return score;
}
function changeConfidence(player, amount) {
return setConfidence(player, getConfidence(player) + amount);
}
function confidenceBar(score) {
const filled = Math.round(score / 10);
return "§a" + "█".repeat(filled) + "§7" + "░".repeat(10 - filled);
}
function updateActionbar(player) {
const score = getConfidence(player);
player.onScreenDisplay.setActionBar(`§2Conclusões §f${confidenceBar(score)} §f${score}/100`);
}
function permissionKey(type) {
return `swat2:permission_${type}`;
}
function hasPermission(player, type) {
const expires = player.getDynamicProperty(permissionKey(type));
return typeof expires === "number" && expires > system.currentTick;
}
function grantPermission(player, type) {
player.setDynamicProperty(permissionKey(type), system.currentTick + PERMISSION_TICKS);
}
function isWood(blockId) {
return blockId.endsWith("_log") || blockId.endsWith("_wood") || blockId.includes("stripped_");
}
function crimeIsOnCooldown(player, crime) {
const key = `${player.id}:${crime}`;
const lastTick = crimeCooldowns.get(key) ?? -Infinity;
if (system.currentTick - lastTick < 40) {
return true;
}
crimeCooldowns.set(key, system.currentTick);
return false;
}
function reportCrime(player, crime, message) {
if (crimeIsOnCooldown(player, crime)) {
return;
}
const score = changeConfidence(player, -10);
player.sendMessage(`§c[SWAT] ${message}`);
player.sendMessage(`§7Conclusões restantes: §f${score}/100`);
summonSwat(player, score);
}
function summonSwat(player, score) {
const dimension = player.dimension;
const units = score >= 70
? ["minecraft:pillager"]
: score >= 40
? ["minecraft:pillager", "minecraft:pillager", "minecraft:vindicator"]
: ["minecraft:pillager", "minecraft:pillager", "minecraft:vindicator", "minecraft:evocation_illager"];
units.forEach((typeId, index) => {
const angle = (Math.PI * 2 * index) / units.length;
const distance = 12 + (index % 2) * 4;
const location = {
x: player.location.x + Math.cos(angle) * distance,
y: player.location.y,
z: player.location.z + Math.sin(angle) * distance
};
try {
const unit = dimension.spawnEntity(typeId, location);
unit.nameTag = score < 40 ? "§4SWAT PESADA" : "§cSWAT";
unit.addTag(SWAT_TAG);
unit.addTag(targetTag(player));
} catch (error) {
player.sendMessage("§cA SWAT não conseguiu chegar até você neste local.");
}
});
player.sendMessage(score < 40
? "§4A SWAT enviou uma equipe fortemente armada!"
: "§cA SWAT está a caminho!");
}
function removePlayerSwat(player) {
for (const entity of player.dimension.getEntities({ tags: [targetTag(player)] })) {
try {
entity.remove();
} catch (error) {
// A entidade pode ter sido removida pelo jogo no mesmo tick.
}
}
}
function findCompanion(player) {
const companions = player.dimension.getEntities({ tags: [ownerTag(player)] });
return companions.find((entity) => entity.hasTag(COMPANION_TAG));
}
function spawnCompanion(player) {
try {
const companion = player.dimension.spawnEntity("minecraft:villager_v2", {
x: player.location.x + 2,
y: player.location.y,
z: player.location.z + 2
});
companion.addTag(COMPANION_TAG);
companion.addTag(ownerTag(player));
companion.nameTag = "§2Villager SWAT\n§aSeu conselheiro neutro";
player.sendMessage("§2Seu Villager SWAT chegou. Fale com ele para pedir autorização ou uma missão.");
} catch (error) {
player.sendMessage("§cNão foi possível convocar o Villager SWAT neste local.");
}
}
function keepCompanionNearPlayer(player) {
let companion = findCompanion(player);
if (!companion) {
spawnCompanion(player);
return;
}
const dx = companion.location.x - player.location.x;
const dy = companion.location.y - player.location.y;
const dz = companion.location.z - player.location.z;
if (dx * dx + dy * dy + dz * dz > 18 * 18) {
try {
companion.teleport({