Merge pull request #11 from BlazingGames/display-names-everywhere

display names everywhere!
This commit is contained in:
Ivy Collective 2025-01-21 10:41:44 -05:00 committed by GitHub
commit 1eea00f83f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 250 additions and 176 deletions

View file

@ -206,7 +206,7 @@ public class BlazingGames extends JavaPlugin {
registerCommand("customgive", new CustomGiveCommand());
registerCommand("killme", new KillMeCommand());
registerCommand("playtime", new PlaytimeCommand());
registerCommand("config", new ConfigCommand());
registerCommand("display", new DisplayCommand());
registerCommand("setaltar", new SetAltar());
// Events

View file

@ -1,121 +0,0 @@
/*
* Copyright 2025 The Blazing Games Maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.blazemcworld.blazinggames.commands;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import de.blazemcworld.blazinggames.utils.PlayerConfig;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
public class ConfigCommand implements CommandExecutor, TabCompleter {
String[] values = {"display", "pronouns", "color"};
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player p)) {
sender.sendMessage(Component.text("Only players can use this command!")
.color(NamedTextColor.RED));
return false;
}
if (args.length < 1) {
return false;
}
String param = args[0];
String value = (args.length > 1) ? Arrays.stream(args).skip(1).collect(Collectors.joining(" "))
: null;
PlayerConfig config = PlayerConfig.forPlayer(p.getUniqueId());
switch (param) {
case "display":
if (!enforceParam(sender, param, value, 1, 36)) return false;
config.setDisplayName(value);
break;
case "pronouns":
if (!enforceParam(sender, param, value, 1, 16)) return false;
config.setPronouns(value);
break;
case "color":
if (!enforceParam(sender, param, value, 6, 6)) return false;
if (value == null || value.isBlank()) {
config.setNameColor(null);
} else {
int realValue;
try {
realValue = Integer.parseInt(value, 16);
} catch (NumberFormatException e) {
sender.sendMessage(Component.text("Invalid color: #" + value).color(NamedTextColor.RED));
return false;
}
config.setNameColor(TextColor.color(realValue));
}
break;
default:
sender.sendMessage(Component.text("Unknown parameter: " + param).color(NamedTextColor.RED));
return false;
}
sendSuccess(sender, param, value);
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (args.length == 1) {
return Arrays.asList(values);
}
return List.of();
}
private static void sendSuccess(CommandSender sender, String param, String value) {
if (value == null || value.isBlank()) {
sender.sendMessage(Component.text("Cleared " + param).color(NamedTextColor.GREEN));
} else {
sender.sendMessage(Component.text("Set " + param + " to " + value).color(NamedTextColor.GREEN));
}
}
private static boolean enforceParam(CommandSender sender, String param, String value, int minChars, int maxChars) {
if (value == null || value.isBlank()) {
// return for unset
return true;
}
if (value.length() < minChars || value.length() > maxChars) {
if (minChars == maxChars) {
sender.sendMessage(Component.text("Parameter " + param + " must be exactly " + minChars + " chars long!").color(NamedTextColor.RED));
} else {
sender.sendMessage(Component.text("Parameter " + param + " must be between " + minChars + " and " + maxChars + " chars long!").color(NamedTextColor.RED));
}
return false;
}
return true;
}
}

View file

@ -0,0 +1,148 @@
/*
* Copyright 2025 The Blazing Games Maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.blazemcworld.blazinggames.commands;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import de.blazemcworld.blazinggames.utils.PlayerConfig;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
public class DisplayCommand implements CommandExecutor, TabCompleter {
public static final TextColor colorSuccess = TextColor.color(0xD1FCDF);
public static final TextColor colorFailure = TextColor.color(0xFC9588);
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(Component.text("Only players can use this command!")
.color(NamedTextColor.RED));
return false;
}
PlayerConfig config = PlayerConfig.forPlayer(player.getUniqueId());
player.sendMessage("");
if (args.length < 1) {
// show help text
player.sendMessage(Component.text("Usage: " + command.getUsage()).color(colorSuccess));
sendNameplates(player);
return true;
}
String param = args[0];
String valueStr = (args.length > 1) ? Arrays.stream(args).skip(1).collect(Collectors.joining(" ")) : null;
String value = (valueStr == null || valueStr.isBlank()) ? null : valueStr;
String pretty;
switch (param) {
case "name":
if (value != null && (value.length() < 2 || value.length() > 40)) {
player.sendMessage(Component.text("Display name must be between 2 and 40 characters long.").color(colorFailure));
return true;
}
config.setDisplayName(value);
pretty = "display name";
break;
case "pronouns":
if (value != null && (value.length() < 2 || value.length() > 20)) {
player.sendMessage(Component.text("Pronouns must be between 2 and 20 characters long.").color(colorFailure));
return true;
}
config.setPronouns(value);
pretty = "pronouns";
break;
case "color":
if (value == null) {
config.setNameColor(null);
player.sendMessage(Component.text("Unset name color.").color(colorSuccess));
sendNameplates(player);
return true;
} else if (value.length() != 6) {
player.sendMessage(Component.text("Colors must be a hex color without the first #. For example, \"ffffff\".").color(colorFailure));
return true;
} else {
int realValue;
try {
realValue = Integer.parseInt(value, 16);
} catch (NumberFormatException e) {
player.sendMessage(Component.text("This isn't a valid color: #" + value).color(colorFailure));
return true;
}
config.setNameColor(TextColor.color(realValue));
}
pretty = "name color";
break;
case "reset":
if ("confirm".equals(value)) {
config.setDisplayName(null);
config.setPronouns(null);
config.setNameColor(null);
player.sendMessage(Component.text("Reset all settings successfully.").color(colorSuccess));
sendNameplates(player);
} else {
player.sendMessage(Component.text("To reset all display settings, run ").color(colorSuccess)
.append(Component.text("/display reset confirm").color(colorFailure)));
}
return true;
default:
player.sendMessage(Component.text("Unknown parameter: " + param).color(colorFailure));
return true;
}
if (value == null) {
player.sendMessage(Component.text("Unset " + pretty + " successfully.").color(colorSuccess));
} else {
player.sendMessage(Component.text("Set " + pretty + " to ").color(colorSuccess)
.append(Component.text(value).color(NamedTextColor.WHITE))
.append(Component.text(" successfully.").color(colorSuccess)));
}
sendNameplates(player);
return true;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (args.length == 1) {
return List.of("name", "pronouns", "color");
}
return List.of();
}
private static void sendNameplates(Player player) {
PlayerConfig config = PlayerConfig.forPlayer(player.getUniqueId());
player.sendMessage(Component.text("Preview:").color(colorSuccess));
player.sendMessage(Component.text("- Current nameplate: ").color(colorSuccess)
.append(config.buildNameComponent(player.getName(), player.isOp())));
player.sendMessage(Component.text("- Current nameplate (short): ").color(colorSuccess)
.append(config.buildNameComponentShort(player.getName(), player.isOp())));
player.sendMessage(Component.text("- Current discord name: ").color(colorSuccess)
.append(Component.text(config.buildNameString(player.getName(), player.isOp())).color(NamedTextColor.WHITE)));
player.sendMessage(Component.text("- Current discord name (short): ").color(colorSuccess)
.append(Component.text(config.buildNameStringShort(player.getName())).color(NamedTextColor.WHITE)));
config.updatePlayer(player);
}
}

View file

@ -174,22 +174,9 @@ public class DiscordApp extends ListenerAdapter {
private void sendDiscordMessage(Player player, String content) {
PlayerConfig config = PlayerConfig.forPlayer(player.getUniqueId());
StringBuilder username = new StringBuilder();
if (config.getDisplayName() != null && !config.getDisplayName().equals(player.getName())) {
username.append(config.getDisplayName()).append(" [aka ").append(player.getName()).append("]");
} else {
username.append(player.getName());
}
if (config.getPronouns() != null) {
username.append(" (").append(config.getPronouns()).append(")");
}
if (player.isOp()) {
username.append(" \u266E");
}
String out;
if (ChatEventListener.meFormat(content) != null) {
out = ((config.getDisplayName() != null) ? config.getDisplayName() : player.getName()) + " " + ChatEventListener.meFormat(content);
out = config.buildNameStringShort(player.getName()) + " " + ChatEventListener.meFormat(content);
} else if (ChatEventListener.greentextFormat(content) != null) {
StringBuilder builder = new StringBuilder();
String[] parts = ChatEventListener.greentextFormat(content);
@ -202,7 +189,7 @@ public class DiscordApp extends ListenerAdapter {
}
WebhookMessage message = new WebhookMessageBuilder()
.setUsername(username.toString())
.setUsername(config.buildNameString(player.getName(), player.isOp()))
.setAvatarUrl("https://cravatar.eu/helmavatar/" + player.getUniqueId() + "/128.png")
.setContent(out)
.build();

View file

@ -15,6 +15,7 @@
*/
package de.blazemcworld.blazinggames.discord;
import de.blazemcworld.blazinggames.utils.PlayerConfig;
import de.blazemcworld.blazinggames.utils.TextUtils;
import io.papermc.paper.advancement.AdvancementDisplay;
import net.dv8tion.jda.api.EmbedBuilder;
@ -88,10 +89,13 @@ public record DiscordNotification(
builder.setTitle("Notification");
builder.setColor(Color.ORANGE);
if (player != null) builder.setAuthor(
player.getName(), null,
"http://cravatar.eu/helmhead/" + player.getUniqueId() + "/128.png"
);
if (player != null) {
PlayerConfig config = PlayerConfig.forPlayer(player.getUniqueId());
builder.setAuthor(
config.buildNameString(player.getName(), player.isOp()), null,
"http://cravatar.eu/helmhead/" + player.getUniqueId() + "/128.png"
);
}
if (title != null) builder.setTitle(title);
if (description != null) builder.setDescription(description);
if (color != null) builder.setColor(color);

View file

@ -22,7 +22,6 @@ import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
@ -43,43 +42,14 @@ public class ChatEventListener implements Listener, ChatRenderer {
@Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) {
// format username
Component username;
PlayerConfig config = PlayerConfig.forPlayer(source.getUniqueId());
if (config.getDisplayName() != null && !config.getDisplayName().equals(source.getName())) {
username = Component.text(config.getDisplayName()).hoverEvent(HoverEvent.showText(Component.text("Real name: " + source.getName())));
} else {
username = Component.text(source.getName()).hoverEvent(HoverEvent.showText(Component.text("Real name: " + source.getName())));
}
if (config.getNameColor() != null) {
username = username.color(config.getNameColor());
}
if (config.getPronouns() != null) {
username = username.appendSpace().append(Component.text("(" + config.getPronouns() + ")")
.color(NamedTextColor.GRAY).hoverEvent(HoverEvent.showText(Component.text("Pronouns"))));
}
if (source.isOp()) {
username = username.appendSpace().append(Component.text("\u266E").color(NamedTextColor.RED)
.hoverEvent(HoverEvent.showText(Component.text("Server Operator"))));
}
// custom formatting
Component username = config.buildNameComponent(source.getName(), source.isOp());
String rawMessage = TextUtils.componentToString(message);
if (meFormat(rawMessage) != null) {
// me when a oneliner needs to be multiline
Component minimalUsername = Component.text(config.getDisplayName() != null ? config.getDisplayName() : source.getName())
.color(config.getNameColor() != null ? config.getNameColor() : NamedTextColor.WHITE)
.hoverEvent(HoverEvent.showText(Component.text("Real name: " + source.getName())
.appendNewline().append(Component.text("Pronouns: " + config.getPronouns() != null ? config.getPronouns() : "None specified"))
.appendNewline().append(Component.text("Server Operator: " + (source.isOp() ? "Yes" : "No")))));
return Component.text("*").color(NamedTextColor.WHITE)
.appendSpace()
.append(minimalUsername)
.append(config.buildNameComponentShort(source.getName(), source.isOp()))
.appendSpace()
.append(TextUtils.colorCodeParser(TextUtils.stringToComponent(meFormat(rawMessage))).color(NamedTextColor.WHITE));
} else if (greentextFormat(rawMessage) != null) {

View file

@ -20,14 +20,18 @@ import de.blazemcworld.blazinggames.computing.api.BlazingAPI;
import de.blazemcworld.blazinggames.discord.DiscordApp;
import de.blazemcworld.blazinggames.discord.DiscordNotification;
import de.blazemcworld.blazinggames.packs.ResourcePackManager.PackConfig;
import de.blazemcworld.blazinggames.utils.PlayerConfig;
import de.blazemcworld.blazinggames.items.recipes.CustomRecipes;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class JoinEventListener implements Listener {
public static final TextColor color = TextColor.color(0xD1F990);
@EventHandler
public void join(PlayerJoinEvent event) {
event.getPlayer().discoverRecipes(CustomRecipes.getAllRecipes().keySet());
@ -44,5 +48,10 @@ public class JoinEventListener implements Listener {
true
);
}
PlayerConfig config = PlayerConfig.forPlayer(event.getPlayer().getUniqueId());
config.updatePlayer(event.getPlayer());
Component name = config.buildNameComponent(event.getPlayer().getName(), event.getPlayer().isOp());
event.joinMessage(Component.empty().append(name).append(Component.text(" joined the game").color(color)));
}
}

View file

@ -18,6 +18,9 @@ package de.blazemcworld.blazinggames.events;
import de.blazemcworld.blazinggames.BlazingGames;
import de.blazemcworld.blazinggames.discord.DiscordApp;
import de.blazemcworld.blazinggames.discord.DiscordNotification;
import de.blazemcworld.blazinggames.utils.PlayerConfig;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
@ -25,6 +28,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
public class QuitEventListener implements Listener {
public static final TextColor color = TextColor.color(0xF99490);
@EventHandler
public void join(PlayerQuitEvent event) {
DiscordApp.send(DiscordNotification.playerLeave(event.getPlayer()));
@ -32,5 +37,9 @@ public class QuitEventListener implements Listener {
if (Bukkit.getOnlinePlayers().size() == 1 && BlazingGames.get().getPackConfig() != null) {
BlazingGames.get().rebuildPack();
}
Component name = PlayerConfig.forPlayer(event.getPlayer().getUniqueId())
.buildNameComponent(event.getPlayer().getName(), event.getPlayer().isOp());
event.quitMessage(Component.empty().append(name).append(Component.text(" left the game").color(color)));
}
}

View file

@ -18,10 +18,15 @@ package de.blazemcworld.blazinggames.utils;
import java.util.Properties;
import java.util.UUID;
import org.bukkit.entity.Player;
import de.blazemcworld.blazinggames.data.DataStorage;
import de.blazemcworld.blazinggames.data.compression.GZipCompressionProvider;
import de.blazemcworld.blazinggames.data.name.UUIDNameProvider;
import de.blazemcworld.blazinggames.data.storage.PropertiesStorageProvider;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
public class PlayerConfig {
@ -48,6 +53,68 @@ public class PlayerConfig {
public void updatePlayer(Player player) {
Component name = buildNameComponent(player.getName(), player.isOp());
player.displayName(name);
player.playerListName(name);
}
public Component buildNameComponent(String playerName, boolean isOp) {
Component username;
if (getDisplayName() != null && !getDisplayName().equals(playerName)) {
username = Component.text(getDisplayName()).hoverEvent(HoverEvent.showText(Component.text("Real name: " + playerName)));
} else {
username = Component.text(playerName).hoverEvent(HoverEvent.showText(Component.text("Real name: " + playerName)));
}
if (getNameColor() != null) {
username = username.color(getNameColor());
}
if (getPronouns() != null) {
username = username.appendSpace().append(Component.text("(" + getPronouns() + ")")
.color(NamedTextColor.GRAY).hoverEvent(HoverEvent.showText(Component.text("Pronouns"))));
}
if (isOp) {
username = username.appendSpace().append(Component.text("\u266E").color(NamedTextColor.RED)
.hoverEvent(HoverEvent.showText(Component.text("Server Operator"))));
}
return username;
}
public Component buildNameComponentShort(String playerName, boolean isOp) {
return Component.text(getDisplayName() != null ? getDisplayName() : playerName)
.color(getNameColor() != null ? getNameColor() : NamedTextColor.WHITE)
.hoverEvent(HoverEvent.showText(Component.text("Real name: " + playerName)
.appendNewline().append(Component.text("Pronouns: " + (getPronouns() != null ? getPronouns() : "None specified")))
.appendNewline().append(Component.text("Server Operator: " + (isOp ? "Yes" : "No")))));
}
public String buildNameString(String playerName, boolean isOp) {
StringBuilder username = new StringBuilder();
if (getDisplayName() != null && !getDisplayName().equals(playerName)) {
username.append(getDisplayName()).append(" [aka ").append(playerName).append("]");
} else {
username.append(playerName);
}
if (getPronouns() != null) {
username.append(" (").append(getPronouns()).append(")");
}
if (isOp) {
username.append(" \u266E");
}
return username.toString();
}
public String buildNameStringShort(String playerName) {
return getDisplayName() != null ? getDisplayName() : playerName;
}
public String getDisplayName() {
String value = props.getProperty("displayname", null);
if (value == null || value.isBlank()) return null;

View file

@ -50,9 +50,10 @@ commands:
playtime:
description: "See how much time you and your friends have wasted on this stupid server."
usage: /playtime [player]
config:
description: "Change display settings for your player. Tab complete for available options."
usage: /config <param> [...value]
display:
description: "Change nameplate display settings for your player."
usage: /display [name|pronouns|color] [value]
aliases: [config, nick]
setaltar:
description: "Set altar with specific level at current location"
usage: /setaltar <level>