Customizable chat formatting (Closes #5387)

This commit is contained in:
Anuken
2021-10-10 12:37:07 -04:00
parent 919db8cc76
commit c60bd6f0dc
4 changed files with 36 additions and 45 deletions

View File

@@ -187,15 +187,15 @@ public class NetClient implements ApplicationListener{
effect(effect, x, y, rotation, color); effect(effect, x, y, rotation, color);
} }
//called on all clients
@Remote(targets = Loc.server, variants = Variant.both) @Remote(targets = Loc.server, variants = Variant.both)
public static void sendMessage(String message, String sender, Player playersender){ public static void sendMessage(String message, @Nullable String unformatted, @Nullable Player playersender){
if(Vars.ui != null){ if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, sender); Vars.ui.chatfrag.addMessage(message);
} }
if(playersender != null){ //display raw unformatted text above player head
playersender.lastText(message); if(playersender != null && unformatted != null){
playersender.lastText(unformatted);
playersender.textFadeTime(1f); playersender.textFadeTime(1f);
} }
} }
@@ -204,7 +204,7 @@ public class NetClient implements ApplicationListener{
@Remote(called = Loc.server, targets = Loc.server) @Remote(called = Loc.server, targets = Loc.server)
public static void sendMessage(String message){ public static void sendMessage(String message){
if(Vars.ui != null){ if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, null); Vars.ui.chatfrag.addMessage(message);
} }
} }
@@ -240,7 +240,7 @@ public class NetClient implements ApplicationListener{
//special case; graphical server needs to see its message //special case; graphical server needs to see its message
if(!headless){ if(!headless){
sendMessage(message, colorizeName(player.id, player.name), player); sendMessage(netServer.chatFormatter.format(player, message), message, player);
} }
//server console logging //server console logging
@@ -248,7 +248,7 @@ public class NetClient implements ApplicationListener{
//invoke event for all clients but also locally //invoke event for all clients but also locally
//this is required so other clients get the correct name even if they don't know who's sending it yet //this is required so other clients get the correct name even if they don't know who's sending it yet
Call.sendMessage(message, colorizeName(player.id(), player.name), player); Call.sendMessage(netServer.chatFormatter.format(player, message), message, player);
}else{ }else{
//a command was sent, now get the output //a command was sent, now get the output
@@ -284,12 +284,6 @@ public class NetClient implements ApplicationListener{
} }
} }
public static String colorizeName(int id, String name){
Player player = Groups.player.getByID(id);
if(name == null || player == null) return null;
return "[#" + player.color().toString().toUpperCase() + "]" + name;
}
@Remote(called = Loc.client, variants = Variant.one) @Remote(called = Loc.client, variants = Variant.one)
public static void connect(String ip, int port){ public static void connect(String ip, int port){
if(!steam && ip.startsWith("steam:")) return; if(!steam && ip.startsWith("steam:")) return;

View File

@@ -64,6 +64,8 @@ public class NetServer implements ApplicationListener{
return state.rules.defaultTeam; return state.rules.defaultTeam;
}; };
/** Converts a message + NULLABLE player sender into a single string. Override for custom prefixes/suffixes. */
public ChatFormatter chatFormatter = (player, message) -> player == null ? message : "[coral][[" + player.coloredName() + "[coral]]:[white] " + message;
private boolean closing = false; private boolean closing = false;
private Interval timer = new Interval(); private Interval timer = new Interval();
@@ -293,17 +295,19 @@ public class NetServer implements ApplicationListener{
clientCommands.<Player>register("t", "<message...>", "Send a message only to your teammates.", (args, player) -> { clientCommands.<Player>register("t", "<message...>", "Send a message only to your teammates.", (args, player) -> {
String message = admins.filterMessage(player, args[0]); String message = admins.filterMessage(player, args[0]);
if(message != null){ if(message != null){
Groups.player.each(p -> p.team() == player.team(), o -> o.sendMessage(message, player, "[#" + player.team().color.toString() + "]<T>" + NetClient.colorizeName(player.id(), player.name))); String raw = "[#" + player.team().color.toString() + "]<T> " + chatFormatter.format(player, message);
Groups.player.each(p -> p.team() == player.team(), o -> o.sendMessage(raw, player, message));
} }
}); });
clientCommands.<Player>register("a", "<message...>", "Send a message only to admins.", (args, player) -> { clientCommands.<Player>register("a", "<message...>", "Send a message only to admins.", (args, player) -> {
if(!player.admin){ if(!player.admin){
player.sendMessage("[scarlet]You must be admin to use this command."); player.sendMessage("[scarlet]You must be an admin to use this command.");
return; return;
} }
Groups.player.each(Player::admin, a -> a.sendMessage(args[0], player, "[#" + Pal.adminChat.toString() + "]<A>" + NetClient.colorizeName(player.id, player.name))); String raw = "[#" + Pal.adminChat.toString() + "]<A> " + chatFormatter.format(player, args[0]);
Groups.player.each(Player::admin, a -> a.sendMessage(raw, player, args[0]));
}); });
//duration of a a kick in seconds //duration of a a kick in seconds
@@ -981,4 +985,9 @@ public class NetServer implements ApplicationListener{
public interface TeamAssigner{ public interface TeamAssigner{
Team assign(Player player, Iterable<Player> players); Team assign(Player player, Iterable<Player> players);
} }
public interface ChatFormatter{
/** @return text to be placed before player name */
String format(@Nullable Player player, String message);
}
} }

View File

@@ -9,7 +9,6 @@ import arc.util.*;
import arc.util.pooling.*; import arc.util.pooling.*;
import mindustry.annotations.Annotations.*; import mindustry.annotations.Annotations.*;
import mindustry.content.*; import mindustry.content.*;
import mindustry.core.*;
import mindustry.entities.units.*; import mindustry.entities.units.*;
import mindustry.game.EventType.*; import mindustry.game.EventType.*;
import mindustry.game.*; import mindustry.game.*;
@@ -310,10 +309,15 @@ abstract class PlayerComp implements UnitController, Entityc, Syncc, Timerc, Dra
Draw.z(z); Draw.z(z);
} }
/** @return name with a markup color prefix */
String coloredName(){
return "[#" + color.toString().toUpperCase() + "]" + name;
}
void sendMessage(String text){ void sendMessage(String text){
if(isLocal()){ if(isLocal()){
if(ui != null){ if(ui != null){
ui.chatfrag.addMessage(text, null); ui.chatfrag.addMessage(text);
} }
}else{ }else{
Call.sendMessage(con, text, null, null); Call.sendMessage(con, text, null, null);
@@ -321,16 +325,16 @@ abstract class PlayerComp implements UnitController, Entityc, Syncc, Timerc, Dra
} }
void sendMessage(String text, Player from){ void sendMessage(String text, Player from){
sendMessage(text, from, NetClient.colorizeName(from.id(), from.name)); sendMessage(text, from, null);
} }
void sendMessage(String text, Player from, String fromName){ void sendMessage(String text, Player from, String unformatted){
if(isLocal()){ if(isLocal()){
if(ui != null){ if(ui != null){
ui.chatfrag.addMessage(text, fromName); ui.chatfrag.addMessage(text);
} }
}else{ }else{
Call.sendMessage(con, text, fromName, from); Call.sendMessage(con, text, unformatted, from);
} }
} }

View File

@@ -23,7 +23,7 @@ import static mindustry.Vars.*;
public class ChatFragment extends Table{ public class ChatFragment extends Table{
private static final int messagesShown = 10; private static final int messagesShown = 10;
private Seq<ChatMessage> messages = new Seq<>(); private Seq<String> messages = new Seq<>();
private float fadetime; private float fadetime;
private boolean shown = false; private boolean shown = false;
private TextField chatfield; private TextField chatfield;
@@ -144,13 +144,13 @@ public class ChatFragment extends Table{
float theight = offsety + spacing + getMarginBottom() + scene.marginBottom; float theight = offsety + spacing + getMarginBottom() + scene.marginBottom;
for(int i = scrollPos; i < messages.size && i < messagesShown + scrollPos && (i < fadetime || shown); i++){ for(int i = scrollPos; i < messages.size && i < messagesShown + scrollPos && (i < fadetime || shown); i++){
layout.setText(font, messages.get(i).formattedMessage, Color.white, textWidth, Align.bottomLeft, true); layout.setText(font, messages.get(i), Color.white, textWidth, Align.bottomLeft, true);
theight += layout.height + textspacing; theight += layout.height + textspacing;
if(i - scrollPos == 0) theight -= textspacing + 1; if(i - scrollPos == 0) theight -= textspacing + 1;
font.getCache().clear(); font.getCache().clear();
font.getCache().setColor(Color.white); font.getCache().setColor(Color.white);
font.getCache().addText(messages.get(i).formattedMessage, fontoffsetx + offsetx, offsety + theight, textWidth, Align.bottomLeft, true); font.getCache().addText(messages.get(i), fontoffsetx + offsetx, offsety + theight, textWidth, Align.bottomLeft, true);
if(!shown && fadetime - i < 1f && fadetime - i >= 0f){ if(!shown && fadetime - i < 1f && fadetime - i >= 0f){
font.getCache().setAlphas((fadetime - i) * opacity); font.getCache().setAlphas((fadetime - i) * opacity);
@@ -257,9 +257,9 @@ public class ChatFragment extends Table{
return shown; return shown;
} }
public void addMessage(String message, String sender){ public void addMessage(String message){
if(sender == null && message == null) return; if(message == null) return;
messages.insert(0, new ChatMessage(message, sender)); messages.insert(0, message);
fadetime += 1f; fadetime += 1f;
fadetime = Math.min(fadetime, messagesShown) + 1f; fadetime = Math.min(fadetime, messagesShown) + 1f;
@@ -267,22 +267,6 @@ public class ChatFragment extends Table{
if(scrollPos > 0) scrollPos++; if(scrollPos > 0) scrollPos++;
} }
private static class ChatMessage{
public final String sender;
public final String message;
public final String formattedMessage;
public ChatMessage(String message, String sender){
this.message = message;
this.sender = sender;
if(sender == null){ //no sender, this is a server message?
formattedMessage = message == null ? "" : message;
}else{
formattedMessage = "[coral][[" + sender + "[coral]]:[white] " + message;
}
}
}
private enum ChatMode{ private enum ChatMode{
normal(""), normal(""),
team("/t"), team("/t"),