-
Notifications
You must be signed in to change notification settings - Fork 0
Insert
Inserting into a list can be done using the data modify command, which is built-in. For dynamic uses, you can use data modify storage|block|entity <identifier> <path> insert <index> from storage|block|entity <identifier> <path>. Note that the index is not dynamic in this case!
Errors that display when executing the command from chat.
| Error | Message |
|---|---|
| Invalid type | Nothing changed, the specified properties already have these values |
| Not a list | Expected list, got: <value> |
| Index out of bounds | Invalid list index: <index> |
execute store result returns the amount of lists that were modified using this operation.
Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"] in the listutils:examples storage. We want to insert the string "baz" into it at index 2:
data modify storage listutils:examples ExampleList insert 2 value "baz"This will result in the list ExampleList: ["foo", "Hello World!", "baz", "foo", "bar"].
The listutils:insert function inserts an element into a list at a dynamic index.
Input is given using the listutils:in storage:
| Field | Meaning |
|---|---|
List |
The list to insert an element into. |
Data |
The element to insert. |
Additionally, the index should be put in the $listutils.index listutils.in score. Negative indices will have the same behavior as using them in a static context.
After defining the input, run the function listutils:insert.
Errors that display when executing the listutils:insert function as a player in debug mode.
| Error | Message |
|---|---|
| TBD | TBD |
The success of the operation is stored in the $listutils.success listutils.out score. This score is 1 on success and 0 otherwise. Success will be 0 when the type of the element to insert is not the same as the type of all other elements in the list, or if the index provided is out of bounds.
The result of the operation is stored inside the List field in the listutils:out storage and contains the list with the inserted element if the operation was successful.
Take the example list ExampleList: ["foo", "Hello World!", "foo", "bar"] in the listutils:examples storage. We want to insert the string "baz" into it at index 2:
# Add List to the input.
data modify storage listutils:in List set from storage listutils:examples ExampleList
# Add Data to the input.
data modify storage listutils:in Data set value "baz"
# Add the index to the input.
scoreboard players set $listutils.index listutils.in 2
# Call the function.
function listutils:insertThis will result in the list ["foo", "Hello World!", "baz", "foo", "bar"].