From 8878dc40484410306375e22bc14f6b8aef85facb Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Thu, 28 Apr 2022 13:16:01 -0400 Subject: [PATCH] better mishap messages for bitwise ops --- .../common/casting/operators/math/MathOpUtils.kt | 11 +++++++++++ .../common/casting/operators/math/bit/OpAnd.kt | 10 +++++----- .../common/casting/operators/math/bit/OpOr.kt | 10 +++++----- .../common/casting/operators/math/bit/OpXor.kt | 15 +++++++-------- .../resources/assets/hexcasting/lang/en_us.json | 1 + 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/MathOpUtils.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/MathOpUtils.kt index 49245675..8665683e 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/MathOpUtils.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/MathOpUtils.kt @@ -17,4 +17,15 @@ object MathOpUtils { TranslatableComponent("hexcasting.mishap.invalid_value.numvec") ) } + + fun GetNumOrList(datum: SpellDatum<*>, reverseIdx: Int): Either>> = + when (datum.payload) { + is Double -> Either.left(datum.payload) + is List<*> -> Either.right(datum.payload.filterIsInstance>()) + else -> throw MishapInvalidIota( + datum, + reverseIdx, + TranslatableComponent("hexcasting.mishap.invalid_value.numlist") + ) + } } diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpAnd.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpAnd.kt index cfe13790..c9e9a5b6 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpAnd.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpAnd.kt @@ -1,26 +1,26 @@ package at.petrak.hexcasting.common.casting.operators.math.bit import at.petrak.hexcasting.api.spell.ConstManaOperator -import at.petrak.hexcasting.api.spell.DatumType 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.common.casting.operators.math.MathOpUtils import kotlin.math.roundToInt object OpAnd : ConstManaOperator { override val argc = 2 override fun execute(args: List>, ctx: CastingContext): List> { - val type = args[0].getType() + val firstParam = MathOpUtils.GetNumOrList(args[0], 0) - if (type == DatumType.LIST) { - val list1 = args.getChecked>>(0) + if (firstParam.right().isPresent) { + val list1 = firstParam.right().get() val list2 = args.getChecked>>(1) return spellListOf(list1.filter { it in list2 }) } - val num1 = args.getChecked(0).roundToInt() + val num1 = firstParam.left().get().roundToInt() val num2 = args.getChecked(1).roundToInt() return spellListOf((num1 and num2).toDouble()) } diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpOr.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpOr.kt index efcd44c5..31f88dd2 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpOr.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpOr.kt @@ -1,26 +1,26 @@ package at.petrak.hexcasting.common.casting.operators.math.bit import at.petrak.hexcasting.api.spell.ConstManaOperator -import at.petrak.hexcasting.api.spell.DatumType 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.common.casting.operators.math.MathOpUtils import kotlin.math.roundToInt object OpOr : ConstManaOperator { override val argc = 2 override fun execute(args: List>, ctx: CastingContext): List> { - val type = args[0].getType() + val firstParam = MathOpUtils.GetNumOrList(args[0], 0) - if (type == DatumType.LIST) { - val list1 = args.getChecked>>(0) + if (firstParam.right().isPresent) { + val list1 = firstParam.right().get() val list2 = args.getChecked>>(1) return spellListOf(list1 + list2.filter { it !in list1 }) } - val num1 = args.getChecked(0).roundToInt() + val num1 = firstParam.left().get().roundToInt() val num2 = args.getChecked(1).roundToInt() return spellListOf((num1 or num2).toDouble()) } diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpXor.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpXor.kt index e016cb7d..321b0287 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpXor.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/math/bit/OpXor.kt @@ -1,27 +1,26 @@ package at.petrak.hexcasting.common.casting.operators.math.bit import at.petrak.hexcasting.api.spell.ConstManaOperator -import at.petrak.hexcasting.api.spell.DatumType import at.petrak.hexcasting.api.spell.Operator.Companion.getChecked -import at.petrak.hexcasting.api.spell.SpellDatum -import at.petrak.hexcasting.api.spell.Widget -import at.petrak.hexcasting.api.spell.casting.CastingContext 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.common.casting.operators.math.MathOpUtils import kotlin.math.roundToInt object OpXor : ConstManaOperator { override val argc = 2 override fun execute(args: List>, ctx: CastingContext): List> { - val type = args[0].getType() + val firstParam = MathOpUtils.GetNumOrList(args[0], 0) - if (type == DatumType.LIST) { - val list1 = args.getChecked>>(0) + if (firstParam.right().isPresent) { + val list1 = firstParam.right().get() val list2 = args.getChecked>>(1) return spellListOf(list1.filter { it !in list2 } + list2.filter { it !in list1 }) } - val num1 = args.getChecked(0).roundToInt() + val num1 = firstParam.left().get().roundToInt() val num2 = args.getChecked(1).roundToInt() return spellListOf((num1 xor num2).toDouble()) } diff --git a/src/main/resources/assets/hexcasting/lang/en_us.json b/src/main/resources/assets/hexcasting/lang/en_us.json index 97e7c443..09c9e7fb 100644 --- a/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/src/main/resources/assets/hexcasting/lang/en_us.json @@ -346,6 +346,7 @@ "hexcasting.mishap.invalid_value.class.entity": "an entity", "hexcasting.mishap.invalid_value.class.unknown": "(unknown, uh-oh, this is a bug)", "hexcasting.mishap.invalid_value.numvec": "a number or vector", + "hexcasting.mishap.invalid_value.numlist": "a number or list", "hexcasting.mishap.invalid_value.list.pattern": "a list of patterns", "hexcasting.mishap.invalid_value.int": "an integer", "hexcasting.mishap.invalid_value.double.between": "a number between %d and %d",