save position and look data (FIXME)
не правильно кодируется pitch и yaw
This commit is contained in:
@@ -6,9 +6,11 @@ package mc.core;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.proto_125.packets.PositionAndLookPacket;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Random;
|
||||
|
||||
@Slf4j
|
||||
public class GameLoop extends Thread {
|
||||
@@ -92,6 +94,7 @@ public class GameLoop extends Thread {
|
||||
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
|
||||
int factTps = 0;
|
||||
long lastTime = System.currentTimeMillis();
|
||||
Random rand = new Random();
|
||||
|
||||
while (!isInterrupted()) {
|
||||
if ((System.currentTimeMillis() - lastTime) > 1000) {
|
||||
@@ -114,6 +117,11 @@ public class GameLoop extends Thread {
|
||||
|
||||
playerManager.getBroadcastChannel().sendTimeUpdate(gameTime);
|
||||
|
||||
PositionAndLookPacket pkt = new PositionAndLookPacket();
|
||||
pkt.setLocation(new Location(0, 65, 0));
|
||||
pkt.setLook(new Look(90, 0));
|
||||
playerManager.getBroadcastChannel().writeAndFlush(pkt);
|
||||
|
||||
/* --- --- --- */
|
||||
|
||||
factTps++;
|
||||
|
||||
@@ -24,7 +24,6 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
private final Object lock = new Object();
|
||||
@Setter
|
||||
private int keepAliveInterval = 1;
|
||||
private BroadcastNetChannel broadcastNetChannel = new BroadcastNetChannel();
|
||||
|
||||
@Autowired
|
||||
public InMemoryPlayerManager(Config config) {
|
||||
@@ -65,12 +64,11 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
|
||||
@Override
|
||||
public NetChannel getBroadcastChannel() {
|
||||
broadcastNetChannel.setPlayerStream(
|
||||
return new BroadcastNetChannel(
|
||||
players.stream()
|
||||
.filter(Player::isOnline)
|
||||
.filter(player -> player.getChannel() != null)
|
||||
);
|
||||
return broadcastNetChannel;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
*/
|
||||
package mc.core.network;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.Player;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BroadcastNetChannel implements NetChannel {
|
||||
@Setter
|
||||
private Stream<Player> playerStream;
|
||||
private final Stream<Player> playerStream;
|
||||
|
||||
@Override
|
||||
public void sendKeepAlive() {
|
||||
|
||||
@@ -37,6 +37,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
if (player != null) {
|
||||
playerManager.removePlayer(player);
|
||||
}
|
||||
context.channel().attr(ATTR_PLAYER).set(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -158,8 +159,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
|
||||
posLookPkt.setLocation(player.getLocation());
|
||||
posLookPkt.setStance(player.getLocation().getY() + 1.64d);
|
||||
posLookPkt.setYaw(player.getLook().getYaw());
|
||||
posLookPkt.setPitch(player.getLook().getPitch());
|
||||
posLookPkt.setLook(player.getLook());
|
||||
posLookPkt.setOnGround(false);
|
||||
channel.write(posLookPkt);
|
||||
|
||||
@@ -174,4 +174,17 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void onPlayerPositionPacket(Channel channel, PlayerPositionPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.getLocation().setX(packet.getX());
|
||||
player.getLocation().setY(packet.getY());
|
||||
player.getLocation().setZ(packet.getZ());
|
||||
}
|
||||
|
||||
public void onPlayerLookPacket(Channel channel, PlayerLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.getLook().setYaw(packet.getYaw());
|
||||
player.getLook().setPitch(packet.getPitch());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ public class PacketManager {
|
||||
.put(0x02, HandshakePacket.class)
|
||||
.put(0x04, TimeUpdatePacket.class)
|
||||
.put(0x06, SpawnPositionPacket.class)
|
||||
.put(0x0B, PlayerPositionPacket.class)
|
||||
.put(0x0C, PlayerLookPacket.class)
|
||||
.put(0x0D, PositionAndLookPacket.class)
|
||||
.put(0x32, ChunkAllocationPacket.class)
|
||||
.put(0x33, ChunkDataPacket.class)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-22
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class PlayerLookPacket implements CSPacket {
|
||||
private float yaw, pitch;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
yaw = netStream.readFloat();
|
||||
pitch = netStream.readFloat();
|
||||
onGround = netStream.readBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-22
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class PlayerPositionPacket implements CSPacket {
|
||||
private double x, y, z;
|
||||
private double stance;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
this.x = netStream.readDouble();
|
||||
this.y = netStream.readDouble();
|
||||
this.stance = netStream.readDouble();
|
||||
this.z = netStream.readDouble();
|
||||
this.onGround = netStream.readBoolean();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.Look;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
@@ -19,7 +20,7 @@ import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
public class PositionAndLookPacket implements SCPacket, CSPacket {
|
||||
private Location location;
|
||||
private double stance;
|
||||
private float yaw, pitch;
|
||||
private Look look;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
@@ -28,11 +29,12 @@ public class PositionAndLookPacket implements SCPacket, CSPacket {
|
||||
double y = netStream.readDouble();
|
||||
stance = netStream.readDouble();
|
||||
double z = netStream.readDouble();
|
||||
yaw = netStream.readFloat();
|
||||
pitch = netStream.readFloat();
|
||||
float yaw = netStream.readFloat();
|
||||
float pitch = netStream.readFloat();
|
||||
onGround = netStream.readBoolean();
|
||||
|
||||
location = new Location(x, y, z);
|
||||
look = new Look(yaw, pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,8 +45,8 @@ public class PositionAndLookPacket implements SCPacket, CSPacket {
|
||||
netStream.writeDouble(location.getY());
|
||||
netStream.writeDouble(stance);
|
||||
netStream.writeDouble(location.getZ());
|
||||
netStream.writeFloat(yaw);
|
||||
netStream.writeFloat(pitch);
|
||||
netStream.writeFloat(look.getYaw());
|
||||
netStream.writeFloat(look.getPitch());
|
||||
netStream.writeBoolean(onGround);
|
||||
|
||||
return netStream.toByteArray();
|
||||
|
||||
Reference in New Issue
Block a user