Added a new option to change the display color; also, removed legacy color codes where needed!

This commit is contained in:
BlueFox 2024-05-21 00:01:20 +02:00
parent a381034a8f
commit 4079f56a90
14 changed files with 393 additions and 44 deletions

View File

@ -13,6 +13,12 @@ Listeners that are implemented by this plugin:
- PlayerQuitEvent (print a beautified quit message "<< PlayerName")
- PlayerChatEvent (deprecated, but beautify chat messages "PlayerName: message")
Commands implemented:
- set-chat-color: lets you set your name's color in the chat over a nice gui (or as a cmd!)
- currently only temporary, static colors (staying the same after quit/join) coming soon!
And all that in beautiful colors!

View File

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

View File

@ -1,10 +1,10 @@
package de.privacynerd.chatbeautifier;
import de.privacynerd.chatbeautifier.displaynamecolor.ChatColorCommand;
import de.privacynerd.chatbeautifier.displaynamecolor.ChooseChatColorListener;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import de.privacynerd.chatbeautifier.commands.*;
import de.privacynerd.chatbeautifier.guis.*;
public final class ChatBeautifier extends JavaPlugin {
@ -27,8 +27,11 @@ public final class ChatBeautifier extends JavaPlugin {
log("JoinQuitListener has been registered.");
pluginManager.registerEvents(new ChatListener(), this);
log("ChatListener has been registered.");
pluginManager.registerEvents(new ChooseChatColorListener(), this);
log("ChooseChatColorListener has been registered.");
Bukkit.getPluginCommand("set-chat-color").setExecutor(new ChatColorCommand());
log("ChatColorCommand has been set as executor for the command 'set-chat-color'.");
log("Plugin enabled.");
}

View File

@ -1,6 +1,7 @@
package de.privacynerd.chatbeautifier;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -15,11 +16,12 @@ public class ChatListener implements Listener {
event.setCancelled(true);
//player.getDisplayName() + ": " + event.getMessage()
Server server = Bukkit.getServer();
Component message = Component.text(player.getDisplayName() + ": " + event.getMessage());
Component message = player.displayName().append(Component.text(": " + event.getMessage(), NamedTextColor.WHITE));
if(player.isOp()) {
message = player.displayName().append(Component.text(": " + event.getMessage(), NamedTextColor.YELLOW));
}
server.broadcast(message);
}
}

View File

@ -1,5 +1,7 @@
package de.privacynerd.chatbeautifier;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -7,21 +9,25 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class JoinQuitListener implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Component newName;
if(player.isOp()) {
player.setDisplayName("§4[OP] "+player.getName()+"§e");
player.setPlayerListName(player.getDisplayName());
newName = Component.text("[OP] "+player.getName(), NamedTextColor.RED);
player.displayName(newName);
player.playerListName(newName);
} else {
player.setDisplayName("§7"+player.getName()+"§7");
player.setPlayerListName(player.getDisplayName());
newName = Component.text(player.getName(), NamedTextColor.GRAY);
player.displayName(Component.text(player.getName()));
player.playerListName(newName);
}
event.setJoinMessage("§a§l>> §7"+ player.getDisplayName());
event.joinMessage(Component.text(">> ", NamedTextColor.GREEN).append(player.displayName()));
player.playSound(player.getLocation(), Sound.AMBIENT_CRIMSON_FOREST_MOOD, 0.5f, 0.4f);
}
@ -30,6 +36,6 @@ public class JoinQuitListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
event.setQuitMessage("§c§l<< §7"+ player.getDisplayName());
event.quitMessage(Component.text("<< ", NamedTextColor.RED).append(player.displayName()));
}
}

View File

@ -1,26 +0,0 @@
package de.privacynerd.chatbeautifier.commands;
import de.privacynerd.chatbeautifier.ChatBeautifier;
import net.kyori.adventure.text.Component;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class ChatColorCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
ChatBeautifier.INSTANCE.log("Command /set-chat-name executed");
if(!(commandSender instanceof Player)) {
ChatBeautifier.INSTANCE.log("Du bist kein Spieler.");
return true;
}
Player player = (Player) commandSender;
player.sendMessage(Component.text("Dieses Kommando befindet sich noch in der Mache!"));
return true;
}
}

View File

