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
This commit is contained in:
Alex 2024-06-10 23:22:17 +02:00 committed by GitHub
parent ebc06dc94e
commit 97b68eddff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 146 additions and 110 deletions

View file

@ -5,6 +5,7 @@ import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.AuthenticationService;
import com.github.steveice10.mc.auth.service.MojangAuthenticationService;
import com.github.steveice10.mc.auth.service.SessionService;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
@ -76,7 +77,7 @@ public class MinecraftProtocolTest {
session.send(new ClientboundLoginPacket(
0,
false,
new String[]{"minecraft:world"},
new Key[]{Key.key("minecraft:world")},
0,
16,
16,
@ -85,7 +86,7 @@ public class MinecraftProtocolTest {
false,
new PlayerSpawnInfo(
0,
"minecraft:world",
Key.key("minecraft:world"),
100,
GameMode.SURVIVAL,
GameMode.SURVIVAL,

View file

@ -140,6 +140,7 @@ public class BasePacketCodecHelper implements PacketCodecHelper {
return value | ((b & 0x7FL) << (size * 7));
}
@Override
public String readString(ByteBuf buf) {
return this.readString(buf, Short.MAX_VALUE);
}

View file

@ -4,6 +4,7 @@ import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.SessionService;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtType;
@ -136,12 +137,15 @@ public class ServerListener extends SessionAdapter {
// 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
for (Map.Entry<String, Object> entry : networkCodec.entrySet()) {
@SuppressWarnings("PatternValidation")
NbtMap entryTag = (NbtMap) entry.getValue();
String typeTag = entryTag.getString("type");
@SuppressWarnings("PatternValidation")
Key typeTag = Key.key(entryTag.getString("type"));
List<NbtMap> valueTag = entryTag.getList("value", NbtType.COMPOUND);
List<RegistryEntry> entries = new ArrayList<>();
for (NbtMap compoundTag : valueTag) {
String nameTag = compoundTag.getString("name");
@SuppressWarnings("PatternValidation")
Key nameTag = Key.key(compoundTag.getString("name"));
int id = compoundTag.getInt("id");
entries.add(id, new RegistryEntry(nameTag, compoundTag.getCompound("element")));
}

View file

@ -7,6 +7,7 @@ import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -20,7 +21,6 @@ import org.cloudburstmc.nbt.NbtType;
import org.geysermc.mcprotocollib.network.codec.BasePacketCodecHelper;
import org.geysermc.mcprotocollib.protocol.data.DefaultComponentSerializer;
import org.geysermc.mcprotocollib.protocol.data.game.Holder;
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.BlankFormat;
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.FixedFormat;
import org.geysermc.mcprotocollib.protocol.data.game.chat.numbers.NumberFormat;
@ -161,12 +161,13 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
}
}
public String readResourceLocation(ByteBuf buf) {
return Identifier.formalize(this.readString(buf));
@SuppressWarnings("PatternValidation")
public Key readResourceLocation(ByteBuf buf) {
return Key.key(this.readString(buf));
}
public void writeResourceLocation(ByteBuf buf, String location) {
this.writeString(buf, location);
public void writeResourceLocation(ByteBuf buf, Key location) {
this.writeString(buf, location.asString());
}
public UUID readUUID(ByteBuf buf) {
@ -563,19 +564,19 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
}
public GlobalPos readGlobalPos(ByteBuf buf) {
String dimension = Identifier.formalize(this.readString(buf));
Key dimension = readResourceLocation(buf);
Vector3i pos = this.readPosition(buf);
return new GlobalPos(dimension, pos);
}
public void writeGlobalPos(ByteBuf buf, GlobalPos pos) {
this.writeString(buf, pos.getDimension());
this.writeResourceLocation(buf, pos.getDimension());
this.writePosition(buf, pos.getPosition());
}
public PlayerSpawnInfo readPlayerSpawnInfo(ByteBuf buf) {
int dimension = this.readVarInt(buf);
String worldName = this.readString(buf);
Key worldName = this.readResourceLocation(buf);
long hashedSeed = buf.readLong();
GameMode gameMode = GameMode.byId(buf.readByte());
GameMode previousGamemode = GameMode.byNullableId(buf.readByte());
@ -588,7 +589,7 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
public void writePlayerSpawnInfo(ByteBuf buf, PlayerSpawnInfo info) {
this.writeVarInt(buf, info.getDimension());
this.writeString(buf, info.getWorldName());
this.writeResourceLocation(buf, info.getWorldName());
buf.writeLong(info.getHashedSeed());
buf.writeByte(info.getGameMode().ordinal());
buf.writeByte(GameMode.toNullableId(info.getPreviousGamemode()));

View file

@ -1,10 +0,0 @@
package org.geysermc.mcprotocollib.protocol.data.game;
public final class Identifier {
private Identifier() {
}
public static String formalize(String identifier) {
return !identifier.contains(":") ? "minecraft:" + identifier : identifier;
}
}

View file

@ -2,12 +2,13 @@ package org.geysermc.mcprotocollib.protocol.data.game;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.nbt.NbtMap;
import org.jetbrains.annotations.Nullable;
@Data
@AllArgsConstructor
public class RegistryEntry {
private final String id;
private final Key id;
private final @Nullable NbtMap data;
}

View file

@ -1,6 +1,7 @@
package org.geysermc.mcprotocollib.protocol.data.game.chat;
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
import net.kyori.adventure.key.Key;
import org.intellij.lang.annotations.Subst;
import java.util.HashMap;
import java.util.Locale;
@ -15,19 +16,20 @@ public enum BuiltinChatType {
TEAM_MSG_COMMAND_OUTGOING,
EMOTE_COMMAND;
private final String resourceLocation;
private final Key resourceLocation;
BuiltinChatType() {
this.resourceLocation = Identifier.formalize(name().toLowerCase(Locale.ROOT));
@Subst("empty") String lowerCase = name().toLowerCase(Locale.ROOT);
this.resourceLocation = Key.key(lowerCase);
}
public String getResourceLocation() {
public Key getResourceLocation() {
return resourceLocation;
}
private static final Map<String, BuiltinChatType> VALUES = new HashMap<>();
private static final Map<Key, BuiltinChatType> VALUES = new HashMap<>();
public static BuiltinChatType from(String resourceLocation) {
public static BuiltinChatType from(Key resourceLocation) {
return VALUES.get(resourceLocation);
}

View file

@ -3,6 +3,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.command;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.command.properties.CommandProperties;
import java.util.OptionalInt;
@ -49,5 +50,5 @@ public class CommandNode {
* Suggestions type, if present.
* See {@link SuggestionType} for vanilla defaults.
*/
private final String suggestionType;
private final Key suggestionType;
}

View file

@ -1,7 +1,8 @@
package org.geysermc.mcprotocollib.protocol.data.game.command;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
import org.intellij.lang.annotations.Subst;
import java.util.HashMap;
import java.util.Locale;
@ -13,20 +14,21 @@ public enum SuggestionType {
AVAILABLE_SOUNDS,
SUMMONABLE_ENTITIES;
private final String resourceLocation;
private final Key resourceLocation;
SuggestionType() {
this.resourceLocation = Identifier.formalize(name().toLowerCase(Locale.ROOT));
@Subst("empty") String lowerCase = name().toLowerCase(Locale.ROOT);
this.resourceLocation = Key.key(lowerCase);
}
public String getResourceLocation() {
public Key getResourceLocation() {
return resourceLocation;
}
private static final Map<String, SuggestionType> VALUES = new HashMap<>();
private static final Map<Key, SuggestionType> VALUES = new HashMap<>();
@NonNull
public static SuggestionType from(String resourceLocation) {
public static SuggestionType from(Key resourceLocation) {
// Vanilla behavior as of 1.19.3
// 1.16.5 still has AVAILABLE_BIOMES and vanilla doesn't care
return VALUES.getOrDefault(resourceLocation, ASK_SERVER);

View file

@ -1,13 +1,13 @@
package org.geysermc.mcprotocollib.protocol.data.game.command.properties;
import lombok.Data;
import org.geysermc.mcprotocollib.protocol.data.game.Identifier;
import net.kyori.adventure.key.Key;
@Data
public class ResourceProperties implements CommandProperties {
private final String registryKey;
private final Key registryKey;
public ResourceProperties(String registryKey) {
this.registryKey = Identifier.formalize(registryKey);
public ResourceProperties(Key registryKey) {
this.registryKey = registryKey;
}
}

View file

@ -2,12 +2,13 @@ package org.geysermc.mcprotocollib.protocol.data.game.entity.attribute;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;
public interface AttributeType {
String getIdentifier();
Key getIdentifier();
int getId();
@ -18,19 +19,18 @@ public interface AttributeType {
// @Getter
// @EqualsAndHashCode
// class Custom implements AttributeType {
// private final String identifier;
// private final Key identifier;
//
// public Custom(String identifier) {
// public Custom(Key identifier) {
// this.identifier = identifier;
// }
//
// public String getIdentifier() {
// public Key getIdentifier() {
// return identifier;
// }
// }
@Getter
@AllArgsConstructor
enum Builtin implements AttributeType {
GENERIC_ARMOR("minecraft:generic.armor", 0, 0, 30),
GENERIC_ARMOR_TOUGHNESS("minecraft:generic.armor_toughness", 0, 0, 20),
@ -55,11 +55,18 @@ public interface AttributeType {
ZOMBIE_SPAWN_REINFORCEMENTS("minecraft:zombie.spawn_reinforcements", 0, 0, 1),
GENERIC_STEP_HEIGHT("minecraft:generic.step_height", 0.6, 0, 10);
private final String identifier;
private final Key identifier;
private final double def;
private final double min;
private final double max;
Builtin(@KeyPattern String identifier, double def, double min, double max) {
this.identifier = Key.key(identifier);
this.def = def;
this.min = min;
this.max = max;
}
public int getId() {
return this.ordinal();
}

View file

@ -1,17 +1,18 @@
package org.geysermc.mcprotocollib.protocol.data.game.entity.metadata;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.math.vector.Vector3i;
public class GlobalPos {
private final String dimension;
private final Key dimension;
private final Vector3i position;
public GlobalPos(String dimension, Vector3i position) {
public GlobalPos(Key dimension, Vector3i position) {
this.dimension = dimension;
this.position = position;
}
public String getDimension() {
public Key getDimension() {
return dimension;
}

View file

@ -3,6 +3,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.entity.player;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos;
@ -10,7 +11,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos;
@AllArgsConstructor
public class PlayerSpawnInfo {
private final int dimension;
private final @NonNull String worldName;
private final @NonNull Key worldName;
private final long hashedSeed;
private final @NonNull GameMode gameMode;
private final @Nullable GameMode previousGamemode;

View file

@ -2,6 +2,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.nbt.NbtMap;
import org.jetbrains.annotations.Nullable;

View file

@ -1,6 +1,7 @@
package org.geysermc.mcprotocollib.protocol.data.game.item.component;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.protocol.data.game.Holder;
@ -9,6 +10,6 @@ public record ArmorTrim(Holder<TrimMaterial> material, Holder<TrimPattern> patte
Int2ObjectMap<String> overrideArmorMaterials, Component description) {
}
public record TrimPattern(String assetId, int templateItemId, Component description, boolean decal) {
public record TrimPattern(Key assetId, int templateItemId, Component description, boolean decal) {
}
}

View file

@ -2,6 +2,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component;
import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.Holder;
@Data
@ -13,7 +14,7 @@ public class BannerPatternLayer {
@Data
@AllArgsConstructor
public static class BannerPattern {
private final String assetId;
private final Key assetId;
private final String translationKey;
}
}

View file

@ -3,6 +3,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component;
import com.github.steveice10.mc.auth.data.GameProfile;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.nbt.NbtList;
import org.cloudburstmc.nbt.NbtMap;
@ -67,7 +68,7 @@ public class DataComponentType<T> {
public static final DataComponentType<Fireworks.FireworkExplosion> FIREWORK_EXPLOSION = new DataComponentType<>(ItemCodecHelper::readFireworkExplosion, ItemCodecHelper::writeFireworkExplosion, ObjectDataComponent::new);
public static final DataComponentType<Fireworks> FIREWORKS = new DataComponentType<>(ItemCodecHelper::readFireworks, ItemCodecHelper::writeFireworks, ObjectDataComponent::new);
public static final DataComponentType<GameProfile> PROFILE = new DataComponentType<>(ItemCodecHelper::readResolvableProfile, ItemCodecHelper::writeResolvableProfile, ObjectDataComponent::new);
public static final DataComponentType<String> NOTE_BLOCK_SOUND = new DataComponentType<>(ItemCodecHelper::readResourceLocation, ItemCodecHelper::writeResourceLocation, ObjectDataComponent::new);
public static final DataComponentType<Key> NOTE_BLOCK_SOUND = new DataComponentType<>(ItemCodecHelper::readResourceLocation, ItemCodecHelper::writeResourceLocation, ObjectDataComponent::new);
public static final DataComponentType<List<BannerPatternLayer>> BANNER_PATTERNS = new DataComponentType<>(listReader(ItemCodecHelper::readBannerPatternLayer), listWriter(ItemCodecHelper::writeBannerPatternLayer), ObjectDataComponent::new);
public static final IntComponentType BASE_COLOR = new IntComponentType(ItemCodecHelper::readVarInt, ItemCodecHelper::writeVarInt, IntDataComponent::new);
public static final DataComponentType<List<Integer>> POT_DECORATIONS = new DataComponentType<>(listReader(ItemCodecHelper::readVarInt), listWriter(ItemCodecHelper::writeVarInt), ObjectDataComponent::new);

View file

@ -2,6 +2,7 @@ package org.geysermc.mcprotocollib.protocol.data.game.item.component;
import lombok.Data;
import lombok.NonNull;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.function.Function;
@ -12,7 +13,7 @@ import java.util.function.Function;
*/
@Data
public final class HolderSet {
private final @Nullable String location;
private final @Nullable Key location;
private final int @Nullable [] holders;
public HolderSet(int @NonNull [] holders) {
@ -20,7 +21,7 @@ public final class HolderSet {
this.holders = holders;
}
public HolderSet(@NonNull String location) {
public HolderSet(@NonNull Key location) {
this.location = location;
this.holders = null;
}
@ -31,7 +32,7 @@ public final class HolderSet {
* @param tagResolver The function to resolve the tag location to get the holders.
* @return The holders.
*/
public int[] resolve(Function<String, int[]> tagResolver) {
public int[] resolve(Function<Key, int[]> tagResolver) {
if (holders != null) {
return holders;
}

View file

@ -5,6 +5,7 @@ import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.nbt.NbtList;
import org.cloudburstmc.nbt.NbtType;
@ -371,7 +372,7 @@ public class ItemCodecHelper extends MinecraftCodecHelper {
}
public ArmorTrim.TrimPattern readTrimPattern(ByteBuf buf) {
String assetId = this.readResourceLocation(buf);
Key assetId = this.readResourceLocation(buf);
int templateItemId = this.readVarInt(buf);
Component description = this.readComponent(buf);
boolean decal = buf.readBoolean();

View file

@ -3,12 +3,13 @@ package org.geysermc.mcprotocollib.protocol.data.game.recipe;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.recipe.data.RecipeData;
@Data
@AllArgsConstructor
public class Recipe {
private final @NonNull RecipeType type;
private final @NonNull String identifier;
private final @NonNull Key identifier;
private final RecipeData data;
}

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -11,7 +12,7 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@With
@AllArgsConstructor
public class ClientboundCookieRequestPacket implements MinecraftPacket {
private final String key;
private final Key key;
public ClientboundCookieRequestPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.key = helper.readResourceLocation(in);

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -12,17 +13,17 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@With
@AllArgsConstructor
public class ClientboundCustomPayloadPacket implements MinecraftPacket {
private final @NonNull String channel;
private final @NonNull Key channel;
private final byte @NonNull [] data;
public ClientboundCustomPayloadPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.channel = helper.readString(in);
this.channel = helper.readResourceLocation(in);
this.data = helper.readByteArray(in, ByteBuf::readableBytes);
}
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeString(out, this.channel);
helper.writeResourceLocation(out, this.channel);
out.writeBytes(this.data);
}
}

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -11,7 +12,7 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@With
@AllArgsConstructor
public class ClientboundStoreCookiePacket implements MinecraftPacket {
private final String key;
private final Key key;
private final byte[] payload;
public ClientboundStoreCookiePacket(ByteBuf in, MinecraftCodecHelper helper) {

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -15,16 +16,16 @@ import java.util.Map;
@With
@AllArgsConstructor
public class ClientboundUpdateTagsPacket implements MinecraftPacket {
private final @NonNull Map<String, Map<String, int[]>> tags = new HashMap<>();
private final @NonNull Map<Key, Map<Key, int[]>> tags = new HashMap<>();
public ClientboundUpdateTagsPacket(ByteBuf in, MinecraftCodecHelper helper) {
int totalTagCount = helper.readVarInt(in);
for (int i = 0; i < totalTagCount; i++) {
Map<String, int[]> tag = new HashMap<>();
String tagName = helper.readResourceLocation(in);
Map<Key, int[]> tag = new HashMap<>();
Key tagName = helper.readResourceLocation(in);
int tagsCount = helper.readVarInt(in);
for (int j = 0; j < tagsCount; j++) {
String name = helper.readResourceLocation(in);
Key name = helper.readResourceLocation(in);
int entriesCount = helper.readVarInt(in);
int[] entries = new int[entriesCount];
for (int index = 0; index < entriesCount; index++) {
@ -40,10 +41,10 @@ public class ClientboundUpdateTagsPacket implements MinecraftPacket {
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeVarInt(out, tags.size());
for (Map.Entry<String, Map<String, int[]>> tagSet : tags.entrySet()) {
for (Map.Entry<Key, Map<Key, int[]>> tagSet : tags.entrySet()) {
helper.writeResourceLocation(out, tagSet.getKey());
helper.writeVarInt(out, tagSet.getValue().size());
for (Map.Entry<String, int[]> tag : tagSet.getValue().entrySet()) {
for (Map.Entry<Key, int[]> tag : tagSet.getValue().entrySet()) {
helper.writeResourceLocation(out, tag.getKey());
helper.writeVarInt(out, tag.getValue().length);
for (int id : tag.getValue()) {

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
import org.jetbrains.annotations.Nullable;
@ -12,7 +13,7 @@ import org.jetbrains.annotations.Nullable;
@With
@AllArgsConstructor
public class ServerboundCookieResponsePacket implements MinecraftPacket {
private final String key;
private final Key key;
private final byte @Nullable [] payload;
public ServerboundCookieResponsePacket(ByteBuf in, MinecraftCodecHelper helper) {

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -12,17 +13,17 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@With
@AllArgsConstructor
public class ServerboundCustomPayloadPacket implements MinecraftPacket {
private final @NonNull String channel;
private final @NonNull Key channel;
private final byte @NonNull [] data;
public ServerboundCustomPayloadPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.channel = helper.readString(in);
this.channel = helper.readResourceLocation(in);
this.data = helper.readByteArray(in, ByteBuf::readableBytes);
}
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeString(out, this.channel);
helper.writeResourceLocation(out, this.channel);
out.writeBytes(this.data);
}
}

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
import org.geysermc.mcprotocollib.protocol.data.game.RegistryEntry;
@ -14,7 +15,7 @@ import java.util.List;
@With
@AllArgsConstructor
public class ClientboundRegistryDataPacket implements MinecraftPacket {
private final String registry;
private final Key registry;
private final List<RegistryEntry> entries;
public ClientboundRegistryDataPacket(ByteBuf in, MinecraftCodecHelper helper) {

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -11,19 +12,19 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@With
@AllArgsConstructor
public class ClientboundUpdateEnabledFeaturesPacket implements MinecraftPacket {
private final String[] features;
private final Key[] features;
public ClientboundUpdateEnabledFeaturesPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.features = new String[helper.readVarInt(in)];
this.features = new Key[helper.readVarInt(in)];
for (int i = 0; i < this.features.length; i++) {
this.features[i] = helper.readString(in);
this.features[i] = helper.readResourceLocation(in);
}
}
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeVarInt(out, this.features.length);
for (String feature : this.features) {
helper.writeString(out, feature);
for (Key feature : this.features) {
helper.writeResourceLocation(out, feature);
}
}
}

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
import org.geysermc.mcprotocollib.protocol.data.game.command.CommandNode;
@ -67,7 +68,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket {
CommandParser parser = null;
CommandProperties properties = null;
String suggestionType = null;
Key suggestionType = null;
if (type == CommandType.ARGUMENT) {
parser = CommandParser.from(helper.readVarInt(in));
switch (parser) {
@ -135,7 +136,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket {
}
case SCORE_HOLDER -> properties = new ScoreHolderProperties(in.readBoolean());
case TIME -> properties = new TimeProperties(in.readInt());
case RESOURCE_OR_TAG, RESOURCE_OR_TAG_KEY, RESOURCE, RESOURCE_KEY -> properties = new ResourceProperties(helper.readString(in));
case RESOURCE_OR_TAG, RESOURCE_OR_TAG_KEY, RESOURCE, RESOURCE_KEY -> properties = new ResourceProperties(helper.readResourceLocation(in));
default -> {
}
}
@ -286,7 +287,7 @@ public class ClientboundCommandsPacket implements MinecraftPacket {
}
case SCORE_HOLDER -> out.writeBoolean(((ScoreHolderProperties) node.getProperties()).isAllowMultiple());
case TIME -> out.writeInt(((TimeProperties) node.getProperties()).getMin());
case RESOURCE_OR_TAG, RESOURCE_OR_TAG_KEY, RESOURCE, RESOURCE_KEY -> helper.writeString(out, ((ResourceProperties) node.getProperties()).getRegistryKey());
case RESOURCE_OR_TAG, RESOURCE_OR_TAG_KEY, RESOURCE, RESOURCE_KEY -> helper.writeResourceLocation(out, ((ResourceProperties) node.getProperties()).getRegistryKey());
default -> {
}
}

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerSpawnInfo;
@ -15,7 +16,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerSpawnIn
public class ClientboundLoginPacket implements MinecraftPacket {
private final int entityId;
private final boolean hardcore;
private final @NonNull String[] worldNames;
private final @NonNull Key[] worldNames;
private final int maxPlayers;
private final int viewDistance;
private final int simulationDistance;
@ -29,9 +30,9 @@ public class ClientboundLoginPacket implements MinecraftPacket {
this.entityId = in.readInt();
this.hardcore = in.readBoolean();
int worldCount = helper.readVarInt(in);
this.worldNames = new String[worldCount];
this.worldNames = new Key[worldCount];
for (int i = 0; i < worldCount; i++) {
this.worldNames[i] = helper.readString(in);
this.worldNames[i] = helper.readResourceLocation(in);
}
this.maxPlayers = helper.readVarInt(in);
this.viewDistance = helper.readVarInt(in);
@ -48,8 +49,8 @@ public class ClientboundLoginPacket implements MinecraftPacket {
out.writeInt(this.entityId);
out.writeBoolean(this.hardcore);
helper.writeVarInt(out, this.worldNames.length);
for (String worldName : this.worldNames) {
helper.writeString(out, worldName);
for (Key worldName : this.worldNames) {
helper.writeResourceLocation(out, worldName);
}
helper.writeVarInt(out, this.maxPlayers);
helper.writeVarInt(out, this.viewDistance);

View file

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -17,7 +18,7 @@ public class ClientboundStopSoundPacket implements MinecraftPacket {
private static final int FLAG_SOUND = 0x02;
private final @Nullable SoundCategory category;
private final @Nullable String sound;
private final @Nullable Key sound;
public ClientboundStopSoundPacket(ByteBuf in, MinecraftCodecHelper helper) {
int flags = in.readByte();

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
@ -30,7 +31,7 @@ public class ClientboundUpdateRecipesPacket implements MinecraftPacket {
public ClientboundUpdateRecipesPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.recipes = new Recipe[helper.readVarInt(in)];
for (int i = 0; i < this.recipes.length; i++) {
String identifier = helper.readResourceLocation(in);
Key identifier = helper.readResourceLocation(in);
RecipeType type = RecipeType.from(helper.readVarInt(in));
RecipeData data;
switch (type) {

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -14,9 +15,9 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@AllArgsConstructor
public class ServerboundSetJigsawBlockPacket implements MinecraftPacket {
private final @NonNull Vector3i position;
private final @NonNull String name;
private final @NonNull String target;
private final @NonNull String pool;
private final @NonNull Key name;
private final @NonNull Key target;
private final @NonNull Key pool;
private final @NonNull String finalState;
private final @NonNull String jointType;
private final int selectionPriority;
@ -24,9 +25,9 @@ public class ServerboundSetJigsawBlockPacket implements MinecraftPacket {
public ServerboundSetJigsawBlockPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.position = helper.readPosition(in);
this.name = helper.readString(in);
this.target = helper.readString(in);
this.pool = helper.readString(in);
this.name = helper.readResourceLocation(in);
this.target = helper.readResourceLocation(in);
this.pool = helper.readResourceLocation(in);
this.finalState = helper.readString(in);
this.jointType = helper.readString(in);
this.selectionPriority = helper.readVarInt(in);
@ -36,9 +37,9 @@ public class ServerboundSetJigsawBlockPacket implements MinecraftPacket {
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writePosition(out, this.position);
helper.writeString(out, this.name);
helper.writeString(out, this.target);
helper.writeString(out, this.pool);
helper.writeResourceLocation(out, this.name);
helper.writeResourceLocation(out, this.target);
helper.writeResourceLocation(out, this.pool);
helper.writeString(out, this.finalState);
helper.writeString(out, this.jointType);
helper.writeVarInt(out, this.selectionPriority);

View file

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftCodecHelper;
import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@ -13,19 +14,19 @@ import org.geysermc.mcprotocollib.protocol.codec.MinecraftPacket;
@AllArgsConstructor
public class ClientboundCustomQueryPacket implements MinecraftPacket {
private final int messageId;
private final @NonNull String channel;
private final @NonNull Key channel;
private final byte @NonNull [] data;
public ClientboundCustomQueryPacket(ByteBuf in, MinecraftCodecHelper helper) {
this.messageId = helper.readVarInt(in);
this.channel = helper.readString(in);
this.channel = helper.readResourceLocation(in);
this.data = helper.readByteArray(in, ByteBuf::readableBytes);
}
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
helper.writeVarInt(out, this.messageId);
helper.writeString(out, this.channel);
helper.writeResourceLocation(out, this.channel);
out.writeBytes(this.data);
}
}

View file

@ -1,5 +1,6 @@
package org.geysermc.mcprotocollib.protocol;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.geysermc.mcprotocollib.network.Server;
import org.geysermc.mcprotocollib.network.Session;
@ -40,7 +41,7 @@ public class MinecraftProtocolTest {
null,
false
);
private static final ClientboundLoginPacket JOIN_GAME_PACKET = new ClientboundLoginPacket(0, false, new String[]{"minecraft:world"}, 0, 16, 16, false, false, false, new PlayerSpawnInfo(0, "minecraft:world", 100, GameMode.SURVIVAL, GameMode.SURVIVAL, false, false, null, 100), true);
private static final ClientboundLoginPacket JOIN_GAME_PACKET = new ClientboundLoginPacket(0, false, new Key[]{Key.key("minecraft:world")}, 0, 16, 16, false, false, false, new PlayerSpawnInfo(0, Key.key("minecraft:world"), 100, GameMode.SURVIVAL, GameMode.SURVIVAL, false, false, null, 100), true);
private static final Logger log = LoggerFactory.getLogger(MinecraftProtocolTest.class);
private static Server server;

View file

@ -1,5 +1,6 @@
package org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import org.geysermc.mcprotocollib.protocol.data.game.recipe.CraftingBookCategory;
import org.geysermc.mcprotocollib.protocol.data.game.recipe.Ingredient;
@ -21,7 +22,7 @@ public class ServerDeclareRecipesTest extends PacketTest {
new Recipe[]{
new Recipe(
RecipeType.CRAFTING_SHAPELESS,
"minecraft:Recipe1",
Key.key("minecraft:recipe1"),
new ShapelessRecipeData(
"Group1",
CraftingBookCategory.MISC,
@ -35,7 +36,7 @@ public class ServerDeclareRecipesTest extends PacketTest {
),
new Recipe(
RecipeType.CRAFTING_SHAPED,
"minecraft:Recipe2",
Key.key("minecraft:recipe2"),
new ShapedRecipeData(
2,
3,
@ -67,7 +68,7 @@ public class ServerDeclareRecipesTest extends PacketTest {
),
new Recipe(
RecipeType.SMELTING,
"minecraft:Recipe3",
Key.key("minecraft:recipe3"),
new CookedRecipeData(
"Group3",
CraftingBookCategory.EQUIPMENT,
@ -81,7 +82,7 @@ public class ServerDeclareRecipesTest extends PacketTest {
),
new Recipe(
RecipeType.STONECUTTING,
"minecraft:Recipe4",
Key.key("minecraft:recipe4"),
new StoneCuttingRecipeData(
"Group4",
new Ingredient(new ItemStack[]{
@ -93,7 +94,7 @@ public class ServerDeclareRecipesTest extends PacketTest {
),
new Recipe(
RecipeType.SMITHING_TRANSFORM,
"minecraft:Recipe5",
Key.key("minecraft:recipe5"),
new SmithingTransformRecipeData(
new Ingredient(new ItemStack[]{
new ItemStack(10)

View file

@ -1,5 +1,6 @@
package org.geysermc.mcprotocollib.protocol.packet.login.clientbound;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.packet.PacketTest;
import org.junit.jupiter.api.BeforeEach;
@ -11,6 +12,6 @@ public class ClientboundCustomQueryPacketTest extends PacketTest {
byte[] data = new byte[1024];
new Random().nextBytes(data);
this.setPackets(new ClientboundCustomQueryPacket(0, "Channel", data));
this.setPackets(new ClientboundCustomQueryPacket(0, Key.key("channel"), data));
}
}