diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 44fb59a3..06bec169 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -1557,7 +1557,9 @@ public class Expression implements Cloneable *
Efficient use of these functions can greatly simplify your programs and speed them up, as these functions - * will internalize most of the operations that need to be applied on multiple values at the same time
+ * will internalize most of the operations that need to be applied on multiple values at the same time. Most + * of them take alist argument which can be any iterable structure in scarpet, including
+ * generators, like rect, or range, and maps, where the iterator returns all the map keys
* for(list,expr(_,_i),exit(_,_i)?)has(container, address)Similar to get, but returns boolean value indicating if the given index / key / path is in the
* container. Can be used to determine if get(...)==null means the element doesn't exist, or the stored value
- * for this address is null.
null, and is cheaper to run than get
*
* delete(container, address)Removes specific entry from the container. For the lists - removes the element and shrinks it. For maps, it * removes the key from the map, and for nbt - removes content from a given path. For lists and maps returns previous - * entry at the address, for nbt's - number of removed objects.
+ * entry at the address, for nbt's - number of removed objects, with 0 indicating that the original value was unaffected. * + *put(container, address, value), put(container, address, value, condition)Modifies the container by replacing the value under the address with the supplied value.
+ * For lists, a valid index is required, but can be negative as well. If null is supplied as the address, it
+ * appends that value to the end of the table.
*
- *
put(list, index, values ...), put(list, null, values ...)Modifies the list by replacing values starting from index with values.
- * use negative numbers to reach elements from the end of the list. put
- * call will always be able to find the index. In case there is few items, it will loop over. In case end
- * of the list is reached before values run out, list is extended to accomodate for more values. in case you
- * want to append at the end of the list, use null as index. returns number of elements inserted.
* a = l(1, 2, 3); put(a, 1, 4); a => [1, 4, 3]
* a = l(1, 2, 3); put(a, null, 4, 5, 6); a => [1, 2, 3, 4, 5, 6]
diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java
index 5c94c308..dd3e75a4 100644
--- a/src/main/java/carpet/script/value/ListValue.java
+++ b/src/main/java/carpet/script/value/ListValue.java
@@ -304,8 +304,23 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
return (double)items.size();
}
+ @Override
+ public Value put(Value where, Value value, Value conditionValue)
+ {
+ String condition = conditionValue.getString();
+ if (condition.equalsIgnoreCase("insert"))
+ return put(where, value, false);
+ if (condition.equalsIgnoreCase("replace"))
+ return put(where, value, true);
+ throw new InternalExpressionException("list put modifier could be either insert, or replace");
+ }
+
@Override
public Value put(Value ind, Value value)
+ {
+ return put(ind, value, true);
+ }
+ private Value put(Value ind, Value value, boolean replace)
{
if (ind == Value.NULL)
{
@@ -323,7 +338,15 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
index = index % numitems;
}
while (index >= items.size()) items.add(Value.NULL);
- return items.set(index, value);
+ if (replace)
+ {
+ return items.set(index, value);
+ }
+ else
+ {
+ items.add(index, value);
+ return value;
+ }
}
}