fix Chat
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ import io.netty.util.AttributeKey;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import mc.core.*;
|
import mc.core.*;
|
||||||
import mc.core.chat.ChatProcessor;
|
import mc.core.chat.ChatProcessor;
|
||||||
import mc.core.chat.ChatStyle;
|
|
||||||
import mc.core.events.*;
|
import mc.core.events.*;
|
||||||
import mc.core.network.CSPacket;
|
import mc.core.network.CSPacket;
|
||||||
import mc.core.network.SCPacket;
|
import mc.core.network.SCPacket;
|
||||||
@@ -35,6 +34,7 @@ import java.util.stream.Stream;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||||
private static final AttributeKey<Player> ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER");
|
private static final AttributeKey<Player> ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER");
|
||||||
|
private static final char SPECIAL_CHAR = '\u00a7'; // §;
|
||||||
@Autowired
|
@Autowired
|
||||||
private Config config;
|
private Config config;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -79,8 +79,8 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
channel.disconnect();
|
channel.disconnect();
|
||||||
} else {
|
} else {
|
||||||
String response = String.format("%s%s%d%s%d",
|
String response = String.format("%s%s%d%s%d",
|
||||||
event.getDescription(), ChatStyle.SPECIAL_CHAR,
|
event.getDescription(), SPECIAL_CHAR,
|
||||||
event.getOnline(), ChatStyle.SPECIAL_CHAR,
|
event.getOnline(), SPECIAL_CHAR,
|
||||||
event.getMaxOnline()
|
event.getMaxOnline()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,15 @@ package mc.core.network.proto_125.netty.wrappers;
|
|||||||
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import mc.core.chat.MessageType;
|
||||||
import mc.core.network.NetChannel;
|
import mc.core.network.NetChannel;
|
||||||
import mc.core.network.SCPacket;
|
import mc.core.network.SCPacket;
|
||||||
import mc.core.network.proto_125.packets.ChatMessagePacket;
|
import mc.core.network.proto_125.packets.ChatMessagePacket;
|
||||||
import mc.core.network.proto_125.packets.KeepAlivePacket;
|
import mc.core.network.proto_125.packets.KeepAlivePacket;
|
||||||
import mc.core.network.proto_125.packets.TimeUpdatePacket;
|
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
|
@RequiredArgsConstructor
|
||||||
public class WrapperNetChannel implements NetChannel {
|
public class WrapperNetChannel implements NetChannel {
|
||||||
@@ -22,13 +26,18 @@ public class WrapperNetChannel implements NetChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendTimeUpdate(long value) {
|
public void sendTimeUpdate(long time, long age) {
|
||||||
channel.writeAndFlush(new TimeUpdatePacket(value));
|
channel.writeAndFlush(new TimeUpdatePacket(time));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendChatMessage(String message) {
|
public void sendChatMessage(Text text, MessageType messageType) {
|
||||||
channel.writeAndFlush(new ChatMessagePacket(message));
|
channel.writeAndFlush(new ChatMessagePacket(TextSerializer.serialize(text)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendTitle(Title title) {
|
||||||
|
/* not support */
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user