From d7e66260dec54c43974129538bb1b672cb5e561f Mon Sep 17 00:00:00 2001
From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com>
Date: Sun, 21 Apr 2024 14:53:25 +0200
Subject: [PATCH] Add 1.20.5 nbt->component conversion base
Has to be implemented into ViaBackwards yet and handlers are still missing
---
.../rewriter/StructuredNBTConverter.java | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/StructuredNBTConverter.java
diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/StructuredNBTConverter.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/StructuredNBTConverter.java
new file mode 100644
index 000000000..c92c58ba8
--- /dev/null
+++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/StructuredNBTConverter.java
@@ -0,0 +1,65 @@
+/*
+ * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
+ * Copyright (C) 2016-2024 ViaVersion and contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter;
+
+import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
+import com.github.steveice10.opennbt.tag.builtin.Tag;
+import com.google.common.base.Preconditions;
+import com.viaversion.viaversion.api.minecraft.data.StructuredData;
+import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+// 1.20.5 nbt -> 1.20.5 data component conversion, technically only needed in VB but kept here to make maintenance easier
+public final class StructuredNBTConverter {
+
+ private static final Map> rewriters = new HashMap<>();
+
+ static {
+ register(StructuredDataKey.CUSTOM_DATA, tag -> (CompoundTag) tag);
+ // TODO Add missing handlers for other data types, this will probably be done after the component rework
+ }
+
+ public static List> toData(final CompoundTag tag) {
+ final List> data = new ArrayList<>();
+ for (final Map.Entry entry : tag.entrySet()) {
+ final StructuredData> structuredData = readFromTag(entry.getKey(), entry.getValue());
+ data.add(structuredData);
+ }
+ return data;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static StructuredData readFromTag(final String identifier, final Tag tag) {
+ final DataConverter converter = (DataConverter) rewriters.get(identifier);
+ Preconditions.checkNotNull(converter, "No converter for %s found", identifier);
+ return (StructuredData) converter.convert(tag);
+ }
+
+ private static void register(final StructuredDataKey key, final DataConverter converter) {
+ rewriters.put(key.identifier(), converter);
+ }
+
+ @FunctionalInterface
+ interface DataConverter {
+
+ T convert(final Tag tag);
+ }
+}
\ No newline at end of file