Improved Locations

This commit is contained in:
LOOHP 2020-08-04 14:59:32 +08:00
parent bd3b72a519
commit c2077a2a5c
5 changed files with 98 additions and 5 deletions

View File

@ -77,4 +77,53 @@ public class Location {
this.pitch = 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;
}
} }

View File

@ -22,6 +22,7 @@ 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.PacketPlayInRotation;
import com.loohp.limbo.Server.Packets.PacketPlayOutDisconnect; 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;
@ -149,7 +150,7 @@ public class ClientConnection extends Thread {
PacketStatusOutResponse packet = new PacketStatusOutResponse(Limbo.getInstance().getServerListResponseJson()); PacketStatusOutResponse packet = new PacketStatusOutResponse(Limbo.getInstance().getServerListResponseJson());
sendPacket(packet); sendPacket(packet);
} else if (packetType.equals(PacketStatusInPing.class)) { } 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()); PacketStatusOutPong packet = new PacketStatusOutPong(ping.getPayload());
sendPacket(packet); sendPacket(packet);
break; break;
@ -166,7 +167,7 @@ public class ClientConnection extends Thread {
if (packetType == null) { if (packetType == null) {
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 = new PacketLoginInLoginStart(input);
String username = start.getUsername(); String username = start.getUsername();
UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8)); UUID uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8));
PacketLoginOutLoginSuccess success = new PacketLoginOutLoginSuccess(uuid, username); PacketLoginOutLoginSuccess success = new PacketLoginOutLoginSuccess(uuid, username);
@ -260,6 +261,12 @@ public class ClientConnection extends Thread {
PacketPlayInPosition pos = new PacketPlayInPosition(input); PacketPlayInPosition pos = new PacketPlayInPosition(input);
player.setLocation(new Location(player.getWorld(), pos.getX(), pos.getY(), pos.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch())); 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); PacketPlayOutUpdateViewPosition response = new PacketPlayOutUpdateViewPosition((int) player.getLocation().getX() >> 4, (int) player.getLocation().getZ() >> 4);
sendPacket(response); sendPacket(response);
} else if (packetType.equals(PacketPlayInKeepAlive.class)) { } 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(); 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) {}
@ -302,6 +308,9 @@ public class ClientConnection extends Thread {
client_socket.close(); client_socket.close();
} catch (IOException e) {} } catch (IOException e) {}
state = ClientState.DISCONNECTED; state = ClientState.DISCONNECTED;
if (player != null) {
Limbo.getInstance().removePlayer(player);
}
Limbo.getInstance().getServerConnection().getClients().remove(this); Limbo.getInstance().getServerConnection().getClients().remove(this);
running = false; running = false;
} }

View File

@ -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;
}
}

View File

@ -13,7 +13,8 @@
"0x10": "PacketPlayInKeepAlive", "0x10": "PacketPlayInKeepAlive",
"0x03": "PacketPlayInChat", "0x03": "PacketPlayInChat",
"0x13": "PacketPlayInPositionAndLook", "0x13": "PacketPlayInPositionAndLook",
"0x12": "PacketPlayInPosition" "0x12": "PacketPlayInPosition",
"0x14": "PacketPlayInRotation"
}, },
"PlayOut": { "PlayOut": {
"PacketPlayOutLogin": "0x25", "PacketPlayOutLogin": "0x25",