Archived
0

Remove LocationSerializer

This commit is contained in:
2018-08-02 12:54:27 +03:00
parent 1f3d103816
commit 3d88f03a45
4 changed files with 23 additions and 31 deletions

View File

@@ -12,6 +12,11 @@ import lombok.Data;
public class Location { public class Location {
private double x, y, z; private double x, y, z;
private static int floor_double(double value) {
int i = (int)value;
return value < (double)i ? i - 1 : i;
}
public static Location copyOf(Location location) { public static Location copyOf(Location location) {
return new Location( return new Location(
location.x, location.x,
@@ -20,12 +25,22 @@ public class Location {
); );
} }
public Location(long compactValue) {
set(compactValue);
}
public void set(Location location) { public void set(Location location) {
this.x = location.x; this.x = location.x;
this.y = location.y; this.y = location.y;
this.z = location.z; this.z = location.z;
} }
public void set(long compactValue) {
this.x = compactValue >> 38;
this.y = (compactValue >> 26) & 0xFFF;
this.z = compactValue << 38 >> 38; // is normal?
}
public Location diff(Location location) { public Location diff(Location location) {
return new Location( return new Location(
this.x - location.x, this.x - location.x,
@@ -45,4 +60,10 @@ public class Location {
public int getBlockZ() { public int getBlockZ() {
return (int) z; return (int) z;
} }
public long toLong() {
return ((floor_double(x) & 0x3FFFFFF) << 38)
| ((floor_double(y) & 0xFFF) << 26)
| (floor_double(z) & 0x3FFFFFF);
}
} }

View File

@@ -11,7 +11,6 @@ import lombok.ToString;
import mc.core.Location; import mc.core.Location;
import mc.core.network.NetOutputStream; import mc.core.network.NetOutputStream;
import mc.core.network.SCPacket; import mc.core.network.SCPacket;
import mc.core.network.proto_1_12_2.serializers.LocationSerializer;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@@ -22,6 +21,6 @@ public class SpawnPositionPacket implements SCPacket {
@Override @Override
public void writeSelf(NetOutputStream netStream) { public void writeSelf(NetOutputStream netStream) {
netStream.writeLong(LocationSerializer.serialize(location)); netStream.writeLong(location.toLong());
} }
} }

View File

@@ -7,7 +7,6 @@ package mc.core.network.proto_1_12_2.packets;
import mc.core.Location; import mc.core.Location;
import mc.core.network.CSPacket; import mc.core.network.CSPacket;
import mc.core.network.NetInputStream; import mc.core.network.NetInputStream;
import mc.core.network.proto_1_12_2.serializers.LocationSerializer;
public class TabCompletePacket implements CSPacket { public class TabCompletePacket implements CSPacket {
private String text; private String text;
@@ -22,7 +21,7 @@ public class TabCompletePacket implements CSPacket {
this.hasPosition = netStream.readBoolean(); this.hasPosition = netStream.readBoolean();
if (this.hasPosition) { if (this.hasPosition) {
this.location = LocationSerializer.deserialize(netStream.readLong()); this.location = new Location(netStream.readLong());
} }
} }
} }

View File

@@ -1,27 +0,0 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-11
*/
package mc.core.network.proto_1_12_2.serializers;
import mc.core.Location;
public class LocationSerializer {
private static int floor_double(double value) {
int i = (int)value;
return value < (double)i ? i - 1 : i;
}
public static long serialize(Location location) {
return ((floor_double(location.getX()) & 0x3FFFFFF) << 38)
| ((floor_double(location.getY()) & 0xFFF) << 26)
| (floor_double(location.getZ()) & 0x3FFFFFF);
}
public static Location deserialize(long location) {
return new Location(
location >> 38,
(location >> 26) & 0xFFF,
location << 38 >> 38);
}
}