geforkt von Mirrors/Velocity
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.BinaryTag;
|
||||||
import net.kyori.adventure.nbt.BinaryTagIO;
|
import net.kyori.adventure.nbt.BinaryTagIO;
|
||||||
import net.kyori.adventure.nbt.BinaryTagType;
|
import net.kyori.adventure.nbt.BinaryTagType;
|
||||||
|
import net.kyori.adventure.nbt.BinaryTagTypes;
|
||||||
import net.kyori.adventure.nbt.ByteArrayBinaryTag;
|
import net.kyori.adventure.nbt.ByteArrayBinaryTag;
|
||||||
import net.kyori.adventure.nbt.ByteBinaryTag;
|
import net.kyori.adventure.nbt.ByteBinaryTag;
|
||||||
import net.kyori.adventure.nbt.CompoundBinaryTag;
|
import net.kyori.adventure.nbt.CompoundBinaryTag;
|
||||||
@ -153,8 +154,19 @@ public class ComponentHolder {
|
|||||||
return ListBinaryTag.empty();
|
return ListBinaryTag.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryTag listTag;
|
List<BinaryTag> tagItems = new ArrayList<>(jsonArray.size());
|
||||||
BinaryTagType<? extends BinaryTag> listType = serialize(jsonArray.get(0)).type();
|
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()) {
|
switch (listType.id()) {
|
||||||
case 1://BinaryTagTypes.BYTE:
|
case 1://BinaryTagTypes.BYTE:
|
||||||
@ -163,42 +175,33 @@ public class ComponentHolder {
|
|||||||
bytes[i] = (Byte) jsonArray.get(i).getAsNumber();
|
bytes[i] = (Byte) jsonArray.get(i).getAsNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
listTag = ByteArrayBinaryTag.byteArrayBinaryTag(bytes);
|
return ByteArrayBinaryTag.byteArrayBinaryTag(bytes);
|
||||||
break;
|
|
||||||
case 3://BinaryTagTypes.INT:
|
case 3://BinaryTagTypes.INT:
|
||||||
int[] ints = new int[jsonArray.size()];
|
int[] ints = new int[jsonArray.size()];
|
||||||
for (int i = 0; i < ints.length; i++) {
|
for (int i = 0; i < ints.length; i++) {
|
||||||
ints[i] = (Integer) jsonArray.get(i).getAsNumber();
|
ints[i] = (Integer) jsonArray.get(i).getAsNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
listTag = IntArrayBinaryTag.intArrayBinaryTag(ints);
|
return IntArrayBinaryTag.intArrayBinaryTag(ints);
|
||||||
break;
|
|
||||||
case 4://BinaryTagTypes.LONG:
|
case 4://BinaryTagTypes.LONG:
|
||||||
long[] longs = new long[jsonArray.size()];
|
long[] longs = new long[jsonArray.size()];
|
||||||
for (int i = 0; i < longs.length; i++) {
|
for (int i = 0; i < longs.length; i++) {
|
||||||
longs[i] = (Long) jsonArray.get(i).getAsNumber();
|
longs[i] = (Long) jsonArray.get(i).getAsNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
listTag = LongArrayBinaryTag.longArrayBinaryTag(longs);
|
return LongArrayBinaryTag.longArrayBinaryTag(longs);
|
||||||
break;
|
case 10://BinaryTagTypes.COMPOUND:
|
||||||
default:
|
tagItems.replaceAll(tag -> {
|
||||||
List<BinaryTag> tagItems = new ArrayList<>(jsonArray.size());
|
if (tag.type() == BinaryTagTypes.COMPOUND) {
|
||||||
|
return tag;
|
||||||
for (JsonElement jsonEl : jsonArray) {
|
} else {
|
||||||
BinaryTag subTag = serialize(jsonEl);
|
return CompoundBinaryTag.builder().put("", tag).build();
|
||||||
|
|
||||||
if (subTag.type() != listType) {
|
|
||||||
throw new IllegalArgumentException("Cannot convert mixed JsonArray to Tag");
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
tagItems.add(subTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
listTag = ListBinaryTag.listBinaryTag(listType, tagItems);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return listTag;
|
return ListBinaryTag.listBinaryTag(listType, tagItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
return EndBinaryTag.endBinaryTag();
|
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