Skip to content

Commit baf1e5d

Browse files
committed
Add NBTSchema API for validating NBT elements
1 parent 5be6965 commit baf1e5d

11 files changed

Lines changed: 626 additions & 29 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
- New API `NBTCodec#writeTag(ByteBuffer, Tag)` for writing NBT tags to a byte buffer.
1010
- New API `NBTCodec#writeTagToByteArray(Tag)` for writing NBT tags to a byte array.
1111
- New API `ArrayTag#getElementType()` for getting the element type of array tags.
12+
- New API `NBTSchema` for validating NBT elements.
1213

14+
### Breaking Changes
15+
16+
- The `NBTValidator` interface has been rewritten and now provides two methods for validation:
17+
- [#test(NBTElement)]: Returns `true` if the element meets the rules of this validator, otherwise returns `false`.
18+
- [#validate(NBTElement)]: Does nothing if the element meets the rules of this validator, otherwise throws an
19+
exception.
20+
1321
## 0.1.0 (2026-03-16)
1422

1523
- Initial release

‎src/main/java/org/glavo/nbt/internal/path/NBTPathNode.java‎

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.glavo.nbt.internal.path;
1919

20+
import org.glavo.nbt.internal.schema.MatchSchema;
2021
import org.glavo.nbt.internal.snbt.SNBTWriter;
2122
import org.glavo.nbt.io.SNBTCodec;
2223
import org.glavo.nbt.tag.*;
@@ -31,30 +32,6 @@ public sealed interface NBTPathNode {
3132
@Unmodifiable
3233
CompoundTag EMPTY_COMPOUND_TAG = new CompoundTag();
3334

34-
private static boolean match(Tag tag, CompoundTag expected) {
35-
if (tag instanceof CompoundTag compoundTag) {
36-
for (Tag expectedSubTag : expected) {
37-
Tag actualSubTag = compoundTag.get(expectedSubTag.getName());
38-
if (actualSubTag == null || expectedSubTag.getClass() != actualSubTag.getClass()) {
39-
return false;
40-
}
41-
42-
if (expectedSubTag instanceof CompoundTag) {
43-
if (!match(actualSubTag, (CompoundTag) expectedSubTag)) {
44-
return false;
45-
}
46-
} else {
47-
if (!expectedSubTag.contentEquals(actualSubTag)) {
48-
return false;
49-
}
50-
}
51-
52-
}
53-
return true;
54-
} else {
55-
return false;
56-
}
57-
}
5835

5936
private static boolean isListOrArray(ParentTag<?> tag) {
6037
return tag instanceof ListTag || tag instanceof ArrayTag;
@@ -85,7 +62,7 @@ record Root(@Unmodifiable CompoundTag tags) implements NBTPathNode {
8562

8663
@Override
8764
public Stream<? extends Tag> operate(Stream<? extends Tag> tags) {
88-
return tags.filter(tag -> match(tag, this.tags));
65+
return tags.filter(tag -> MatchSchema.match(tag, this.tags));
8966
}
9067

9168
@Override
@@ -136,7 +113,7 @@ public Stream<? extends Tag> operate(Stream<? extends Tag> tags) {
136113
return tags.flatMap(tag -> {
137114
if (tag instanceof CompoundTag compoundTag) {
138115
Tag subTag = compoundTag.get(name);
139-
if (subTag != null && NBTPathNode.match(subTag, this.tags)) {
116+
if (subTag != null && MatchSchema.match(subTag, this.tags)) {
140117
return Stream.of(subTag);
141118
}
142119
}
@@ -236,7 +213,7 @@ record CompoundElements(@Unmodifiable CompoundTag tags) implements NBTPathNode {
236213
public Stream<? extends Tag> operate(Stream<? extends Tag> tags) {
237214
return tags.flatMap(tag -> {
238215
if (tag instanceof ListTag<?> listTag) {
239-
return listTag.stream().filter(subTag -> match(subTag, this.tags));
216+
return listTag.stream().filter(subTag -> MatchSchema.match(subTag, this.tags));
240217
}
241218

242219
return Stream.empty();
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.glavo.nbt.internal.schema;
19+
20+
import org.glavo.nbt.tag.CompoundTag;
21+
import org.glavo.nbt.tag.Tag;
22+
import org.glavo.nbt.tag.TagType;
23+
import org.glavo.nbt.validation.NBTSchema;
24+
import org.glavo.nbt.validation.NBTValidationException;
25+
import org.jetbrains.annotations.Unmodifiable;
26+
27+
import java.util.LinkedHashMap;
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
public final class CompoundTagSchema implements TagSchema<CompoundTag> {
32+
private final @Unmodifiable List<SchemaEntry> entries;
33+
34+
public CompoundTagSchema(@Unmodifiable List<SchemaEntry> entries) {
35+
this.entries = entries;
36+
}
37+
38+
@Override
39+
public boolean testTag(Tag tag) {
40+
if (!(tag instanceof CompoundTag compoundTag)) {
41+
return false;
42+
}
43+
44+
for (SchemaEntry entry : entries) {
45+
Tag subTag = compoundTag.get(entry.name());
46+
if (subTag == null) {
47+
if (entry.required) {
48+
return false;
49+
}
50+
} else {
51+
if (!entry.schema.testTag(subTag)) {
52+
return false;
53+
}
54+
}
55+
}
56+
return true;
57+
}
58+
59+
@Override
60+
public void validateTag(Tag tag) throws NBTValidationException {
61+
if (!(tag instanceof CompoundTag compoundTag)) {
62+
throw new NBTValidationException("Expected TAG_Compound, but got " + tag.getType());
63+
}
64+
65+
for (SchemaEntry entry : entries) {
66+
Tag subTag = compoundTag.get(entry.name());
67+
if (subTag != null) {
68+
try {
69+
entry.schema.validateTag(subTag);
70+
} catch (NBTValidationException e) {
71+
throw new NBTValidationException("Invalid subtag " + entry.name(), e);
72+
}
73+
} else {
74+
if (entry.required) {
75+
throw new NBTValidationException("Missing required subtag: " + entry.name());
76+
}
77+
}
78+
}
79+
}
80+
81+
public record SchemaEntry(String name, TagSchema<?> schema, boolean required) {
82+
}
83+
84+
public static final class Builder implements NBTSchema.Builder.OfCompoundTag {
85+
private final LinkedHashMap<String, SchemaEntry> entries = new LinkedHashMap<>();
86+
87+
88+
private void add(String name, NBTSchema<?> schema, boolean required) {
89+
entries.put(name, new SchemaEntry(name, (TagSchema<?>) schema, required));
90+
}
91+
92+
@Override
93+
public OfCompoundTag addRequired(String name, NBTSchema<?> schema) {
94+
Objects.requireNonNull(name, "name");
95+
Objects.requireNonNull(schema, "schema");
96+
97+
add(name, schema, true);
98+
return this;
99+
}
100+
101+
@Override
102+
public OfCompoundTag addOptional(String name, NBTSchema<?> schema) {
103+
Objects.requireNonNull(name, "name");
104+
Objects.requireNonNull(schema, "schema");
105+
106+
add(name, schema, false);
107+
return this;
108+
}
109+
110+
@Override
111+
public OfCompoundTag addRequired(String name, TagType<?> type) {
112+
Objects.requireNonNull(name, "name");
113+
Objects.requireNonNull(type, "type");
114+
115+
add(name, new TypeIsSchema<>(type), true);
116+
return this;
117+
}
118+
119+
@Override
120+
public OfCompoundTag addOptional(String name, TagType<?> type) {
121+
Objects.requireNonNull(name, "name");
122+
Objects.requireNonNull(type, "type");
123+
124+
add(name, new TypeIsSchema<>(type), false);
125+
return this;
126+
}
127+
128+
@Override
129+
public OfCompoundTag addRequired(String name, Tag tag) {
130+
Objects.requireNonNull(name, "name");
131+
Objects.requireNonNull(tag, "tag");
132+
133+
add(name, new MatchSchema<>(tag.clone()), true);
134+
return this;
135+
}
136+
137+
@Override
138+
public OfCompoundTag addOptional(String name, Tag tag) {
139+
Objects.requireNonNull(name, "name");
140+
Objects.requireNonNull(tag, "tag");
141+
142+
add(name, new MatchSchema<>(tag.clone()), false);
143+
return this;
144+
}
145+
146+
@Override
147+
public NBTSchema<CompoundTag> build() {
148+
return new CompoundTagSchema(List.copyOf(entries.values()));
149+
}
150+
}
151+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.glavo.nbt.internal.schema;
19+
20+
import org.glavo.nbt.tag.Tag;
21+
import org.glavo.nbt.validation.NBTSchema;
22+
import org.glavo.nbt.validation.NBTValidationException;
23+
import org.jetbrains.annotations.Unmodifiable;
24+
25+
import java.util.List;
26+
27+
public record IntersectionSchema<T extends Tag>(
28+
@Unmodifiable List<NBTSchema<? extends T>> schemas) implements TagSchema<T> {
29+
30+
public IntersectionSchema {
31+
assert schemas.size() > 1;
32+
}
33+
34+
@Override
35+
public boolean testTag(Tag tag) {
36+
for (NBTSchema<?> schema : schemas) {
37+
if (!((TagSchema<?>) schema).testTag(tag)) {
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
44+
@Override
45+
public void validateTag(Tag tag) throws NBTValidationException {
46+
for (NBTSchema<?> schema : schemas) {
47+
try {
48+
((TagSchema<?>) schema).validateTag(tag);
49+
} catch (NBTValidationException e) {
50+
throw new NBTValidationException("Failed to validate tag against schema: " + schema, e);
51+
}
52+
}
53+
}
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.glavo.nbt.internal.schema;
19+
20+
import org.glavo.nbt.tag.CompoundTag;
21+
import org.glavo.nbt.tag.Tag;
22+
import org.glavo.nbt.validation.NBTValidationException;
23+
import org.jetbrains.annotations.Unmodifiable;
24+
25+
public record MatchSchema<T extends Tag>(@Unmodifiable T expected) implements TagSchema<T> {
26+
public static boolean match(Tag tag, CompoundTag expected) {
27+
if (tag instanceof CompoundTag compoundTag) {
28+
for (Tag expectedSubTag : expected) {
29+
Tag actualSubTag = compoundTag.get(expectedSubTag.getName());
30+
if (actualSubTag == null || expectedSubTag.getClass() != actualSubTag.getClass()) {
31+
return false;
32+
}
33+
34+
if (expectedSubTag instanceof CompoundTag) {
35+
if (!match(actualSubTag, (CompoundTag) expectedSubTag)) {
36+
return false;
37+
}
38+
} else {
39+
if (!expectedSubTag.contentEquals(actualSubTag)) {
40+
return false;
41+
}
42+
}
43+
44+
}
45+
return true;
46+
} else {
47+
return false;
48+
}
49+
}
50+
51+
@Override
52+
public boolean testTag(Tag tag) {
53+
if (expected instanceof CompoundTag compoundTag) {
54+
return match(tag, compoundTag);
55+
} else {
56+
return expected.equals(tag);
57+
}
58+
}
59+
60+
@Override
61+
public void validateTag(Tag tag) throws NBTValidationException {
62+
if (!testTag(tag)) {
63+
throw new NBTValidationException("Tag does not match the expected schema: " + expected);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)