Added proper disconnect packet, kick command

This commit is contained in:
LOOHP 2020-08-04 14:36:14 +08:00
parent 2a48ead90d
commit bd3b72a519
22 changed files with 302 additions and 138 deletions

View File

@ -9,10 +9,8 @@ import java.io.PrintStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit;
import com.loohp.limbo.Server.ClientConnection; import com.loohp.limbo.Player.Player;
import com.loohp.limbo.Server.ClientConnection.ClientState;
import com.loohp.limbo.Utils.CustomStringUtils; import com.loohp.limbo.Utils.CustomStringUtils;
public class Console { public class Console {
@ -42,12 +40,24 @@ public class Console {
if (input.length > 1) { if (input.length > 1) {
String message = "[Server] " + String.join(" ", Arrays.copyOfRange(input, 1, input.length)); String message = "[Server] " + String.join(" ", Arrays.copyOfRange(input, 1, input.length));
sendMessage(message); sendMessage(message);
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { for (Player each : Limbo.getInstance().getPlayers()) {
if (client.getClientState().equals(ClientState.PLAY)) { each.sendMessage(message);
client.sendMessage(message);
}
} }
} }
} else if (input[0].equalsIgnoreCase("kick")) {
String reason = "Disconnected!";
Player player = input.length > 1 ? Limbo.getInstance().getPlayer(input[1]) : null;
if (player != null) {
if (input.length < 2) {
player.disconnect();
} else {
reason = String.join(" ", Arrays.copyOfRange(input, 2, input.length));
player.disconnect(reason);
}
sendMessage("Kicked the player " + input[1] + " for the reason: " + reason);
} else {
sendMessage("Player is not online!");
}
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -7,9 +7,13 @@ import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
@ -17,7 +21,7 @@ import org.json.simple.parser.ParseException;
import com.loohp.limbo.File.ServerProperties; import com.loohp.limbo.File.ServerProperties;
import com.loohp.limbo.Location.Location; import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Server.ClientConnection; import com.loohp.limbo.Player.Player;
import com.loohp.limbo.Server.ServerConnection; import com.loohp.limbo.Server.ServerConnection;
import com.loohp.limbo.Server.Packets.Packet; import com.loohp.limbo.Server.Packets.Packet;
import com.loohp.limbo.Server.Packets.PacketIn; import com.loohp.limbo.Server.Packets.PacketIn;
@ -47,9 +51,13 @@ public class Limbo {
private Console console; private Console console;
private List<World> worlds = new ArrayList<>(); private List<World> worlds = new ArrayList<>();
private Map<String, Player> playersByName = new HashMap<>();
private Map<UUID, Player> playersByUUID = new HashMap<>();
private ServerProperties properties; private ServerProperties properties;
public AtomicInteger entityCount = new AtomicInteger();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Limbo() throws IOException, ParseException, NumberFormatException, ClassNotFoundException { public Limbo() throws IOException, ParseException, NumberFormatException, ClassNotFoundException {
instance = this; instance = this;
@ -166,6 +174,28 @@ public class Limbo {
return console; return console;
} }
public Set<Player> getPlayers() {
return new HashSet<>(playersByUUID.values());
}
public Player getPlayer(String name) {
return playersByName.get(name);
}
public Player getPlayer(UUID uuid) {
return playersByUUID.get(uuid);
}
public void addPlayer(Player player) {
playersByName.put(player.getName(), player);
playersByUUID.put(player.getUUID(), player);
}
public void removePlayer(Player player) {
playersByName.remove(player.getName());
playersByUUID.remove(player.getUUID());
}
public List<World> getWorlds() { public List<World> getWorlds() {
return new ArrayList<>(worlds); return new ArrayList<>(worlds);
} }
@ -197,18 +227,12 @@ public class Limbo {
return base; return base;
} }
@SuppressWarnings("deprecation")
public void stopServer() { public void stopServer() {
Limbo.getInstance().getConsole().sendMessage("Stopping Server..."); Limbo.getInstance().getConsole().sendMessage("Stopping Server...");
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { for (Player player : getPlayers()) {
try { player.disconnect("Server closed");
client.getSocket().close();
} catch (IOException e) {
e.printStackTrace();
client.destroy();
}
} }
while (!Limbo.getInstance().getServerConnection().getClients().isEmpty()) { while (!getPlayers().isEmpty()) {
try { try {
TimeUnit.MILLISECONDS.sleep(500); TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -0,0 +1,106 @@
package com.loohp.limbo.Player;
import java.io.IOException;
import java.util.UUID;
import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Server.ClientConnection;
import com.loohp.limbo.Server.Packets.PacketPlayOutChat;
import com.loohp.limbo.Server.Packets.PacketPlayOutPositionAndLook;
import com.loohp.limbo.World.World;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
public class Player {
public final ClientConnection clientConnection;
private final String username;
private final UUID uuid;
private int entityId;
private Location location;
public Player(ClientConnection clientConnection, String username, UUID uuid, int entityId, Location location) {
this.clientConnection = clientConnection;
this.username = username;
this.uuid = uuid;
this.entityId = entityId;
this.location = location.clone();
}
public World getWorld() {
return location.clone().getWorld();
}
public void setEntityId(int entityId) {
this.entityId = entityId;
}
public int getEntityId() {
return entityId;
}
public Location getLocation() {
return location.clone();
}
public void setLocation(Location location) {
this.location = location;
}
public String getName() {
return username;
}
public UUID getUUID() {
return uuid;
}
public void sendMessage(String message) {
sendMessage(TextComponent.fromLegacyText(message));
}
public void sendMessage(BaseComponent component) {
sendMessage(new BaseComponent[] { component });
}
public void teleport(Location location) {
try {
PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(location.getX(),
location.getY(), location.getZ(), location.getYaw(), location.getPitch(), 1);
clientConnection.sendPacket(positionLook);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(BaseComponent[] component) {
try {
PacketPlayOutChat chat = new PacketPlayOutChat(ComponentSerializer.toString(component), 0, new UUID(0, 0));
clientConnection.sendPacket(chat);
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
disconnect("Disconnected!");
}
public void disconnect(String reason) {
disconnect(TextComponent.fromLegacyText(reason));
}
public void disconnect(BaseComponent reason) {
disconnect(new BaseComponent[] {reason});
}
public void disconnect(BaseComponent[] reason) {
clientConnection.disconnect(reason);
}
}

View File

@ -12,15 +12,17 @@ import java.util.concurrent.TimeUnit;
import com.loohp.limbo.Limbo; import com.loohp.limbo.Limbo;
import com.loohp.limbo.File.ServerProperties; import com.loohp.limbo.File.ServerProperties;
import com.loohp.limbo.Location.Location; import com.loohp.limbo.Location.Location;
import com.loohp.limbo.Player.Player;
import com.loohp.limbo.Server.Packets.Packet; import com.loohp.limbo.Server.Packets.Packet;
import com.loohp.limbo.Server.Packets.PacketHandshakingIn; import com.loohp.limbo.Server.Packets.PacketHandshakingIn;
import com.loohp.limbo.Server.Packets.PacketLoginInLoginStart; import com.loohp.limbo.Server.Packets.PacketLoginInLoginStart;
import com.loohp.limbo.Server.Packets.PacketLoginOutLoginSuccess; import com.loohp.limbo.Server.Packets.PacketLoginOutLoginSuccess;
import com.loohp.limbo.Server.Packets.PacketOut;
import com.loohp.limbo.Server.Packets.PacketPlayInChat; import com.loohp.limbo.Server.Packets.PacketPlayInChat;
import com.loohp.limbo.Server.Packets.PacketPlayInKeepAlive; import com.loohp.limbo.Server.Packets.PacketPlayInKeepAlive;
import com.loohp.limbo.Server.Packets.PacketPlayInPosition; import com.loohp.limbo.Server.Packets.PacketPlayInPosition;
import com.loohp.limbo.Server.Packets.PacketPlayInPositionAndLook; import com.loohp.limbo.Server.Packets.PacketPlayInPositionAndLook;
import com.loohp.limbo.Server.Packets.PacketPlayOutChat; import com.loohp.limbo.Server.Packets.PacketPlayOutDisconnect;
import com.loohp.limbo.Server.Packets.PacketPlayOutLogin; import com.loohp.limbo.Server.Packets.PacketPlayOutLogin;
import com.loohp.limbo.Server.Packets.PacketPlayOutMapChunk; import com.loohp.limbo.Server.Packets.PacketPlayOutMapChunk;
import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerAbilities; import com.loohp.limbo.Server.Packets.PacketPlayOutPlayerAbilities;
@ -65,13 +67,15 @@ public class ClientConnection extends Thread {
private boolean running; private boolean running;
private ClientState state; private ClientState state;
private String username; private Player player;
private UUID uuid;
private Location location;
private long lastKeepAlivePayLoad; private long lastKeepAlivePayLoad;
private DataOutputStream output;
public ClientConnection(Socket client_socket) {
this.client_socket = client_socket;
}
public long getLastKeepAlivePayLoad() { public long getLastKeepAlivePayLoad() {
return lastKeepAlivePayLoad; return lastKeepAlivePayLoad;
} }
@ -80,25 +84,9 @@ public class ClientConnection extends Thread {
this.lastKeepAlivePayLoad = payLoad; this.lastKeepAlivePayLoad = payLoad;
} }
public Location getLocation() { public Player getPlayer() {
return location; return player;
} }
public void setLocation(Location location) {
this.location = location;
}
public String getUsername() {
return username;
}
public UUID getUuid() {
return uuid;
}
public ClientConnection(Socket client_socket) {
this.client_socket = client_socket;
}
public ClientState getClientState() { public ClientState getClientState() {
return state; return state;
@ -112,37 +100,24 @@ public class ClientConnection extends Thread {
return running; return running;
} }
public int getEntityId() { public void sendPacket(PacketOut packet) throws IOException {
return Limbo.getInstance().getServerConnection().getClients().indexOf(this); byte[] packetByte = packet.serializePacket();
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
} }
public void sendMessage(String message) { public void disconnect(BaseComponent[] reason) {
sendMessage(TextComponent.fromLegacyText(message));
}
public void sendMessage(BaseComponent component) {
sendMessage(new BaseComponent[] {component});
}
public void teleport(Location location) {
try { try {
DataOutputStream output = new DataOutputStream(client_socket.getOutputStream()); PacketPlayOutDisconnect packet = new PacketPlayOutDisconnect(ComponentSerializer.toString(reason));
PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), 1); byte[] packetByte = packet.serializePacket();
byte[] packetByte = positionLook.getBytes();
DataTypeIO.writeVarInt(output, packetByte.length); DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte); output.write(packetByte);
output.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
public void sendMessage(BaseComponent[] component) {
try { try {
DataOutputStream output = new DataOutputStream(client_socket.getOutputStream()); client_socket.close();
PacketPlayOutChat chat = new PacketPlayOutChat(ComponentSerializer.toString(component), 0, new UUID(0, 0));
byte[] packetByte = chat.getBytes();
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -155,7 +130,7 @@ public class ClientConnection extends Thread {
try { try {
client_socket.setKeepAlive(true); client_socket.setKeepAlive(true);
DataInputStream input = new DataInputStream(client_socket.getInputStream()); DataInputStream input = new DataInputStream(client_socket.getInputStream());
DataOutputStream output = new DataOutputStream(client_socket.getOutputStream()); output = new DataOutputStream(client_socket.getOutputStream());
DataTypeIO.readVarInt(input); DataTypeIO.readVarInt(input);
int handShakeId = DataTypeIO.readVarInt(input); int handShakeId = DataTypeIO.readVarInt(input);
PacketHandshakingIn handshake = (PacketHandshakingIn) Packet.getHandshakeIn().get(handShakeId).getConstructor(DataInputStream.class).newInstance(input); PacketHandshakingIn handshake = (PacketHandshakingIn) Packet.getHandshakeIn().get(handShakeId).getConstructor(DataInputStream.class).newInstance(input);
@ -172,15 +147,11 @@ public class ClientConnection extends Thread {
String str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort(); String str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort();
System.out.println("[/" + str + "] <-> InitialHandler has pinged"); System.out.println("[/" + str + "] <-> InitialHandler has pinged");
PacketStatusOutResponse packet = new PacketStatusOutResponse(Limbo.getInstance().getServerListResponseJson()); PacketStatusOutResponse packet = new PacketStatusOutResponse(Limbo.getInstance().getServerListResponseJson());
byte[] packetByte = packet.getBytes(); sendPacket(packet);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
} else if (packetType.equals(PacketStatusInPing.class)) { } else if (packetType.equals(PacketStatusInPing.class)) {
PacketStatusInPing ping = (PacketStatusInPing) packetType.getConstructor(DataInputStream.class).newInstance(input); PacketStatusInPing ping = (PacketStatusInPing) packetType.getConstructor(DataInputStream.class).newInstance(input);
PacketStatusOutPong packet = new PacketStatusOutPong(ping.getPayload()); PacketStatusOutPong packet = new PacketStatusOutPong(ping.getPayload());
byte[] packetByte = packet.getBytes(); sendPacket(packet);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
break; break;
} }
} }
@ -196,14 +167,16 @@ public class ClientConnection extends Thread {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId)); input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
} else if (packetType.equals(PacketLoginInLoginStart.class)) { } else if (packetType.equals(PacketLoginInLoginStart.class)) {
PacketLoginInLoginStart start = (PacketLoginInLoginStart) packetType.getConstructor(DataInputStream.class).newInstance(input); PacketLoginInLoginStart start = (PacketLoginInLoginStart) packetType.getConstructor(DataInputStream.class).newInstance(input);
username = start.getUsername(); String username = start.getUsername();
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8)); UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
PacketLoginOutLoginSuccess success = new PacketLoginOutLoginSuccess(uuid, start.getUsername()); PacketLoginOutLoginSuccess success = new PacketLoginOutLoginSuccess(uuid, username);
byte[] packetByte = success.getBytes(); sendPacket(success);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
state = ClientState.PLAY; state = ClientState.PLAY;
player = new Player(this, username, uuid, Limbo.getInstance().entityCount.getAndIncrement(), Limbo.getInstance().getServerProperties().getWorldSpawn());
Limbo.getInstance().addPlayer(player);
break; break;
} }
} }
@ -215,10 +188,8 @@ public class ClientConnection extends Thread {
TimeUnit.MILLISECONDS.sleep(500); TimeUnit.MILLISECONDS.sleep(500);
ServerProperties p = Limbo.getInstance().getServerProperties(); ServerProperties p = Limbo.getInstance().getServerProperties();
PacketPlayOutLogin join = new PacketPlayOutLogin(getEntityId(), false, p.getDefaultGamemode(), new String[] {p.getLevelName().toString()}, DimensionRegistry.getCodec(), p.getLevelDimension().toString(), p.getLevelName().toString(), 0, (byte) p.getMaxPlayers(), 8, p.isReducedDebugInfo(), true, false, false); PacketPlayOutLogin join = new PacketPlayOutLogin(player.getEntityId(), false, p.getDefaultGamemode(), new String[] {p.getLevelName().toString()}, DimensionRegistry.getCodec(), p.getLevelDimension().toString(), p.getLevelName().toString(), 0, (byte) p.getMaxPlayers(), 8, p.isReducedDebugInfo(), true, false, false);
byte[] packetByte = join.getBytes(); sendPacket(join);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
Location s = p.getWorldSpawn(); Location s = p.getWorldSpawn();
@ -231,31 +202,23 @@ public class ClientConnection extends Thread {
Chunk chunk = world.getChunks()[x][z]; Chunk chunk = world.getChunks()[x][z];
if (chunk != null) { if (chunk != null) {
PacketPlayOutMapChunk chunkdata = new PacketPlayOutMapChunk(x, z, chunk); PacketPlayOutMapChunk chunkdata = new PacketPlayOutMapChunk(x, z, chunk);
packetByte = chunkdata.getBytes(); sendPacket(chunkdata);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
//System.out.println(x + ", " + z); //System.out.println(x + ", " + z);
} }
} }
} }
PacketPlayOutSpawnPosition spawnPos = new PacketPlayOutSpawnPosition(BlockPosition.from(s)); PacketPlayOutSpawnPosition spawnPos = new PacketPlayOutSpawnPosition(BlockPosition.from(s));
packetByte = spawnPos.getBytes(); sendPacket(spawnPos);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(s.getX(), s.getY(), s.getZ(), s.getYaw(), s.getPitch(), 1); PacketPlayOutPositionAndLook positionLook = new PacketPlayOutPositionAndLook(s.getX(), s.getY(), s.getZ(), s.getYaw(), s.getPitch(), 1);
location = new Location(world, s.getX(), s.getY(), s.getZ(), s.getYaw(), s.getPitch()); player.setLocation(new Location(world, s.getX(), s.getY(), s.getZ(), s.getYaw(), s.getPitch()));
packetByte = positionLook.getBytes(); sendPacket(positionLook);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
SkinResponse skinresponce = SkinUtils.getSkinFromMojangServer(username); SkinResponse skinresponce = SkinUtils.getSkinFromMojangServer(player.getName());
PlayerSkinProperty skin = skinresponce != null ? new PlayerSkinProperty(skinresponce.getSkin(), skinresponce.getSignature()) : null; PlayerSkinProperty skin = skinresponce != null ? new PlayerSkinProperty(skinresponce.getSkin(), skinresponce.getSignature()) : null;
PacketPlayOutPlayerInfo info = new PacketPlayOutPlayerInfo(PlayerInfoAction.ADD_PLAYER, uuid, new PlayerInfoData.PlayerInfoDataAddPlayer(username, Optional.ofNullable(skin), p.getDefaultGamemode(), 0, false, Optional.empty())); PacketPlayOutPlayerInfo info = new PacketPlayOutPlayerInfo(PlayerInfoAction.ADD_PLAYER, player.getUUID(), new PlayerInfoData.PlayerInfoDataAddPlayer(player.getName(), Optional.ofNullable(skin), p.getDefaultGamemode(), 0, false, Optional.empty()));
packetByte = info.getBytes(); sendPacket(info);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
/* /*
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) {
DataOutputStream other = new DataOutputStream(client.getSocket().getOutputStream()); DataOutputStream other = new DataOutputStream(client.getSocket().getOutputStream());
@ -264,10 +227,8 @@ public class ClientConnection extends Thread {
} }
*/ */
PacketPlayOutShowPlayerSkins show = new PacketPlayOutShowPlayerSkins(getEntityId()); PacketPlayOutShowPlayerSkins show = new PacketPlayOutShowPlayerSkins(player.getEntityId());
packetByte = show.getBytes(); sendPacket(show);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
PacketPlayOutPlayerAbilities abilities; PacketPlayOutPlayerAbilities abilities;
if (p.isAllowFlight()) { if (p.isAllowFlight()) {
@ -275,11 +236,9 @@ public class ClientConnection extends Thread {
} else { } else {
abilities = new PacketPlayOutPlayerAbilities(0.05F, 0.1F); abilities = new PacketPlayOutPlayerAbilities(0.05F, 0.1F);
} }
packetByte = abilities.getBytes(); sendPacket(abilities);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
String str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + username; String str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + player.getName();
System.out.println("[/" + str + "] <-> Player had connected to the Limbo server!"); System.out.println("[/" + str + "] <-> Player had connected to the Limbo server!");
while (client_socket.isConnected()) { while (client_socket.isConnected()) {
@ -293,24 +252,20 @@ public class ClientConnection extends Thread {
input.skipBytes(size - DataTypeIO.getVarIntLength(packetId)); input.skipBytes(size - DataTypeIO.getVarIntLength(packetId));
} else if (packetType.equals(PacketPlayInPositionAndLook.class)) { } else if (packetType.equals(PacketPlayInPositionAndLook.class)) {
PacketPlayInPositionAndLook pos = new PacketPlayInPositionAndLook(input); PacketPlayInPositionAndLook pos = new PacketPlayInPositionAndLook(input);
location = new Location(location.getWorld(), pos.getX(), pos.getY(), pos.getZ(), pos.getYaw(), pos.getPitch()); player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), pos.getYaw(), pos.getPitch()));
PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) location.getX() >> 4, (int) location.getZ() >> 4); PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4);
packetByte = response.getBytes(); sendPacket(response);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
} else if (packetType.equals(PacketPlayInPosition.class)) { } else if (packetType.equals(PacketPlayInPosition.class)) {
PacketPlayInPosition pos = new PacketPlayInPosition(input); PacketPlayInPosition pos = new PacketPlayInPosition(input);
location = new Location(location.getWorld(), pos.getX(), pos.getY(), pos.getZ(), location.getYaw(), location.getPitch()); player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch()));
PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) location.getX() >> 4, (int) location.getZ() >> 4); PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4);
packetByte = response.getBytes(); sendPacket(response);
DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte);
} else if (packetType.equals(PacketPlayInKeepAlive.class)) { } else if (packetType.equals(PacketPlayInKeepAlive.class)) {
PacketPlayInKeepAlive alive = new PacketPlayInKeepAlive(input); PacketPlayInKeepAlive alive = new PacketPlayInKeepAlive(input);
if (alive.getPayload() != lastKeepAlivePayLoad) { if (alive.getPayload() != lastKeepAlivePayLoad) {
System.out.println("Incorrect Payload recieved in KeepAlive packet for player " + username); System.out.println("Incorrect Payload recieved in KeepAlive packet for player " + player.getName());
break; break;
} }
} else if (packetType.equals(PacketPlayInChat.class)) { } else if (packetType.equals(PacketPlayInChat.class)) {
@ -319,15 +274,15 @@ public class ClientConnection extends Thread {
//TO-DO COMMANDS //TO-DO COMMANDS
String[] command = CustomStringUtils.splitStringToArgs(chat.getMessage().substring(1)); String[] command = CustomStringUtils.splitStringToArgs(chat.getMessage().substring(1));
if (command[0].equalsIgnoreCase("spawn")) { if (command[0].equalsIgnoreCase("spawn")) {
teleport(p.getWorldSpawn()); player.teleport(p.getWorldSpawn());
Limbo.getInstance().getConsole().sendMessage(username + " executed server command: /spawn"); Limbo.getInstance().getConsole().sendMessage(player.getName() + " executed server command: /spawn");
sendMessage(new TextComponent(ChatColor.GOLD + "Teleporting you to spawn!")); player.sendMessage(new TextComponent(ChatColor.GOLD + "Teleporting you to spawn!"));
} }
} else { } else {
String message = "<" + username + "> " + chat.getMessage(); String message = "<" + player.getName() + "> " + chat.getMessage();
Limbo.getInstance().getConsole().sendMessage(message); Limbo.getInstance().getConsole().sendMessage(message);
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { for (Player each : Limbo.getInstance().getPlayers()) {
client.sendMessage(message); each.sendMessage(message);
} }
} }
} }
@ -336,8 +291,9 @@ public class ClientConnection extends Thread {
break; break;
} }
} }
str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + username; str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + player.getName();
System.out.println("[/" + str + "] <-> Player had disconnected!"); System.out.println("[/" + str + "] <-> Player had disconnected!");
Limbo.getInstance().removePlayer(player);
} }
} catch (Exception e) {} } catch (Exception e) {}

