Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-16 21:10:30 +01:00
Support conversion of mixed json array components
Dieser Commit ist enthalten in:
Ursprung
21ae82e7c0
Commit
a54d8c681f
@ -28,6 +28,7 @@ import io.netty.buffer.ByteBuf;
|
||||
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;
|
||||
@ -153,8 +154,19 @@ public class ComponentHolder {
|
||||
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:
|
||||
@ -163,42 +175,33 @@ public class ComponentHolder {
|
||||
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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren