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.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.core.network.proto_125.packets.PositionAndLookPacket;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GameLoop extends Thread {
|
public class GameLoop extends Thread {
|
||||||
@@ -92,6 +94,7 @@ public class GameLoop extends Thread {
|
|||||||
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
|
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
|
||||||
int factTps = 0;
|
int factTps = 0;
|
||||||
long lastTime = System.currentTimeMillis();
|
long lastTime = System.currentTimeMillis();
|
||||||
|
Random rand = new Random();
|
||||||
|
|
||||||
while (!isInterrupted()) {
|
while (!isInterrupted()) {
|
||||||
if ((System.currentTimeMillis() - lastTime) > 1000) {
|
if ((System.currentTimeMillis() - lastTime) > 1000) {
|
||||||
@@ -114,6 +117,11 @@ public class GameLoop extends Thread {
|
|||||||
|
|
||||||
playerManager.getBroadcastChannel().sendTimeUpdate(gameTime);
|
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++;
|
factTps++;
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
|||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@Setter
|
@Setter
|
||||||
private int keepAliveInterval = 1;
|
private int keepAliveInterval = 1;
|
||||||
private BroadcastNetChannel broadcastNetChannel = new BroadcastNetChannel();
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public InMemoryPlayerManager(Config config) {
|
public InMemoryPlayerManager(Config config) {
|
||||||
@@ -65,12 +64,11 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NetChannel getBroadcastChannel() {
|
public NetChannel getBroadcastChannel() {
|
||||||
broadcastNetChannel.setPlayerStream(
|
return new BroadcastNetChannel(
|
||||||
players.stream()
|
players.stream()
|
||||||
.filter(Player::isOnline)
|
.filter(Player::isOnline)
|
||||||
.filter(player -> player.getChannel() != null)
|
.filter(player -> player.getChannel() != null)
|
||||||
);
|
);
|
||||||
return broadcastNetChannel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
*/
|
*/
|
||||||
package mc.core.network;
|
package mc.core.network;
|
||||||
|
|
||||||
import lombok.Setter;
|
import lombok.RequiredArgsConstructor;
|
||||||
import mc.core.Player;
|
import mc.core.Player;
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class BroadcastNetChannel implements NetChannel {
|
public class BroadcastNetChannel implements NetChannel {
|
||||||
@Setter
|
private final Stream<Player> playerStream;
|
||||||
private Stream<Player> playerStream;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendKeepAlive() {
|
public void sendKeepAlive() {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
playerManager.removePlayer(player);
|
playerManager.removePlayer(player);
|
||||||
}
|
}
|
||||||
|
context.channel().attr(ATTR_PLAYER).set(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -158,8 +159,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
|
PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
|
||||||
posLookPkt.setLocation(player.getLocation());
|
posLookPkt.setLocation(player.getLocation());
|
||||||
posLookPkt.setStance(player.getLocation().getY() + 1.64d);
|
posLookPkt.setStance(player.getLocation().getY() + 1.64d);
|
||||||
posLookPkt.setYaw(player.getLook().getYaw());
|
posLookPkt.setLook(player.getLook());
|
||||||
posLookPkt.setPitch(player.getLook().getPitch());
|
|
||||||
posLookPkt.setOnGround(false);
|
posLookPkt.setOnGround(false);
|
||||||
channel.write(posLookPkt);
|
channel.write(posLookPkt);
|
||||||
|
|
||||||
@@ -174,4 +174,17 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
|||||||
channel.disconnect();
|
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(0x02, HandshakePacket.class)
|
||||||
.put(0x04, TimeUpdatePacket.class)
|
.put(0x04, TimeUpdatePacket.class)
|
||||||
.put(0x06, SpawnPositionPacket.class)
|
.put(0x06, SpawnPositionPacket.class)
|
||||||
|
.put(0x0B, PlayerPositionPacket.class)
|
||||||
|
.put(0x0C, PlayerLookPacket.class)
|
||||||
.put(0x0D, PositionAndLookPacket.class)
|
.put(0x0D, PositionAndLookPacket.class)
|
||||||
.put(0x32, ChunkAllocationPacket.class)
|
.put(0x32, ChunkAllocationPacket.class)
|
||||||
.put(0x33, ChunkDataPacket.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.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import mc.core.Location;
|
import mc.core.Location;
|
||||||
|
import mc.core.Look;
|
||||||
import mc.core.network.CSPacket;
|
import mc.core.network.CSPacket;
|
||||||
import mc.core.network.NetStream;
|
import mc.core.network.NetStream;
|
||||||
import mc.core.network.SCPacket;
|
import mc.core.network.SCPacket;
|
||||||
@@ -19,7 +20,7 @@ import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
|||||||
public class PositionAndLookPacket implements SCPacket, CSPacket {
|
public class PositionAndLookPacket implements SCPacket, CSPacket {
|
||||||
private Location location;
|
private Location location;
|
||||||
private double stance;
|
private double stance;
|
||||||
private float yaw, pitch;
|
private Look look;
|
||||||
private boolean onGround;
|
private boolean onGround;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -28,11 +29,12 @@ public class PositionAndLookPacket implements SCPacket, CSPacket {
|
|||||||
double y = netStream.readDouble();
|
double y = netStream.readDouble();
|
||||||
stance = netStream.readDouble();
|
stance = netStream.readDouble();
|
||||||
double z = netStream.readDouble();
|
double z = netStream.readDouble();
|
||||||
yaw = netStream.readFloat();
|
float yaw = netStream.readFloat();
|
||||||
pitch = netStream.readFloat();
|
float pitch = netStream.readFloat();
|
||||||
onGround = netStream.readBoolean();
|
onGround = netStream.readBoolean();
|
||||||
|
|
||||||
location = new Location(x, y, z);
|
location = new Location(x, y, z);
|
||||||
|
look = new Look(yaw, pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -43,8 +45,8 @@ public class PositionAndLookPacket implements SCPacket, CSPacket {
|
|||||||
netStream.writeDouble(location.getY());
|
netStream.writeDouble(location.getY());
|
||||||
netStream.writeDouble(stance);
|
netStream.writeDouble(stance);
|
||||||
netStream.writeDouble(location.getZ());
|
netStream.writeDouble(location.getZ());
|
||||||
netStream.writeFloat(yaw);
|
netStream.writeFloat(look.getYaw());
|
||||||
netStream.writeFloat(pitch);
|
netStream.writeFloat(look.getPitch());
|
||||||
netStream.writeBoolean(onGround);
|
netStream.writeBoolean(onGround);
|
||||||
|
|
||||||
return netStream.toByteArray();
|
return netStream.toByteArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user