@ -0,0 +1,154 @@
package de.privacynerd.chatbeautifier.displaynamecolor;
import de.privacynerd.chatbeautifier.ChatBeautifier;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class ChatColorCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
ChatBeautifier.INSTANCE.log("Command /set-chat-name executed.");
if(!(commandSender instanceof Player)) {
ChatBeautifier.INSTANCE.log("This command was not executed by a player. Not doing anything.");
return false;
}
Player player = (Player) commandSender;
String validArgument = "";
Component newDisplayName;
// check for the right args
if(args.length == 1) {
switch (args[0]) {
case "black":
validArgument = "black";
newDisplayName = Component.text("", ChatColorUtils.getChooseColorCodes().get(1)).append(player.displayName());
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "gray":
validArgument = "gray";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(1));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "light-gray":
validArgument = "light-gray";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(2));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "white":
validArgument = "white";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(3));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "brown":
validArgument = "brown";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(4));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "red":
validArgument = "red";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(5));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "orange":
validArgument = "orange";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(6));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "yellow":
validArgument = "yellow";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(7));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "light-green":
validArgument = "light-green";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(8));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "dark-green":
validArgument = "dark-green";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(9));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "cyan":
validArgument = "cyan";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(10));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "blue":
validArgument = "blue";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(11));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "purple":
validArgument = "purple";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(12));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "magenta":
validArgument = "magenta";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(13));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "pink":
validArgument = "pink";
newDisplayName = player.displayName().color(ChatColorUtils.getChooseColorCodes().get(14));
player.displayName(newDisplayName);
player.playerListName(newDisplayName);
break;
case "list":
ChatBeautifier.INSTANCE.log("Got valid argument 'list', printing list of valid colors...");
ArrayList<String> colorNames = ChatColorUtils.getChooseColorStrings();
// now compose the message containing all possible color args
StringBuilder message = new StringBuilder(ChatBeautifier.PREFIX + "§r§lMögliche Farben: §r§o");
message.append(colorNames.get(0).toLowerCase().replace(" ", "-")).append(", ");
for (int i=1; i<colorNames.toArray().length; i++) {
message.append(colorNames.get(i).toLowerCase().replace(" ", "-")).append(", ");
}
// and send it
player.sendMessage(message.toString());
return true;
case "gui":
ChatBeautifier.INSTANCE.log("Got valid argument 'gui', opening gui...");
player.openInventory(new ChooseChatColorGUI().getGUI(player));
return true;
default:
// print a help string if no valid argument is given
player.sendMessage(ChatBeautifier.PREFIX + "Verwendung: /" + alias + " [<color>, list, gui]");
return true;
}
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.");
return true;
} 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]");
return true;
} else {
player.openInventory(new ChooseChatColorGUI().getGUI(player));
return true;
}
}
}

View File

@ -0,0 +1,75 @@
package de.privacynerd.chatbeautifier.displaynamecolor;
import de.privacynerd.chatbeautifier.ChatBeautifier;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
// Class containing functions returning arrays for gui creation
public final class ChatColorUtils {
public static @NotNull ArrayList<ItemStack> getChooseColorItems() {
ArrayList<ItemStack> chooseColors = new ArrayList<>();
chooseColors.add(new ItemStack(Material.BLACK_WOOL));
chooseColors.add(new ItemStack(Material.GRAY_WOOL));
chooseColors.add(new ItemStack(Material.LIGHT_GRAY_WOOL));
chooseColors.add(new ItemStack(Material.WHITE_WOOL));
chooseColors.add(new ItemStack(Material.BROWN_WOOL));
chooseColors.add(new ItemStack(Material.RED_WOOL));
chooseColors.add(new ItemStack(Material.ORANGE_WOOL));
chooseColors.add(new ItemStack(Material.YELLOW_WOOL));
chooseColors.add(new ItemStack(Material.LIME_WOOL));
chooseColors.add(new ItemStack(Material.GREEN_WOOL));
chooseColors.add(new ItemStack(Material.CYAN_WOOL));
chooseColors.add(new ItemStack(Material.BLUE_WOOL));
chooseColors.add(new ItemStack(Material.PURPLE_WOOL));
chooseColors.add(new ItemStack(Material.MAGENTA_WOOL));
chooseColors.add(new ItemStack(Material.PINK_WOOL));
return chooseColors;
}
public static @NotNull ArrayList<String> getChooseColorStrings() {
ArrayList<String> chooseColorNames = new ArrayList<>();
chooseColorNames.add("Black");
chooseColorNames.add("Gray");
chooseColorNames.add("Light gray");
chooseColorNames.add("White");
chooseColorNames.add("Brown");
chooseColorNames.add("Red");
chooseColorNames.add("Orange");
chooseColorNames.add("Yellow");
chooseColorNames.add("Light green");
chooseColorNames.add("Dark green");
chooseColorNames.add("Cyan");
chooseColorNames.add("Blue");
chooseColorNames.add("Purple");
chooseColorNames.add("Magenta");
chooseColorNames.add("Pink");
return chooseColorNames;
}
public static @NotNull ArrayList<TextColor> getChooseColorCodes() {
ArrayList<TextColor> getChooseColorCodes = new ArrayList<>();
getChooseColorCodes.add(NamedTextColor.BLACK);
getChooseColorCodes.add(NamedTextColor.DARK_GRAY);
getChooseColorCodes.add(NamedTextColor.GRAY);
getChooseColorCodes.add(NamedTextColor.WHITE);
getChooseColorCodes.add(TextColor.color(135, 72, 0));
getChooseColorCodes.add(TextColor.color(255, 0, 0));
getChooseColorCodes.add(TextColor.color(255, 112, 0));
getChooseColorCodes.add(NamedTextColor.YELLOW);
getChooseColorCodes.add(TextColor.color(103, 255, 0));
getChooseColorCodes.add(TextColor.color(38, 157, 0));
getChooseColorCodes.add(TextColor.color(0, 252, 255));
getChooseColorCodes.add(TextColor.color(0, 92, 255));
getChooseColorCodes.add(TextColor.color(157, 0, 255));
getChooseColorCodes.add(TextColor.color(255, 0, 179));
getChooseColorCodes.add(TextColor.color(255, 87, 228));
return getChooseColorCodes;
}
}

