diff --git a/pom.xml b/pom.xml index cdc109a..a5cbd5b 100644 --- a/pom.xml +++ b/pom.xml @@ -76,5 +76,10 @@ adventure-platform-bukkit 4.3.3 + + net.kyori + adventure-text-minimessage + 4.17.0 + diff --git a/src/main/java/org/twipnetwork/colorfulwords/ChatListener.java b/src/main/java/org/twipnetwork/colorfulwords/ChatListener.java index 92d7ccb..20dcde6 100644 --- a/src/main/java/org/twipnetwork/colorfulwords/ChatListener.java +++ b/src/main/java/org/twipnetwork/colorfulwords/ChatListener.java @@ -2,6 +2,8 @@ package org.twipnetwork.colorfulwords; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -16,10 +18,21 @@ public class ChatListener implements Listener { @EventHandler public void onPlayerChat(AsyncPlayerChatEvent event) { String colorHex = plugin.getPlayerColor(event.getPlayer().getUniqueId()); - if (colorHex != null) { - TextColor color = TextColor.fromHexString(colorHex); - String coloredMessage = Component.text(event.getMessage()).color(color).toString(); - event.setFormat("%1$s: " + coloredMessage); + if (colorHex == null) { + colorHex = "#ffffff"; } + String playerName = event.getPlayer().getName(); + String messageContent = event.getMessage(); + TextColor color = TextColor.fromHexString(colorHex); + var mm = MiniMessage.miniMessage(); + var recipents = event.getRecipients(); + var newmessage = mm.deserialize( + "<<" + color + ">" + playerName + "> <" + color + ">" + messageContent + "" + ); + for (Player recipient :recipents){ + recipient.sendMessage(newmessage); + } + + event.setCancelled(true); } } diff --git a/src/main/java/org/twipnetwork/colorfulwords/ColorCommand.java b/src/main/java/org/twipnetwork/colorfulwords/ColorCommand.java index 1b5fbc2..f051c13 100644 --- a/src/main/java/org/twipnetwork/colorfulwords/ColorCommand.java +++ b/src/main/java/org/twipnetwork/colorfulwords/ColorCommand.java @@ -1,40 +1,43 @@ package org.twipnetwork.colorfulwords; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.ChatColor; 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 ColorCommand implements CommandExecutor { private final ColorfulWords plugin; + private final MiniMessage mm; public ColorCommand(ColorfulWords plugin) { + this.mm = MiniMessage.miniMessage(); this.plugin = plugin; } @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) { + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (!(sender instanceof Player player)) { sender.sendMessage("This command can only be used by players."); return true; } - Player player = (Player) sender; if (args.length != 1) { - player.sendMessage(ChatColor.RED + "Usage: /color |reset"); + player.sendMessage(mm.deserialize("Usage: /color |reset")); return true; } String colorHex = args[0]; if (colorHex.equalsIgnoreCase("reset")) { plugin.resetPlayerColor(player.getUniqueId()); - player.sendMessage(ChatColor.GREEN + "Your chat color has been reset."); + player.sendMessage(mm.deserialize("Your chat color has been reset.")); } else if (colorHex.matches("^#[a-fA-F0-9]{6}$")) { plugin.setPlayerColor(player.getUniqueId(), colorHex); - player.sendMessage(ChatColor.GREEN + "Your chat color has been set to " + colorHex + "."); + player.sendMessage(mm.deserialize("Your chat color has been set to <" + colorHex + ">" + colorHex + ".")); } else { - player.sendMessage(ChatColor.RED + "Invalid hex color. Please use a valid hex code."); + player.sendMessage(mm.deserialize("Invalid hex color. Please use a valid hex code.")); } return true; diff --git a/src/main/java/org/twipnetwork/colorfulwords/ColorfulWords.java b/src/main/java/org/twipnetwork/colorfulwords/ColorfulWords.java index cc27639..02ba0f5 100644 --- a/src/main/java/org/twipnetwork/colorfulwords/ColorfulWords.java +++ b/src/main/java/org/twipnetwork/colorfulwords/ColorfulWords.java @@ -3,6 +3,7 @@ package org.twipnetwork.colorfulwords; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Objects; import java.util.UUID; public final class ColorfulWords extends JavaPlugin { @@ -12,7 +13,7 @@ public final class ColorfulWords extends JavaPlugin { // Plugin startup logic saveDefaultConfig(); getServer().getPluginManager().registerEvents(new ChatListener(this), this); - getCommand("color").setExecutor(new ColorCommand(this)); + Objects.requireNonNull(getCommand("color")).setExecutor(new ColorCommand(this)); } @Override diff --git a/target/ColorfulWords-1.0-1.21-shaded.jar b/target/ColorfulWords-1.0-1.21-shaded.jar index dadd1fa..87f3e43 100644 Binary files a/target/ColorfulWords-1.0-1.21-shaded.jar and b/target/ColorfulWords-1.0-1.21-shaded.jar differ diff --git a/target/ColorfulWords-1.0-1.21.jar b/target/ColorfulWords-1.0-1.21.jar index dadd1fa..87f3e43 100644 Binary files a/target/ColorfulWords-1.0-1.21.jar and b/target/ColorfulWords-1.0-1.21.jar differ diff --git a/target/classes/org/twipnetwork/colorfulwords/ChatListener.class b/target/classes/org/twipnetwork/colorfulwords/ChatListener.class index a0f90f2..fdad9c8 100644 Binary files a/target/classes/org/twipnetwork/colorfulwords/ChatListener.class and b/target/classes/org/twipnetwork/colorfulwords/ChatListener.class differ diff --git a/target/classes/org/twipnetwork/colorfulwords/ColorCommand.class b/target/classes/org/twipnetwork/colorfulwords/ColorCommand.class index e2ffc1e..5344493 100644 Binary files a/target/classes/org/twipnetwork/colorfulwords/ColorCommand.class and b/target/classes/org/twipnetwork/colorfulwords/ColorCommand.class differ diff --git a/target/classes/org/twipnetwork/colorfulwords/ColorfulWords.class b/target/classes/org/twipnetwork/colorfulwords/ColorfulWords.class index a89e34a..9361440 100644 Binary files a/target/classes/org/twipnetwork/colorfulwords/ColorfulWords.class and b/target/classes/org/twipnetwork/colorfulwords/ColorfulWords.class differ diff --git a/target/original-ColorfulWords-1.0-1.21.jar b/target/original-ColorfulWords-1.0-1.21.jar index 399e47e..bcba65a 100644 Binary files a/target/original-ColorfulWords-1.0-1.21.jar and b/target/original-ColorfulWords-1.0-1.21.jar differ