Archived
0

Login and Disconnect

This commit is contained in:
2018-06-11 02:47:12 +03:00
parent e6aa6fe758
commit 2a25253fae
5 changed files with 114 additions and 8 deletions

View File

@@ -11,10 +11,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mc.core.network.CSPacket;
import mc.core.network.SCPacket;
import mc.core.network.proto_1_12_2.packets.HandshakePacket;
import mc.core.network.proto_1_12_2.packets.PingPacket;
import mc.core.network.proto_1_12_2.packets.StatusRequestPacket;
import mc.core.network.proto_1_12_2.packets.StatusResponsePacket;
import mc.core.network.proto_1_12_2.packets.*;
@Slf4j
@RequiredArgsConstructor
@@ -35,11 +32,20 @@ public enum State {
.put(StatusResponsePacket.class, 0)
.put(PingPacket.class, 1)
.build()
),
LOGIN(2,
ImmutableBiMap.<Integer, Class<? extends CSPacket>>builder()
.put(0, LoginStartPacket.class)
.build(),
ImmutableBiMap.<Class<? extends SCPacket>, Integer>builder()
.put(DisconnectPacket.class, 0)
.build()
);
public static State valueOf(int id) {
if (id == 0) return HANDSHAKE;
else if (id == 1) return STATUS;
else if (id == 2) return LOGIN;
else {
log.warn("Unknown state: {}", id);
return UNKNOWN;

View File

@@ -0,0 +1,46 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-11
*/
package mc.core.network.proto_1_12_2;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import mc.core.text.Text;
public class TextSerializer {
public static JsonObject serialize(Text text) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("text", text.getString());
if (text.getColor() != null) {
jsonObject.addProperty("color", text.getColor().getName());
}
if (text.getStyle() != null) {
if (text.getStyle().getBold().isPresent()) {
jsonObject.addProperty("bold", text.getStyle().getBold().get());
}
if (text.getStyle().getItalic().isPresent()) {
jsonObject.addProperty("italic", text.getStyle().getItalic().get());
}
if (text.getStyle().getObfuscated().isPresent()) {
jsonObject.addProperty("obfuscated", text.getStyle().getObfuscated().get());
}
if (text.getStyle().getStrikethrough().isPresent()) {
jsonObject.addProperty("strikethrough", text.getStyle().getStrikethrough().get());
}
if (text.getStyle().getUnderline().isPresent()) {
jsonObject.addProperty("underlined", text.getStyle().getUnderline().get());
}
}
if (text.getChilds() != null) {
JsonArray extra = new JsonArray();
text.getChilds().forEach(child -> extra.add(serialize(child)));
jsonObject.add("extra", extra);
}
return jsonObject;
}
}

View File

@@ -0,0 +1,27 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-10
*/
package mc.core.network.proto_1_12_2.packets;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import mc.core.network.SCPacket;
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
import mc.core.network.proto_1_12_2.TextSerializer;
import mc.core.text.Text;
@AllArgsConstructor
@NoArgsConstructor
@Setter
public class DisconnectPacket implements SCPacket {
private Text reason;
@Override
public byte[] toByteArray() {
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeString(TextSerializer.serialize(reason).toString());
return netStream.toByteArray();
}
}

View File

@@ -0,0 +1,21 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-10
*/
package mc.core.network.proto_1_12_2.packets;
import lombok.Getter;
import lombok.ToString;
import mc.core.network.CSPacket;
import mc.core.network.NetStream;
@Getter
@ToString
public class LoginStartPacket implements CSPacket {
private String playerName;
@Override
public void readSelf(NetStream netStream) {
this.playerName = netStream.readString();
}
}

View File

@@ -13,12 +13,12 @@ import mc.core.Config;
import mc.core.chat.ChatProcessor;
import mc.core.network.CSPacket;
import mc.core.network.proto_1_12_2.State;
import mc.core.network.proto_1_12_2.packets.HandshakePacket;
import mc.core.network.proto_1_12_2.packets.PingPacket;
import mc.core.network.proto_1_12_2.packets.StatusRequestPacket;
import mc.core.network.proto_1_12_2.packets.StatusResponsePacket;
import mc.core.network.proto_1_12_2.packets.*;
import mc.core.player.Player;
import mc.core.player.PlayerManager;
import mc.core.text.Text;
import mc.core.text.TextColor;
import mc.core.text.TextStyle;
import mc.core.world.World;
import org.springframework.beans.factory.annotation.Autowired;
@@ -69,6 +69,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
private void onHandshake(Channel channel, HandshakePacket packet) {
if (packet.getNextState().equals(State.UNKNOWN)) return;
log.debug("New state: {}", packet.getNextState());
channel.attr(ATTR_STATE).set(packet.getNextState());
}
@@ -88,4 +89,9 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
if (!channel.attr(ATTR_STATE).get().equals(State.STATUS)) return;
channel.writeAndFlush(packet);
}
private void onLoginStart(Channel channel, LoginStartPacket packet) {
if (!channel.attr(ATTR_STATE).get().equals(State.LOGIN)) return;
channel.writeAndFlush(new DisconnectPacket(Text.of("Server is not ready :(", null, TextStyle.ITALIC)));
}
}