View File

@ -0,0 +1,63 @@
package de.privacynerd.chatbeautifier.displaynamecolor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import de.privacynerd.chatbeautifier.displaynamecolor.ChatColorUtils;
import java.util.ArrayList;
public class ChooseChatColorGUI {
public Inventory getGUI(Player player) {
Inventory gui = Bukkit.createInventory(player, 45, Component.text("Set chat color (0x01)", NamedTextColor.DARK_GRAY));
// create item stacks to be filled into the gui later
ItemStack nothing = new ItemStack(Material.LIGHT_BLUE_STAINED_GLASS_PANE); // for filling the gaps
ArrayList<ItemStack> chooseColorItems = ChatColorUtils.getChooseColorItems();
ArrayList<String> chooseColorNames = ChatColorUtils.getChooseColorStrings();
ArrayList<TextColor> chooseColorCodes = ChatColorUtils.getChooseColorCodes();
for (int i=0; i<15; i++) {
ItemMeta color_meta = nothing.getItemMeta();
if(i==0) { // make it the black text readable
color_meta.displayName(Component.text(chooseColorNames.get(i), NamedTextColor.WHITE));
} else {
color_meta.displayName(Component.text(chooseColorNames.get(i), chooseColorCodes.get(i)));
}
chooseColorItems.get(i).setItemMeta(color_meta);
}
// set item metas (so that a listener can correctly identify the items)
ItemMeta nothing_meta = nothing.getItemMeta();
nothing_meta.displayName(Component.text(" "));
nothing.setItemMeta(nothing_meta);
// fill the glass panes
for (int i=0; i<11; i++) { gui.setItem(i, nothing); }
for (int i=0; i<3; i++) {
gui.setItem(16 + (8 * i) + i, nothing);
gui.setItem(16 + (8 * i) + i + 1, nothing);
gui.setItem(16 + (8 * i) + i + 2, nothing);
gui.setItem(16 + (8 * i) + i + 3, nothing);
}
for (int i=38; i<45; i++) { gui.setItem(i, nothing); }
for(int i=0; i<3; i++) {
gui.setItem(11 + (8 * i) + i, chooseColorItems.get((4 * i) + i));
gui.setItem(11 + (8 * i) + i +1, chooseColorItems.get((4 * i) + i +1));
gui.setItem(11 + (8 * i) + i +2, chooseColorItems.get((4 * i) + i +2));
gui.setItem(11 + (8 * i) + i +3, chooseColorItems.get((4 * i) + i +3));
gui.setItem(11 + (8 * i) + i +4, chooseColorItems.get((4 * i) + i +4));
}
return gui;
}
}

View File

@ -0,0 +1,71 @@
package de.privacynerd.chatbeautifier.displaynamecolor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
public class ChooseChatColorListener implements Listener {
@EventHandler
public void onClickOnItem(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (event.getView().title().equals(Component.text("Set chat color (0x01)", NamedTextColor.DARK_GRAY))) {
switch (event.getCurrentItem().getType()) {
case BLACK_WOOL:
player.performCommand("scc black");
break;
case GRAY_WOOL:
player.performCommand("scc gray");
break;
case LIGHT_GRAY_WOOL:
player.performCommand("scc light-gray");
break;
case WHITE_WOOL:
player.performCommand("scc white");
break;
case BROWN_WOOL:
player.performCommand("scc brown");
break;
case RED_WOOL:
player.performCommand("scc red");
break;
case ORANGE_WOOL:
player.performCommand("scc orange");
break;
case YELLOW_WOOL:
player.performCommand("scc yellow");
break;
case LIME_WOOL:
player.performCommand("scc light-green");
break;
case GREEN_WOOL:
player.performCommand("scc dark-green");
break;
case CYAN_WOOL:
player.performCommand("scc cyan");
break;
case BLUE_WOOL:
player.performCommand("scc blue");
break;
case PURPLE_WOOL:
player.performCommand("scc purple");
break;
case MAGENTA_WOOL:
player.performCommand("scc magenta");
break;
case PINK_WOOL:
player.performCommand("scc pink");
break;
default:
event.setCancelled(true);
return;
}
player.openInventory(new ChooseChatColorGUI().getGUI(player));
event.setCancelled(true);
}
}
}

View File

@ -1,5 +0,0 @@
package de.privacynerd.chatbeautifier.guis;
public class ChooseChatColor {
}

View File

@ -7,4 +7,4 @@ commands:
set-chat-color:
description: Mit diesem Befehl kannst du die Farbe deines Namens setzen.
aliases: ["scc"]
usage: /set-chat-color
usage: /set-chat-color [<color>, list, gui]

Binary file not shown.

Binary file not shown.