Skip to content

Commit 51b5e93

Browse files
committed
Location Encoding
1 parent 77a3677 commit 51b5e93

File tree

10 files changed

+28
-14
lines changed

10 files changed

+28
-14
lines changed
Binary file not shown.
Binary file not shown.

out/production/resources/logback.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%d{HH:mm:ss} %level]: %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
<root level="debug">
8+
<appender-ref ref="STDOUT" />
9+
</root>
10+
</configuration>
1.9 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package me.outspending.position;
22

3-
public record Location(double x, double y, double z) {
3+
public record Location(long x, long y, long z) {
44
public static Location ZERO = new Location(0, 0, 0);
55
public static Location ONE = new Location(1, 1, 1);
66
}

src/main/java/me/outspending/protocol/AnnotatedPacketHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void onPingRequest(@NotNull Connection client, @NotNull PingRequestPacket
8080
@PacketReceiver
8181
public void onLoginStart(@NotNull Connection client, @NotNull LoginStartPacket packet) {
8282
client.sendPacket(new SetCompressionPacket(-1));
83-
client.sendPacket(new LoginSuccessPacket(packet.uuid(), packet.name(), new ArrayList<>()));
83+
client.sendPacket(new LoginSuccessPacket(packet.uuid(), packet.name(), new LoginSuccessPacket.Property[0]));
8484
}
8585

8686
@PacketReceiver
@@ -100,7 +100,7 @@ public void onAcknowledgeConfiguration(@NotNull Connection client, @NotNull Ackn
100100
0,
101101
false,
102102
1,
103-
List.of("minecraft:overworld"),
103+
new String[]{"minecraft:overworld"},
104104
20,
105105
10,
106106
8,

src/main/java/me/outspending/protocol/PacketReader.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,23 @@ public double readDouble() {
117117
return new UUID(readLong(), readLong());
118118
}
119119

120-
public <T> @Nullable List<T> readArray(@NotNull Function<PacketReader, T> reader, @NotNull IntFunction<T[]> generator) {
120+
public <T> @Nullable T[] readArray(@NotNull Function<PacketReader, T> reader, @NotNull IntFunction<T[]> generator) {
121121
int length = readVarInt();
122122
T[] array = generator.apply(length);
123123
for (int i = 0; i < length; i++) {
124124
array[i] = reader.apply(this);
125125
}
126126

127-
return Arrays.asList(array);
127+
return array;
128128
}
129129

130130
public @Nullable Location readLocation() {
131-
return new Location(readDouble(), readDouble(), readDouble());
131+
long encoded = readLong();
132+
return new Location(
133+
(encoded >> 38),
134+
(encoded << 52 >> 52),
135+
(encoded << 26 >> 38)
136+
);
132137
}
133138

134139
public @Nullable CompoundBinaryTag readNBTCompound() {

src/main/java/me/outspending/protocol/PacketWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.*;
1313
import java.util.UUID;
14+
import java.util.function.BiConsumer;
1415
import java.util.function.Consumer;
1516

1617
@Getter(AccessLevel.PUBLIC)
@@ -113,7 +114,8 @@ public void writeUUID(@NotNull UUID uuid) {
113114
writeLong(uuid.getLeastSignificantBits());
114115
}
115116

116-
public <T> void writeArray(@NotNull Iterable<T> array, @NotNull Consumer<T> consumer) {
117+
public <T> void writeArray(@NotNull T[] array, @NotNull Consumer<T> consumer) {
118+
writeVarInt(array.length);
117119
for (T element : array) {
118120
consumer.accept(element);
119121
}
@@ -128,9 +130,7 @@ public void writePacketWriter(@NotNull PacketWriter writer) {
128130
}
129131

130132
public void writeLocation(@NotNull Location location) {
131-
writeDouble(location.x());
132-
writeDouble(location.y());
133-
writeDouble(location.z());
133+
writeLong(((location.x() & 0x3FFFFFF) << 38) | ((location.z() & 0x3FFFFFF) << 12) | (location.y() & 0xFFF));
134134
}
135135

136136
public void writeNBTCompound(@NotNull CompoundBinaryTag tag) {

src/main/java/me/outspending/protocol/packets/login/client/LoginSuccessPacket.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010
import java.util.UUID;
1111

12-
public record LoginSuccessPacket(UUID uuid, String username, List<Property> properties) implements Packet {
12+
public record LoginSuccessPacket(UUID uuid, String username, Property[] properties) implements Packet {
1313
public static @NotNull LoginSuccessPacket of(@NotNull PacketReader reader) {
1414
return new LoginSuccessPacket(
1515
reader.readUUID(),
@@ -33,7 +33,6 @@ private void writeProperty(@NotNull Property property, @NotNull PacketWriter wri
3333
public void write(@NotNull PacketWriter writer) {
3434
writer.writeUUID(uuid);
3535
writer.writeString(username);
36-
writer.writeVarInt(properties.size());
3736
writer.writeArray(properties, (property) -> writeProperty(property, writer));
3837
}
3938

src/main/java/me/outspending/protocol/packets/play/client/LoginPlayPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public record LoginPlayPacket(
1414
int entityID,
1515
boolean isHardcore,
1616
int dimensionCount,
17-
List<String> dimensionNames,
17+
String[] dimensionNames, // Might be the issue
1818
int maxPlayers,
1919
int viewDistance,
2020
int simulationDistance,
@@ -30,7 +30,7 @@ public record LoginPlayPacket(
3030
boolean isFlatWorld,
3131
boolean hasDeathLocation,
3232
String deathDimensionName,
33-
Location deathLocation,
33+
Location deathLocation, // Might be the issue
3434
int portalCooldown
3535
) implements Packet {
3636
public static LoginPlayPacket of(@NotNull PacketReader reader) {

0 commit comments

Comments
 (0)