Skip to content

Commit a1cb4d3

Browse files
Add Types#CUSTOM_CLICK_ACTION_TAG for VB (ViaVersion#4557)
* Add LengthPrefixedTagType for CUSTOM_CLICK_ACTION * Move type into own class, add instance into Types --------- Co-authored-by: FlorianMichael <[email protected]>
1 parent 1d5070a commit a1cb4d3

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

api/src/main/java/com/viaversion/viaversion/api/type/Types.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import com.viaversion.viaversion.api.type.types.misc.HolderSetType;
109109
import com.viaversion.viaversion.api.type.types.misc.HolderType;
110110
import com.viaversion.viaversion.api.type.types.misc.KeyType;
111+
import com.viaversion.viaversion.api.type.types.misc.LengthPrefixedTagType;
111112
import com.viaversion.viaversion.api.type.types.misc.NamedCompoundTagType;
112113
import com.viaversion.viaversion.api.type.types.misc.PlayerMessageSignatureType;
113114
import com.viaversion.viaversion.api.type.types.misc.ProfileKeyType;
@@ -199,6 +200,7 @@ public final class Types {
199200
public static final Type<Tag> TAG = new TagType();
200201
public static final Type<Tag[]> TAG_ARRAY = new ArrayType<>(TAG);
201202
public static final Type<Tag> OPTIONAL_TAG = new TagType.OptionalTagType();
203+
public static final Type<Tag> CUSTOM_CLICK_ACTION_TAG = new LengthPrefixedTagType(65536);
202204

203205
public static final Type<GlobalBlockPosition> GLOBAL_POSITION = new GlobalBlockPositionType();
204206
public static final Type<GlobalBlockPosition> OPTIONAL_GLOBAL_POSITION = new GlobalBlockPositionType.OptionalGlobalPositionType();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
3+
* Copyright (C) 2016-2025 ViaVersion and contributors
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
package com.viaversion.viaversion.api.type.types.misc;
24+
25+
import com.google.common.base.Preconditions;
26+
import com.viaversion.nbt.tag.Tag;
27+
import com.viaversion.viaversion.api.type.Type;
28+
import com.viaversion.viaversion.api.type.Types;
29+
import io.netty.buffer.ByteBuf;
30+
31+
public final class LengthPrefixedTagType extends Type<Tag> {
32+
33+
private final int maxLength;
34+
35+
public LengthPrefixedTagType(int maxLength) {
36+
super(Tag.class);
37+
this.maxLength = maxLength;
38+
}
39+
40+
@Override
41+
public Tag read(final ByteBuf buffer) {
42+
final int length = Types.VAR_INT.readPrimitive(buffer);
43+
if (length <= 0) {
44+
return null;
45+
}
46+
47+
Preconditions.checkArgument(length <= maxLength,
48+
"Cannot receive tag longer than %s bytes (got %s bytes)", maxLength, length);
49+
50+
return Types.TAG.read(buffer.readSlice(length));
51+
}
52+
53+
@Override
54+
public void write(final ByteBuf buffer, final Tag tag) {
55+
if (tag == null) {
56+
Types.VAR_INT.writePrimitive(buffer, 0);
57+
return;
58+
}
59+
60+
final ByteBuf tempBuf = buffer.alloc().buffer();
61+
try {
62+
Types.TAG.write(tempBuf, tag);
63+
64+
Preconditions.checkArgument(tempBuf.readableBytes() <= maxLength,
65+
"Cannot send tag longer than %s bytes (got %s bytes)", maxLength, tempBuf.readableBytes());
66+
67+
Types.VAR_INT.writePrimitive(buffer, tempBuf.readableBytes());
68+
buffer.writeBytes(tempBuf);
69+
} finally {
70+
tempBuf.release();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)