Removed all legacy color codes and replaced it with Components :); some code refactoring for better readability

This commit is contained in:
BlueFox 2024-05-21 12:49:39 +02:00
parent 4079f56a90
commit eb429dfaed
6 changed files with 68 additions and 62 deletions

View File

@ -6,7 +6,7 @@
<groupId>de.privacynerd</groupId> <groupId>de.privacynerd</groupId>
<artifactId>ChatBeautifier</artifactId> <artifactId>ChatBeautifier</artifactId>
<version>0.2.2</version> <version>0.2.3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>ChatBeautifier</name> <name>ChatBeautifier</name>

View File

@ -2,21 +2,28 @@ package de.privacynerd.chatbeautifier;
import de.privacynerd.chatbeautifier.displaynamecolor.ChatColorCommand; import de.privacynerd.chatbeautifier.displaynamecolor.ChatColorCommand;
import de.privacynerd.chatbeautifier.displaynamecolor.ChooseChatColorListener; import de.privacynerd.chatbeautifier.displaynamecolor.ChooseChatColorListener;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public final class ChatBeautifier extends JavaPlugin { public final class ChatBeautifier extends JavaPlugin {
public static String PREFIX = "§a[ChatBeautifier] §7§o"; public static Component PREFIX = Component.text("[ChatBeautifier] ", NamedTextColor.DARK_AQUA);
public static ChatBeautifier INSTANCE; public static ChatBeautifier INSTANCE;
public ChatBeautifier() { public ChatBeautifier() {
INSTANCE = this; INSTANCE = this;
} }
public void log(String text) {
Bukkit.getConsoleSender().sendMessage(PREFIX + text); // some log functions
} public void log(String message) { Bukkit.getConsoleSender().sendMessage(PREFIX.append(Component.text(message, NamedTextColor.GRAY))); }
public void log(@NotNull Component message) { Bukkit.getConsoleSender().sendMessage(PREFIX.append(message.color(NamedTextColor.GRAY))); }
public void tellPlayer(String message, @NotNull Player player) { player.sendMessage(PREFIX.append(Component.text(message, NamedTextColor.GRAY))); }
public void tellPlayer(@NotNull Component message, @NotNull Player player) { player.sendMessage(PREFIX.append(message.color(NamedTextColor.GRAY))); }
@Override @Override

View File

@ -2,6 +2,7 @@ package de.privacynerd.chatbeautifier;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -18,16 +19,28 @@ public class JoinQuitListener implements Listener {
Component newName; Component newName;
if(player.isOp()) { if(player.isOp()) {
newName = Component.text("[OP] "+player.getName(), NamedTextColor.RED); newName = Component.text("[OP] ")
.color(NamedTextColor.RED)
.decoration(TextDecoration.BOLD, true)
.append(Component.text(player.getName())
.color(NamedTextColor.GRAY)
.decoration(TextDecoration.BOLD, false)
);
player.displayName(newName); player.displayName(newName);
player.playerListName(newName); player.playerListName(newName);
} else { } else {
newName = Component.text(player.getName(), NamedTextColor.GRAY); newName = Component.text(player.getName())
player.displayName(Component.text(player.getName())); .color(NamedTextColor.GRAY)
.decoration(TextDecoration.BOLD, false);
player.displayName(newName);
player.playerListName(newName); player.playerListName(newName);
} }
event.joinMessage(Component.text(">> ", NamedTextColor.GREEN).append(player.displayName())); event.joinMessage(Component.text(">> ")
.color(NamedTextColor.GREEN)
.decoration(TextDecoration.BOLD, true)
.append(player.displayName()
));
player.playSound(player.getLocation(), Sound.AMBIENT_CRIMSON_FOREST_MOOD, 0.5f, 0.4f); player.playSound(player.getLocation(), Sound.AMBIENT_CRIMSON_FOREST_MOOD, 0.5f, 0.4f);
} }
@ -35,7 +48,11 @@ public class JoinQuitListener implements Listener {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Component displayName = player.displayName();
event.quitMessage(Component.text("<< ", NamedTextColor.RED).append(player.displayName())); event.quitMessage(Component.text("<< ")
.color(NamedTextColor.RED)
.decoration(TextDecoration.BOLD, true)
.append(displayName));
} }
} }

View File

