From 94b28cdfaed5f61ac3b971cd16f4435e81e78127 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 14 Mar 2025 01:57:34 +0100 Subject: [PATCH 1/4] init commit --- .../java/ch/njol/skript/config/Config.java | 10 ++++-- src/main/java/ch/njol/skript/config/Node.java | 12 +++++-- .../java/ch/njol/skript/config/VoidNode.java | 36 +++++++------------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/main/java/ch/njol/skript/config/Config.java b/src/main/java/ch/njol/skript/config/Config.java index 82d370e1a39..b3473c88081 100644 --- a/src/main/java/ch/njol/skript/config/Config.java +++ b/src/main/java/ch/njol/skript/config/Config.java @@ -223,6 +223,7 @@ public boolean updateNodes(@NotNull Config newer) { if (nodesToUpdate.isEmpty()) return false; + Map offsets = new HashMap<>(); for (Node node : nodesToUpdate) { /* prevents nodes that are already in the config from being added again @@ -270,9 +271,14 @@ public boolean updateNodes(@NotNull Config newer) { Node existing = parent.getAt(index); if (existing != null) { // there's already something at the node we want to add the new node + int offset = offsets.getOrDefault(parent, 0); - Skript.debug("Adding node %s to %s at index %s", node, parent, index); - parent.add(index, node); + Skript.debug("Adding node %s to %s at index %s", node, parent, index + offset); + // for some reason, the node list isn't updated when adding a node at an index, + // so we have to manually shift the nodes after the index here + parent.add(Math.min(parent.size(), index + offset), node); + + offsets.put(parent, offset + 1); } else { // there's nothing at the index we want to add the new node diff --git a/src/main/java/ch/njol/skript/config/Node.java b/src/main/java/ch/njol/skript/config/Node.java index f7002456f20..bc4eacd0bf8 100644 --- a/src/main/java/ch/njol/skript/config/Node.java +++ b/src/main/java/ch/njol/skript/config/Node.java @@ -455,7 +455,7 @@ int getIndex() { /** * Returns the node names in the path to this node from the config root. - * If this is not a section node, returns the path to its parent node. + * For a node with no key, this will return the path from the root node to its parent. * *

* Getting the path of node {@code z} in the following example would @@ -470,7 +470,7 @@ int getIndex() { */ public @NotNull String[] getPathSteps() { List path = new ArrayList<>(); - Node node = this; + Node node = parent; while (node != null) { if (node.getKey() == null || node.getKey().isEmpty()) @@ -480,8 +480,14 @@ int getIndex() { node = node.getParent(); } - if (path.isEmpty()) + // allow getting the path steps of a node without key + if (getKey() != null) { + path.add(getKey()); + } + + if (path.isEmpty()) { return new String[0]; + } return path.toArray(new String[0]); } diff --git a/src/main/java/ch/njol/skript/config/VoidNode.java b/src/main/java/ch/njol/skript/config/VoidNode.java index 389f40b4d12..e642c5cf516 100644 --- a/src/main/java/ch/njol/skript/config/VoidNode.java +++ b/src/main/java/ch/njol/skript/config/VoidNode.java @@ -2,6 +2,9 @@ import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.Objects; + /** * An empty line or a comment. *

@@ -11,13 +14,8 @@ */ public class VoidNode extends Node { -// private final int initialLevel; -// private final String initialIndentation; - VoidNode(final String line, final String comment, final SectionNode parent, final int lineNum) { - super("" + line.trim(), comment, parent, lineNum); -// initialLevel = getLevel(); -// initialIndentation = "" + line.replaceFirst("\\S.*$", ""); + super(line.trim(), comment, parent, lineNum); } @SuppressWarnings("null") @@ -26,30 +24,14 @@ public String getKey() { return key; } + @Deprecated public void set(final String s) { key = s; } - // doesn't work reliably -// @Override -// protected String getIndentation() { -// int levelDiff = getLevel() - initialLevel; -// if (levelDiff >= 0) { -// return StringUtils.multiply(config.getIndentation(), levelDiff) + initialIndentation; -// } else { -// final String ci = config.getIndentation(); -// String ind = initialIndentation; -// while (levelDiff < 0 && ind.startsWith(ci)) { -// levelDiff++; -// ind = "" + ind.substring(ci.length()); -// } -// return ind; -// } -// } - @Override String save_i() { - return "" + key; + return key; } @Override @@ -57,4 +39,10 @@ String save_i() { return null; } + @Override + public int hashCode() { + // ensures that two void nodes can exist with the same parent as long as they are + // at a different position + return Objects.hash(Arrays.hashCode(getPathSteps()), comment, getIndex()); + } } From a8a98e19d703ea69de356eed35dacf0c03d3add4 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 14 Mar 2025 02:01:17 +0100 Subject: [PATCH 2/4] remove deprecated --- src/main/java/ch/njol/skript/config/VoidNode.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/config/VoidNode.java b/src/main/java/ch/njol/skript/config/VoidNode.java index e642c5cf516..0a4972c8e5d 100644 --- a/src/main/java/ch/njol/skript/config/VoidNode.java +++ b/src/main/java/ch/njol/skript/config/VoidNode.java @@ -24,7 +24,6 @@ public String getKey() { return key; } - @Deprecated public void set(final String s) { key = s; } From b813e6d3973838ea8c7fd4ede487b1be894ebd6a Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 14 Mar 2025 02:20:06 +0100 Subject: [PATCH 3/4] fix add failing if node is added from same parent --- src/main/java/ch/njol/skript/config/SectionNode.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/ch/njol/skript/config/SectionNode.java b/src/main/java/ch/njol/skript/config/SectionNode.java index 14309d7d6b2..5331700afdd 100644 --- a/src/main/java/ch/njol/skript/config/SectionNode.java +++ b/src/main/java/ch/njol/skript/config/SectionNode.java @@ -80,6 +80,9 @@ public void add(final Node n) { public void add(int index, @NotNull Node node) { Preconditions.checkArgument(index >= 0 && index <= size(), "index out of bounds: %s", index); + if (node.getParent() == this) { // this is not ideal. too bad! + index = Math.min(index, nodes.size() - 1); + } node.remove(); nodes.add(index, node); node.parent = this; From 7e0603b1723d5d21bb3e8eb36672bab406e70675 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 1 Apr 2025 23:09:46 +0200 Subject: [PATCH 4/4] review comments --- src/main/java/ch/njol/skript/config/SectionNode.java | 6 +++++- src/main/java/ch/njol/skript/config/VoidNode.java | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/config/SectionNode.java b/src/main/java/ch/njol/skript/config/SectionNode.java index 5331700afdd..51020b2114d 100644 --- a/src/main/java/ch/njol/skript/config/SectionNode.java +++ b/src/main/java/ch/njol/skript/config/SectionNode.java @@ -80,7 +80,11 @@ public void add(final Node n) { public void add(int index, @NotNull Node node) { Preconditions.checkArgument(index >= 0 && index <= size(), "index out of bounds: %s", index); - if (node.getParent() == this) { // this is not ideal. too bad! + // if a node is moved within the same section, and is at the end of that section, + // the index should be at most nodes.size() - 1 + // since the node is removed before being added, the size of nodes is now one smaller. + // if this check wasn't present, adding node at index could cause an IndexOutOfBoundsException + if (node.getParent() == this) { index = Math.min(index, nodes.size() - 1); } node.remove(); diff --git a/src/main/java/ch/njol/skript/config/VoidNode.java b/src/main/java/ch/njol/skript/config/VoidNode.java index 0a4972c8e5d..2f916a45dab 100644 --- a/src/main/java/ch/njol/skript/config/VoidNode.java +++ b/src/main/java/ch/njol/skript/config/VoidNode.java @@ -44,4 +44,5 @@ public int hashCode() { // at a different position return Objects.hash(Arrays.hashCode(getPathSteps()), comment, getIndex()); } + }