Files
src
build.gradle
settings.gradle
Code
7 801
src/main/java/com/kodari/lostnames/LostNamesPlugin.java
package com.kodari.lostnames;
import com.kodari.lostnames.api.LostNamesAPI;
import com.kodari.lostnames.hook.LostNamesPlaceholderExpansion;
import io.papermc.paper.event.player.AsyncChatEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
public final class LostNamesPlugin extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private static final String HIDDEN_TEAM = "lostnames_hidden";
private final Map<UUID, NameProfile> profiles = new ConcurrentHashMap<>();
private final Map<UUID, String> knownNames = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastNameChange = new ConcurrentHashMap<>();
private File dataFile;
private YamlConfiguration data;
private LostNamesPlaceholderExpansion placeholderExpansion;
@Override
public void onEnable() {
saveDefaultConfig();
dataFile = new File(getDataFolder(), "data.yml");
loadData();
LostNamesAPI.initialize(this);
Bukkit.getPluginManager().registerEvents(this, this);
for (String name : List.of("namehide", "namechange", "namedefault", "namecheck", "namereset", "namelist")) {
getCommand(name).setExecutor(this);
getCommand(name).setTabCompleter(this);
}
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
placeholderExpansion = new LostNamesPlaceholderExpansion(this);
placeholderExpansion.register();
}
for (Player player : Bukkit.getOnlinePlayers()) {
knownNames.put(player.getUniqueId(), player.getName());
refreshProfileRealName(player);
applyPlayerName(player);
}
saveData();
}
@Override
public void onDisable() {
saveData();
for (Player player : Bukkit.getOnlinePlayers()) {
removeFromHiddenTeam(player.getName());
player.setPlayerListName(player.getName());
}
LostNamesAPI.shutdown(this);
}
public String getDisplayName(UUID uuid) {
NameProfile profile = profiles.get(uuid);
if (profile != null) {
return profile.fakeName();
}
return getRealName(uuid);
}
public String getRealName(UUID uuid) {
Player online = Bukkit.getPlayer(uuid);
if (online != null) {
return online.getName();
}
String known = knownNames.get(uuid);
if (known != null) {
return known;
}
OfflinePlayer offline = Bukkit.getOfflinePlayer(uuid);
return offline.getName() == null ? uuid.toString() : offline.getName();
}
private void loadData() {
data = YamlConfiguration.loadConfiguration(dataFile);
ConfigurationSection playerSection = data.getConfigurationSection("players");
if (playerSection != null) {
for (String key : playerSection.getKeys(false)) {
try {
UUID uuid = UUID.fromString(key);
ConfigurationSection section = playerSection.getConfigurationSection(key);
if (section == null) {
continue;
}
String fakeName = section.getString("fake_name", "");
String originalName = section.getString("original_name", "");
if (!fakeName.isBlank() && !originalName.isBlank()) {
profiles.put(uuid, new NameProfile(section.getBoolean("hidden"), fakeName, originalName));
}
} catch (IllegalArgumentException ignored) {
getLogger().warning("Ignoring invalid UUID in data.yml: " + key);
}
}
}
ConfigurationSection knownSection = data.getConfigurationSection("known_names");
if (knownSection != null) {
for (String key : knownSection.getKeys(false)) {
try {
String name = knownSection.getString(key);
if (name != null && !name.isBlank()) {
knownNames.put(UUID.fromString(key), name);
}
} catch (IllegalArgumentException ignored) {
getLogger().warning("Ignoring invalid UUID in known_names: " + key);
}
}
}
}
private void saveData() {
YamlConfiguration output = new YamlConfiguration();
for (Map.Entry<UUID, NameProfile> entry : profiles.entrySet()) {
String path = "players." + entry.getKey();
NameProfile profile = entry.getValue();
output.set(path + ".hidden", profile.hidden());
output.set(path + ".fake_name", profile.fakeName());
output.set(path + ".original_name", profile.originalName());
}
for (Map.Entry<UUID, String> entry : knownNames.entrySet()) {
output.set("known_names." + entry.getKey(), entry.getValue());
}
try {
output.save(dataFile);
data = output;
} catch (IOException exception) {
getLogger().severe("Could not save data.yml: " + exception.getMessage());
}
}
private void refreshProfileRealName(Player player) {
NameProfile profile = profiles.get(player.getUniqueId());
if (profile != null && !profile.originalName().equals(player.getName())) {
profiles.put(player.getUniqueId(), new NameProfile(profile.hidden(), profile.fakeName(), player.getName()));
}
}
private void applyPlayerName(Player player) {
NameProfile profile = profiles.get(player.getUniqueId());
if (profile == null) {
player.setPlayerListName(player.getName());
removeFromHiddenTeam(player.getName());
return;
}
player.setPlayerListName(profile.fakeName());
if (getConfig().getBoolean("hide_nametag", true)) {
hiddenTeam().addEntry(player.getName());
} else {
removeFromHiddenTeam(player.getName());
}
}
private Team hiddenTeam() {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.getTeam(HIDDEN_TEAM);
if (team == null) {
team = scoreboard.registerNewTeam(HIDDEN_TEAM);
}
team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.NEVER);
return team;
}