This commit is contained in:
Eoghanmc22 2020-06-29 20:44:08 -04:00
commit a194a3f40d
6 changed files with 165 additions and 126 deletions

View File

@ -1,5 +1,7 @@
package net.minestom.server.chat;
import net.minestom.server.utils.validate.Check;
import java.util.HashMap;
import java.util.Map;
@ -7,39 +9,22 @@ public class ChatColor {
public static final ChatColor NO_COLOR = new ChatColor();
public static final ChatColor BLACK = new ChatColor("black", 0);
public static final ChatColor DARK_BLUE = new ChatColor("dark_blue", 1);
public static final ChatColor DARK_GREEN = new ChatColor("dark_green", 2);
public static final ChatColor DARK_CYAN = new ChatColor("dark_cyan", 3);
public static final ChatColor DARK_RED = new ChatColor("dark_red", 4);
public static final ChatColor PURPLE = new ChatColor("dark_purple", 5);
public static final ChatColor GOLD = new ChatColor("gold", 6);
public static final ChatColor GRAY = new ChatColor("gray", 7);
public static final ChatColor DARK_GRAY = new ChatColor("dark_gray", 8);
public static final ChatColor BLUE = new ChatColor("blue", 9);
public static final ChatColor BRIGHT_GREEN = new ChatColor("green", 10);
public static final ChatColor CYAN = new ChatColor("aqua", 11);
public static final ChatColor RED = new ChatColor("red", 12);
public static final ChatColor PINK = new ChatColor("light_purple", 13);
public static final ChatColor YELLOW = new ChatColor("yellow", 14);
public static final ChatColor WHITE = new ChatColor("white", 15);
/*public static final ChatColor BLACK = fromRGB(0, 0, 0);
public static final ChatColor DARK_BLUE = fromRGB(0, 0, 170);
public static final ChatColor DARK_GREEN = fromRGB(0, 170, 0);
public static final ChatColor DARK_CYAN = fromRGB(0, 170, 170);
public static final ChatColor DARK_RED = fromRGB(170, 0, 0);
public static final ChatColor PURPLE = fromRGB(170, 0, 170);
public static final ChatColor GOLD = fromRGB(255, 170, 0);
public static final ChatColor GRAY = fromRGB(170, 170, 170);
public static final ChatColor DARK_GRAY = fromRGB(85, 85, 85);
public static final ChatColor BLUE = fromRGB(85, 85, 255);
public static final ChatColor BRIGHT_GREEN = fromRGB(85, 255, 85);
public static final ChatColor CYAN = fromRGB(85, 255, 255);
public static final ChatColor RED = fromRGB(255, 85, 85);
public static final ChatColor PINK = fromRGB(255, 85, 255);
public static final ChatColor YELLOW = fromRGB(255, 255, 85);
public static final ChatColor WHITE = fromRGB(255, 255, 255);*/
public static final ChatColor BLACK = fromRGB(0, 0, 0, 0);
public static final ChatColor DARK_BLUE = fromRGB(0, 0, 170, 1);
public static final ChatColor DARK_GREEN = fromRGB(0, 170, 0, 2);
public static final ChatColor DARK_CYAN = fromRGB(0, 170, 170, 3);
public static final ChatColor DARK_RED = fromRGB(170, 0, 0, 4);
public static final ChatColor PURPLE = fromRGB(170, 0, 170, 5);
public static final ChatColor GOLD = fromRGB(255, 170, 0, 6);
public static final ChatColor GRAY = fromRGB(170, 170, 170, 7);
public static final ChatColor DARK_GRAY = fromRGB(85, 85, 85, 8);
public static final ChatColor BLUE = fromRGB(85, 85, 255, 9);
public static final ChatColor BRIGHT_GREEN = fromRGB(85, 255, 85, 10);
public static final ChatColor CYAN = fromRGB(85, 255, 255, 11);
public static final ChatColor RED = fromRGB(255, 85, 85, 12);
public static final ChatColor PINK = fromRGB(255, 85, 255, 13);
public static final ChatColor YELLOW = fromRGB(255, 255, 85, 14);
public static final ChatColor WHITE = fromRGB(255, 255, 255, 15);
private static Map<String, ChatColor> colorCode = new HashMap<>();
private static Map<Character, ChatColor> legacyColorCodesMap = new HashMap<>();
@ -82,21 +67,13 @@ public class ChatColor {
private boolean empty;
private int red, green, blue;
private int id;
// 1.15
private String name;
// 1.15
private ChatColor(String name, int id) {
this.name = name;
this.id = id;
}
// 1.16
private ChatColor(int r, int g, int b) {
private ChatColor(int r, int g, int b, int id) {
this.empty = false;
this.red = r;
this.green = g;
this.blue = b;
this.id = id;
}
private ChatColor() {
@ -104,7 +81,11 @@ public class ChatColor {
}
public static ChatColor fromRGB(int r, int g, int b) {
return new ChatColor(r, g, b);
return fromRGB(r, g, b, -1);
}
private static ChatColor fromRGB(int r, int g, int b, int id) {
return new ChatColor(r, g, b, id);
}
public static ChatColor fromName(String name) {
@ -132,20 +113,12 @@ public class ChatColor {
}
public int getId() {
Check.stateCondition(id == -1, "Please use one of the ChatColor constant instead");
return id;
}
// 1.15
public String getName() {
return name;
}
@Override
public String toString() {
// 1.15
if (name != null)
return "{#" + name + "}";
// 1.16
if (empty)
return "";

View File

@ -156,11 +156,11 @@ public class ColoredText {
String colorCode = formatString.substring(1);
ChatColor color = ChatColor.fromName(colorCode);
if (color == ChatColor.NO_COLOR) {
// Use rgb formatting
currentColor = colorCode;
// Use rgb formatting (#ffffff)
currentColor = "#" + colorCode;
} else {
// Use color name formatiing
currentColor = color.getName();
// Use color name formatting (white)
currentColor = colorCode;
}
continue;
}

View File

@ -568,28 +568,28 @@ public class Player extends LivingEntity implements CommandSender {
}
}
@Override
public Consumer<PacketWriter> getMetadataConsumer() {
return packet -> {
super.getMetadataConsumer().accept(packet);
fillMetadataIndex(packet, 14);
fillMetadataIndex(packet, 16);
};
}
@Override
public Consumer<PacketWriter> getMetadataConsumer() {
return packet -> {
super.getMetadataConsumer().accept(packet);
fillMetadataIndex(packet, 14);
fillMetadataIndex(packet, 16);
};
}
@Override
protected void fillMetadataIndex(PacketWriter packet, int index) {
super.fillMetadataIndex(packet, index);
if (index == 14) {
packet.writeByte((byte) 14);
packet.writeByte(METADATA_FLOAT);
packet.writeFloat(additionalHearts);
} else if (index == 16) {
packet.writeByte((byte) 16);
packet.writeByte(METADATA_BYTE);
packet.writeByte(getSettings().getDisplayedSkinParts());
}
}
@Override
protected void fillMetadataIndex(PacketWriter packet, int index) {
super.fillMetadataIndex(packet, index);
if (index == 14) {
packet.writeByte((byte) 14);
packet.writeByte(METADATA_FLOAT);
packet.writeFloat(additionalHearts);
} else if (index == 16) {
packet.writeByte((byte) 16);
packet.writeByte(METADATA_BYTE);
packet.writeByte(getSettings().getDisplayedSkinParts());
}
}
/**
* Send a {@link BlockBreakAnimationPacket} packet to the player and his viewers
@ -620,16 +620,16 @@ public class Player extends LivingEntity implements CommandSender {
* @param coloredText the text to send
*/
public void sendMessage(ColoredText coloredText) {
playerConnection.sendPacket(new ChatMessagePacket(coloredText.toString(), ChatMessagePacket.Position.CHAT));
sendJsonMessage(coloredText.toString());
}
/**
* Send a message to the player
* Send a rich message to the player
*
* @param richMessage the rich text to send
*/
public void sendMessage(RichMessage richMessage) {
playerConnection.sendPacket(new ChatMessagePacket(richMessage.toString(), ChatMessagePacket.Position.CHAT));
sendJsonMessage(richMessage.toString());
}
/**
@ -640,7 +640,7 @@ public class Player extends LivingEntity implements CommandSender {
*/
public void sendLegacyMessage(String text, char colorChar) {
ColoredText coloredText = ColoredText.ofLegacy(text, colorChar);
sendMessage(coloredText);
sendJsonMessage(coloredText.toString());
}
/**
@ -650,7 +650,13 @@ public class Player extends LivingEntity implements CommandSender {
*/
public void sendLegacyMessage(String text) {
ColoredText coloredText = ColoredText.ofLegacy(text, Chat.COLOR_CHAR);
sendMessage(coloredText);
sendJsonMessage(coloredText.toString());
}
public void sendJsonMessage(String json) {
ChatMessagePacket chatMessagePacket =
new ChatMessagePacket(json, ChatMessagePacket.Position.CHAT);
playerConnection.sendPacket(chatMessagePacket);
}
public void playSound(Sound sound, SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) {
@ -1975,15 +1981,15 @@ public class Player extends LivingEntity implements CommandSender {
connection.sendPacket(getPassengersPacket());
}
// Team
if (team != null)
connection.sendPacket(team.getTeamsCreationPacket());
// Team
if (team != null)
connection.sendPacket(team.getTeamsCreationPacket());
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = position.getYaw();
connection.sendPacket(entityHeadLookPacket);
}
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = position.getYaw();
connection.sendPacket(entityHeadLookPacket);
}
@Override
public ItemStack getItemInMainHand() {
@ -2132,28 +2138,28 @@ public class Player extends LivingEntity implements CommandSender {
return mainHand;
}
/**
* Change the player settings internally
* <p>
* WARNING: the player will not be noticed by this change, probably unsafe
*
* @param locale the player locale
* @param viewDistance the player view distance
* @param chatMode the player chat mode
* @param chatColors the player chat colors
* @param displayedSkinParts the player displayed skin parts
* @param mainHand the player main handœ
*/
public void refresh(String locale, byte viewDistance, ChatMode chatMode, boolean chatColors, byte displayedSkinParts, MainHand mainHand) {
this.locale = locale;
this.viewDistance = viewDistance;
this.chatMode = chatMode;
this.chatColors = chatColors;
this.displayedSkinParts = displayedSkinParts;
this.mainHand = mainHand;
sendMetadataIndex(16);
}
/**
* Change the player settings internally
* <p>
* WARNING: the player will not be noticed by this change, probably unsafe
*
* @param locale the player locale
* @param viewDistance the player view distance
* @param chatMode the player chat mode
* @param chatColors the player chat colors
* @param displayedSkinParts the player displayed skin parts
* @param mainHand the player main handœ
*/
public void refresh(String locale, byte viewDistance, ChatMode chatMode, boolean chatColors, byte displayedSkinParts, MainHand mainHand) {
this.locale = locale;
this.viewDistance = viewDistance;
this.chatMode = chatMode;
this.chatColors = chatColors;
this.displayedSkinParts = displayedSkinParts;
this.mainHand = mainHand;
sendMetadataIndex(16);
}
}
}
}

View File

@ -6,7 +6,9 @@ import net.minestom.server.command.CommandManager;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerChatEvent;
import net.minestom.server.event.player.PlayerCommandEvent;
import net.minestom.server.network.PacketWriterUtils;
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import java.util.Collection;
import java.util.function.Function;
@ -50,8 +52,15 @@ public class ChatMessageListener {
textObject = buildDefaultChatMessage(playerChatEvent);
}
for (Player recipient : playerChatEvent.getRecipients()) {
recipient.sendMessage(textObject);
Collection<Player> recipients = playerChatEvent.getRecipients();
if (!recipients.isEmpty()) {
String jsonMessage = textObject.toString();
// Send the message with the correct player UUID
ChatMessagePacket chatMessagePacket =
new ChatMessagePacket(jsonMessage, ChatMessagePacket.Position.CHAT, player.getUuid());
PacketWriterUtils.writeAndSend(recipients, chatMessagePacket);
}
});

View File

@ -1,8 +1,10 @@
package net.minestom.server.network;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.RichMessage;
import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketConsumer;
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import net.minestom.server.network.player.PlayerConnection;
import java.util.*;
@ -47,6 +49,30 @@ public final class ConnectionManager {
return null;
}
/**
* Send a rich message to all online players who validate the condition {@code condition}
*
* @param richMessage the rich message to send
* @param condition the condition to receive the message
*/
public void broadcastMessage(RichMessage richMessage, Function<Player, Boolean> condition) {
Collection<Player> recipients = getRecipients(condition);
if (!recipients.isEmpty()) {
String jsonText = richMessage.toString();
broadcastJson(jsonText, recipients);
}
}
/**
* Send a rich message to all online players without exception
*
* @param richMessage the rich message to send
*/
public void broadcastMessage(RichMessage richMessage) {
broadcastMessage(richMessage, null);
}
/**
* Send a message to all online players who validate the condition {@code condition}
*
@ -54,14 +80,11 @@ public final class ConnectionManager {
* @param condition the condition to receive the message
*/
public void broadcastMessage(ColoredText coloredText, Function<Player, Boolean> condition) {
if (condition == null) {
getOnlinePlayers().forEach(player -> player.sendMessage(coloredText));
} else {
getOnlinePlayers().forEach(player -> {
boolean result = condition.apply(player);
if (result)
player.sendMessage(coloredText);
});
Collection<Player> recipients = getRecipients(condition);
if (!recipients.isEmpty()) {
String jsonText = coloredText.toString();
broadcastJson(jsonText, recipients);
}
}
@ -74,6 +97,30 @@ public final class ConnectionManager {
broadcastMessage(coloredText, null);
}
private void broadcastJson(String json, Collection<Player> recipients) {
ChatMessagePacket chatMessagePacket =
new ChatMessagePacket(json, ChatMessagePacket.Position.SYSTEM_MESSAGE);
PacketWriterUtils.writeAndSend(recipients, chatMessagePacket);
}
private Collection<Player> getRecipients(Function<Player, Boolean> condition) {
Collection<Player> recipients;
// Get the recipients
if (condition == null) {
recipients = getOnlinePlayers();
} else {
recipients = new ArrayList<>();
getOnlinePlayers().forEach(player -> {
boolean result = condition.apply(player);
if (result)
recipients.add(player);
});
}
return recipients;
}
/**
* Those are all the listeners which are called for each packet received
*

View File

@ -10,12 +10,16 @@ public class ChatMessagePacket implements ServerPacket {
private String jsonMessage;
private Position position;
//TODO use real uuid
private UUID uuid = UUID.randomUUID();
private UUID uuid;
public ChatMessagePacket(String jsonMessage, Position position) {
public ChatMessagePacket(String jsonMessage, Position position, UUID uuid) {
this.jsonMessage = jsonMessage;
this.position = position;
this.uuid = uuid;
}
public ChatMessagePacket(String jsonMessage, Position position) {
this(jsonMessage, position, new UUID(0, 0));
}
@Override