-
Notifications
You must be signed in to change notification settings - Fork 1
Add NBTPath#of(Tag, Tag) #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0ce1c57
6feb8db
2436731
7617c8c
656c87b
3e247e8
85da61f
dce1b9c
58da77a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,21 +15,22 @@ | |
| */ | ||
| package org.glavo.nbt.internal.path; | ||
|
|
||
| import org.glavo.nbt.NBTElement; | ||
| import org.glavo.nbt.NBTParent; | ||
| import org.glavo.nbt.NBTPath; | ||
| import org.glavo.nbt.chunk.Chunk; | ||
| import org.glavo.nbt.chunk.ChunkRegion; | ||
| import org.glavo.nbt.internal.snbt.SNBTWriter; | ||
| import org.glavo.nbt.io.SNBTCodec; | ||
| import org.glavo.nbt.tag.CompoundTag; | ||
| import org.glavo.nbt.tag.ParentTag; | ||
| import org.glavo.nbt.tag.Tag; | ||
| import org.glavo.nbt.tag.TagType; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.Unmodifiable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
| import java.util.*; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public final class NBTPathImpl<T extends Tag> implements NBTPath<T> { | ||
|
|
@@ -59,6 +60,41 @@ public static <T extends Tag> Stream<T> select(NBTParent<?> parent, NBTPath<? ex | |
| return (Stream<T>) tags; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static @Nullable <T extends Tag> NBTPath<T> of(T tag, @Nullable Tag expectedRoot) throws IllegalArgumentException, IllegalStateException { | ||
| List<NBTPathNode> paths = new ArrayList<>(); | ||
|
|
||
| NBTPathNode indicator = NBTPathImpl.getIndicator(tag); | ||
| NBTParent<?> next = tag.getParent(); | ||
| while (next != null && indicator != null) { | ||
| paths.add(indicator); | ||
| indicator = NBTPathImpl.getIndicator(next); | ||
| if (next == expectedRoot) break; | ||
| else next = next.getParent(); | ||
| } | ||
|
|
||
| if (expectedRoot != null && expectedRoot != next) { | ||
| throw new IllegalStateException("Unexpected root tag " + expectedRoot + ", expected " + next + "."); | ||
| } else if (paths.isEmpty()) { | ||
| return null; | ||
| } | ||
| Collections.reverse(paths); | ||
| return new NBTPathImpl<>(paths.toArray(NBTPathNode[]::new), (TagType<T>) tag.getType()); | ||
| } | ||
|
|
||
| /// Get the indicator of the given tag, depends on its parent tag. | ||
| /// | ||
| /// @return the name node if parent is [CompoundTag], the index node if parent is other [ParentTag], or `null` if parent is null. | ||
| public static @Nullable NBTPathNode getIndicator(@Nullable NBTElement tag) { | ||
| if (!(tag instanceof Tag currentTag)) return null; | ||
| NBTParent<?> parentTag = tag.getParent(); | ||
| if (parentTag instanceof ParentTag<?>) { | ||
| return parentTag instanceof CompoundTag ? new NBTPathNode.NamedSubTag(currentTag.getName()) : new NBTPathNode.Index(currentTag.getIndex()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本审查建议由 GPT-5 生成 [P2] 先支持 ListTag 根再返回索引路径 当 |
||
| } else { // Chunk, ChunkRegion or null | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private final NBTPathNode @Unmodifiable [] nodes; | ||
| private final @Nullable TagType<T> tagType; | ||
|
|
||
|
|
@@ -107,33 +143,31 @@ public int hashCode() { | |
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (cachedString == null) { | ||
| StringBuilder builder = new StringBuilder(); | ||
|
|
||
| if (tagType != null) { | ||
| builder.append("<").append(tagType).append("> "); | ||
| public String toPathString(boolean omitDots) { | ||
| StringBuilder builder = new StringBuilder(); | ||
|
|
||
| SNBTWriter<StringBuilder> writer = new SNBTWriter<>(SNBTCodec.ofCompact(), builder); | ||
| for (int i = 0; i < nodes.length; i++) { | ||
| NBTPathNode node = nodes[i]; | ||
| try { | ||
| node.appendTo(writer); | ||
| } catch (IOException e) { | ||
| throw new AssertionError(e); | ||
| } | ||
|
|
||
| var writer = new SNBTWriter<>(SNBTCodec.ofCompact(), builder); | ||
|
|
||
| boolean first = true; | ||
| for (NBTPathNode node : nodes) { | ||
| if (first) { | ||
| first = false; | ||
| } else if (node.needDot()) { | ||
| writer.getAppendable().append('.'); | ||
| } | ||
|
|
||
| try { | ||
| node.appendTo(writer); | ||
| } catch (IOException e) { | ||
| throw new AssertionError(e); | ||
| } | ||
| if (i + 1 < nodes.length && (!omitDots || nodes[i + 1].needDot())) { | ||
| writer.getAppendable().append('.'); | ||
| } | ||
| } | ||
|
|
||
| builder.append(']'); | ||
| cachedString = builder.toString(); | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (cachedString == null) { | ||
| String pathString = toPathString(); | ||
| if (tagType != null) pathString = "<" + tagType + ">" + " " + pathString; | ||
| cachedString = pathString; | ||
| } | ||
|
|
||
| return cachedString; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.