@Override public void removeKey(final String key) { final String[] parts = (String[]) Iterables.toArray( Splitter.on('.').split((CharSequence) this.createRelativeKey(key)), (Class) String.class); final Map<String, Tag> parent = this.findLastParent(parts); parent.remove(parts[parts.length - 1]); }
private Map<String, Tag> findLastParent(final String[] parts) { Map<String, Tag> map = NBTStorage.this.root; for (int i = 0; i < parts.length - 1; ++i) { if (!map.containsKey(parts[i]) || !(map.get(parts[i]) instanceof CompoundTag)) { return null; } map = map.get(parts[i]).getValue(); } return map; }
private Tag findLastTag(final String key, final boolean relative) { final String[] parts = (String[]) Iterables.toArray( Splitter.on('.') .omitEmptyStrings() .split((CharSequence) (relative ? this.createRelativeKey(key) : key)), (Class) String.class); if (parts.length == 0) { return new CompoundTag(NBTStorage.this.name, NBTStorage.this.root); } final Map<String, Tag> map = this.findLastParent(parts); if (!map.containsKey(parts[parts.length - 1])) { return null; } return map.get(parts[parts.length - 1]); }
@Override public Map<String, Object> getValuesDeep() { final Tag tag = this.findLastTag(this.path, false); if (!(tag instanceof CompoundTag)) { return Collections.emptyMap(); } final Queue<Node> node = new ArrayDeque<Node>( (Collection<? extends Node>) ImmutableList.of((Object) new Node(tag))); final Map<String, Object> values = (Map<String, Object>) Maps.newHashMap(); while (!node.isEmpty()) { final Node root = node.poll(); for (final Map.Entry<String, Tag> entry : root.values.entrySet()) { final String key = this.createRelativeKey(root.parent, entry.getKey()); if (entry.getValue() instanceof CompoundTag) { node.add(new Node(key, entry.getValue())); } else { values.put(key, entry.getValue().getValue()); } } } return values; }
private void putTag(final String key, final Tag tag) { final String[] parts = (String[]) Iterables.toArray( Splitter.on('.').split((CharSequence) this.createRelativeKey(key)), (Class) String.class); Map<String, Tag> parent = NBTStorage.this.root; for (int i = 0; i < parts.length - 1; ++i) { if (!parent.containsKey(parts[i]) || !(parent.get(parts[i]) instanceof CompoundTag)) { parent.put(parts[i], new CompoundTag(parts[i])); } parent = parent.get(parts[i]).getValue(); } parent.put(tag.getName(), tag); }