lib/identity.ts
1.4 KB · sha256:4e0d4b2172b4d829e524225351431448723dea6931e6f41b0cc07a2e0ea5bfa3
import { findPlayerByName, upsertPlayer } from "../models/Player";
export interface ResolvedIdentity {
uuid: string;
name: string;
}
function findOnline(name: string): any | null {
const lc = name.toLowerCase();
for (const p of rune.bukkit.getOnlinePlayers() as any[]) {
if (String(p.name).toLowerCase() === lc) return p;
}
return null;
}
export async function resolveByName(
name: string,
): Promise<ResolvedIdentity | null> {
const online = findOnline(name);
if (online) {
return {
uuid: String(online.getUniqueId()),
name: String(online.getName()),
};
}
const doc = await findPlayerByName(name);
if (doc) return { uuid: doc.uuid, name: doc.username || name };
try {
const off = rune.bukkit.getOfflinePlayer(name);
if (off?.hasPlayedBefore?.()) {
return {
uuid: String(off.getUniqueId()),
name: off.getName() ?? name,
};
}
} catch {
}
return null;
}
export async function resolveOrCreate(
name: string,
): Promise<ResolvedIdentity | null> {
const found = await resolveByName(name);
if (!found) return null;
await upsertPlayer(found.uuid, found.name);
return found;
}
export function onlineByUuid(uuid: string): any | null {
const lc = String(uuid).toLowerCase();
for (const p of rune.bukkit.getOnlinePlayers() as any[]) {
if (String(p.getUniqueId()).toLowerCase() === lc) return p;
}
return null;
}