Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/dev/3.0.0' into dev/3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Dec 21, 2023
2 parents 9488237 + a54d8c6 commit 38c8345
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.BinaryTagIO;
import net.kyori.adventure.nbt.BinaryTagType;
import net.kyori.adventure.nbt.BinaryTagTypes;
import net.kyori.adventure.nbt.ByteArrayBinaryTag;
import net.kyori.adventure.nbt.ByteBinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
Expand Down Expand Up @@ -153,8 +154,19 @@ public static BinaryTag serialize(JsonElement json) {
return ListBinaryTag.empty();
}

BinaryTag listTag;
BinaryTagType<? extends BinaryTag> listType = serialize(jsonArray.get(0)).type();
List<BinaryTag> tagItems = new ArrayList<>(jsonArray.size());
BinaryTagType<? extends BinaryTag> listType = null;

for (JsonElement jsonEl : jsonArray) {
BinaryTag tag = serialize(jsonEl);
tagItems.add(tag);

if (listType == null) {
listType = tag.type();
} else if (listType != tag.type()) {
listType = BinaryTagTypes.COMPOUND;
}
}

switch (listType.id()) {
case 1://BinaryTagTypes.BYTE:
Expand All @@ -163,42 +175,33 @@ public static BinaryTag serialize(JsonElement json) {
bytes[i] = (Byte) jsonArray.get(i).getAsNumber();
}

listTag = ByteArrayBinaryTag.byteArrayBinaryTag(bytes);
break;
return ByteArrayBinaryTag.byteArrayBinaryTag(bytes);
case 3://BinaryTagTypes.INT:
int[] ints = new int[jsonArray.size()];
for (int i = 0; i < ints.length; i++) {
ints[i] = (Integer) jsonArray.get(i).getAsNumber();
}

listTag = IntArrayBinaryTag.intArrayBinaryTag(ints);
break;
return IntArrayBinaryTag.intArrayBinaryTag(ints);
case 4://BinaryTagTypes.LONG:
long[] longs = new long[jsonArray.size()];
for (int i = 0; i < longs.length; i++) {
longs[i] = (Long) jsonArray.get(i).getAsNumber();
}

listTag = LongArrayBinaryTag.longArrayBinaryTag(longs);
break;
default:
List<BinaryTag> tagItems = new ArrayList<>(jsonArray.size());

for (JsonElement jsonEl : jsonArray) {
BinaryTag subTag = serialize(jsonEl);

if (subTag.type() != listType) {
throw new IllegalArgumentException("Cannot convert mixed JsonArray to Tag");
return LongArrayBinaryTag.longArrayBinaryTag(longs);
case 10://BinaryTagTypes.COMPOUND:
tagItems.replaceAll(tag -> {
if (tag.type() == BinaryTagTypes.COMPOUND) {
return tag;
} else {
return CompoundBinaryTag.builder().put("", tag).build();
}

tagItems.add(subTag);
}

listTag = ListBinaryTag.listBinaryTag(listType, tagItems);
});
break;
}

return listTag;
return ListBinaryTag.listBinaryTag(listType, tagItems);
}

return EndBinaryTag.endBinaryTag();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2021-2023 Velocity 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 <https://www.gnu.org/licenses/>.
*/

package com.velocitypowered.proxy.component;

import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.junit.jupiter.api.Test;

/**
* ComponentHolder tests.
*/
public class ComponentHolderTest {

@Test
void testJsonToBinary() {
Component component = MiniMessage.miniMessage().deserialize(
"<#09add3>A <reset><reset>Velocity <#09add3>Server");
ComponentHolder holder = new ComponentHolder(ProtocolVersion.MINECRAFT_1_20_3, component);
holder.getJson();
holder.getBinaryTag();
}
}

0 comments on commit 38c8345

Please sign in to comment.