Skip to content
PeerHeer edited this page Dec 31, 2019 · 16 revisions

Using a static index

Usage

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

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>

Return values

execute store result returns the amount of lists that were modified using this operation.

Example

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"].

Using a dynamic index

Description

The listutils:insert function inserts an element into a list at a dynamic index.

Usage

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

Errors that display when executing the listutils:insert function as a player in debug mode.

Error Message
TBD TBD

Return values

Success

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.

Result

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.

Example

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:insert

This will result in the list ["foo", "Hello World!", "baz", "foo", "bar"].

External links

Clone this wiki locally