Archived
0

Chat message

This commit is contained in:
2018-04-30 12:16:22 +03:00
parent 80a351adfd
commit 9a346c2081
6 changed files with 54 additions and 2 deletions

View File

@@ -23,6 +23,11 @@ public class BroadcastNetChannel implements NetChannel {
playerStream.forEach(player -> player.getChannel().sendTimeUpdate(value));
}
@Override
public void sendChatMessage(final String message) {
playerStream.forEach(player -> player.getChannel().sendChatMessage(message));
}
@Override
public void writeAndFlush(final SCPacket pkt) {
playerStream.forEach(player -> player.getChannel().writeAndFlush(pkt));

View File

@@ -7,6 +7,7 @@ package mc.core.network;
public interface NetChannel {
void sendKeepAlive();
void sendTimeUpdate(long value);
void sendChatMessage(String message);
void writeAndFlush(SCPacket pkt);
}

View File

@@ -0,0 +1,32 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-30
*/
package mc.core.network.proto_125.packets;
import lombok.*;
import mc.core.network.CSPacket;
import mc.core.network.NetStream;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class ChatMessagePacket implements SCPacket, CSPacket {
private String message;
@Override
public void readSelf(NetStream netStream) {
message = netStream.readString();
}
@Override
public byte[] toByteArray() {
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeString(message);
return netStream.toByteArray();
}
}

View File

@@ -8,13 +8,13 @@ import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import mc.core.network.CSPacket;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.packets.*;
public class PacketManager {
private static final BiMap<Integer, Class<?>> packetMap = ImmutableBiMap.<Integer, Class<?>>builder()
.put(0x00, KeepAlivePacket.class)
.put(0x01, LoginPacket.class)
.put(0x02, HandshakePacket.class)
.put(0x03, ChatMessagePacket.class)
.put(0x04, TimeUpdatePacket.class)
.put(0x06, SpawnPositionPacket.class)
.put(0x0B, PlayerPositionPacket.class)

View File

@@ -15,6 +15,9 @@ import mc.core.*;
import mc.core.network.CSPacket;
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
import mc.core.network.proto_125.packets.*;
import org.slf4j.Marker;
import org.slf4j.helpers.BasicMarker;
import org.slf4j.helpers.BasicMarkerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Method;
@@ -24,6 +27,7 @@ import java.util.Optional;
@Slf4j
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
private static final AttributeKey<Player> ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER");
private static final Marker CHAT_MARKER = new BasicMarkerFactory().getMarker("Chat");
@Autowired
private Config config;
@Autowired
@@ -167,4 +171,9 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
Player player = channel.attr(ATTR_PLAYER).get();
player.setFlying(packet.isFlying());
}
public void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
log.info(CHAT_MARKER, "<{}>: {}", channel.attr(ATTR_PLAYER).get().getName(), packet.getMessage());
playerManager.getBroadcastChannel().writeAndFlush(packet);
}
}

View File

@@ -8,8 +8,8 @@ import io.netty.channel.Channel;
import lombok.RequiredArgsConstructor;
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.PingPacket;
import mc.core.network.proto_125.packets.TimeUpdatePacket;
@RequiredArgsConstructor
@@ -26,6 +26,11 @@ public class WrapperNetChannel implements NetChannel {
channel.writeAndFlush(new TimeUpdatePacket(value));
}
@Override
public void sendChatMessage(String message) {
channel.writeAndFlush(new ChatMessagePacket(message));
}
@Override
public void writeAndFlush(SCPacket pkt) {
channel.writeAndFlush(pkt);