add slice to the book

This commit is contained in:
yrsegal@gmail.com 2022-04-28 12:04:33 -04:00
parent 46621cfa17
commit 1c82fe482e
3 changed files with 17 additions and 5 deletions

View file

@ -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.Operator.Companion.spellListOf
import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.max
import kotlin.math.min import kotlin.math.min
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -14,9 +14,12 @@ object OpSlice : ConstManaOperator {
override val argc = 3 override val argc = 3
override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> { override fun execute(args: List<SpellDatum<*>>, ctx: CastingContext): List<SpellDatum<*>> {
val list = args.getChecked<List<SpellDatum<*>>>(0) val list = args.getChecked<List<SpellDatum<*>>>(0)
val index1 = max(0, args.getChecked<Double>(1).roundToInt()) val index1 = Mth.clamp(args.getChecked<Double>(1).roundToInt(), 0, list.size)
val index2 = min(args.getChecked<Double>(2).roundToInt(), list.size) val index2 = Mth.clamp(args.getChecked<Double>(2).roundToInt(), 0, list.size)
return spellListOf(list.subList(index1, index2)) if (index1 == index2)
return spellListOf(listOf<SpellDatum<*>>())
return spellListOf(list.subList(min(index1, index2), max(index1, index2)))
} }
} }

View file

@ -787,6 +787,7 @@
"hexcasting.entry.lists": "List Manipulation", "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.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.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.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.", "hexcasting.page.lists.empty_list": "Push an empty list to the top of the stack.",

View file

@ -14,6 +14,14 @@
"output": "any", "output": "any",
"text": "hexcasting.page.lists.index" "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", "type": "hexcasting:pattern",
"op_id": "hexcasting:append", "op_id": "hexcasting:append",
@ -76,7 +84,7 @@
"anchor": "hexcasting:list_remove", "anchor": "hexcasting:list_remove",
"input": "list, num", "input": "list, num",
"output": "list", "output": "list",
"text": "hexcasting.page.lists.remove" "text": "hexcasting.page.lists.list_remove"
} }
] ]
} }