From 33f364039c6f0f9798a4890d78ea3e02b3a110d4 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Sun, 29 May 2022 15:59:48 -0400 Subject: [PATCH] some kt component utils, also fix a display bug on fabric --- .../hexcasting/api/item/DataHolderItem.java | 10 +-- .../hexcasting/api/spell/OperatorUtils.kt | 6 +- .../petrak/hexcasting/api/spell/SpellDatum.kt | 59 +++++-------- .../api/spell/casting/CastingHarness.kt | 9 +- .../api/spell/casting/OperatorSideEffect.kt | 6 +- .../hexcasting/api/spell/mishaps/Mishap.kt | 45 ++++++++-- .../spell/mishaps/MishapAlreadyBrainswept.kt | 10 +-- .../api/spell/mishaps/MishapBadBlock.kt | 15 ++-- .../api/spell/mishaps/MishapBadBrainsweep.kt | 7 +- .../api/spell/mishaps/MishapBadItem.kt | 14 ++- .../api/spell/mishaps/MishapBadOffhandItem.kt | 22 ++--- .../spell/mishaps/MishapDisallowedSpell.kt | 3 +- .../api/spell/mishaps/MishapDivideByZero.kt | 19 ++-- .../spell/mishaps/MishapEntityTooFarAway.kt | 23 +---- .../api/spell/mishaps/MishapError.kt | 4 +- .../api/spell/mishaps/MishapEvalTooDeep.kt | 5 +- .../api/spell/mishaps/MishapImmuneEntity.kt | 23 +---- .../api/spell/mishaps/MishapInvalidIota.kt | 15 ++-- .../api/spell/mishaps/MishapInvalidPattern.kt | 3 +- .../mishaps/MishapInvalidSpellDatumType.kt | 6 +- .../mishaps/MishapLocationInWrongDimension.kt | 11 ++- .../spell/mishaps/MishapLocationTooFarAway.kt | 23 +---- .../spell/mishaps/MishapNoAkashicRecord.kt | 5 +- .../api/spell/mishaps/MishapNoSpellCircle.kt | 3 +- .../api/spell/mishaps/MishapNotEnoughArgs.kt | 2 +- .../api/spell/mishaps/MishapOthersName.kt | 5 +- .../spell/mishaps/MishapTooManyCloseParens.kt | 2 +- .../api/spell/mishaps/MishapUnescapedValue.kt | 20 +++-- .../petrak/hexcasting/api/utils/HexUtils.kt | 87 +++++++++++++++++++ .../hexcasting/client/gui/GuiSpellcasting.kt | 4 +- .../casting/operators/lists/OpLastNToList.kt | 4 +- .../casting/operators/math/trig/OpArcCos.kt | 4 +- .../casting/operators/math/trig/OpArcSin.kt | 4 +- .../OpAlwinfyHasAscendedToABeingOfPureMath.kt | 4 +- .../casting/operators/stack/OpDuplicateN.kt | 6 +- .../casting/operators/stack/OpFisherman.kt | 6 +- .../assets/hexcasting/lang/en_us.json | 2 + 37 files changed, 259 insertions(+), 237 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/item/DataHolderItem.java b/Common/src/main/java/at/petrak/hexcasting/api/item/DataHolderItem.java index ea010ce3..6c11a534 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/item/DataHolderItem.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/item/DataHolderItem.java @@ -6,6 +6,7 @@ import net.minecraft.ChatFormatting; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.item.ItemStack; @@ -21,12 +22,7 @@ public interface DataHolderItem { @Nullable default SpellDatum readDatum(ItemStack stack, ServerLevel world) { - if (!(stack.getItem() instanceof DataHolderItem dh)) { - // this should be checked via mishap beforehand - throw new IllegalArgumentException("stack's item must be an ItemDataholder but was " + stack.getItem()); - } - - var tag = dh.readDatumTag(stack); + var tag = readDatumTag(stack); if (tag != null) { return SpellDatum.fromNBT(tag, world); } else { @@ -51,7 +47,7 @@ public interface DataHolderItem { pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem", component)); if (pIsAdvanced.isAdvanced()) { - pTooltipComponents.add(NbtUtils.toPrettyComponent(datumTag)); + pTooltipComponents.add(new TextComponent("").append(NbtUtils.toPrettyComponent(datumTag))); } } else if (NBTHelper.hasString(pStack, DataHolderItem.TAG_OVERRIDE_VISUALLY)) { pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem", diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/OperatorUtils.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/OperatorUtils.kt index a3545d53..0dc23016 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/OperatorUtils.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/OperatorUtils.kt @@ -5,10 +5,10 @@ package at.petrak.hexcasting.api.spell import at.petrak.hexcasting.api.spell.math.HexPattern import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs +import at.petrak.hexcasting.api.utils.asTranslatedComponent import com.mojang.datafixers.util.Either import com.mojang.math.Vector3f import net.minecraft.core.BlockPos -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.entity.Entity import net.minecraft.world.phys.Vec3 import kotlin.math.abs @@ -20,7 +20,7 @@ fun numOrVec(datum: SpellDatum<*>, reverseIdx: Int): Either = else -> throw MishapInvalidIota( datum, reverseIdx, - TranslatableComponent("hexcasting.mishap.invalid_value.numvec") + "hexcasting.mishap.invalid_value.numvec".asTranslatedComponent ) } @@ -31,7 +31,7 @@ fun numOrList(datum: SpellDatum<*>, reverseIdx: Int): Either else -> throw MishapInvalidIota( datum, reverseIdx, - TranslatableComponent("hexcasting.mishap.invalid_value.numlist") + "hexcasting.mishap.invalid_value.numlist".asTranslatedComponent ) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt index e611291a..1ea946e8 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/SpellDatum.kt @@ -4,13 +4,10 @@ import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.math.HexPattern import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidSpellDatumType import at.petrak.hexcasting.api.utils.* -import net.minecraft.ChatFormatting import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.NbtUtils import net.minecraft.nbt.Tag import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TextComponent -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.server.level.ServerLevel import net.minecraft.world.entity.Entity import net.minecraft.world.phys.Vec3 @@ -155,69 +152,59 @@ class SpellDatum private constructor(val payload: T) { if (keys.size != 1) throw IllegalArgumentException("Expected exactly one kv pair: $nbt") - return when (val key = keys.iterator().next()) { - TAG_DOUBLE -> TextComponent( - String.format( - "%.4f", - nbt.getDouble(TAG_DOUBLE) - ) - ).withStyle(ChatFormatting.GREEN) + val out = "".asTextComponent + when (val key = keys.iterator().next()) { + TAG_DOUBLE -> out += String.format( + "%.4f", + nbt.getDouble(TAG_DOUBLE) + ).green TAG_VEC3 -> { val vec = vecFromNBT(nbt.getLongArray(key)) // the focus color is really more red, but we don't want to show an error-y color - TextComponent( - String.format( + out += String.format( "(%.2f, %.2f, %.2f)", vec.x, vec.y, vec.z - ) - ).withStyle(ChatFormatting.LIGHT_PURPLE) + ).lightPurple } TAG_LIST -> { - val out = TextComponent("[").withStyle(ChatFormatting.WHITE) + out += "[".white val arr = nbt.getList(key, Tag.TAG_COMPOUND) for ((i, subtag) in arr.withIndex()) { - // this is safe because otherwise we wouldn't have been able to get the list before - out.append(displayFromNBT(subtag as CompoundTag)) + out += displayFromNBT(subtag.asCompound) if (i != arr.lastIndex) { - out.append(", ") + out += ", ".white } } - out.append("]") + out += "]".white } TAG_WIDGET -> { val widget = Widget.valueOf(nbt.getString(key)) - if (widget == Widget.GARBAGE) TextComponent("arimfexendrapuse").withStyle( - ChatFormatting.DARK_GRAY, - ChatFormatting.OBFUSCATED - ) - // use dark purple instead of pink, so that vec3 can be pink instead of error red - else TextComponent(widget.toString()).withStyle(ChatFormatting.DARK_PURPLE) + out += if (widget == Widget.GARBAGE) + "arimfexendrapuse".darkGray.obfuscated + else + widget.toString().darkPurple } TAG_PATTERN -> { val pat = HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN)) var angleDesc = pat.anglesSignature() if (angleDesc.isNotBlank()) angleDesc = " $angleDesc"; - val out = TextComponent("HexPattern(").withStyle(ChatFormatting.GOLD) - out.append(TextComponent("${pat.startDir}$angleDesc").withStyle(ChatFormatting.WHITE)) - out.append(")") + out += "HexPattern(".gold + out += "${pat.startDir}$angleDesc".white + out += ")".gold } TAG_ENTITY -> { + val subtag = nbt.getCompound(TAG_ENTITY) + val json = subtag.getString(TAG_ENTITY_NAME_CHEATY) // handle pre-0.5.0 foci not having the tag - try { - val subtag = nbt.getCompound(TAG_ENTITY) - val json = subtag.getString(TAG_ENTITY_NAME_CHEATY) - val out = Component.Serializer.fromJson(json)!! - out.withStyle(ChatFormatting.AQUA) - } catch (exn: NullPointerException) { - TranslatableComponent("hexcasting.spelldata.entity.whoknows").withStyle(ChatFormatting.WHITE) - } + out += Component.Serializer.fromJson(json)?.aqua ?: "hexcasting.spelldata.entity.whoknows".asTranslatedComponent.white } else -> throw IllegalArgumentException("Unknown key $key: $nbt") } + return out } // Set of legal types to go in a spell diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt index 3e526403..07f684de 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/CastingHarness.kt @@ -16,7 +16,6 @@ import at.petrak.hexcasting.api.utils.* import at.petrak.hexcasting.xplat.IXplatAbstractions import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.Tag -import net.minecraft.network.chat.Component import net.minecraft.resources.ResourceLocation import net.minecraft.server.level.ServerLevel import net.minecraft.world.phys.Vec3 @@ -236,13 +235,7 @@ class CastingHarness private constructor( } } - fun generateDescs(): List { - val descs = ArrayList(this.stack.size) - for (datum in this.stack) { - descs.add(datum.display()) - } - return descs - } + fun generateDescs() = stack.map(SpellDatum<*>::display) /** * Return the functional update represented by the current state (for use with `copy`) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/OperatorSideEffect.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/OperatorSideEffect.kt index 68c0e221..5de171e6 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/OperatorSideEffect.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/casting/OperatorSideEffect.kt @@ -7,10 +7,10 @@ import at.petrak.hexcasting.api.mod.HexStatistics import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.RenderedSpell import at.petrak.hexcasting.api.spell.mishaps.Mishap +import at.petrak.hexcasting.api.utils.asTranslatedComponent import at.petrak.hexcasting.common.lib.HexItems import at.petrak.hexcasting.common.lib.HexSounds import net.minecraft.Util -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.sounds.SoundSource import net.minecraft.world.item.DyeColor import net.minecraft.world.item.ItemStack @@ -25,7 +25,7 @@ sealed class OperatorSideEffect { data class RequiredEnlightenment(val awardStat: Boolean) : OperatorSideEffect() { override fun performEffect(harness: CastingHarness): Boolean { harness.ctx.caster.sendMessage( - TranslatableComponent("hexcasting.message.cant_great_spell"), + "hexcasting.message.cant_great_spell".asTranslatedComponent, Util.NIL_UUID ) @@ -53,7 +53,7 @@ sealed class OperatorSideEffect { val leftoverMana = harness.withdrawMana(this.amount, overcastOk) if (leftoverMana > 0 && !overcastOk) { harness.ctx.caster.sendMessage( - TranslatableComponent("hexcasting.message.cant_overcast"), + "hexcasting.message.cant_overcast".asTranslatedComponent, Util.NIL_UUID ) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt index c06bfb9c..b29f3b6a 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/Mishap.kt @@ -1,19 +1,22 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer +import at.petrak.hexcasting.api.mod.HexItemTags import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType import at.petrak.hexcasting.api.spell.math.HexPattern +import at.petrak.hexcasting.api.utils.asTranslatedComponent +import at.petrak.hexcasting.api.utils.lightPurple +import at.petrak.hexcasting.api.utils.obfuscated import at.petrak.hexcasting.common.lib.HexItems import at.petrak.hexcasting.mixin.accessor.AccessorLivingEntity -import net.minecraft.ChatFormatting import net.minecraft.Util +import net.minecraft.core.BlockPos import net.minecraft.network.chat.Component -import net.minecraft.network.chat.Style -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.resources.ResourceLocation +import net.minecraft.world.InteractionHand import net.minecraft.world.damagesource.DamageSource import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.item.ItemEntity @@ -40,6 +43,8 @@ sealed class Mishap : Throwable() { abstract fun errorMessage(ctx: CastingContext, errorCtx: Context): Component + // Useful helper functions + protected fun dyeColor(color: DyeColor): FrozenColorizer = FrozenColorizer( ItemStack(HexItems.DYE_COLORIZERS[color]!!), @@ -47,11 +52,35 @@ sealed class Mishap : Throwable() { ) protected fun error(stub: String, vararg args: Any): Component = - TranslatableComponent("hexcasting.mishap.$stub", *args) + "hexcasting.mishap.$stub".asTranslatedComponent(*args) protected fun actionName(action: ResourceLocation?): Component = - TranslatableComponent("hexcasting.spell.${action ?: "unknown"}") - .setStyle(Style.EMPTY.withColor(ChatFormatting.LIGHT_PURPLE).withUnderlined(true)) + "hexcasting.spell.${action ?: "unknown"}".asTranslatedComponent.lightPurple.obfuscated + + protected fun yeetHeldItemsTowards(ctx: CastingContext, targetPos: Vec3) { + // Knock the player's items out of their hands + val items = mutableListOf() + for (hand in InteractionHand.values()) { + if (hand != ctx.castingHand || ctx.caster.getItemInHand(hand).`is`(HexItemTags.WANDS)) { + items.add(ctx.caster.getItemInHand(hand).copy()) + ctx.caster.setItemInHand(hand, ItemStack.EMPTY) + } + } + + val delta = targetPos.subtract(ctx.position).normalize().scale(0.5) + + for (item in items) { + yeetItem(item, ctx, delta) + } + } + + protected fun yeetHeldItem(ctx: CastingContext, hand: InteractionHand) { + val item = ctx.caster.getItemInHand(hand).copy() + ctx.caster.setItemInHand(hand, ItemStack.EMPTY) + + val delta = ctx.caster.lookAngle.scale(0.5) + yeetItem(item, ctx, delta) + } protected fun yeetItem(stack: ItemStack, ctx: CastingContext, delta: Vec3) { val entity = ItemEntity( @@ -66,6 +95,10 @@ sealed class Mishap : Throwable() { ctx.world.addWithUUID(entity) } + protected fun blockAtPos(ctx: CastingContext, pos: BlockPos): Component { + return ctx.world.getBlockState(pos).block.name + } + data class Context(val pattern: HexPattern, val action: ResourceLocation?) companion object { diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt index 9928c36d..9e199151 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapAlreadyBrainswept.kt @@ -5,7 +5,6 @@ import at.petrak.hexcasting.api.misc.HexDamageSources import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component import net.minecraft.world.entity.npc.Villager import net.minecraft.world.item.DyeColor @@ -14,14 +13,13 @@ class MishapAlreadyBrainswept(val villager: Villager) : Mishap() { dyeColor(DyeColor.GREEN) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - villager.hurt(HexDamageSources.overcastDamageFrom(ctx.caster), villager.health) + trulyHurt(villager, HexDamageSources.overcastDamageFrom(ctx.caster), villager.health) } - override fun particleSpray(ctx: CastingContext): ParticleSpray { - return ParticleSpray.burst(villager.eyePosition, 1.0) - } + override fun particleSpray(ctx: CastingContext) = + ParticleSpray.burst(villager.eyePosition, 1.0) - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("already_brainswept") } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBlock.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBlock.kt index 5da84097..55788104 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBlock.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBlock.kt @@ -4,9 +4,9 @@ import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext +import at.petrak.hexcasting.api.utils.asTranslatedComponent import net.minecraft.core.BlockPos import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.item.DyeColor import net.minecraft.world.level.Explosion import net.minecraft.world.phys.Vec3 @@ -19,19 +19,16 @@ class MishapBadBlock(val pos: BlockPos, val expected: Component) : Mishap() { ctx.world.explode(null, pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, 0.25f, Explosion.BlockInteraction.NONE) } - override fun particleSpray(ctx: CastingContext): ParticleSpray { - return ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0) - } + override fun particleSpray(ctx: CastingContext) = + ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0) - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - val bs = ctx.world.getBlockState(this.pos) - return error("bad_block", expected, this.pos.toShortString(), bs.block.name) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("bad_block", expected, this.pos.toShortString(), blockAtPos(ctx, this.pos)) companion object { @JvmStatic fun of(pos: BlockPos, stub: String): MishapBadBlock { - return MishapBadBlock(pos, TranslatableComponent("hexcasting.mishap.bad_block.$stub")) + return MishapBadBlock(pos, "hexcasting.mishap.bad_block.$stub".asTranslatedComponent) } } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt index fbf5c1d3..f52d3b72 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadBrainsweep.kt @@ -6,7 +6,6 @@ import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext import net.minecraft.core.BlockPos -import net.minecraft.network.chat.Component import net.minecraft.world.entity.npc.Villager import net.minecraft.world.item.DyeColor import net.minecraft.world.phys.Vec3 @@ -23,8 +22,6 @@ class MishapBadBrainsweep(val villager: Villager, val pos: BlockPos) : Mishap() return ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - val bs = ctx.world.getBlockState(this.pos) - return error("bad_brainsweep", bs.block.name) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("bad_brainsweep", blockAtPos(ctx, this.pos)) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadItem.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadItem.kt index cbf7568f..abc9b921 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadItem.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadItem.kt @@ -3,8 +3,8 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext +import at.petrak.hexcasting.api.utils.asTranslatedComponent import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.item.DyeColor @@ -16,17 +16,15 @@ class MishapBadItem(val item: ItemEntity, val wanted: Component) : Mishap() { item.deltaMovement = item.deltaMovement.add((Math.random() - 0.5) * 0.05, 0.75, (Math.random() - 0.5) * 0.05) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - return if (item.item.isEmpty) - error("no_item", actionName(errorCtx.action), wanted) - else - error("bad_item", actionName(errorCtx.action), wanted, item.item.count, item.item.displayName) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = if (item.item.isEmpty) + error("no_item", actionName(errorCtx.action), wanted) + else + error("bad_item", actionName(errorCtx.action), wanted, item.item.count, item.item.displayName) companion object { @JvmStatic fun of(item: ItemEntity, stub: String): MishapBadItem { - return MishapBadItem(item, TranslatableComponent("hexcasting.mishap.bad_item.$stub")) + return MishapBadItem(item, "hexcasting.mishap.bad_item.$stub".asTranslatedComponent) } } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadOffhandItem.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadOffhandItem.kt index c5339092..4453c829 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadOffhandItem.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapBadOffhandItem.kt @@ -1,10 +1,10 @@ package at.petrak.hexcasting.api.spell.mishaps +import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.misc.FrozenColorizer +import at.petrak.hexcasting.api.utils.asTranslatedComponent import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.InteractionHand import net.minecraft.world.item.DyeColor import net.minecraft.world.item.ItemStack @@ -14,24 +14,18 @@ class MishapBadOffhandItem(val item: ItemStack, val hand: InteractionHand, val w dyeColor(DyeColor.BROWN) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - val item = ctx.caster.getItemInHand(hand).copy() - ctx.caster.setItemInHand(hand, ItemStack.EMPTY.copy()) - - val delta = ctx.caster.lookAngle.scale(0.5) - yeetItem(item, ctx, delta) + yeetHeldItem(ctx, hand) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - if (item.isEmpty) - return error("no_item.offhand", actionName(errorCtx.action), wanted) - - return error("bad_item.offhand", actionName(errorCtx.action), wanted, item.count, item.displayName) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = if (item.isEmpty) + error("no_item.offhand", actionName(errorCtx.action), wanted) + else + error("bad_item.offhand", actionName(errorCtx.action), wanted, item.count, item.displayName) companion object { @JvmStatic fun of(item: ItemStack, hand: InteractionHand, stub: String, vararg args: Any): MishapBadOffhandItem { - return MishapBadOffhandItem(item, hand, TranslatableComponent("hexcasting.mishap.bad_item.$stub", *args)) + return MishapBadOffhandItem(item, hand, "hexcasting.mishap.bad_item.$stub".asTranslatedComponent(*args)) } } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDisallowedSpell.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDisallowedSpell.kt index 03986d1a..4ff6edc4 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDisallowedSpell.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDisallowedSpell.kt @@ -4,7 +4,6 @@ import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapDisallowedSpell : Mishap() { @@ -17,6 +16,6 @@ class MishapDisallowedSpell : Mishap() { // NO-OP } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("disallowed", actionName(errorCtx.action)) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt index 6d502581..555abf01 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapDivideByZero.kt @@ -5,8 +5,8 @@ import at.petrak.hexcasting.api.misc.HexDamageSources 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.utils.asTranslatedComponent import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.item.DyeColor import net.minecraft.world.phys.Vec3 @@ -20,9 +20,8 @@ class MishapDivideByZero(val operand1: Component, val operand2: Component, val s trulyHurt(ctx.caster, HexDamageSources.OVERCAST, ctx.caster.health / 2) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - return error("divide_by_zero.$suffix", operand1, operand2) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("divide_by_zero.$suffix", operand1, operand2) companion object { private const val PREFIX = "hexcasting.mishap.divide_by_zero" @@ -38,25 +37,25 @@ class MishapDivideByZero(val operand1: Component, val operand2: Component, val s fun tan(angle: Double): MishapDivideByZero { val translatedAngle = translate(angle) return MishapDivideByZero( - TranslatableComponent("$PREFIX.sin", translatedAngle), - TranslatableComponent("$PREFIX.cos", translatedAngle) + "$PREFIX.sin".asTranslatedComponent(translatedAngle), + "$PREFIX.cos".asTranslatedComponent(translatedAngle) ) } @JvmStatic val zero - get() = TranslatableComponent("$PREFIX.zero") + get() = "$PREFIX.zero".asTranslatedComponent @JvmStatic val zerothPower - get() = TranslatableComponent("$PREFIX.zero.power") + get() = "$PREFIX.zero.power".asTranslatedComponent @JvmStatic val zeroVector - get() = TranslatableComponent("$PREFIX.zero.vec") + get() = "$PREFIX.zero.vec".asTranslatedComponent @JvmStatic - fun powerOf(power: Component) = TranslatableComponent("$PREFIX.power", power) + fun powerOf(power: Component) = "$PREFIX.power".asTranslatedComponent(power) @JvmStatic fun powerOf(datum: Any) = when (datum) { diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEntityTooFarAway.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEntityTooFarAway.kt index 2d340bbf..30c418c2 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEntityTooFarAway.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEntityTooFarAway.kt @@ -1,36 +1,19 @@ package at.petrak.hexcasting.api.spell.mishaps +import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.misc.FrozenColorizer -import at.petrak.hexcasting.api.mod.HexItemTags -import net.minecraft.network.chat.Component -import net.minecraft.world.InteractionHand import net.minecraft.world.entity.Entity import net.minecraft.world.item.DyeColor -import net.minecraft.world.item.ItemStack class MishapEntityTooFarAway(val entity: Entity) : Mishap() { override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer = dyeColor(DyeColor.PINK) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - // Knock the player's items out of their hands - val items = mutableListOf() - for (hand in InteractionHand.values()) { - if (hand != ctx.castingHand || ctx.caster.getItemInHand(hand).`is`(HexItemTags.WANDS)) { - items.add(ctx.caster.getItemInHand(hand).copy()) - ctx.caster.setItemInHand(hand, ItemStack.EMPTY) - } - } - - val delta = entity.position().subtract(ctx.position).normalize().scale(0.5) - - for (item in items) { - yeetItem(item, ctx, delta) - } + yeetHeldItemsTowards(ctx, entity.position()) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("entity_too_far", SpellDatum.make(entity).display(), actionName(errorCtx.action)) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapError.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapError.kt index 0b9b55ff..a0e99b17 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapError.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapError.kt @@ -3,7 +3,6 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapError(val exception: Exception) : Mishap() { @@ -11,8 +10,9 @@ class MishapError(val exception: Exception) : Mishap() { dyeColor(DyeColor.BLACK) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { + // NO-OP } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("unknown", actionName(errorCtx.action), exception) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEvalTooDeep.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEvalTooDeep.kt index 5280ca49..36203743 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEvalTooDeep.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapEvalTooDeep.kt @@ -1,9 +1,8 @@ package at.petrak.hexcasting.api.spell.mishaps +import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.misc.FrozenColorizer -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapEvalTooDeep : Mishap() { @@ -14,6 +13,6 @@ class MishapEvalTooDeep : Mishap() { ctx.caster.airSupply -= 290 } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("eval_too_deep") } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapImmuneEntity.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapImmuneEntity.kt index 2a0808ca..a0d6a79b 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapImmuneEntity.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapImmuneEntity.kt @@ -1,36 +1,19 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer -import at.petrak.hexcasting.api.mod.HexItemTags import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component -import net.minecraft.world.InteractionHand import net.minecraft.world.entity.Entity import net.minecraft.world.item.DyeColor -import net.minecraft.world.item.ItemStack class MishapImmuneEntity(val entity: Entity) : Mishap() { override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer = dyeColor(DyeColor.BLUE) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - val items = mutableListOf() - for (hand in InteractionHand.values()) { - if (hand != ctx.castingHand || ctx.caster.getItemInHand(hand).`is`(HexItemTags.WANDS)) { - items.add(ctx.caster.getItemInHand(hand).copy()) - ctx.caster.setItemInHand(hand, ItemStack.EMPTY) - } - } - - val delta = entity.position().subtract(ctx.position).normalize().scale(0.5) - - for (item in items) { - yeetItem(item, ctx, delta) - } + yeetHeldItemsTowards(ctx, entity.position()) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { - return error("immune_entity", actionName(errorCtx.action), entity.displayName) - } + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("immune_entity", actionName(errorCtx.action), entity.displayName) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidIota.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidIota.kt index 0cb9b1fb..4296a0aa 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidIota.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidIota.kt @@ -6,8 +6,8 @@ import at.petrak.hexcasting.api.spell.SpellList import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.math.HexPattern +import at.petrak.hexcasting.api.utils.asTranslatedComponent import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TranslatableComponent import net.minecraft.world.entity.Entity import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.item.ItemEntity @@ -33,14 +33,9 @@ class MishapInvalidIota( stack[stack.size - 1 - reverseIdx] = SpellDatum.make(Widget.GARBAGE) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = - error( - "invalid_value", - actionName(errorCtx.action), - expected, - reverseIdx, - perpetrator.display() - ) + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("invalid_value", actionName(errorCtx.action), expected, reverseIdx, + perpetrator.display()) companion object { @JvmStatic @@ -60,7 +55,7 @@ class MishapInvalidIota( else -> "unknown" } - return MishapInvalidIota(perpetrator, reverseIdx, TranslatableComponent(key)) + return MishapInvalidIota(perpetrator, reverseIdx, key.asTranslatedComponent) } } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidPattern.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidPattern.kt index 68b07acb..6ad4ca9e 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidPattern.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidPattern.kt @@ -5,7 +5,6 @@ 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.casting.ResolvedPatternType -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapInvalidPattern : Mishap() { @@ -18,6 +17,6 @@ class MishapInvalidPattern : Mishap() { stack.add(SpellDatum.make(Widget.GARBAGE)) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("invalid_pattern") } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidSpellDatumType.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidSpellDatumType.kt index ca5a13c6..e0c768f5 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidSpellDatumType.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapInvalidSpellDatumType.kt @@ -1,10 +1,9 @@ package at.petrak.hexcasting.api.spell.mishaps +import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.misc.FrozenColorizer import net.minecraft.Util -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor /** @@ -15,10 +14,11 @@ class MishapInvalidSpellDatumType(val perpetrator: Any) : Mishap() { dyeColor(DyeColor.BLACK) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { + // Send it twice, just to make it clear val msg = this.errorMessage(ctx, errorCtx) ctx.caster.sendMessage(msg, Util.NIL_UUID) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("invalid_spell_datum_type", this.perpetrator.toString(), this.perpetrator.javaClass.typeName) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationInWrongDimension.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationInWrongDimension.kt index 8633a0cb..321e1f3a 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationInWrongDimension.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationInWrongDimension.kt @@ -1,10 +1,9 @@ package at.petrak.hexcasting.api.spell.mishaps -import at.petrak.hexcasting.api.spell.SpellDatum -import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.misc.FrozenColorizer -import net.minecraft.network.chat.Component +import at.petrak.hexcasting.api.spell.SpellDatum +import at.petrak.hexcasting.api.spell.Widget +import at.petrak.hexcasting.api.spell.casting.CastingContext import net.minecraft.resources.ResourceLocation import net.minecraft.world.item.DyeColor @@ -16,7 +15,7 @@ class MishapLocationInWrongDimension(val properDimension: ResourceLocation) : Mi stack.add(SpellDatum.make(Widget.GARBAGE)) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = - error("wrong_dimension", actionName(errorCtx.action!!), properDimension.toString(), + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("wrong_dimension", actionName(errorCtx.action), properDimension.toString(), ctx.world.dimension().location().toString()) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationTooFarAway.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationTooFarAway.kt index 9fee97bd..8b1fa206 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationTooFarAway.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapLocationTooFarAway.kt @@ -1,13 +1,9 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer -import at.petrak.hexcasting.api.mod.HexItemTags import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component -import net.minecraft.world.InteractionHand import net.minecraft.world.item.DyeColor -import net.minecraft.world.item.ItemStack import net.minecraft.world.phys.Vec3 class MishapLocationTooFarAway(val location: Vec3, val type: String = "too_far") : Mishap() { @@ -15,22 +11,9 @@ class MishapLocationTooFarAway(val location: Vec3, val type: String = "too_far") dyeColor(DyeColor.MAGENTA) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - // Knock the player's items out of their hands - val items = mutableListOf() - for (hand in InteractionHand.values()) { - if (hand != ctx.castingHand || ctx.caster.getItemInHand(hand).`is`(HexItemTags.WANDS)) { - items.add(ctx.caster.getItemInHand(hand).copy()) - ctx.caster.setItemInHand(hand, ItemStack.EMPTY) - } - } - - val delta = location.subtract(ctx.position).normalize().scale(0.5) - - for (item in items) { - yeetItem(item, ctx, delta) - } + yeetHeldItemsTowards(ctx, location) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = - error("location_$type", SpellDatum.make(location).display(), actionName(errorCtx.action!!)) + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("location_$type", SpellDatum.make(location).display(), actionName(errorCtx.action)) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoAkashicRecord.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoAkashicRecord.kt index a259fc41..fd49c74d 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoAkashicRecord.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoAkashicRecord.kt @@ -1,10 +1,9 @@ package at.petrak.hexcasting.api.spell.mishaps +import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import at.petrak.hexcasting.api.misc.FrozenColorizer import net.minecraft.core.BlockPos -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapNoAkashicRecord(val pos: BlockPos) : Mishap() { @@ -15,6 +14,6 @@ class MishapNoAkashicRecord(val pos: BlockPos) : Mishap() { ctx.caster.giveExperiencePoints(-100) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("no_akashic_record", pos.toShortString()) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoSpellCircle.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoSpellCircle.kt index 49e52eeb..b5f3e8b9 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoSpellCircle.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNoSpellCircle.kt @@ -3,7 +3,6 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor class MishapNoSpellCircle : Mishap() { @@ -14,6 +13,6 @@ class MishapNoSpellCircle : Mishap() { ctx.caster.inventory.dropAll() } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("no_spell_circle", actionName(errorCtx.action)) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNotEnoughArgs.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNotEnoughArgs.kt index 6b663d90..33ef2159 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNotEnoughArgs.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapNotEnoughArgs.kt @@ -16,6 +16,6 @@ class MishapNotEnoughArgs(val expected: Int, val got: Int) : Mishap() { stack.add(SpellDatum.make(Widget.GARBAGE)) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("not_enough_args", actionName(errorCtx.action), expected, got) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapOthersName.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapOthersName.kt index ed728cc4..7c93912f 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapOthersName.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapOthersName.kt @@ -15,11 +15,10 @@ class MishapOthersName(val other: Player) : Mishap() { dyeColor(DyeColor.BLACK) override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { - val effect = MobEffectInstance(MobEffects.BLINDNESS, 20 * 60) - ctx.caster.addEffect(effect) + ctx.caster.addEffect(MobEffectInstance(MobEffects.BLINDNESS, 20 * 60)) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("others_name", other.name) companion object { diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapTooManyCloseParens.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapTooManyCloseParens.kt index 89917452..e5048f25 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapTooManyCloseParens.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapTooManyCloseParens.kt @@ -14,6 +14,6 @@ class MishapTooManyCloseParens : Mishap() { stack.add(SpellDatum.make(errorCtx.pattern)) } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = error("too_many_close_parens") } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapUnescapedValue.kt b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapUnescapedValue.kt index 1de55c98..5ecfcaec 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapUnescapedValue.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/spell/mishaps/MishapUnescapedValue.kt @@ -3,9 +3,9 @@ package at.petrak.hexcasting.api.spell.mishaps import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.spell.DatumType import at.petrak.hexcasting.api.spell.SpellDatum +import at.petrak.hexcasting.api.spell.SpellList import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.spell.casting.CastingContext -import net.minecraft.network.chat.Component import net.minecraft.world.item.DyeColor /** @@ -19,13 +19,17 @@ class MishapUnescapedValue( override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { val idx = stack.indexOfLast { it.getType() == DatumType.LIST } - if (idx != -1) - stack[idx] = SpellDatum.make(Widget.GARBAGE) + if (idx != -1) { + val list = stack[idx].payload as SpellList + val idxOfIota = list.indexOfFirst { it == perpetrator } + if (idxOfIota != -1) { + stack[idx] = SpellDatum.make(list.modifyAt(idxOfIota) { + SpellList.LPair(SpellDatum.make(Widget.GARBAGE), it.cdr) + }) + } + } } - override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = - error( - "unescaped", - perpetrator.display() - ) + override fun errorMessage(ctx: CastingContext, errorCtx: Context) = + error("unescaped", perpetrator.display()) } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/utils/HexUtils.kt b/Common/src/main/java/at/petrak/hexcasting/api/utils/HexUtils.kt index e5bf298f..7ad3ac78 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/utils/HexUtils.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/utils/HexUtils.kt @@ -3,9 +3,11 @@ package at.petrak.hexcasting.api.utils import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.math.HexCoord +import net.minecraft.ChatFormatting import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.ListTag import net.minecraft.nbt.LongArrayTag +import net.minecraft.network.chat.* import net.minecraft.world.InteractionHand import net.minecraft.world.item.ItemStack import net.minecraft.world.phys.Vec2 @@ -81,6 +83,91 @@ fun pxToCoord(px: Vec2, size: Float, offset: Vec2): HexCoord { HexCoord(q, r + (rf + 0.5 * qf).roundToInt()) } +fun String.withStyle(op: (Style) -> Style): MutableComponent = asTextComponent.withStyle(op) +fun String.withStyle(style: Style): MutableComponent = asTextComponent.withStyle(style) +fun String.withStyle(formatting: ChatFormatting): MutableComponent = asTextComponent.withStyle(formatting) +fun String.withStyle(vararg formatting: ChatFormatting): MutableComponent = asTextComponent.withStyle(*formatting) + +infix fun String.styledWith(op: (Style) -> Style) = withStyle(op) +infix fun String.styledWith(style: Style) = withStyle(style) +infix fun String.styledWith(formatting: ChatFormatting) = withStyle(formatting) + +infix fun MutableComponent.styledWith(op: (Style) -> Style): MutableComponent = withStyle(op) +infix fun MutableComponent.styledWith(style: Style): MutableComponent = withStyle(style) +infix fun MutableComponent.styledWith(formatting: ChatFormatting): MutableComponent = withStyle(formatting) + +val String.black get() = this styledWith ChatFormatting.BLACK +val MutableComponent.black get() = this styledWith ChatFormatting.BLACK + +val String.darkBlue get() = this styledWith ChatFormatting.DARK_BLUE +val MutableComponent.darkBlue get() = this styledWith ChatFormatting.DARK_BLUE + +val String.darkGreen get() = this styledWith ChatFormatting.DARK_GREEN +val MutableComponent.darkGreen get() = this styledWith ChatFormatting.DARK_GREEN + +val String.darkAqua get() = this styledWith ChatFormatting.DARK_AQUA +val MutableComponent.darkAqua get() = this styledWith ChatFormatting.DARK_AQUA + +val String.darkRed get() = this styledWith ChatFormatting.DARK_RED +val MutableComponent.darkRed get() = this styledWith ChatFormatting.DARK_RED + +val String.darkPurple get() = this styledWith ChatFormatting.DARK_PURPLE +val MutableComponent.darkPurple get() = this styledWith ChatFormatting.DARK_PURPLE + +val String.gold get() = this styledWith ChatFormatting.GOLD +val MutableComponent.gold get() = this styledWith ChatFormatting.GOLD + +val String.gray get() = this styledWith ChatFormatting.GRAY +val MutableComponent.gray get() = this styledWith ChatFormatting.GRAY + +val String.darkGray get() = this styledWith ChatFormatting.DARK_GRAY +val MutableComponent.darkGray get() = this styledWith ChatFormatting.DARK_GRAY + +val String.blue get() = this styledWith ChatFormatting.BLUE +val MutableComponent.blue get() = this styledWith ChatFormatting.BLUE + +val String.green get() = this styledWith ChatFormatting.GREEN +val MutableComponent.green get() = this styledWith ChatFormatting.GREEN + +val String.aqua get() = this styledWith ChatFormatting.AQUA +val MutableComponent.aqua get() = this styledWith ChatFormatting.AQUA + +val String.red get() = this styledWith ChatFormatting.RED +val MutableComponent.red get() = this styledWith ChatFormatting.RED + +val String.lightPurple get() = this styledWith ChatFormatting.LIGHT_PURPLE +val MutableComponent.lightPurple get() = this styledWith ChatFormatting.LIGHT_PURPLE + +val String.yellow get() = this styledWith ChatFormatting.YELLOW +val MutableComponent.yellow get() = this styledWith ChatFormatting.YELLOW + +val String.white get() = this styledWith ChatFormatting.WHITE +val MutableComponent.white get() = this styledWith ChatFormatting.WHITE + +val String.obfuscated get() = this styledWith ChatFormatting.OBFUSCATED +val MutableComponent.obfuscated get() = this styledWith ChatFormatting.OBFUSCATED + +val String.bold get() = this styledWith ChatFormatting.BOLD +val MutableComponent.bold get() = this styledWith ChatFormatting.BOLD + +val String.strikethrough get() = this styledWith ChatFormatting.STRIKETHROUGH +val MutableComponent.strikethrough get() = this styledWith ChatFormatting.STRIKETHROUGH + +val String.underline get() = this styledWith ChatFormatting.UNDERLINE +val MutableComponent.underline get() = this styledWith ChatFormatting.UNDERLINE + +val String.italic get() = this styledWith ChatFormatting.ITALIC +val MutableComponent.italic get() = this styledWith ChatFormatting.ITALIC + +operator fun MutableComponent.plusAssign(component: Component) { + append(component) +} + +val String.asTextComponent get() = TextComponent(this) +val String.asTranslatedComponent get() = TranslatableComponent(this) + +fun String.asTranslatedComponent(vararg args: Any) = TranslatableComponent(this, *args) + fun Iterable>.serializeToNBT(): ListTag { val tag = ListTag() for (elt in this) diff --git a/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt b/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt index 38892a11..3779b3a1 100644 --- a/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt +++ b/Common/src/main/java/at/petrak/hexcasting/client/gui/GuiSpellcasting.kt @@ -8,6 +8,7 @@ import at.petrak.hexcasting.api.spell.math.HexAngle import at.petrak.hexcasting.api.spell.math.HexCoord import at.petrak.hexcasting.api.spell.math.HexDir import at.petrak.hexcasting.api.spell.math.HexPattern +import at.petrak.hexcasting.api.utils.asTranslatedComponent import at.petrak.hexcasting.api.utils.otherHand import at.petrak.hexcasting.client.drawPatternFromPoints import at.petrak.hexcasting.client.drawSpot @@ -25,7 +26,6 @@ import net.minecraft.client.gui.screens.Screen import net.minecraft.client.renderer.GameRenderer import net.minecraft.client.resources.sounds.SimpleSoundInstance import net.minecraft.network.chat.Component -import net.minecraft.network.chat.TextComponent import net.minecraft.sounds.SoundSource import net.minecraft.util.Mth import net.minecraft.world.InteractionHand @@ -38,7 +38,7 @@ class GuiSpellcasting( private val handOpenedWith: InteractionHand, private var patterns: MutableList, private var stackDescs: List -) : Screen(TextComponent("")) { +) : Screen("gui.hexcasting.spellcasting".asTranslatedComponent) { private var drawState: PatternDrawState = PatternDrawState.BetweenPatterns private val usedSpots: MutableSet = HashSet() diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpLastNToList.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpLastNToList.kt index f2dc9db5..bfc8f6fe 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpLastNToList.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/lists/OpLastNToList.kt @@ -5,7 +5,7 @@ import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.SpellContinuation import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs -import net.minecraft.network.chat.TranslatableComponent +import at.petrak.hexcasting.api.utils.asTranslatedComponent import kotlin.math.abs import kotlin.math.roundToInt @@ -20,7 +20,7 @@ object OpLastNToList : Operator { throw MishapInvalidIota( datum, 0, - TranslatableComponent("hexcasting.mishap.invalid_value.int.between", 0, stack.size) + "hexcasting.mishap.invalid_value.int.between".asTranslatedComponent(0, stack.size) ) } val output = mutableListOf>() diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcCos.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcCos.kt index 7a55ad72..8cda419a 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcCos.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcCos.kt @@ -6,7 +6,7 @@ import at.petrak.hexcasting.api.spell.asSpellResult import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota -import net.minecraft.network.chat.TranslatableComponent +import at.petrak.hexcasting.api.utils.asTranslatedComponent import kotlin.math.acos object OpArcCos : ConstManaOperator { @@ -19,7 +19,7 @@ object OpArcCos : ConstManaOperator { throw MishapInvalidIota( SpellDatum.make(value), 0, - TranslatableComponent("hexcasting.mishap.invalid_value.double.between", -1, 1) + "hexcasting.mishap.invalid_value.double.between".asTranslatedComponent(-1, 1) ) return acos(value).asSpellResult } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcSin.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcSin.kt index de3cf6ee..1bf6792f 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcSin.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/math/trig/OpArcSin.kt @@ -6,7 +6,7 @@ import at.petrak.hexcasting.api.spell.asSpellResult import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota -import net.minecraft.network.chat.TranslatableComponent +import at.petrak.hexcasting.api.utils.asTranslatedComponent import kotlin.math.asin object OpArcSin : ConstManaOperator { @@ -19,7 +19,7 @@ object OpArcSin : ConstManaOperator { throw MishapInvalidIota( SpellDatum.make(value), 0, - TranslatableComponent("hexcasting.mishap.invalid_value.double.between", -1, 1) + "hexcasting.mishap.invalid_value.double.between".asTranslatedComponent(-1, 1) ) return asin(value).asSpellResult } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpAlwinfyHasAscendedToABeingOfPureMath.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpAlwinfyHasAscendedToABeingOfPureMath.kt index 2b7db1da..5eaf531f 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpAlwinfyHasAscendedToABeingOfPureMath.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpAlwinfyHasAscendedToABeingOfPureMath.kt @@ -10,8 +10,8 @@ import at.petrak.hexcasting.api.spell.casting.SpellContinuation import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs +import at.petrak.hexcasting.api.utils.asTranslatedComponent import it.unimi.dsi.fastutil.ints.IntArrayList -import net.minecraft.network.chat.TranslatableComponent import kotlin.math.abs import kotlin.math.ln import kotlin.math.roundToInt @@ -32,7 +32,7 @@ object OpAlwinfyHasAscendedToABeingOfPureMath : Operator { throw MishapInvalidIota( stack.last(), 0, - TranslatableComponent("hexcasting.mishap.invalid_value.int", 0) + "hexcasting.mishap.invalid_value.int".asTranslatedComponent(0) ) stack.removeLast() val code = codeDouble.roundToInt() diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpDuplicateN.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpDuplicateN.kt index cb3ff8a6..e8812508 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpDuplicateN.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpDuplicateN.kt @@ -1,11 +1,11 @@ package at.petrak.hexcasting.common.casting.operators.stack import at.petrak.hexcasting.api.spell.ConstManaOperator -import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext +import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota -import net.minecraft.network.chat.TranslatableComponent +import at.petrak.hexcasting.api.utils.asTranslatedComponent import kotlin.math.abs import kotlin.math.roundToInt @@ -20,7 +20,7 @@ object OpDuplicateN : ConstManaOperator { throw MishapInvalidIota( args[1], 0, - TranslatableComponent("hexcasting.mishap.invalid_value.int.between", 0, args.size) + "hexcasting.mishap.invalid_value.int.between".asTranslatedComponent(0, args.size) ) val count = countDouble.roundToInt() diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpFisherman.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpFisherman.kt index 93218736..6804ebe9 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpFisherman.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/operators/stack/OpFisherman.kt @@ -2,13 +2,13 @@ package at.petrak.hexcasting.common.casting.operators.stack import at.petrak.hexcasting.api.spell.OperationResult import at.petrak.hexcasting.api.spell.Operator -import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.SpellContinuation +import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs -import net.minecraft.network.chat.TranslatableComponent +import at.petrak.hexcasting.api.utils.asTranslatedComponent import kotlin.math.abs import kotlin.math.roundToInt @@ -28,7 +28,7 @@ object OpFisherman : Operator { throw MishapInvalidIota( datum, 0, - TranslatableComponent("hexcasting.mishap.invalid_value.int.between", 1, stack.size) + "hexcasting.mishap.invalid_value.int.between".asTranslatedComponent(1, stack.size) ) } diff --git a/Common/src/main/resources/assets/hexcasting/lang/en_us.json b/Common/src/main/resources/assets/hexcasting/lang/en_us.json index fb88990a..ee38fe16 100644 --- a/Common/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/Common/src/main/resources/assets/hexcasting/lang/en_us.json @@ -130,6 +130,8 @@ "hexcasting.spelldata.entity.whoknows": "An Entity (this should only show up if this was stored before the 0.5.0 update, use Scribe's Reflection, Scribe's Gambit to fix)", "hexcasting.spelldata.akashic.nopos": "The owning record does not know of any iota here (this is a bug)", + "gui.hexcasting.spellcasting": "Hex Grid", + "advancement.hexcasting:root": "Hexcasting Research", "advancement.hexcasting:root.desc": "Find a concentrated form of media growing deep beneath the earth.", "advancement.hexcasting:enlightenment": "Achieve Enlightenment",