Archived
0

видно новых игроков и их передвижения

This commit is contained in:
2018-05-12 00:32:45 +03:00
parent b915b50cd8
commit 8a9183bd6d
12 changed files with 274 additions and 6 deletions

View File

@@ -7,8 +7,8 @@ package mc.core;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@Data
@AllArgsConstructor @AllArgsConstructor
@Data
public class Location { public class Location {
private double x, y, z; private double x, y, z;
@@ -16,6 +16,20 @@ public class Location {
return new Location(location.x, location.y, location.z); 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() { public int getBlockX() {
return (int) x; return (int) x;
} }

View File

@@ -32,4 +32,14 @@ public class BroadcastNetChannel implements NetChannel {
public void writeAndFlush(final SCPacket pkt) { public void writeAndFlush(final SCPacket pkt) {
playerStream.forEach(player -> player.getChannel().writeAndFlush(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());
}
} }

View File

@@ -10,4 +10,6 @@ public interface NetChannel {
void sendChatMessage(String message); void sendChatMessage(String message);
void writeAndFlush(SCPacket pkt); void writeAndFlush(SCPacket pkt);
void write(SCPacket pkt);
void flush();
} }

View File

@@ -15,4 +15,9 @@ public class Look {
public static Look copy(Look look) { public static Look copy(Look look) {
return new Look(look.yaw, look.pitch); return new Look(look.yaw, look.pitch);
} }
public void set(Look look) {
this.yaw = look.yaw;
this.pitch = look.pitch;
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -22,6 +22,11 @@ public class PacketManager {
.put(0x0D, PositionAndLookPacket.class) .put(0x0D, PositionAndLookPacket.class)
.put(0x14, SpawnNamedEntityPacket.class) .put(0x14, SpawnNamedEntityPacket.class)
.put(0x1D, DestroyEntityPacket.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(0x32, ChunkAllocationPacket.class)
.put(0x33, ChunkDataPacket.class) .put(0x33, ChunkDataPacket.class)
.put(0xC9, PlayerInfoPacket.class) .put(0xC9, PlayerInfoPacket.class)

View File

@@ -16,6 +16,7 @@ import mc.core.chat.ChatProcessor;
import mc.core.chat.ChatStyle; import mc.core.chat.ChatStyle;
import mc.core.events.*; import mc.core.events.*;
import mc.core.network.CSPacket; 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.netty.wrappers.WrapperNetChannel;
import mc.core.network.proto_125.packets.*; import mc.core.network.proto_125.packets.*;
import mc.core.player.Look; import mc.core.player.Look;
@@ -29,6 +30,7 @@ import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
@Slf4j @Slf4j
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> { public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
@@ -192,11 +194,22 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
EventBusGetter.INSTANCE.post(event); EventBusGetter.INSTANCE.post(event);
if (!event.isCanceled()) { if (!event.isCanceled()) {
player.getLocation().setX(event.getNewPosition().getX()); Location diffLoc = event.getNewPosition().diff(player.getLocation());
player.getLocation().setY(event.getNewPosition().getY()); player.getLocation().set(event.getNewPosition());
player.getLocation().setZ(event.getNewPosition().getZ());
//TODO если позиция была изменена, нужно оповестить клиент //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); EventBusGetter.INSTANCE.post(event);
if (!event.isCanceled()) { if (!event.isCanceled()) {
player.getLook().setYaw(event.getNewLook().getYaw()); player.getLook().set(event.getNewLook());
player.getLook().setPitch(event.getNewLook().getPitch());
//TODO если обзор был изменен, нужно оповестить клиент //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();
});
} }
} }

View File

@@ -35,4 +35,14 @@ public class WrapperNetChannel implements NetChannel {
public void writeAndFlush(SCPacket pkt) { public void writeAndFlush(SCPacket pkt) {
channel.writeAndFlush(pkt); channel.writeAndFlush(pkt);
} }
@Override
public void write(SCPacket pkt) {
channel.write(pkt);
}
@Override
public void flush() {
channel.flush();
}
} }