fix: no player found notes fix

This commit is contained in:
2026-09-26 03:52:18 +02:00
parent 5a1dab600f
commit d0aee94235
+168 -47
View File
@@ -166,9 +166,7 @@
} }
var author = getMapValue(input, "author"); var author = getMapValue(input, "author");
var text = getMapValue(input, "text"); var text = getMapValue(input, "text");
var createdAt = getMapValue(input, "createdAt"); var createdAt = getMapValue(input, "createdAt");
if (author !== null && author !== undefined) { if (author !== null && author !== undefined) {
@@ -212,10 +210,11 @@
while (entries.hasNext()) { while (entries.hasNext()) {
var entry = entries.next(); var entry = entries.next();
var adminUuid = String(entry.getKey()); var adminUuid = String(entry.getKey());
player.notes[adminUuid] = normaliseNote(entry.getValue()); player.notes[adminUuid] = normaliseNote(
entry.getValue(),
);
} }
} else { } else {
for (var adminUuid in loadedNotes) { for (var adminUuid in loadedNotes) {
@@ -261,10 +260,11 @@
while (entries.hasNext()) { while (entries.hasNext()) {
var entry = entries.next(); var entry = entries.next();
var uuid = String(entry.getKey()); var uuid = String(entry.getKey());
data.players[uuid] = normalisePlayer(entry.getValue()); data.players[uuid] = normalisePlayer(
entry.getValue(),
);
} }
} else { } else {
for (var uuid in loadedPlayers) { for (var uuid in loadedPlayers) {
@@ -328,7 +328,9 @@
playerOutput.notes[String(adminUuid)] = { playerOutput.notes[String(adminUuid)] = {
author: String(note.author || "Unknown"), author: String(note.author || "Unknown"),
text: String(note.text || ""), text: String(note.text || ""),
createdAt: Math.floor(getNumber(note.createdAt, 0)), createdAt: Math.floor(
getNumber(note.createdAt, 0),
),
}; };
} }
@@ -358,8 +360,7 @@
function getOnlinePlayer(name) { function getOnlinePlayer(name) {
try { try {
var players = Utils.server.getPlayerList().getPlayers(); var players = Utils.server.getPlayerList().getPlayers();
var target = String(name).trim().toLowerCase();
var target = String(name).toLowerCase();
for (var i = 0; i < players.size(); i++) { for (var i = 0; i < players.size(); i++) {
var player = players.get(i); var player = players.get(i);
@@ -378,14 +379,22 @@
} }
function findPlayerUuidByName(name) { function findPlayerUuidByName(name) {
var online = getOnlinePlayer(name); var target = String(name).trim();
if (!target) {
return null;
}
var lowerTarget = target.toLowerCase();
// 1. Check currently online players.
var online = getOnlinePlayer(target);
if (online) { if (online) {
return String(online.uuid); return String(online.uuid);
} }
var target = String(name).toLowerCase(); // 2. Check players already stored in notes.json.
for (var uuid in data.players) { for (var uuid in data.players) {
var player = data.players[uuid]; var player = data.players[uuid];
@@ -393,22 +402,56 @@
continue; continue;
} }
if (String(player.name).toLowerCase() === target) { if (String(player.name).toLowerCase() === lowerTarget) {
return String(uuid); return String(uuid);
} }
} }
// 3. Check Minecraft's profile cache.
try {
var profileCache = Utils.server.getProfileCache();
if (profileCache) {
var result = profileCache.get(target);
if (result) {
// Java Optional<GameProfile>
if (typeof result.isPresent === "function") {
if (result.isPresent()) {
var profile = result.get();
if (
profile &&
typeof profile.getId === "function"
) {
return String(profile.getId());
}
}
}
// Fallback if KubeJS exposes the profile directly.
else if (typeof result.getId === "function") {
return String(result.getId());
}
}
}
} catch (error) {
console.error(
"[Notes] Failed to resolve player profile for '" +
target +
"': " +
String(error),
);
}
return null; return null;
} }
function getOrCreatePlayer(uuid, name) { function getOrCreatePlayer(uuid, name) {
var key = String(uuid); var key = String(uuid);
var player = data.players[key]; var player = data.players[key];
if (!player) { if (!player) {
player = createPlayerData(name); player = createPlayerData(name);
data.players[key] = player; data.players[key] = player;
} else { } else {
player.name = String(name); player.name = String(name);
@@ -456,29 +499,35 @@
if (noteText.length === 0) { if (noteText.length === 0) {
player.tell(Text.of("Note cannot be empty.").red()); player.tell(Text.of("Note cannot be empty.").red());
return false; return false;
} }
var targetUuid = findPlayerUuidByName(targetName); var targetUuid = findPlayerUuidByName(targetName);
if (!targetUuid) { if (!targetUuid) {
player.tell(Text.of("Player not found.").red()); player.tell(
Text.of("Player not found: " + String(targetName)).red(),
);
return false; return false;
} }
var online = getOnlinePlayer(targetName); var online = getOnlinePlayer(targetName);
var targetNameStored = String(targetName).trim();
var targetNameStored;
if (online) { if (online) {
targetNameStored = String(online.username); targetNameStored = String(online.username);
} else { } else if (
data.players[targetUuid] &&
data.players[targetUuid].name
) {
targetNameStored = String(data.players[targetUuid].name); targetNameStored = String(data.players[targetUuid].name);
} }
var targetData = getOrCreatePlayer(targetUuid, targetNameStored); var targetData = getOrCreatePlayer(
targetUuid,
targetNameStored,
);
if (!targetData.notes || typeof targetData.notes !== "object") { if (!targetData.notes || typeof targetData.notes !== "object") {
targetData.notes = {}; targetData.notes = {};
@@ -486,7 +535,10 @@
var adminUuid = String(player.uuid); var adminUuid = String(player.uuid);
targetData.notes[adminUuid] = createNote(player.username, noteText); targetData.notes[adminUuid] = createNote(
player.username,
noteText,
);
saveData(); saveData();
@@ -504,12 +556,20 @@
if (!targetUuid) { if (!targetUuid) {
player.tell(Text.of("Player not found.").red()); player.tell(Text.of("Player not found.").red());
return false; return false;
} }
var targetData = data.players[targetUuid]; var targetData = data.players[targetUuid];
if (!targetData) {
player.tell(
Text.of("No notes found for " + String(targetName) + ".")
.yellow(),
);
return false;
}
if (!targetData.notes || typeof targetData.notes !== "object") { if (!targetData.notes || typeof targetData.notes !== "object") {
targetData.notes = {}; targetData.notes = {};
} }
@@ -545,12 +605,20 @@
if (!targetUuid) { if (!targetUuid) {
source.sendFailure(Text.of("Player not found.").red()); source.sendFailure(Text.of("Player not found.").red());
return false; return false;
} }
var targetData = data.players[targetUuid]; var targetData = data.players[targetUuid];
if (!targetData) {
source.sendFailure(
Text.of("No notes found for " + String(targetName) + ".")
.yellow(),
);
return false;
}
if (!targetData.notes || typeof targetData.notes !== "object") { if (!targetData.notes || typeof targetData.notes !== "object") {
targetData.notes = {}; targetData.notes = {};
} }
@@ -559,7 +627,6 @@
for (var adminUuid in targetData.notes) { for (var adminUuid in targetData.notes) {
delete targetData.notes[adminUuid]; delete targetData.notes[adminUuid];
removed++; removed++;
} }
@@ -580,12 +647,20 @@
if (!targetUuid) { if (!targetUuid) {
source.sendFailure(Text.of("Player not found.").red()); source.sendFailure(Text.of("Player not found.").red());
return false; return false;
} }
var targetData = data.players[targetUuid]; var targetData = data.players[targetUuid];
if (!targetData) {
source.sendFailure(
Text.of("No notes found for " + String(targetName) + ".")
.yellow(),
);
return false;
}
if (!targetData.notes || typeof targetData.notes !== "object") { if (!targetData.notes || typeof targetData.notes !== "object") {
targetData.notes = {}; targetData.notes = {};
} }
@@ -595,10 +670,15 @@
false, false,
); );
source.sendSuccess(Text.of("PLAYER NOTES").gold().bold(), false); source.sendSuccess(
Text.of("PLAYER NOTES").gold().bold(),
false,
);
source.sendSuccess( source.sendSuccess(
Text.of("Player: ").gray().append(Text.of(targetData.name).white()), Text.of("Player: ")
.gray()
.append(Text.of(targetData.name).white()),
false, false,
); );
@@ -613,7 +693,10 @@
count++; count++;
source.sendSuccess(Text.of("").white(), false); source.sendSuccess(
Text.of("").white(),
false,
);
source.sendSuccess( source.sendSuccess(
Text.of("[" + String(note.author) + "]") Text.of("[" + String(note.author) + "]")
@@ -622,7 +705,10 @@
false, false,
); );
source.sendSuccess(Text.of(String(note.text)).white(), false); source.sendSuccess(
Text.of(String(note.text)).white(),
false,
);
source.sendSuccess( source.sendSuccess(
Text.of(formatDate(note.createdAt)).darkGray(), Text.of(formatDate(note.createdAt)).darkGray(),
@@ -632,7 +718,9 @@
if (count === 0) { if (count === 0) {
source.sendSuccess( source.sendSuccess(
Text.of("No notes have been added for this player.").gray(), Text.of(
"No notes have been added for this player.",
).gray(),
false, false,
); );
} }
@@ -647,7 +735,6 @@
ServerEvents.commandRegistry(function (event) { ServerEvents.commandRegistry(function (event) {
var Commands = event.commands; var Commands = event.commands;
var Arguments = event.arguments; var Arguments = event.arguments;
var addCommand = Commands.literal("add") var addCommand = Commands.literal("add")
@@ -674,12 +761,17 @@
"player", "player",
); );
var noteText = Arguments.GREEDY_STRING.getResult( var noteText =
Arguments.GREEDY_STRING.getResult(
ctx, ctx,
"note", "note",
); );
return addNote(player, targetName, noteText) return addNote(
player,
targetName,
noteText,
)
? 1 ? 1
: 0; : 0;
} catch (error) { } catch (error) {
@@ -689,7 +781,8 @@
ctx.source.sendFailure( ctx.source.sendFailure(
Text.of( Text.of(
"Failed to add note: " + String(error), "Failed to add note: " +
String(error),
).red(), ).red(),
); );
@@ -714,16 +807,23 @@
try { try {
return removeNote( return removeNote(
ctx.source.player, ctx.source.player,
Arguments.STRING.getResult(ctx, "player"), Arguments.STRING.getResult(
ctx,
"player",
),
) )
? 1 ? 1
: 0; : 0;
} catch (error) { } catch (error) {
console.error("[Notes] Remove error: " + String(error)); console.error(
"[Notes] Remove error: " +
String(error),
);
ctx.source.sendFailure( ctx.source.sendFailure(
Text.of( Text.of(
"Failed to remove note: " + String(error), "Failed to remove note: " +
String(error),
).red(), ).red(),
); );
@@ -734,7 +834,10 @@
var clearCommand = Commands.literal("clear") var clearCommand = Commands.literal("clear")
.requires(function (source) { .requires(function (source) {
return sourceHasPermission(source, PERMISSION_CLEAR); return sourceHasPermission(
source,
PERMISSION_CLEAR,
);
}) })
.then( .then(
Commands.argument( Commands.argument(
@@ -744,16 +847,23 @@
try { try {
return clearNotes( return clearNotes(
ctx.source, ctx.source,
Arguments.STRING.getResult(ctx, "player"), Arguments.STRING.getResult(
ctx,
"player",
),
) )
? 1 ? 1
: 0; : 0;
} catch (error) { } catch (error) {
console.error("[Notes] Clear error: " + String(error)); console.error(
"[Notes] Clear error: " +
String(error),
);
ctx.source.sendFailure( ctx.source.sendFailure(
Text.of( Text.of(
"Failed to clear notes: " + String(error), "Failed to clear notes: " +
String(error),
).red(), ).red(),
); );
@@ -770,26 +880,37 @@
.then(removeCommand) .then(removeCommand)
.then(clearCommand) .then(clearCommand)
.then( .then(
Commands.argument("player", Arguments.STRING.create(event)) Commands.argument(
"player",
Arguments.STRING.create(event),
)
.requires(function (source) { .requires(function (source) {
return sourceHasPermission(source, PERMISSION_READ); return sourceHasPermission(
source,
PERMISSION_READ,
);
}) })
.executes(function (ctx) { .executes(function (ctx) {
try { try {
return showNotes( return showNotes(
ctx.source, ctx.source,
Arguments.STRING.getResult(ctx, "player"), Arguments.STRING.getResult(
ctx,
"player",
),
) )
? 1 ? 1
: 0; : 0;
} catch (error) { } catch (error) {
console.error( console.error(
"[Notes] Read error: " + String(error), "[Notes] Read error: " +
String(error),
); );
ctx.source.sendFailure( ctx.source.sendFailure(
Text.of( Text.of(
"Failed to read notes: " + String(error), "Failed to read notes: " +
String(error),
).red(), ).red(),
); );