From 7feb987c5cec0bb444f3d5cb6ae28728274c3361 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sat, 14 Jul 2018 14:24:53 +0300 Subject: [PATCH] fix Chat --- .../proto_125/serializers/TextSerializer.java | 54 +++++++++++++++++++ .../proto_125/netty/PacketHandler.java | 6 +-- .../netty/wrappers/WrapperNetChannel.java | 17 ++++-- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 proto125/src/main/java/mc/core/network/proto_125/serializers/TextSerializer.java diff --git a/proto125/src/main/java/mc/core/network/proto_125/serializers/TextSerializer.java b/proto125/src/main/java/mc/core/network/proto_125/serializers/TextSerializer.java new file mode 100644 index 0000000..9b53e3a --- /dev/null +++ b/proto125/src/main/java/mc/core/network/proto_125/serializers/TextSerializer.java @@ -0,0 +1,54 @@ +/* + * DmitriyMX + * 2018-07-14 + */ +package mc.core.network.proto_125.serializers; + +import mc.core.text.Text; +import mc.core.text.TextStyle; + +import java.util.StringJoiner; + +public class TextSerializer { + private static final char SPECIAL_CHAR = '\u00a7'; // §; + private static final String BOLD = SPECIAL_CHAR + "l"; + private static final String ITALIC = SPECIAL_CHAR + "o"; + private static final String UNDERLINE = SPECIAL_CHAR + "n"; + private static final String STRIKETHOUGH = SPECIAL_CHAR + "m"; + private static final String OBFUSCATED = SPECIAL_CHAR + "k"; + private static final String RESET = SPECIAL_CHAR + "r"; + + private static void processText(StringJoiner sj, Text text) { + if (text.getStyle() != null) { + if (text.getStyle().equals(TextStyle.RESET)) { + sj.add(RESET); + } else { + sj.add(text.getStyle().getBold().orElse(false) ? BOLD : ""); + sj.add(text.getStyle().getItalic().orElse(false) ? ITALIC : ""); + sj.add(text.getStyle().getUnderline().orElse(false) ? UNDERLINE : ""); + sj.add(text.getStyle().getStrikethrough().orElse(false) ? STRIKETHOUGH : ""); + sj.add(text.getStyle().getObfuscated().orElse(false) ? OBFUSCATED : ""); + } + } + + if (text.getColor() != null) { + sj.add(SPECIAL_CHAR + text.getColor().getCode() + ""); + } + + if (text.getContent() != null) { + sj.add(text.getContent()); + } + } + + public static String serialize(Text text) { + StringJoiner sj = new StringJoiner(""); + + processText(sj, text); + + for (Text childText : text.getChildren()) { + processText(sj, childText); + } + + return sj.toString(); + } +} diff --git a/proto125_netty/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java b/proto125_netty/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java index 41ce17c..a5c16ca 100644 --- a/proto125_netty/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java +++ b/proto125_netty/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java @@ -13,7 +13,6 @@ import io.netty.util.AttributeKey; import lombok.extern.slf4j.Slf4j; import mc.core.*; import mc.core.chat.ChatProcessor; -import mc.core.chat.ChatStyle; import mc.core.events.*; import mc.core.network.CSPacket; import mc.core.network.SCPacket; @@ -35,6 +34,7 @@ import java.util.stream.Stream; @Slf4j public class PacketHandler extends SimpleChannelInboundHandler { private static final AttributeKey ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER"); + private static final char SPECIAL_CHAR = '\u00a7'; // §; @Autowired private Config config; @Autowired @@ -79,8 +79,8 @@ public class PacketHandler extends SimpleChannelInboundHandler { channel.disconnect(); } else { String response = String.format("%s%s%d%s%d", - event.getDescription(), ChatStyle.SPECIAL_CHAR, - event.getOnline(), ChatStyle.SPECIAL_CHAR, + event.getDescription(), SPECIAL_CHAR, + event.getOnline(), SPECIAL_CHAR, event.getMaxOnline() ); diff --git a/proto125_netty/src/main/java/mc/core/network/proto_125/netty/wrappers/WrapperNetChannel.java b/proto125_netty/src/main/java/mc/core/network/proto_125/netty/wrappers/WrapperNetChannel.java index d9c7905..c248445 100644 --- a/proto125_netty/src/main/java/mc/core/network/proto_125/netty/wrappers/WrapperNetChannel.java +++ b/proto125_netty/src/main/java/mc/core/network/proto_125/netty/wrappers/WrapperNetChannel.java @@ -6,11 +6,15 @@ package mc.core.network.proto_125.netty.wrappers; import io.netty.channel.Channel; import lombok.RequiredArgsConstructor; +import mc.core.chat.MessageType; import mc.core.network.NetChannel; import mc.core.network.SCPacket; import mc.core.network.proto_125.packets.ChatMessagePacket; import mc.core.network.proto_125.packets.KeepAlivePacket; import mc.core.network.proto_125.packets.TimeUpdatePacket; +import mc.core.network.proto_125.serializers.TextSerializer; +import mc.core.text.Text; +import mc.core.text.Title; @RequiredArgsConstructor public class WrapperNetChannel implements NetChannel { @@ -22,13 +26,18 @@ public class WrapperNetChannel implements NetChannel { } @Override - public void sendTimeUpdate(long value) { - channel.writeAndFlush(new TimeUpdatePacket(value)); + public void sendTimeUpdate(long time, long age) { + channel.writeAndFlush(new TimeUpdatePacket(time)); } @Override - public void sendChatMessage(String message) { - channel.writeAndFlush(new ChatMessagePacket(message)); + public void sendChatMessage(Text text, MessageType messageType) { + channel.writeAndFlush(new ChatMessagePacket(TextSerializer.serialize(text))); + } + + @Override + public void sendTitle(Title title) { + /* not support */ } @Override