@ -3,6 +3,7 @@ package de.privacynerd.chatbeautifier.displaynamecolor;
import de.privacynerd.chatbeautifier.ChatBeautifier; import de.privacynerd.chatbeautifier.ChatBeautifier;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -23,113 +24,88 @@ public class ChatColorCommand implements CommandExecutor {
Player player = (Player) commandSender; Player player = (Player) commandSender;
String validArgument = ""; String validArgument = "";
Component newDisplayName; Component newDisplayName;
int colorCodesListIndex; // stores the index of the color code which was requested (0 for black color code, ...)
// check for the right args // check for the right args
if(args.length == 1) { if(args.length == 1) {
switch (args[0]) { switch (args[0]) {
case "black": case "black":
validArgument = "black"; validArgument = "black";
newDisplayName = Component.text("", ChatColorUtils.getChooseColorCodes().get(1)).append(player.displayName()); colorCodesListIndex = 0;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "gray": case "gray":
validArgument = "gray"; validArgument = "gray";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(1)); colorCodesListIndex = 1;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "light-gray": case "light-gray":
validArgument = "light-gray"; validArgument = "light-gray";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(2)); colorCodesListIndex = 2;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "white": case "white":
validArgument = "white"; validArgument = "white";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(3)); colorCodesListIndex = 3;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "brown": case "brown":
validArgument = "brown"; validArgument = "brown";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(4)); colorCodesListIndex = 4;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "red": case "red":
validArgument = "red"; validArgument = "red";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(5)); colorCodesListIndex = 5;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "orange": case "orange":
validArgument = "orange"; validArgument = "orange";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(6)); colorCodesListIndex = 6;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "yellow": case "yellow":
validArgument = "yellow"; validArgument = "yellow";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(7)); colorCodesListIndex = 7;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "light-green": case "light-green":
validArgument = "light-green"; validArgument = "light-green";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(8)); colorCodesListIndex = 8;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "dark-green": case "dark-green":
validArgument = "dark-green"; validArgument = "dark-green";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(9)); colorCodesListIndex = 9;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "cyan": case "cyan":
validArgument = "cyan"; validArgument = "cyan";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(10)); colorCodesListIndex = 10;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "blue": case "blue":
validArgument = "blue"; validArgument = "blue";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(11)); colorCodesListIndex = 11;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "purple": case "purple":
validArgument = "purple"; validArgument = "purple";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(12)); colorCodesListIndex = 12;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "magenta": case "magenta":
validArgument = "magenta"; validArgument = "magenta";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(13)); colorCodesListIndex = 13;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "pink": case "pink":
validArgument = "pink"; validArgument = "pink";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(14)); colorCodesListIndex = 14;
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break; break;
case "list": case "list":
ChatBeautifier.INSTANCE.log("Got valid argument 'list', printing list of valid colors..."); ChatBeautifier.INSTANCE.log("Got valid argument 'list', printing list of valid colors...");
ArrayList<String> colorNames = ChatColorUtils.getChooseColorStrings(); ArrayList<String> colorNames = ChatColorUtils.getChooseColorStrings();
// now compose the message containing all possible color args // now compose the message containing all possible color args
StringBuilder message = new StringBuilder(ChatBeautifier.PREFIX + "§r§lMögliche Farben: §r§o"); Component message = Component.text("Mögliche Farben: ").decoration(TextDecoration.BOLD, true);
message.append(colorNames.get(0).toLowerCase().replace(" ", "-")).append(", ");
String newMessageItem = colorNames.get(0).toLowerCase().replace(" ", "-") + ", ";
message = message.append(Component.text(newMessageItem, NamedTextColor.GRAY).decoration(TextDecoration.BOLD, true));
for (int i=1; i<colorNames.toArray().length; i++) { for (int i=1; i<colorNames.toArray().length; i++) {
message.append(colorNames.get(i).toLowerCase().replace(" ", "-")).append(", "); newMessageItem = colorNames.get(i).toLowerCase().replace(" ", "-") + ", ";
message = message.append(Component.text(newMessageItem, NamedTextColor.GRAY).decoration(TextDecoration.BOLD, true));
} }
// and send it // and send it
player.sendMessage(message.toString()); ChatBeautifier.INSTANCE.tellPlayer(message, player);
return true; return true;
case "gui": case "gui":
ChatBeautifier.INSTANCE.log("Got valid argument 'gui', opening gui..."); ChatBeautifier.INSTANCE.log("Got valid argument 'gui', opening gui...");
@ -137,14 +113,20 @@ public class ChatColorCommand implements CommandExecutor {
return true; return true;
default: default:
// print a help string if no valid argument is given // print a help string if no valid argument is given
player.sendMessage(ChatBeautifier.PREFIX + "Verwendung: /" + alias + " [<color>, list, gui]"); ChatBeautifier.INSTANCE.tellPlayer("Verwendung: /" + alias + " [<color>, list, gui]", player);
return true; return true;
} }
newDisplayName = player.displayName()
.color(ChatColorUtils.getChooseColorCodes().get(colorCodesListIndex));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
ChatBeautifier.INSTANCE.log("Got valid color " + validArgument); ChatBeautifier.INSTANCE.log("Got valid color " + validArgument);
player.sendMessage(ChatBeautifier.PREFIX + "Name temporär " + validArgument + " gefärbt. Dieses Feature ist aktuell noch in der Mache, dauerhaft coming soon."); ChatBeautifier.INSTANCE.tellPlayer("Name temporär " + validArgument + " gefärbt. Dieses Feature ist aktuell noch in der Mache, dauerhaft coming soon.", player);
return true; return true;
} else if (args.length > 1) { // print a help string if more than one argument is given } else if (args.length > 1) { // print a help string if more than one argument is given
player.sendMessage(ChatBeautifier.PREFIX + "Verwendung: /" + alias + " [<color>, list, gui]"); ChatBeautifier.INSTANCE.tellPlayer("Verwendung: /" + alias + " [<color>, list, gui]", player);
return true; return true;
} else { } else {
player.openInventory(new ChooseChatColorGUI().getGUI(player)); player.openInventory(new ChooseChatColorGUI().getGUI(player));

Binary file not shown.

Binary file not shown.