View File

@ -27,7 +27,7 @@ public class KeepAliveSender extends Thread {
try { try {
DataOutputStream output = new DataOutputStream(client.getSocket().getOutputStream()); DataOutputStream output = new DataOutputStream(client.getSocket().getOutputStream());
PacketPlayOutKeepAlive packet = new PacketPlayOutKeepAlive(random.nextLong()); PacketPlayOutKeepAlive packet = new PacketPlayOutKeepAlive(random.nextLong());
byte[] packetByte = packet.getBytes(); byte[] packetByte = packet.serializePacket();
DataTypeIO.writeVarInt(output, packetByte.length); DataTypeIO.writeVarInt(output, packetByte.length);
output.write(packetByte); output.write(packetByte);
client.setLastKeepAlivePayLoad(packet.getPayload()); client.setLastKeepAlivePayLoad(packet.getPayload());

View File

@ -0,0 +1,33 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketLoginOutDisconnect extends PacketOut {
private String jsonReason;
public PacketLoginOutDisconnect(String jsonReason) {
this.jsonReason = jsonReason;
}
public String getJsonReason() {
return jsonReason;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getLoginOut().get(getClass()));
DataTypeIO.writeString(output, jsonReason, StandardCharsets.UTF_8);
return buffer.toByteArray();
}
}

View File

@ -27,7 +27,7 @@ public class PacketLoginOutLoginSuccess extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -4,6 +4,6 @@ import java.io.IOException;
public abstract class PacketOut extends Packet { public abstract class PacketOut extends Packet {
public abstract byte[] getBytes() throws IOException; public abstract byte[] serializePacket() throws IOException;
} }

View File

@ -33,7 +33,7 @@ public class PacketPlayOutChat extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -0,0 +1,33 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
public class PacketPlayOutDisconnect extends PacketOut {
private String jsonReason;
public PacketPlayOutDisconnect(String jsonReason) {
this.jsonReason = jsonReason;
}
public String getJsonReason() {
return jsonReason;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeString(output, jsonReason, StandardCharsets.UTF_8);
return buffer.toByteArray();
}
}

View File

@ -17,7 +17,7 @@ public class PacketPlayOutKeepAlive extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -104,7 +104,7 @@ public class PacketPlayOutLogin extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -39,7 +39,7 @@ public class PacketPlayOutMapChunk extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -46,7 +46,7 @@ public class PacketPlayOutPlayerAbilities extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -50,7 +50,7 @@ public class PacketPlayOutPlayerInfo extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -76,7 +76,7 @@ public class PacketPlayOutPositionAndLook extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -19,7 +19,7 @@ public class PacketPlayOutShowPlayerSkins extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -18,7 +18,7 @@ public class PacketPlayOutSpawnPosition extends PacketOut {
return position; return position;
} }
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -25,7 +25,7 @@ public class PacketPlayOutUpdateViewPosition extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -17,7 +17,7 @@ public class PacketStatusOutPong extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -20,7 +20,7 @@ public class PacketStatusOutResponse extends PacketOut {
} }
@Override @Override
public byte[] getBytes() throws IOException { public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer); DataOutputStream output = new DataOutputStream(buffer);

View File

@ -6,7 +6,8 @@
"0x00": "PacketLoginInLoginStart" "0x00": "PacketLoginInLoginStart"
}, },
"LoginOut": { "LoginOut": {
"PacketLoginOutLoginSuccess": "0x02" "PacketLoginOutLoginSuccess": "0x02",
"PacketLoginOutDisconnect": "0x00"
}, },
"PlayIn": { "PlayIn": {
"0x10": "PacketPlayInKeepAlive", "0x10": "PacketPlayInKeepAlive",
@ -24,7 +25,8 @@
"PacketPlayOutKeepAlive": "0x20", "PacketPlayOutKeepAlive": "0x20",
"PacketPlayOutPlayerInfo": "0x33", "PacketPlayOutPlayerInfo": "0x33",
"PacketPlayOutUpdateViewPosition": "0x40", "PacketPlayOutUpdateViewPosition": "0x40",
"PacketPlayOutShowPlayerSkins": "0x44" "PacketPlayOutShowPlayerSkins": "0x44",
"PacketPlayOutDisconnect": "0x1A"
}, },
"StatusIn": { "StatusIn": {
"0x01": "PacketStatusInPing", "0x01": "PacketStatusInPing",