Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use built-in ResourceLocation type #811

Merged
merged 13 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -76,7 +77,7 @@ public static void main(String[] args) {
session.send(new ClientboundLoginPacket(
0,
false,
new String[]{"minecraft:world"},
new Key[]{Key.key("minecraft:world")},
0,
16,
16,
Expand All @@ -85,7 +86,7 @@ public static void main(String[] args) {
false,
new PlayerSpawnInfo(
0,
"minecraft:world",
Key.key("minecraft:world"),
100,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public long readVarLong(ByteBuf buf) {
return value | ((b & 0x7FL) << (size * 7));
}

@Override
public String readString(ByteBuf buf) {
return this.readString(buf, Short.MAX_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -136,12 +137,15 @@ public void packetReceived(Session session, Packet packet) {

// 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")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -20,7 +21,6 @@
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;
Expand Down Expand Up @@ -161,12 +161,13 @@ public <T> void writeHolder(ByteBuf buf, Holder<T> holder, BiConsumer<ByteBuf, T
}
}

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) {
Expand Down Expand Up @@ -563,19 +564,19 @@ public void writeMetadataType(ByteBuf buf, MetadataType<?> type) {
}

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());
Expand All @@ -588,7 +589,7 @@ public PlayerSpawnInfo readPlayerSpawnInfo(ByteBuf buf) {

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()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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();

Expand All @@ -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),
Expand All @@ -55,11 +55,18 @@ enum Builtin implements 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
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;

@Data
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.nbt.NbtMap;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,6 +10,6 @@ public record TrimMaterial(String assetName, int ingredientId, float itemModelIn
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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.key.Key;
import org.geysermc.mcprotocollib.protocol.data.game.Holder;

@Data
Expand All @@ -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;
}
}
Loading
Loading