Added some message, fixed problem with packet reading

This commit is contained in:
LOOHP
2020-08-04 23:44:58 +08:00
parent b01cbad932
commit 4600303f96
11 changed files with 330 additions and 16 deletions
@@ -6,10 +6,13 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import com.loohp.limbo.DeclareCommands;
import com.loohp.limbo.Limbo;
import com.loohp.limbo.File.ServerProperties;
import com.loohp.limbo.Location.Location;
@@ -25,6 +28,8 @@ import com.loohp.limbo.Server.Packets.PacketPlayInKeepAlive;
import com.loohp.limbo.Server.Packets.PacketPlayInPosition;
import com.loohp.limbo.Server.Packets.PacketPlayInPositionAndLook;
import com.loohp.limbo.Server.Packets.PacketPlayInRotation;
import com.loohp.limbo.Server.Packets.PacketPlayInTabComplete;
import com.loohp.limbo.Server.Packets.PacketPlayOutDeclareCommands;
import com.loohp.limbo.Server.Packets.PacketPlayOutDisconnect;
import com.loohp.limbo.Server.Packets.PacketPlayOutLogin;
import com.loohp.limbo.Server.Packets.PacketPlayOutMapChunk;
@@ -37,6 +42,8 @@ import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerInfo.PlayerInfoData.Pla
import com.loohp.limbo.Server.Packets.PacketPlayOutPositionAndLook;
import com.loohp.limbo.Server.Packets.PacketPlayOutShowPlayerSkins;
import com.loohp.limbo.Server.Packets.PacketPlayOutSpawnPosition;
import com.loohp.limbo.Server.Packets.PacketPlayOutTabComplete;
import com.loohp.limbo.Server.Packets.PacketPlayOutTabComplete.TabCompleteMatches;
import com.loohp.limbo.Server.Packets.PacketPlayOutUpdateViewPosition;
import com.loohp.limbo.Server.Packets.PacketStatusInPing;
import com.loohp.limbo.Server.Packets.PacketStatusInRequest;
@@ -73,7 +80,8 @@ public class ClientConnection extends Thread {
private Player player;
private long lastKeepAlivePayLoad;
private DataOutputStream output;
protected DataOutputStream output;
protected DataInputStream input;
private InetAddress iNetAddress;
@@ -156,7 +164,7 @@ public class ClientConnection extends Thread {
state = ClientState.HANDSHAKE;
try {
client_socket.setKeepAlive(true);
DataInputStream input = new DataInputStream(client_socket.getInputStream());
input = new DataInputStream(client_socket.getInputStream());
output = new DataOutputStream(client_socket.getOutputStream());
DataTypeIO.readVarInt(input);
@@ -236,6 +244,8 @@ public class ClientConnection extends Thread {
Limbo.getInstance().addPlayer(player);
break;
} else {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
}
}
break;
@@ -299,13 +309,15 @@ public class ClientConnection extends Thread {
String str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + player.getName();
Limbo.getInstance().getConsole().sendMessage("[/" + str + "] <-> Player had connected to the Limbo server!");
PacketPlayOutDeclareCommands declare = DeclareCommands.getDeclareCommandPacket();
sendPacket(declare);
while (client_socket.isConnected()) {
try {
try {
int size = DataTypeIO.readVarInt(input);
int packetId = DataTypeIO.readVarInt(input);
Class<? extends Packet> packetType = Packet.getPlayIn().get(packetId);
//System.out.println(packetId);
//System.out.println(packetType);
if (packetType == null) {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
} else if (packetType.equals(PacketPlayInPositionAndLook.class)) {
@@ -332,6 +344,30 @@ public class ClientConnection extends Thread {
Limbo.getInstance().getConsole().sendMessage("Incorrect Payload recieved in KeepAlive packet for player " + player.getName());
break;
}
} else if (packetType.equals(PacketPlayInTabComplete.class)) {
PacketPlayInTabComplete request = new PacketPlayInTabComplete(input);
String[] command = CustomStringUtils.splitStringToArgs(request.getText());
System.out.println(request.getText());
List<TabCompleteMatches> matches = new ArrayList<TabCompleteMatches>();
TabCompleteMatches spawn = new TabCompleteMatches("spawn", new TextComponent(ChatColor.GOLD + "Teleports you back to the Limbo spawn!"));
switch (command.length) {
case 0:
matches.add(spawn);
break;
case 1:
if ("spawn".startsWith(command[0])) {
matches.add(spawn);
}
break;
}
int start = CustomStringUtils.getIndexOfArg(request.getText(), command.length - 1);
int length = command[command.length - 1].length();
PacketPlayOutTabComplete response = new PacketPlayOutTabComplete(request.getId(), start, length, matches.toArray(new TabCompleteMatches[matches.size()]));
sendPacket(response);
} else if (packetType.equals(PacketPlayInChat.class)) {
PacketPlayInChat chat = new PacketPlayInChat(input);
if (chat.getMessage().startsWith("/")) {
@@ -349,6 +385,8 @@ public class ClientConnection extends Thread {
each.sendMessage(message);
}
}
} else {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
}
} catch (Exception e) {
@@ -1,6 +1,5 @@
package com.loohp.limbo.Server;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@@ -12,9 +11,10 @@ import com.loohp.limbo.Utils.DataTypeIO;
public class KeepAliveSender extends Thread {
private Random random = new Random();
private Random random;
public KeepAliveSender() {
random = new Random();
start();
}
@@ -25,11 +25,10 @@ public class KeepAliveSender extends Thread {
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) {
if (client.getClientState().equals(ClientState.PLAY)) {
try {
DataOutputStream output = new DataOutputStream(client.getSocket().getOutputStream());
PacketPlayOutKeepAlive packet = new PacketPlayOutKeepAlive(random.nextLong());
byte[] packetByte = packet.serializePacket();
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
DataTypeIO.writeVarInt(client.output, packetByte.length);
client.output.write(packetByte);
client.setLastKeepAlivePayLoad(packet.getPayload());
} catch (IOException ignore) {}
}
@@ -0,0 +1,30 @@
package com.loohp.limbo.Server.Packets;
import java.io.DataInputStream;
import java.io.IOException;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayInTabComplete extends PacketIn {
private int id;
private String text;
public PacketPlayInTabComplete(int id, String text) {
this.id = id;
this.text = text;
}
public PacketPlayInTabComplete(DataInputStream in) throws IOException {
this(DataTypeIO.readVarInt(in), DataTypeIO.readString(in));
}
public int getId() {
return id;
}
public String getText() {
return text;
}
}
@@ -0,0 +1,30 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketPlayOutDeclareCommands extends PacketOut {
private byte[] data;
public PacketPlayOutDeclareCommands(byte[] data) {
this.data = data;
}
public byte[] getData() {
return data;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
output.write(data);
return buffer.toByteArray();
}
}
@@ -0,0 +1,87 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import com.loohp.limbo.Utils.DataTypeIO;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
public class PacketPlayOutTabComplete extends PacketOut {
private int id;
private int start;
private int length;
private TabCompleteMatches[] matches;
public PacketPlayOutTabComplete(int id, int start, int length, TabCompleteMatches... matches) {
this.id = id;
this.start = start;
this.length = length;
}
public int getId() {
return id;
}
public int getStart() {
return start;
}
public int getLength() {
return length;
}
public TabCompleteMatches[] getMatches() {
return matches;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeVarInt(output, id);
DataTypeIO.writeVarInt(output, start);
DataTypeIO.writeVarInt(output, length);
DataTypeIO.writeVarInt(output, matches.length);
for (TabCompleteMatches match : matches) {
DataTypeIO.writeString(output, match.getMatch(), StandardCharsets.UTF_8);
if (match.getTooltip().isPresent()) {
output.writeBoolean(true);
DataTypeIO.writeString(output, ComponentSerializer.toString(match.getTooltip().get()), StandardCharsets.UTF_8);
} else {
output.writeBoolean(false);
}
}
return buffer.toByteArray();
}
public static class TabCompleteMatches {
private String match;
private Optional<BaseComponent[]> tooltip;
public TabCompleteMatches(String match, BaseComponent... tooltip) {
this.match = match;
this.tooltip = tooltip.length > 0 ? Optional.of(tooltip) : Optional.empty();
}
public String getMatch() {
return match;
}
public Optional<BaseComponent[]> getTooltip() {
return tooltip;
}
}
}
@@ -27,7 +27,7 @@ public class ServerConnection extends Thread {
public void run() {
try {
serverSocket = new ServerSocket(port, 50, InetAddress.getByName(ip));
System.out.println("Server listening on [" + serverSocket.getInetAddress().getHostName() + ":" + serverSocket.getLocalPort() + "]");
System.out.println("Limbo server listening on /" + serverSocket.getInetAddress().getHostName() + ":" + serverSocket.getLocalPort());
while (true) {
Socket connection = serverSocket.accept();
//String str = connection.getInetAddress().getHostName() + ":" + connection.getPort();