From d7a417368f5f2d2e30d518ffecb7a99d47f3594e Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Thu, 28 Apr 2022 12:04:33 -0400 Subject: [PATCH] add slice to the book --- .../common/casting/operators/lists/OpSlice.kt | 11 +++++++---- src/main/resources/assets/hexcasting/lang/en_us.json | 1 + .../thehexbook/en_us/entries/patterns/lists.json | 10 +++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpSlice.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpSlice.kt index 60d9a641..195e5b36 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpSlice.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpSlice.kt @@ -5,7 +5,7 @@ import at.petrak.hexcasting.api.spell.Operator.Companion.getChecked import at.petrak.hexcasting.api.spell.Operator.Companion.spellListOf import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.spell.Widget +import net.minecraft.util.Mth import kotlin.math.max import kotlin.math.min import kotlin.math.roundToInt @@ -14,9 +14,12 @@ object OpSlice : ConstManaOperator { override val argc = 3 override fun execute(args: List>, ctx: CastingContext): List> { val list = args.getChecked>>(0) - val index1 = max(0, args.getChecked(1).roundToInt()) - val index2 = min(args.getChecked(2).roundToInt(), list.size) + val index1 = Mth.clamp(args.getChecked(1).roundToInt(), 0, list.size) + val index2 = Mth.clamp(args.getChecked(2).roundToInt(), 0, list.size) - return spellListOf(list.subList(index1, index2)) + if (index1 == index2) + return spellListOf(listOf>()) + + return spellListOf(list.subList(min(index1, index2), max(index1, index2))) } } diff --git a/src/main/resources/assets/hexcasting/lang/en_us.json b/src/main/resources/assets/hexcasting/lang/en_us.json index 169d2764..214de1ba 100644 --- a/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/src/main/resources/assets/hexcasting/lang/en_us.json @@ -786,6 +786,7 @@ "hexcasting.entry.lists": "List Manipulation", "hexcasting.page.lists.index": "Remove the number at the top of the stack, then replace the list at the top with the nth element of that list (where n is the number you removed). Replaces the list with $(l:casting/influences)$(thing)Null/$ if the number is out of bounds.", + "hexcasting.page.lists.slice": "Remove the two numbers at the top of the stack, then take a sublist of the list at the top of the stack between those indices, lower bound inclusive, upper bound exclusive. For example, the 0, 2 sublist of [0, 1, 2, 3, 4] would be [0, 1].", "hexcasting.page.lists.append": "Remove the top of the stack, then add it to the end of the list at the top of the stack.", "hexcasting.page.lists.concat": "Remove the list at the top of the stack, then add all its elements to the end of the list at the top of the stack.", "hexcasting.page.lists.empty_list": "Push an empty list to the top of the stack.", diff --git a/src/main/resources/data/hexcasting/patchouli_books/thehexbook/en_us/entries/patterns/lists.json b/src/main/resources/data/hexcasting/patchouli_books/thehexbook/en_us/entries/patterns/lists.json index f1160582..7410770f 100644 --- a/src/main/resources/data/hexcasting/patchouli_books/thehexbook/en_us/entries/patterns/lists.json +++ b/src/main/resources/data/hexcasting/patchouli_books/thehexbook/en_us/entries/patterns/lists.json @@ -14,6 +14,14 @@ "output": "any", "text": "hexcasting.page.lists.index" }, + { + "type": "hexcasting:pattern", + "op_id": "hexcasting:slice", + "anchor": "hexcasting:slice", + "input": "list, num, num", + "output": "list", + "text": "hexcasting.page.lists.slice" + }, { "type": "hexcasting:pattern", "op_id": "hexcasting:append", @@ -76,7 +84,7 @@ "anchor": "hexcasting:list_remove", "input": "list, num", "output": "list", - "text": "hexcasting.page.lists.remove" + "text": "hexcasting.page.lists.list_remove" } ] }