diff --git a/src/com/loohp/limbo/Limbo.java b/src/com/loohp/limbo/Limbo.java index bdc9869..f41cadf 100644 --- a/src/com/loohp/limbo/Limbo.java +++ b/src/com/loohp/limbo/Limbo.java @@ -142,7 +142,7 @@ public class Limbo { Location spawn = properties.getWorldSpawn(); properties.setWorldSpawn(new Location(getWorld(properties.getLevelName().getKey()), spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getYaw(), spawn.getPitch())); - server = new ServerConnection(properties.getServerIp(), properties.getServerPort()); + server = new ServerConnection(properties.getServerIp(), properties.getServerPort()); console.run(); } diff --git a/src/com/loohp/limbo/Location/Location.java b/src/com/loohp/limbo/Location/Location.java index fb5fc42..325b503 100644 --- a/src/com/loohp/limbo/Location/Location.java +++ b/src/com/loohp/limbo/Location/Location.java @@ -75,6 +75,55 @@ public class Location { public void setPitch(float pitch) { this.pitch = pitch; + } + + @Override + public String toString() { + return "Location{" + "world=" + world + ",x=" + x + ",y=" + y + ",z=" + z + ",pitch=" + pitch + ",yaw=" + yaw + "}"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Float.floatToIntBits(pitch); + result = prime * result + ((world == null) ? 0 : world.hashCode()); + long temp; + temp = Double.doubleToLongBits(x); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(y); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Float.floatToIntBits(yaw); + temp = Double.doubleToLongBits(z); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Location other = (Location) obj; + if (Float.floatToIntBits(pitch) != Float.floatToIntBits(other.pitch)) + return false; + if (world == null) { + if (other.world != null) + return false; + } else if (!world.equals(other.world)) + return false; + if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) + return false; + if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) + return false; + if (Float.floatToIntBits(yaw) != Float.floatToIntBits(other.yaw)) + return false; + if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z)) + return false; + return true; } } diff --git a/src/com/loohp/limbo/Server/ClientConnection.java b/src/com/loohp/limbo/Server/ClientConnection.java index 6ce9cdc..80925a8 100644 --- a/src/com/loohp/limbo/Server/ClientConnection.java +++ b/src/com/loohp/limbo/Server/ClientConnection.java @@ -22,6 +22,7 @@ import com.loohp.limbo.Server.Packets.PacketPlayInChat; 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.PacketPlayOutDisconnect; import com.loohp.limbo.Server.Packets.PacketPlayOutLogin; import com.loohp.limbo.Server.Packets.PacketPlayOutMapChunk; @@ -149,7 +150,7 @@ public class ClientConnection extends Thread { PacketStatusOutResponse packet = new PacketStatusOutResponse(Limbo.getInstance().getServerListResponseJson()); sendPacket(packet); } else if (packetType.equals(PacketStatusInPing.class)) { - PacketStatusInPing ping = (PacketStatusInPing) packetType.getConstructor(DataInputStream.class).newInstance(input); + PacketStatusInPing ping = new PacketStatusInPing(input); PacketStatusOutPong packet = new PacketStatusOutPong(ping.getPayload()); sendPacket(packet); break; @@ -166,7 +167,7 @@ public class ClientConnection extends Thread { if (packetType == null) { input.skipBytes(size - DataTypeIO.getVarIntLength(packetId)); } else if (packetType.equals(PacketLoginInLoginStart.class)) { - PacketLoginInLoginStart start = (PacketLoginInLoginStart) packetType.getConstructor(DataInputStream.class).newInstance(input); + PacketLoginInLoginStart start = new PacketLoginInLoginStart(input); String username = start.getUsername(); UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8)); PacketLoginOutLoginSuccess success = new PacketLoginOutLoginSuccess(uuid, username); @@ -260,6 +261,12 @@ public class ClientConnection extends Thread { PacketPlayInPosition pos = new PacketPlayInPosition(input); player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch())); + PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); + sendPacket(response); + } else if (packetType.equals(PacketPlayInRotation.class)) { + PacketPlayInRotation pos = new PacketPlayInRotation(input); + player.setLocation(new Location(player.getWorld(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), pos.getYaw(), pos.getPitch())); + PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4); sendPacket(response); } else if (packetType.equals(PacketPlayInKeepAlive.class)) { @@ -293,7 +300,6 @@ public class ClientConnection extends Thread { } str = client_socket.getInetAddress().getHostName() + ":" + client_socket.getPort() + "|" + player.getName(); System.out.println("[/" + str + "] <-> Player had disconnected!"); - Limbo.getInstance().removePlayer(player); } } catch (Exception e) {} @@ -302,6 +308,9 @@ public class ClientConnection extends Thread { client_socket.close(); } catch (IOException e) {} state = ClientState.DISCONNECTED; + if (player != null) { + Limbo.getInstance().removePlayer(player); + } Limbo.getInstance().getServerConnection().getClients().remove(this); running = false; } diff --git a/src/com/loohp/limbo/Server/Packets/PacketPlayInRotation.java b/src/com/loohp/limbo/Server/Packets/PacketPlayInRotation.java new file mode 100644 index 0000000..fbd486c --- /dev/null +++ b/src/com/loohp/limbo/Server/Packets/PacketPlayInRotation.java @@ -0,0 +1,34 @@ +package com.loohp.limbo.Server.Packets; + +import java.io.DataInputStream; +import java.io.IOException; + +public class PacketPlayInRotation extends PacketIn { + + private float yaw; + private float pitch; + private boolean onGround; + + public PacketPlayInRotation(float yaw, float pitch, boolean onGround) { + this.yaw = yaw; + this.pitch = pitch; + this.onGround = onGround; + } + + public PacketPlayInRotation(DataInputStream in) throws IOException { + this(in.readFloat(), in.readFloat(), in.readBoolean()); + } + + public float getYaw() { + return yaw; + } + + public float getPitch() { + return pitch; + } + + public boolean onGround() { + return onGround; + } + +} diff --git a/src/mapping.json b/src/mapping.json index c80b110..b4657b6 100644 --- a/src/mapping.json +++ b/src/mapping.json @@ -13,7 +13,8 @@ "0x10": "PacketPlayInKeepAlive", "0x03": "PacketPlayInChat", "0x13": "PacketPlayInPositionAndLook", - "0x12": "PacketPlayInPosition" + "0x12": "PacketPlayInPosition", + "0x14": "PacketPlayInRotation" }, "PlayOut": { "PacketPlayOutLogin": "0x25",