Archived
0

apply upgraded Location & EntityLocation

This commit is contained in:
2018-08-08 13:19:45 +03:00
parent 83de8629e1
commit a0f91730d0
18 changed files with 69 additions and 77 deletions

View File

@@ -1,14 +0,0 @@
package mc.core;
import lombok.AllArgsConstructor;
import lombok.Data;
import mc.core.player.Look;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class WarpPosition implements Serializable {
private Location location;
private Look look;
}

View File

@@ -4,11 +4,10 @@
*/ */
package mc.core.embedded; package mc.core.embedded;
import mc.core.Location; import mc.core.EntityLocation;
import mc.core.chat.MessageType; import mc.core.chat.MessageType;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
import mc.core.network.SCPacket; import mc.core.network.SCPacket;
import mc.core.player.Look;
import mc.core.player.Player; import mc.core.player.Player;
import mc.core.player.PlayerManager; import mc.core.player.PlayerManager;
import mc.core.text.Text; import mc.core.text.Text;
@@ -52,7 +51,7 @@ public class FakePlayerManager implements PlayerManager {
private static final NetChannel FAKE_NET_CHANNEL = new FakeNetChannet(); private static final NetChannel FAKE_NET_CHANNEL = new FakeNetChannet();
@Override @Override
public Player createPlayer(String name, Location defaultLocation, Look defaultLook) { public Player createPlayer(String name, EntityLocation defaultLocation) {
return null; return null;
} }

View File

@@ -7,7 +7,7 @@ package mc.core.events;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import mc.core.player.Look; import mc.core.EntityLocation;
import mc.core.player.Player; import mc.core.player.Player;
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -15,5 +15,5 @@ import mc.core.player.Player;
@Setter @Setter
public class PlayerLookEvent extends EventBase { public class PlayerLookEvent extends EventBase {
private final Player player; private final Player player;
private Look newLook; private EntityLocation newLook;
} }

View File

@@ -7,6 +7,7 @@ package mc.core.player;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.Config; import mc.core.Config;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.network.BroadcastNetChannel; import mc.core.network.BroadcastNetChannel;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
@@ -31,13 +32,13 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
} }
@Override @Override
public Player createPlayer(String name, Location defaultLocation, Look defaultLook) { public Player createPlayer(String name, EntityLocation defaultLocation) {
SimplePlayer player = new SimplePlayer(); SimplePlayer player = new SimplePlayer();
player.setId(rand.nextInt(10000)); player.setId(rand.nextInt(10000));
player.setUUID(UUID.nameUUIDFromBytes(name.getBytes())); player.setUUID(UUID.nameUUIDFromBytes(name.getBytes()));
player.setName(name); player.setName(name);
player.getLocation().set(defaultLocation); player.getLocation().setXYZ(defaultLocation);
player.getLook().set(defaultLook); player.getLocation().setYawPitch(defaultLocation);
player.setSettings(new PlayerSettings()); player.setSettings(new PlayerSettings());
synchronized (lock) { synchronized (lock) {

View File

@@ -4,6 +4,7 @@
*/ */
package mc.core.player; package mc.core.player;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
@@ -18,9 +19,7 @@ public interface Player {
NetChannel getChannel(); NetChannel getChannel();
void setChannel(NetChannel channel); void setChannel(NetChannel channel);
Location getLocation(); EntityLocation getLocation();
Look getLook();
boolean isFlying(); boolean isFlying();
void setFlying(boolean value); void setFlying(boolean value);

View File

@@ -4,6 +4,7 @@
*/ */
package mc.core.player; package mc.core.player;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
@@ -11,7 +12,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface PlayerManager { public interface PlayerManager {
Player createPlayer(String name, Location defaultLocation, Look defaultLook); Player createPlayer(String name, EntityLocation defaultLocation);
void joinServer(Player player); void joinServer(Player player);
void leftServer(Player player); void leftServer(Player player);
Optional<Player> getPlayer(String name); Optional<Player> getPlayer(String name);

View File

@@ -5,6 +5,7 @@
package mc.core.player; package mc.core.player;
import lombok.Data; import lombok.Data;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.network.NetChannel; import mc.core.network.NetChannel;
@@ -17,17 +18,13 @@ public class SimplePlayer implements Player {
private String name; private String name;
private boolean online = false; private boolean online = false;
private NetChannel channel; private NetChannel channel;
private Location location = new Location(0, 0, 0); private EntityLocation location = new EntityLocation(0d, 0d, 0d, 0f, 0f, null);
private Look look = new Look(0, 0);
private boolean flying = false; private boolean flying = false;
private PlayerSettings settings; private PlayerSettings settings;
public void setLocation(Location location) { public void setLocation(EntityLocation location) {
this.location.set(location); this.location.setXYZ(location);
} this.location.setYawPitch(location);
public void setLook(Look look) {
this.look.set(look);
} }
@Override @Override

View File

@@ -4,7 +4,7 @@
*/ */
package mc.core.world; package mc.core.world;
import mc.core.WarpPosition; import mc.core.EntityLocation;
import mc.core.nbt.Taggable; import mc.core.nbt.Taggable;
import mc.core.world.chunk.Chunk; import mc.core.world.chunk.Chunk;
@@ -46,8 +46,8 @@ public interface World extends Taggable, Serializable{
UUID getWorldId(); UUID getWorldId();
IWorldType getWorldType(); IWorldType getWorldType();
WarpPosition getSpawn(); EntityLocation getSpawn();
void setSpawn(WarpPosition location); void setSpawn(EntityLocation location);
Chunk getChunk(int x, int y, int z); Chunk getChunk(int x, int y, int z);
void setChunk(int x, int y, int z, Chunk chunk); void setChunk(int x, int y, int z, Chunk chunk);

View File

@@ -22,7 +22,7 @@ public class BlockFactory {
private class EmbeddedBlock extends AbstractBlock { private class EmbeddedBlock extends AbstractBlock {
EmbeddedBlock(BlockType type, int meta, int x, int y, int z) { EmbeddedBlock(BlockType type, int meta, int x, int y, int z) {
super(type, meta); super(type, meta);
super.setLocation(new Location(x,y,z)); super.setLocation(new Location(x,y,z, null));
} }
} }
} }

View File

@@ -7,9 +7,7 @@ package mc.world.flat;
import com.flowpowered.nbt.Tag; import com.flowpowered.nbt.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import mc.core.Location; import mc.core.EntityLocation;
import mc.core.WarpPosition;
import mc.core.player.Look;
import mc.core.world.*; import mc.core.world.*;
import mc.core.world.chunk.Chunk; import mc.core.world.chunk.Chunk;
@@ -27,7 +25,7 @@ public class FlatWorld implements World {
@Getter @Getter
@Setter @Setter
private WarpPosition spawn = new WarpPosition(new Location(0, 6, 0), new Look(0, 0)); private EntityLocation spawn = new EntityLocation(0d, 6d, 0d, 0f, 0f, this);
private Chunk chunk = new SimpleChunk(0, 0, 0); //FIXME temporary dummy private Chunk chunk = new SimpleChunk(0, 0, 0); //FIXME temporary dummy
@Override @Override

View File

@@ -2,7 +2,7 @@ package mc.world.generated_world.serialization;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.WarpPosition; import mc.core.EntityLocation;
import mc.core.world.World; import mc.core.world.World;
import mc.world.generated_world.world.CubicWorld; import mc.world.generated_world.world.CubicWorld;
@@ -55,7 +55,7 @@ public class WorldReaderWriter {
@Data @Data
public static class WorldInfo implements Serializable { public static class WorldInfo implements Serializable {
private WarpPosition spawn; private EntityLocation spawn;
private String name; private String name;
private int seed; private int seed;
} }

View File

@@ -4,10 +4,9 @@ import com.flowpowered.nbt.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.WarpPosition;
import mc.core.world.block.BlockType; import mc.core.world.block.BlockType;
import mc.core.player.Look;
import mc.core.world.chunk.Chunk; import mc.core.world.chunk.Chunk;
import mc.core.world.IWorldType; import mc.core.world.IWorldType;
import mc.core.world.Region; import mc.core.world.Region;
@@ -27,7 +26,7 @@ public class CubicWorld implements World {
@Getter @Getter
private final UUID worldId; private final UUID worldId;
private final int seed; private final int seed;
private volatile WarpPosition warpPosition; private volatile EntityLocation warpPosition;
private final transient Object spawnLocationLock = new Object(); private final transient Object spawnLocationLock = new Object();
private final Map<String, Tag<?>> nbtTagMap = new HashMap<>(); private final Map<String, Tag<?>> nbtTagMap = new HashMap<>();
@Autowired @Autowired
@@ -61,20 +60,20 @@ public class CubicWorld implements World {
} }
@Override @Override
public WarpPosition getSpawn() { public EntityLocation getSpawn() {
if (warpPosition == null) { if (warpPosition == null) {
synchronized (spawnLocationLock) { synchronized (spawnLocationLock) {
if (warpPosition == null) { if (warpPosition == null) {
log.warn("Spawn location is not defined. Trying to select best location"); log.warn("Spawn location is not defined. Trying to select best location");
warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0, 0)); warpPosition = new EntityLocation(0d, 10d, 0d, 0f, 0f, this);
for (int y = WORLD_MAX_HEIGHT; y > 0; y --) { for (int y = WORLD_MAX_HEIGHT; y > 0; y --) {
Chunk chunk = getChunk(0,y / WORLD_CHUNK_SIZE, 0); Chunk chunk = getChunk(0,y / WORLD_CHUNK_SIZE, 0);
if (chunk.getBlock(0, y, 0).getBlockType() != BlockType.AIR) { if (chunk.getBlock(0, y, 0).getBlockType() != BlockType.AIR) {
warpPosition = new WarpPosition(new Location(0, y + 1, 0), new Look(0, 0)); warpPosition = new EntityLocation(0d, y + 1d, 0d, 0f, 0f, this);
break; break;
} }
} }
warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0,0)); warpPosition = new EntityLocation(0d, 10d, 0d, 0f, 0f, this);
} }
} }
} }
@@ -82,7 +81,7 @@ public class CubicWorld implements World {
} }
@Override @Override
public void setSpawn(WarpPosition warpPosition) { public void setSpawn(EntityLocation warpPosition) {
synchronized (spawnLocationLock) { synchronized (spawnLocationLock) {
this.warpPosition = warpPosition; this.warpPosition = warpPosition;
} }

View File

@@ -5,6 +5,7 @@
package mc.core.network.proto_1_12_2; package mc.core.network.proto_1_12_2;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import mc.core.EntityLocation;
import mc.core.Location; import mc.core.Location;
import mc.core.player.Player; import mc.core.player.Player;
@@ -22,7 +23,7 @@ public class TeleportManager {
@AllArgsConstructor @AllArgsConstructor
private class TpData { private class TpData {
public Player player; public Player player;
public Location newLocation; public EntityLocation newLocation;
// TODO необходимо добавить TimeStamp, что бы понимать, когда клиент отвергнул телепортацию // TODO необходимо добавить TimeStamp, что бы понимать, когда клиент отвергнул телепортацию
// т.е. идея такова: долгое молчание клиента знак отвержения телепортации. // т.е. идея такова: долгое молчание клиента знак отвержения телепортации.
} }
@@ -32,7 +33,7 @@ public class TeleportManager {
private TeleportManager() {} private TeleportManager() {}
public int append(Player player, Location location) { public int append(Player player, EntityLocation location) {
int teleportId; int teleportId;
do { do {
teleportId = RAND.nextInt(9999); teleportId = RAND.nextInt(9999);
@@ -45,7 +46,8 @@ public class TeleportManager {
public void apply(int teleportId) { public void apply(int teleportId) {
if (teleportMap.containsKey(teleportId)) { if (teleportMap.containsKey(teleportId)) {
TpData data = teleportMap.remove(teleportId); TpData data = teleportMap.remove(teleportId);
data.player.getLocation().set(data.newLocation); data.player.getLocation().setXYZ(data.newLocation);
data.player.getLocation().setYawPitch(data.newLocation);
} }
} }

View File

@@ -8,17 +8,15 @@ import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import mc.core.Location; import mc.core.EntityLocation;
import mc.core.network.*; import mc.core.network.*;
import mc.core.player.Look;
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
@ToString @ToString
public class PlayerPositionAndLookPacket implements SCPacket, CSPacket { public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
private Location location; private EntityLocation location;
private Look look;
private int teleportId; private int teleportId;
private boolean onGround = false; private boolean onGround = false;
@@ -27,9 +25,9 @@ public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
netStream.writeDouble(location.getX()); netStream.writeDouble(location.getX());
netStream.writeDouble(location.getY()); netStream.writeDouble(location.getY());
netStream.writeDouble(location.getZ()); netStream.writeDouble(location.getZ());
netStream.writeFloat(look.getYaw()); netStream.writeFloat(location.getYaw());
netStream.writeFloat(look.getPitch()); netStream.writeFloat(location.getPitch());
netStream.writeByte(0); // It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is set, the x value is relative and not absolute. netStream.writeByte(0); // It's a bitfield, X/Y/Z/Y_ROT/X_ROT. If X is setXYZ, the x value is relative and not absolute.
/* X - 0x01 /* X - 0x01
* Y - 0x02 * Y - 0x02
* Z - 0x04 * Z - 0x04
@@ -41,15 +39,13 @@ public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
@Override @Override
public void readSelf(NetInputStream netStream) { public void readSelf(NetInputStream netStream) {
this.location = new Location( this.location = new EntityLocation(
netStream.readDouble(),
netStream.readDouble(), netStream.readDouble(),
netStream.readDouble(), netStream.readDouble(),
netStream.readDouble()
);
this.look = new Look(
netStream.readFloat(), netStream.readFloat(),
netStream.readFloat() netStream.readFloat(),
null
); );
this.onGround = netStream.readBoolean(); this.onGround = netStream.readBoolean();

View File

@@ -19,8 +19,19 @@ import mc.core.network.SCPacket;
public class SpawnPositionPacket implements SCPacket { public class SpawnPositionPacket implements SCPacket {
private Location location; private Location location;
private int floor_double(double value) {
int i = (int)value;
return value < (double)i ? i - 1 : i;
}
private long location2long(Location location) {
return ((floor_double(location.getX()) & 0x3FFFFFF) << 38)
| ((floor_double(location.getY()) & 0xFFF) << 26)
| (floor_double(location.getZ()) & 0x3FFFFFF);
}
@Override @Override
public void writeSelf(NetOutputStream netStream) { public void writeSelf(NetOutputStream netStream) {
netStream.writeLong(location.toLong()); netStream.writeLong(location2long(location));
} }
} }

View File

@@ -21,7 +21,13 @@ public class TabCompletePacket implements CSPacket {
this.hasPosition = netStream.readBoolean(); this.hasPosition = netStream.readBoolean();
if (this.hasPosition) { if (this.hasPosition) {
this.location = new Location(netStream.readLong()); long compactValue = netStream.readLong();
double x = compactValue >> 38;
double y = (compactValue >> 26) & 0xFFF;
double z = compactValue << 38 >> 38; // is normal?
this.location = new Location(x, y, z, null);
} }
} }
} }

View File

@@ -10,7 +10,6 @@ import mc.core.network.proto_1_12_2.State;
import mc.core.network.proto_1_12_2.TeleportManager; import mc.core.network.proto_1_12_2.TeleportManager;
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetChannel; import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetChannel;
import mc.core.network.proto_1_12_2.packets.*; import mc.core.network.proto_1_12_2.packets.*;
import mc.core.player.Look;
import mc.core.player.Player; import mc.core.player.Player;
import mc.core.player.PlayerManager; import mc.core.player.PlayerManager;
import mc.core.player.PlayerMode; import mc.core.player.PlayerMode;
@@ -48,8 +47,7 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
Player player = playerManager.getPlayer(packet.getPlayerName()) Player player = playerManager.getPlayer(packet.getPlayerName())
.orElseGet(() -> playerManager.createPlayer( .orElseGet(() -> playerManager.createPlayer(
packet.getPlayerName(), packet.getPlayerName(),
world.getSpawn().getLocation(), world.getSpawn()));
new Look(0f, 0f)));
channel.writeAndFlush(new LoginSuccessPacket( channel.writeAndFlush(new LoginSuccessPacket(
player.getUUID(), player.getUUID(),
@@ -68,7 +66,7 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
// Spawn Position // Spawn Position
SpawnPositionPacket pkt2 = new SpawnPositionPacket(); SpawnPositionPacket pkt2 = new SpawnPositionPacket();
pkt2.setLocation(world.getSpawn().getLocation()); pkt2.setLocation(world.getSpawn());
channel.write(pkt2); channel.write(pkt2);
// Player Abilities // Player Abilities
@@ -91,7 +89,6 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
// Player Position And Look // Player Position And Look
PlayerPositionAndLookPacket pkt4 = new PlayerPositionAndLookPacket(); PlayerPositionAndLookPacket pkt4 = new PlayerPositionAndLookPacket();
pkt4.setLocation(player.getLocation()); pkt4.setLocation(player.getLocation());
pkt4.setLook(player.getLook());
pkt4.setTeleportId(TeleportManager.getInstance().append(player, player.getLocation())); pkt4.setTeleportId(TeleportManager.getInstance().append(player, player.getLocation()));
channel.writeAndFlush(pkt4); channel.writeAndFlush(pkt4);

View File

@@ -52,8 +52,8 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle
@Handler @Handler
public void onPositionAndLook(Channel channel, PlayerPositionAndLookPacket packet) { public void onPositionAndLook(Channel channel, PlayerPositionAndLookPacket packet) {
Player player = channel.attr(ATTR_PLAYER).get(); Player player = channel.attr(ATTR_PLAYER).get();
player.getLocation().set(packet.getLocation()); player.getLocation().setXYZ(packet.getLocation());
player.getLook().set(packet.getLook()); player.getLocation().setYawPitch(packet.getLocation());
} }
@Handler @Handler