видно новых игроков и их передвижения
This commit is contained in:
@@ -7,8 +7,8 @@ package mc.core;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class Location {
|
||||
private double x, y, z;
|
||||
|
||||
@@ -16,6 +16,20 @@ public class Location {
|
||||
return new Location(location.x, location.y, location.z);
|
||||
}
|
||||
|
||||
public void set(Location location) {
|
||||
this.x = location.x;
|
||||
this.y = location.y;
|
||||
this.z = location.z;
|
||||
}
|
||||
|
||||
public Location diff(Location location) {
|
||||
return new Location(
|
||||
this.x - location.x,
|
||||
this.y - location.y,
|
||||
this.z - location.z
|
||||
);
|
||||
}
|
||||
|
||||
public int getBlockX() {
|
||||
return (int) x;
|
||||
}
|
||||
|
||||
@@ -32,4 +32,14 @@ public class BroadcastNetChannel implements NetChannel {
|
||||
public void writeAndFlush(final SCPacket pkt) {
|
||||
playerStream.forEach(player -> player.getChannel().writeAndFlush(pkt));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(SCPacket pkt) {
|
||||
playerStream.forEach(player -> player.getChannel().write(pkt));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
playerStream.forEach(player -> player.getChannel().flush());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ public interface NetChannel {
|
||||
void sendChatMessage(String message);
|
||||
|
||||
void writeAndFlush(SCPacket pkt);
|
||||
void write(SCPacket pkt);
|
||||
void flush();
|
||||
}
|
||||
|
||||
@@ -15,4 +15,9 @@ public class Look {
|
||||
public static Look copy(Look look) {
|
||||
return new Look(look.yaw, look.pitch);
|
||||
}
|
||||
|
||||
public void set(Look look) {
|
||||
this.yaw = look.yaw;
|
||||
this.pitch = look.pitch;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-12
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class EntityLookHeadPacket implements SCPacket {
|
||||
private int id;
|
||||
private double yaw;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(id);
|
||||
netStream.writeByte((byte)(int)((yaw * 256f) / 360f));
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-12
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
import mc.core.player.Look;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class EntityLookPacket implements SCPacket {
|
||||
private int id;
|
||||
private Look look;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(id);
|
||||
netStream.writeByte((byte)(int)((look.getYaw() * 256f) / 360f));
|
||||
netStream.writeByte((byte)(int)((look.getPitch() * 256f) / 360f));
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-12
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
import mc.core.player.Look;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class EntityLookRelativeMovePacket implements SCPacket {
|
||||
private int id;
|
||||
private Location location;
|
||||
private Look look;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(id);
|
||||
netStream.writeByte((byte) (location.getX() * 32d));
|
||||
netStream.writeByte((byte) (location.getY() * 32d));
|
||||
netStream.writeByte((byte) (location.getZ() * 32d));
|
||||
netStream.writeByte((byte)(int)((look.getYaw() * 256f) / 360f));
|
||||
netStream.writeByte((byte)(int)((look.getPitch() * 256f) / 360f));
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-11
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class EntityRelativeMovePacket implements SCPacket {
|
||||
private int id;
|
||||
private Location location;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(id);
|
||||
netStream.writeByte((byte) (location.getX() * 32d));
|
||||
netStream.writeByte((byte) (location.getY() * 32d));
|
||||
netStream.writeByte((byte) (location.getZ() * 32d));
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-11
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
import mc.core.player.Look;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class EntityTeleportPacket implements SCPacket {
|
||||
private int id;
|
||||
private Location location;
|
||||
private Look look;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(id);
|
||||
netStream.writeInt((int) (location.getBlockX() * 32d));
|
||||
netStream.writeInt((int) (location.getBlockY() * 32d));
|
||||
netStream.writeInt((int) (location.getBlockZ() * 32d));
|
||||
netStream.writeByte((byte)(int)((look.getYaw() * 256f) / 360f));
|
||||
netStream.writeByte((byte)(int)((look.getPitch() * 256f) / 360f));
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,11 @@ public class PacketManager {
|
||||
.put(0x0D, PositionAndLookPacket.class)
|
||||
.put(0x14, SpawnNamedEntityPacket.class)
|
||||
.put(0x1D, DestroyEntityPacket.class)
|
||||
.put(0x1F, EntityRelativeMovePacket.class)
|
||||
.put(0x20, EntityLookPacket.class)
|
||||
.put(0x21, EntityLookRelativeMovePacket.class)
|
||||
.put(0x22, EntityTeleportPacket.class)
|
||||
.put(0x23, EntityLookHeadPacket.class)
|
||||
.put(0x32, ChunkAllocationPacket.class)
|
||||
.put(0x33, ChunkDataPacket.class)
|
||||
.put(0xC9, PlayerInfoPacket.class)
|
||||
|
||||
@@ -16,6 +16,7 @@ import mc.core.chat.ChatProcessor;
|
||||
import mc.core.chat.ChatStyle;
|
||||
import mc.core.events.*;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
import mc.core.player.Look;
|
||||
@@ -29,6 +30,7 @@ import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
@@ -192,11 +194,22 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
player.getLocation().setX(event.getNewPosition().getX());
|
||||
player.getLocation().setY(event.getNewPosition().getY());
|
||||
player.getLocation().setZ(event.getNewPosition().getZ());
|
||||
Location diffLoc = event.getNewPosition().diff(player.getLocation());
|
||||
player.getLocation().set(event.getNewPosition());
|
||||
|
||||
//TODO если позиция была изменена, нужно оповестить клиент
|
||||
|
||||
final SCPacket pkt;
|
||||
if ((diffLoc.getBlockX() >= 4 || diffLoc.getBlockX() <= -4)
|
||||
|| (diffLoc.getBlockY() >= 4 || diffLoc.getBlockY() <= -4)
|
||||
|| (diffLoc.getBlockZ() >= 4 || diffLoc.getBlockZ() <= -4)) {
|
||||
pkt = new EntityTeleportPacket(player.getId(), player.getLocation(), player.getLook());
|
||||
} else {
|
||||
pkt = new EntityRelativeMovePacket(player.getId(), diffLoc);
|
||||
}
|
||||
playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId())
|
||||
.forEach(pl -> pl.getChannel().writeAndFlush(pkt));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,10 +220,45 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
player.getLook().setYaw(event.getNewLook().getYaw());
|
||||
player.getLook().setPitch(event.getNewLook().getPitch());
|
||||
player.getLook().set(event.getNewLook());
|
||||
|
||||
//TODO если обзор был изменен, нужно оповестить клиент
|
||||
|
||||
final SCPacket pkt1 = new EntityLookPacket(player.getId(), player.getLook());
|
||||
final SCPacket pkt2 = new EntityLookHeadPacket(player.getId(), player.getLook().getYaw());
|
||||
playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId())
|
||||
.forEach(pl -> {
|
||||
pl.getChannel().write(pkt1);
|
||||
pl.getChannel().write(pkt2);
|
||||
pl.getChannel().flush();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onPositionAndLookPacket(Channel channel, PositionAndLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
|
||||
Location diffLoc = packet.getLocation().diff(player.getLocation());
|
||||
player.getLocation().set(packet.getLocation());
|
||||
player.getLook().set(packet.getLook());
|
||||
|
||||
Stream<Player> stream = playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId());
|
||||
|
||||
if ((diffLoc.getBlockX() >= 4 || diffLoc.getBlockX() <= -4)
|
||||
|| (diffLoc.getBlockY() >= 4 || diffLoc.getBlockY() <= -4)
|
||||
|| (diffLoc.getBlockZ() >= 4 || diffLoc.getBlockZ() <= -4)) {
|
||||
final SCPacket pkt = new EntityTeleportPacket(player.getId(), player.getLocation(), player.getLook());
|
||||
stream.forEach(pl -> pl.getChannel().writeAndFlush(pkt));
|
||||
} else {
|
||||
final SCPacket pkt1 = new EntityLookRelativeMovePacket(player.getId(), diffLoc, player.getLook());
|
||||
final SCPacket pkt2 = new EntityLookHeadPacket(player.getId(), player.getLook().getYaw());
|
||||
stream.forEach(pl -> {
|
||||
pl.getChannel().write(pkt1);
|
||||
pl.getChannel().write(pkt2);
|
||||
pl.getChannel().flush();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,4 +35,14 @@ public class WrapperNetChannel implements NetChannel {
|
||||
public void writeAndFlush(SCPacket pkt) {
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(SCPacket pkt) {
|
||||
channel.write(pkt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
channel.flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user