Skip to content

Commit 0284d57

Browse files
25w34a
1 parent a713727 commit 0284d57

File tree

8 files changed

+152
-46
lines changed

8 files changed

+152
-46
lines changed

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftCodec.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundKeepAlivePacket;
2020
import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundPongPacket;
2121
import org.geysermc.mcprotocollib.protocol.packet.common.serverbound.ServerboundResourcePackPacket;
22+
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundCodeOfConductPacket;
2223
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
2324
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundRegistryDataPacket;
2425
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundResetChatPacket;
2526
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundSelectKnownPacks;
2627
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundShowDialogConfigurationPacket;
2728
import org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound.ClientboundUpdateEnabledFeaturesPacket;
29+
import org.geysermc.mcprotocollib.protocol.packet.configuration.serverbound.ServerboundAcceptCodeOfConductPacket;
2830
import org.geysermc.mcprotocollib.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
2931
import org.geysermc.mcprotocollib.protocol.packet.configuration.serverbound.ServerboundSelectKnownPacks;
3032
import org.geysermc.mcprotocollib.protocol.packet.cookie.clientbound.ClientboundCookieRequestPacket;
@@ -224,8 +226,8 @@
224226

225227
public class MinecraftCodec {
226228
public static final PacketCodec CODEC = PacketCodec.builder()
227-
.protocolVersion((1 << 30) | 262)
228-
.minecraftVersion("25w33a")
229+
.protocolVersion((1 << 30) | 263)
230+
.minecraftVersion("25w34a")
229231
.state(ProtocolState.HANDSHAKE, MinecraftPacketRegistry.builder()
230232
.registerServerboundPacket(ClientIntentionPacket.class, ClientIntentionPacket::new)
231233
)
@@ -266,6 +268,7 @@ public class MinecraftCodec {
266268
.registerClientboundPacket(ClientboundServerLinksPacket.class, ClientboundServerLinksPacket::new)
267269
.registerClientboundPacket(ClientboundClearDialogPacket.class, ClientboundClearDialogPacket::new)
268270
.registerClientboundPacket(ClientboundShowDialogConfigurationPacket.class, ClientboundShowDialogConfigurationPacket::new)
271+
.registerClientboundPacket(ClientboundCodeOfConductPacket.class, ClientboundCodeOfConductPacket::new)
269272
.registerServerboundPacket(ServerboundClientInformationPacket.class, ServerboundClientInformationPacket::new)
270273
.registerServerboundPacket(ServerboundCookieResponsePacket.class, ServerboundCookieResponsePacket::new)
271274
.registerServerboundPacket(ServerboundCustomPayloadPacket.class, ServerboundCustomPayloadPacket::new)
@@ -275,6 +278,7 @@ public class MinecraftCodec {
275278
.registerServerboundPacket(ServerboundResourcePackPacket.class, ServerboundResourcePackPacket::new)
276279
.registerServerboundPacket(ServerboundSelectKnownPacks.class, ServerboundSelectKnownPacks::new)
277280
.registerServerboundPacket(ServerboundCustomClickActionPacket.class, ServerboundCustomClickActionPacket::new)
281+
.registerServerboundPacket(ServerboundAcceptCodeOfConductPacket.class, ServerboundAcceptCodeOfConductPacket::new)
278282
).state(ProtocolState.GAME, MinecraftPacketRegistry.builder()
279283
.registerClientboundPacket(ClientboundDelimiterPacket.class, ClientboundDelimiterPacket::new)
280284
.registerClientboundPacket(ClientboundAddEntityPacket.class, ClientboundAddEntityPacket::new)

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/codec/MinecraftTypes.java

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -678,39 +678,59 @@ public static Vector3d readLpVec3(ByteBuf buf) {
678678
int second = buf.readUnsignedByte();
679679
long third = buf.readUnsignedInt();
680680
long packed = third << 16 | second << 8 | first;
681-
double encodedX = packed >> 3 & Short.MAX_VALUE;
682-
double encodedY = packed >> 18 & Short.MAX_VALUE;
683-
double encodedZ = packed >> 33 & Short.MAX_VALUE;
684-
int k = first & 3;
681+
long magicMultiplier = first & 3;
685682
if ((first & 4) == 4) {
686-
k |= MinecraftTypes.readVarInt(buf) << 2;
683+
magicMultiplier |= (MinecraftTypes.readVarInt(buf) & 0xFFFFFFFFL) << 2;
687684
}
688685

689-
return Vector3d.from((encodedX / 16383.0 - 1.0) * k, (encodedY / 16383.0 - 1.0) * k, (encodedZ / 16383.0 - 1.0) * k);
686+
return Vector3d.from(
687+
unpackLpVec3Component(packed >> 3) * magicMultiplier,
688+
unpackLpVec3Component(packed >> 18) * magicMultiplier,
689+
unpackLpVec3Component(packed >> 33) * magicMultiplier
690+
);
691+
}
692+
693+
private static double unpackLpVec3Component(long packed) {
694+
return Math.min(packed & 32767L, 32766.0) * 2.0 / 32766.0 - 1.0;
690695
}
691696

692697
public static void writeLpVec3(ByteBuf buf, Vector3d vec) {
693-
double maxVal = Math.max(Math.abs(vec.getX()), Math.max(Math.abs(vec.getY()), Math.abs(vec.getZ())));
694-
if (maxVal < 1.0E-5F) {
698+
double sanitizedX = sanitizeLpVec3Component(vec.getX());
699+
double sanitizedY = sanitizeLpVec3Component(vec.getY());
700+
double sanitizedZ = sanitizeLpVec3Component(vec.getZ());
701+
double maxVal = Math.max(Math.abs(sanitizedX), Math.max(Math.abs(sanitizedY), Math.abs(sanitizedZ)));
702+
if (maxVal < 3.051944088384301E-5) {
695703
buf.writeByte(0);
696704
return;
697705
}
698-
699-
int scale = (int) Math.ceil(maxVal);
700-
double quantizationFactor = 0.5 / scale;
701-
long encodedX = (long)((vec.getX() * quantizationFactor + 0.5) * 32767.0);
702-
long encodedY = (long)((vec.getY() * quantizationFactor + 0.5) * 32767.0);
703-
long encodedZ = (long)((vec.getZ() * quantizationFactor + 0.5) * 32767.0);
704-
705-
boolean scaleTooLargeForBits = (scale & 3) != scale;
706-
int scaleBits = scaleTooLargeForBits ? scale & 3 | 4 : scale;
707-
long packed = scaleBits | encodedX << 3 | encodedY << 18 | encodedZ << 33;
708-
buf.writeByte((byte)packed);
709-
buf.writeByte((byte)(packed >> 8));
710-
buf.writeInt((int)(packed >> 16));
706+
long scale = (long) Math.ceil(maxVal);
707+
boolean scaleTooLargeForBits = (scale & 3L) != scale;
708+
long scaleBits = scaleTooLargeForBits ? scale & 3L | 4L : scale;
709+
long encodedX = packLpVec3Component(sanitizedX / scale) << 3;
710+
long encodedY = packLpVec3Component(sanitizedY / scale) << 18;
711+
long encodedZ = packLpVec3Component(sanitizedZ / scale) << 33;
712+
long packed = scaleBits | encodedX | encodedY | encodedZ;
713+
buf.writeByte((byte) packed);
714+
buf.writeByte((byte) (packed >> 8));
715+
buf.writeInt((int) (packed >> 16));
711716
if (scaleTooLargeForBits) {
712-
MinecraftTypes.writeVarInt(buf, scale >> 2);
717+
MinecraftTypes.writeVarInt(buf, (int) (scale >> 2));
718+
}
719+
}
720+
721+
private static double sanitizeLpVec3Component(double d) {
722+
if (Double.isNaN(d)) {
723+
return 0.0;
724+
} else if (d < -1.7179869183E10) {
725+
return -1.7179869183E10;
726+
} else if (d > 1.7179869183E10) {
727+
return 1.7179869183E10;
713728
}
729+
return d;
730+
}
731+
732+
private static long packLpVec3Component(double d) {
733+
return Math.round((d * 0.5 + 0.5) * 32766.0);
714734
}
715735

716736
public static Vector3i readPosition(ByteBuf buf) {
@@ -1494,6 +1514,36 @@ public static void writeFixedBitSet(ByteBuf buf, BitSet bitSet, int length) {
14941514
}
14951515
}
14961516

1517+
public static GameProfile readStaticGameProfile(ByteBuf buf) {
1518+
GameProfile profile = new GameProfile(MinecraftTypes.readUUID(buf), MinecraftTypes.readString(buf));
1519+
profile.setProperties(MinecraftTypes.readList(buf, MinecraftTypes::readProperty));
1520+
return profile;
1521+
}
1522+
1523+
public static void writeStaticGameProfile(ByteBuf buf, GameProfile profile) {
1524+
MinecraftTypes.writeUUID(buf, profile.getId());
1525+
MinecraftTypes.writeString(buf, profile.getName());
1526+
MinecraftTypes.writeList(buf, profile.getProperties(), MinecraftTypes::writeProperty);
1527+
}
1528+
1529+
public static GameProfile readDynamicGameProfile(ByteBuf buf) {
1530+
String name = MinecraftTypes.readNullable(buf, MinecraftTypes::readString);
1531+
UUID id = MinecraftTypes.readNullable(buf, MinecraftTypes::readUUID);
1532+
GameProfile profile = new GameProfile(id, name);
1533+
1534+
List<GameProfile.Property> properties = MinecraftTypes.readList(buf, MinecraftTypes::readProperty);
1535+
profile.setProperties(properties);
1536+
1537+
return profile;
1538+
}
1539+
1540+
public static void writeDynamicGameProfile(ByteBuf buf, GameProfile profile) {
1541+
MinecraftTypes.writeNullable(buf, profile.getName(), MinecraftTypes::writeString);
1542+
MinecraftTypes.writeNullable(buf, profile.getId(), MinecraftTypes::writeUUID);
1543+
1544+
MinecraftTypes.writeList(buf, profile.getProperties(), MinecraftTypes::writeProperty);
1545+
}
1546+
14971547
public static GameProfile.Property readProperty(ByteBuf buf) {
14981548
String name = MinecraftTypes.readString(buf);
14991549
String value = MinecraftTypes.readString(buf);

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/DataComponentTypes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import net.kyori.adventure.text.Component;
88
import org.cloudburstmc.nbt.NbtList;
99
import org.cloudburstmc.nbt.NbtMap;
10-
import org.geysermc.mcprotocollib.auth.GameProfile;
1110
import org.geysermc.mcprotocollib.protocol.codec.MinecraftTypes;
1211
import org.geysermc.mcprotocollib.protocol.data.game.Holder;
1312
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.PaintingVariant;
@@ -87,7 +86,7 @@ public class DataComponentTypes {
8786
public static final DataComponentType<LodestoneTracker> LODESTONE_TRACKER = register(id -> new DataComponentType<>(id, "lodestone_tracker", ItemTypes::readLodestoneTarget, ItemTypes::writeLodestoneTarget, ObjectDataComponent::new));
8887
public static final DataComponentType<Fireworks.FireworkExplosion> FIREWORK_EXPLOSION = register(id -> new DataComponentType<>(id, "firework_explosion", ItemTypes::readFireworkExplosion, ItemTypes::writeFireworkExplosion, ObjectDataComponent::new));
8988
public static final DataComponentType<Fireworks> FIREWORKS = register(id -> new DataComponentType<>(id, "fireworks", ItemTypes::readFireworks, ItemTypes::writeFireworks, ObjectDataComponent::new));
90-
public static final DataComponentType<GameProfile> PROFILE = register(id -> new DataComponentType<>(id, "profile", ItemTypes::readResolvableProfile, ItemTypes::writeResolvableProfile, ObjectDataComponent::new));
89+
public static final DataComponentType<ResolvableProfile> PROFILE = register(id -> new DataComponentType<>(id, "profile", ItemTypes::readResolvableProfile, ItemTypes::writeResolvableProfile, ObjectDataComponent::new));
9190
public static final DataComponentType<Key> NOTE_BLOCK_SOUND = register(id -> new DataComponentType<>(id, "note_block_sound", MinecraftTypes::readResourceLocation, MinecraftTypes::writeResourceLocation, ObjectDataComponent::new));
9291
public static final DataComponentType<List<BannerPatternLayer>> BANNER_PATTERNS = register(id -> new DataComponentType<>(id, "banner_patterns", listReader(ItemTypes::readBannerPatternLayer), listWriter(ItemTypes::writeBannerPatternLayer), ObjectDataComponent::new));
9392
public static final IntComponentType BASE_COLOR = register(id -> new IntComponentType(id, "base_color", MinecraftTypes::readVarInt, MinecraftTypes::writeVarInt, IntDataComponent::new));

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/item/component/ItemTypes.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -697,22 +697,19 @@ public static void writeFireworkExplosion(ByteBuf buf, Fireworks.FireworkExplosi
697697
buf.writeBoolean(explosion.isHasTwinkle());
698698
}
699699

700-
public static GameProfile readResolvableProfile(ByteBuf buf) {
701-
String name = MinecraftTypes.readNullable(buf, MinecraftTypes::readString);
702-
UUID id = MinecraftTypes.readNullable(buf, MinecraftTypes::readUUID);
703-
GameProfile profile = new GameProfile(id, name);
704-
705-
List<GameProfile.Property> properties = MinecraftTypes.readList(buf, MinecraftTypes::readProperty);
706-
profile.setProperties(properties);
707-
708-
return profile;
700+
public static ResolvableProfile readResolvableProfile(ByteBuf buf) {
701+
return buf.readBoolean()
702+
? new ResolvableProfile(MinecraftTypes.readStaticGameProfile(buf), false)
703+
: new ResolvableProfile(MinecraftTypes.readDynamicGameProfile(buf), true);
709704
}
710705

711-
public static void writeResolvableProfile(ByteBuf buf, GameProfile profile) {
712-
MinecraftTypes.writeNullable(buf, profile.getName(), MinecraftTypes::writeString);
713-
MinecraftTypes.writeNullable(buf, profile.getId(), MinecraftTypes::writeUUID);
714-
715-
MinecraftTypes.writeList(buf, profile.getProperties(), MinecraftTypes::writeProperty);
706+
public static void writeResolvableProfile(ByteBuf buf, ResolvableProfile profile) {
707+
buf.writeBoolean(!profile.isDynamic());
708+
if (!profile.isDynamic()) {
709+
MinecraftTypes.writeStaticGameProfile(buf, profile.getProfile());
710+
} else {
711+
MinecraftTypes.writeDynamicGameProfile(buf, profile.getProfile());
712+
}
716713
}
717714

718715
public static BannerPatternLayer readBannerPatternLayer(ByteBuf buf) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.geysermc.mcprotocollib.protocol.data.game.item.component;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.With;
6+
import org.geysermc.mcprotocollib.auth.GameProfile;
7+
8+
@Data
9+
@With
10+
@AllArgsConstructor
11+
public class ResolvableProfile {
12+
private final GameProfile profile;
13+
private final boolean dynamic;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.geysermc.mcprotocollib.protocol.packet.configuration.clientbound;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import lombok.With;
7+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
8+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftTypes;
9+
10+
@Data
11+
@With
12+
@AllArgsConstructor
13+
public class ClientboundCodeOfConductPacket implements MinecraftPacket {
14+
private final String codeOfConduct;
15+
16+
public ClientboundCodeOfConductPacket(ByteBuf in) {
17+
this.codeOfConduct = MinecraftTypes.readString(in);
18+
}
19+
20+
@Override
21+
public void serialize(ByteBuf buf) {
22+
MinecraftTypes.writeString(buf, codeOfConduct);
23+
}
24+
25+
@Override
26+
public boolean shouldRunOnGameThread() {
27+
return true;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.geysermc.mcprotocollib.protocol.packet.configuration.serverbound;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
5+
6+
public class ServerboundAcceptCodeOfConductPacket implements MinecraftPacket {
7+
8+
public ServerboundAcceptCodeOfConductPacket(ByteBuf in) {}
9+
10+
@Override
11+
public void serialize(ByteBuf buf) {}
12+
13+
@Override
14+
public boolean shouldRunOnGameThread() {
15+
return true;
16+
}
17+
}

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginFinishedPacket.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ public class ClientboundLoginFinishedPacket implements MinecraftPacket {
1616
private final @NonNull GameProfile profile;
1717

1818
public ClientboundLoginFinishedPacket(ByteBuf in) {
19-
GameProfile profile = new GameProfile(MinecraftTypes.readUUID(in), MinecraftTypes.readString(in));
20-
profile.setProperties(MinecraftTypes.readList(in, MinecraftTypes::readProperty));
21-
this.profile = profile;
19+
this.profile = MinecraftTypes.readStaticGameProfile(in);
2220
}
2321

2422
@Override
2523
public void serialize(ByteBuf out) {
26-
MinecraftTypes.writeUUID(out, this.profile.getId());
27-
MinecraftTypes.writeString(out, this.profile.getName());
28-
MinecraftTypes.writeList(out, this.profile.getProperties(), MinecraftTypes::writeProperty);
24+
MinecraftTypes.writeStaticGameProfile(out, profile);
2925
}
3026

3127
@Override

0 commit comments

Comments
 (0)