From 7b06ab0dc41b80b2393e59d732107ed13e422918 Mon Sep 17 00:00:00 2001 From: Iaiao Date: Tue, 26 Jan 2021 21:20:04 +0200 Subject: [PATCH 1/2] Add formatting to config.yml --- config.yml | 16 ++++++++++++++-- .../lightchatbubbles/ChatBubbles.java | 10 +++++++--- .../netgamer/lightchatbubbles/ChatBuffer.java | 19 ++++++++++++++++++- src/cl/netgamer/lightchatbubbles/Main.java | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/config.yml b/config.yml index d63b124..cf37c1b 100644 --- a/config.yml +++ b/config.yml @@ -22,5 +22,17 @@ handicapChars: 10 # will chat messages only appear in chat bubbles and won't echo on chat window? disableChatWindow: false -# chat bubbles text format, omit section signs, see http://minecraft.gamepedia.com/Formatting_codes -chatFormat: EL +# chat formatting codes for messages, without &. Examples: "EL", "C", "L" +# consider using "&e&l{message}" in chatFormat. This is here only for backwards compatibility purposes +chatFormat: "" + +# chat message format. Applied for each message. Set to "SERVER" to make it the same as in chat. +# examples: "&e{player} said: &f{message}", "{player}: {message}", "&l{message}", "SERVER" +messageFormat: "{message}" + +# bubble format. Applied to every bubble. +# examples: "&e{player} said: &f{message}", "{player}: {message}", "&l{message}", "SERVER" +bubbleFormat: "{message}" + +# row format. Applied for each row. +rowFormat: "&e&l{message}" \ No newline at end of file diff --git a/src/cl/netgamer/lightchatbubbles/ChatBubbles.java b/src/cl/netgamer/lightchatbubbles/ChatBubbles.java index 24dc3b1..19e390b 100644 --- a/src/cl/netgamer/lightchatbubbles/ChatBubbles.java +++ b/src/cl/netgamer/lightchatbubbles/ChatBubbles.java @@ -1,12 +1,11 @@ package cl.netgamer.lightchatbubbles; -import java.util.ArrayList; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.entity.AreaEffectCloud; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; public class ChatBubbles @@ -14,6 +13,7 @@ public class ChatBubbles private int handicapChars; private int readSpeed; private String chatFormat; + private String rowFormat; // constructor public ChatBubbles(Main plugin) @@ -21,6 +21,7 @@ public ChatBubbles(Main plugin) handicapChars = plugin.getConfig().getInt("handicapChars"); readSpeed = plugin.getConfig().getInt("readSpeed"); chatFormat = plugin.getConfig().getString("chatFormat").replaceAll("(.)", "\u00A7$1"); + rowFormat = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("rowFormat")); } // recieve chat to be displayed, return display duration in ticks so previous method can schedule next @@ -28,7 +29,6 @@ int receiveMessage(Player player, String playerId, String chat) { // prepare chat message and empty bubble String[] chatLines = chat.split("\n"); - new ArrayList(); // calculate bubble duration, 1200 = ticks per minute, to convert readSpeed to ticks int duration = (chat.length()+(handicapChars*chatLines.length))*1200/readSpeed; @@ -45,6 +45,10 @@ int receiveMessage(Player player, String playerId, String chat) // spawn a nameplate and return it to caller so it can stack together private AreaEffectCloud spawnNameTag(Entity vehicle, String text, Location spawnPoint, int duration) { + text = rowFormat + .replaceAll("\\{player}", vehicle.getType() == EntityType.PLAYER ? ((Player) vehicle).getDisplayName() : vehicle.getName()) + .replaceAll("\\{message}", text); + // spawn name tag away from player in same chunk, then set invisible AreaEffectCloud nameTag = (AreaEffectCloud) spawnPoint.getWorld().spawnEntity(spawnPoint, EntityType.AREA_EFFECT_CLOUD); nameTag.setParticle(Particle.TOWN_AURA); // ITEM_TAKE was deprecated so i found mycelium (TOWN_AURA) has the tiniest particle diff --git a/src/cl/netgamer/lightchatbubbles/ChatBuffer.java b/src/cl/netgamer/lightchatbubbles/ChatBuffer.java index 8e5b555..2fbff38 100644 --- a/src/cl/netgamer/lightchatbubbles/ChatBuffer.java +++ b/src/cl/netgamer/lightchatbubbles/ChatBuffer.java @@ -5,6 +5,8 @@ import java.util.Map; import java.util.Queue; +import org.bukkit.ChatColor; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -16,6 +18,8 @@ public class ChatBuffer private int maxBubbleWidth; private int bubblesInterval; private Map> chatQueue = new HashMap>(); + private String bubbleFormat; + private String messageFormat; // constructor ChatBuffer(Main plugin) @@ -23,12 +27,22 @@ public class ChatBuffer maxBubbleHeight = plugin.getConfig().getInt("maxBubbleHeight"); maxBubbleWidth = plugin.getConfig().getInt("maxBubbleWidth"); bubblesInterval = plugin.getConfig().getInt("bubblesInterval"); + bubbleFormat = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("bubbleFormat")); + messageFormat = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("messageFormat")); this.plugin = plugin; } // wrap pre-trimmed chat and put in a player buffer - void receiveChat(Player player, String msg) + void receiveChat(Player player, String msg, String format) { + if(messageFormat.equals("SERVER")) { + msg = String.format(format, player.getName(), msg); + } else { + msg = messageFormat + .replaceAll("\\{player}", player.getDisplayName()) + .replaceAll("\\{message}", msg); + } + // most probable case, 1 line chat if (msg.length() <= maxBubbleWidth) { @@ -69,6 +83,9 @@ void receiveChat(Player player, String msg) // get word wrapped chat and queues in a player buffer, creates if not exists private void queueChat(Player player, String chat) { + chat = bubbleFormat + .replaceAll("\\{player}", player.getDisplayName()) + .replaceAll("\\{message}", chat); // if no player buffer yet, create it and schedule this message String playerId = ""+player.getUniqueId(); if (!chatQueue.containsKey(playerId)) diff --git a/src/cl/netgamer/lightchatbubbles/Main.java b/src/cl/netgamer/lightchatbubbles/Main.java index f1be653..e4b4e60 100644 --- a/src/cl/netgamer/lightchatbubbles/Main.java +++ b/src/cl/netgamer/lightchatbubbles/Main.java @@ -28,7 +28,7 @@ public void onPlayerChat(AsyncPlayerChatEvent e) { if (!e.isCancelled()) { - buffer.receiveChat(e.getPlayer(), e.getMessage()); + buffer.receiveChat(e.getPlayer(), e.getMessage(), e.getFormat()); if(disableChatWindow) e.setCancelled(true); From e37bf1990affe665b57eb03928630135a8376f35 Mon Sep 17 00:00:00 2001 From: Iaiao Date: Tue, 26 Jan 2021 21:25:57 +0200 Subject: [PATCH 2/2] Remove SERVER from bubbleFormat example as it's only available in messageFormat --- config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.yml b/config.yml index cf37c1b..a08603e 100644 --- a/config.yml +++ b/config.yml @@ -31,8 +31,8 @@ chatFormat: "" messageFormat: "{message}" # bubble format. Applied to every bubble. -# examples: "&e{player} said: &f{message}", "{player}: {message}", "&l{message}", "SERVER" +# examples: "&e{player} said: &f{message}", "{player}: {message}", "&l{message}" bubbleFormat: "{message}" # row format. Applied for each row. -rowFormat: "&e&l{message}" \ No newline at end of file +rowFormat: "&e&l{message}"