Skip to content

Commit 97b68ed

Browse files
Use built-in ResourceLocation type (#811)
* Use built-in ResourceLocation type * Use kyori Key instead of built-in type * Fix missing keys * Fix packets with incorrect resource location types * Fix more packets with invalid resource locations * Update jigsaw packet to use ResourceLocation * Fix test compilation * Fully use Key for ResourceProperties * Fix all Key.key() warnings * Fix HolderSet revert * Address review
1 parent ebc06dc commit 97b68ed

37 files changed

+146
-110
lines changed

example/src/main/java/org/geysermc/mcprotocollib/protocol/example/MinecraftProtocolTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.github.steveice10.mc.auth.service.AuthenticationService;
66
import com.github.steveice10.mc.auth.service.MojangAuthenticationService;
77
import com.github.steveice10.mc.auth.service.SessionService;
8+
import net.kyori.adventure.key.Key;
89
import net.kyori.adventure.text.Component;
910
import net.kyori.adventure.text.format.NamedTextColor;
1011
import net.kyori.adventure.text.format.TextDecoration;
@@ -76,7 +77,7 @@ public static void main(String[] args) {
7677
session.send(new ClientboundLoginPacket(
7778
0,
7879
false,
79-
new String[]{"minecraft:world"},
80+
new Key[]{Key.key("minecraft:world")},
8081
0,
8182
16,
8283
16,
@@ -85,7 +86,7 @@ public static void main(String[] args) {
8586
false,
8687
new PlayerSpawnInfo(
8788
0,
88-
"minecraft:world",
89+
Key.key("minecraft:world"),
8990
100,
9091
GameMode.SURVIVAL,
9192
GameMode.SURVIVAL,

protocol/src/main/java/org/geysermc/mcprotocollib/network/codec/BasePacketCodecHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public long readVarLong(ByteBuf buf) {
140140
return value | ((b & 0x7FL) << (size * 7));
141141
}
142142

143+
@Override
143144
public String readString(ByteBuf buf) {
144145
return this.readString(buf, Short.MAX_VALUE);
145146
}

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.steveice10.mc.auth.exception.request.RequestException;
55
import com.github.steveice10.mc.auth.service.SessionService;
66
import lombok.RequiredArgsConstructor;
7+
import net.kyori.adventure.key.Key;
78
import net.kyori.adventure.text.Component;
89
import org.cloudburstmc.nbt.NbtMap;
910
import org.cloudburstmc.nbt.NbtType;
@@ -136,12 +137,15 @@ public void packetReceived(Session session, Packet packet) {
136137

137138
// Credit ViaVersion: https://github.com/ViaVersion/ViaVersion/blob/dev/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java
138139
for (Map.Entry<String, Object> entry : networkCodec.entrySet()) {
140+
@SuppressWarnings("PatternValidation")
139141
NbtMap entryTag = (NbtMap) entry.getValue();
140-
String typeTag = entryTag.getString("type");
142+
@SuppressWarnings("PatternValidation")
143+
Key typeTag = Key.key(entryTag.getString("type"));
141144
List<NbtMap> valueTag = entryTag.getList("value", NbtType.COMPOUND);
142145
List<RegistryEntry> entries = new ArrayList<>();
143146
for (NbtMap compoundTag : valueTag) {
144-
String nameTag = compoundTag.getString("name");
147+
@SuppressWarnings("PatternValidation")
148+
Key nameTag = Key.key(compoundTag.getString("name"));
145149
int id = compoundTag.getInt("id");
146150
entries.add(id, new RegistryEntry(nameTag, compoundTag.getCompound("element")));
147151
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.netty.buffer.ByteBufOutputStream;
88
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
99
import lombok.RequiredArgsConstructor;
10+
import net.kyori.adventure.key.Key;
1011
import net.kyori.adventure.text.Component;
1112
import org.checkerframework.checker.nullness.qual.NonNull;
1213
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -20,7 +21,6 @@
2021
import org.geysermc.mcprotocollib.network.codec.BasePacketCodecHelper;
2122
import org.geysermc.mcprotocollib.protocol.data.DefaultComponentSerializer;
2223
import org.geysermc.mcprotocollib.protocol.data.game.Holder;
23-
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
2424
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.BlankFormat;
2525
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.FixedFormat;
2626
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.NumberFormat;
@@ -161,12 +161,13 @@ public <T> void writeHolder(ByteBuf buf, Holder<T> holder, BiConsumer<ByteBuf, T
161161
}
162162
}
163163

164-
public String readResourceLocation(ByteBuf buf) {
165-
return Identifier.formalize(this.readString(buf));
164+
@SuppressWarnings("PatternValidation")
165+
public Key readResourceLocation(ByteBuf buf) {
166+
return Key.key(this.readString(buf));
166167
}
167168

168-
public void writeResourceLocation(ByteBuf buf, String location) {
169-
this.writeString(buf, location);
169+
public void writeResourceLocation(ByteBuf buf, Key location) {
170+
this.writeString(buf, location.asString());
170171
}
171172

172173
public UUID readUUID(ByteBuf buf) {
@@ -563,19 +564,19 @@ public void writeMetadataType(ByteBuf buf, MetadataType<?> type) {
563564
}
564565

565566
public GlobalPos readGlobalPos(ByteBuf buf) {
566-
String dimension = Identifier.formalize(this.readString(buf));
567+
Key dimension = readResourceLocation(buf);
567568
Vector3i pos = this.readPosition(buf);
568569
return new GlobalPos(dimension, pos);
569570
}
570571

571572
public void writeGlobalPos(ByteBuf buf, GlobalPos pos) {
572-
this.writeString(buf, pos.getDimension());
573+
this.writeResourceLocation(buf, pos.getDimension());
573574
this.writePosition(buf, pos.getPosition());
574575
}
575576

576577
public PlayerSpawnInfo readPlayerSpawnInfo(ByteBuf buf) {
577578
int dimension = this.readVarInt(buf);
578-
String worldName = this.readString(buf);
579+
Key worldName = this.readResourceLocation(buf);
579580
long hashedSeed = buf.readLong();
580581
GameMode gameMode = GameMode.byId(buf.readByte());
581582
GameMode previousGamemode = GameMode.byNullableId(buf.readByte());
@@ -588,7 +589,7 @@ public PlayerSpawnInfo readPlayerSpawnInfo(ByteBuf buf) {
588589

589590
public void writePlayerSpawnInfo(ByteBuf buf, PlayerSpawnInfo info) {
590591
this.writeVarInt(buf, info.getDimension());
591-
this.writeString(buf, info.getWorldName());
592+
this.writeResourceLocation(buf, info.getWorldName());
592593
buf.writeLong(info.getHashedSeed());
593594
buf.writeByte(info.getGameMode().ordinal());
594595
buf.writeByte(GameMode.toNullableId(info.getPreviousGamemode()));

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/Identifier.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/RegistryEntry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Data;
5+
import net.kyori.adventure.key.Key;
56
import org.cloudburstmc.nbt.NbtMap;
67
import org.jetbrains.annotations.Nullable;
78

89
@Data
910
@AllArgsConstructor
1011
public class RegistryEntry {
11-
private final String id;
12+
private final Key id;
1213
private final @Nullable NbtMap data;
1314
}

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/chat/BuiltinChatType.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.geysermc.mcprotocollib.protocol.data.game.chat;
22

3-
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
3+
import net.kyori.adventure.key.Key;
4+
import org.intellij.lang.annotations.Subst;
45

56
import java.util.HashMap;
67
import java.util.Locale;
@@ -15,19 +16,20 @@ public enum BuiltinChatType {
1516
TEAM_MSG_COMMAND_OUTGOING,
1617
EMOTE_COMMAND;
1718

18-
private final String resourceLocation;
19+
private final Key resourceLocation;
1920

2021
BuiltinChatType() {
21-
this.resourceLocation = Identifier.formalize(name().toLowerCase(Locale.ROOT));
22+
@Subst("empty") String lowerCase = name().toLowerCase(Locale.ROOT);
23+
this.resourceLocation = Key.key(lowerCase);
2224
}
2325

24-
public String getResourceLocation() {
26+
public Key getResourceLocation() {
2527
return resourceLocation;
2628
}
2729

28-
private static final Map<String, BuiltinChatType> VALUES = new HashMap<>();
30+
private static final Map<Key, BuiltinChatType> VALUES = new HashMap<>();
2931

30-
public static BuiltinChatType from(String resourceLocation) {
32+
public static BuiltinChatType from(Key resourceLocation) {
3133
return VALUES.get(resourceLocation);
3234
}
3335

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/command/CommandNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Data;
55
import lombok.NonNull;
6+
import net.kyori.adventure.key.Key;
67
import org.geysermc.mcprotocollib.protocol.data.game.command.properties.CommandProperties;
78

89
import java.util.OptionalInt;
@@ -49,5 +50,5 @@ public class CommandNode {
4950
* Suggestions type, if present.
5051
* See {@link SuggestionType} for vanilla defaults.
5152
*/
52-
private final String suggestionType;
53+
private final Key suggestionType;
5354
}

protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/game/command/SuggestionType.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.geysermc.mcprotocollib.protocol.data.game.command;
22

3+
import net.kyori.adventure.key.Key;
34
import org.checkerframework.checker.nullness.qual.NonNull;
4-
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
5+
import org.intellij.lang.annotations.Subst;
56

67
import java.util.HashMap;
78
import java.util.Locale;
@@ -13,20 +14,21 @@ public enum SuggestionType {
1314
AVAILABLE_SOUNDS,
1415
SUMMONABLE_ENTITIES;
1516

16-
private final String resourceLocation;
17+
private final Key resourceLocation;
1718

1819
SuggestionType() {
19-
this.resourceLocation = Identifier.formalize(name().toLowerCase(Locale.ROOT));
20+
@Subst("empty") String lowerCase = name().toLowerCase(Locale.ROOT);
21+
this.resourceLocation = Key.key(lowerCase);
2022
}
2123

22-
public String getResourceLocation() {
24+
public Key getResourceLocation() {
2325
return resourceLocation;
2426
}
2527

26-
private static final Map<String, SuggestionType> VALUES = new HashMap<>();
28+
private static final Map<Key, SuggestionType> VALUES = new HashMap<>();
2729

2830
@NonNull
29-
public static SuggestionType from(String resourceLocation) {
31+
public static SuggestionType from(Key resourceLocation) {
3032
// Vanilla behavior as of 1.19.3
3133
// 1.16.5 still has AVAILABLE_BIOMES and vanilla doesn't care
3234
return VALUES.getOrDefault(resourceLocation, ASK_SERVER);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.geysermc.mcprotocollib.protocol.data.game.command.properties;
22

33
import lombok.Data;
4-
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
4+
import net.kyori.adventure.key.Key;
55

66
@Data
77
public class ResourceProperties implements CommandProperties {
8-
private final String registryKey;
8+
private final Key registryKey;
99

10-
public ResourceProperties(String registryKey) {
11-
this.registryKey = Identifier.formalize(registryKey);
10+
public ResourceProperties(Key registryKey) {
11+
this.registryKey = registryKey;
1212
}
1313
}

0 commit comments

Comments
 (0)