some kt component utils, also fix a display bug on fabric

This commit is contained in:
yrsegal@gmail.com 2022-05-29 15:59:48 -04:00
parent 92a705987e
commit 33f364039c
37 changed files with 259 additions and 237 deletions

View file

@ -6,6 +6,7 @@ import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -21,12 +22,7 @@ public interface DataHolderItem {
@Nullable @Nullable
default SpellDatum<?> readDatum(ItemStack stack, ServerLevel world) { default SpellDatum<?> readDatum(ItemStack stack, ServerLevel world) {
if (!(stack.getItem() instanceof DataHolderItem dh)) { var tag = readDatumTag(stack);
// 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);
if (tag != null) { if (tag != null) {
return SpellDatum.fromNBT(tag, world); return SpellDatum.fromNBT(tag, world);
} else { } else {
@ -51,7 +47,7 @@ public interface DataHolderItem {
pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem", component)); pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem", component));
if (pIsAdvanced.isAdvanced()) { 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)) { } else if (NBTHelper.hasString(pStack, DataHolderItem.TAG_OVERRIDE_VISUALLY)) {
pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem", pTooltipComponents.add(new TranslatableComponent("hexcasting.spelldata.onitem",

View file

@ -5,10 +5,10 @@ package at.petrak.hexcasting.api.spell
import at.petrak.hexcasting.api.spell.math.HexPattern import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota
import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.utils.asTranslatedComponent
import com.mojang.datafixers.util.Either import com.mojang.datafixers.util.Either
import com.mojang.math.Vector3f import com.mojang.math.Vector3f
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.entity.Entity import net.minecraft.world.entity.Entity
import net.minecraft.world.phys.Vec3 import net.minecraft.world.phys.Vec3
import kotlin.math.abs import kotlin.math.abs
@ -20,7 +20,7 @@ fun numOrVec(datum: SpellDatum<*>, reverseIdx: Int): Either<Double, Vec3> =
else -> throw MishapInvalidIota( else -> throw MishapInvalidIota(
datum, datum,
reverseIdx, reverseIdx,
TranslatableComponent("hexcasting.mishap.invalid_value.numvec") "hexcasting.mishap.invalid_value.numvec".asTranslatedComponent
) )
} }
@ -31,7 +31,7 @@ fun numOrList(datum: SpellDatum<*>, reverseIdx: Int): Either<Double, SpellList>
else -> throw MishapInvalidIota( else -> throw MishapInvalidIota(
datum, datum,
reverseIdx, reverseIdx,
TranslatableComponent("hexcasting.mishap.invalid_value.numlist") "hexcasting.mishap.invalid_value.numlist".asTranslatedComponent
) )
} }

View file

@ -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.math.HexPattern
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidSpellDatumType import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidSpellDatumType
import at.petrak.hexcasting.api.utils.* import at.petrak.hexcasting.api.utils.*
import net.minecraft.ChatFormatting
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.NbtUtils import net.minecraft.nbt.NbtUtils
import net.minecraft.nbt.Tag import net.minecraft.nbt.Tag
import net.minecraft.network.chat.Component 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.server.level.ServerLevel
import net.minecraft.world.entity.Entity import net.minecraft.world.entity.Entity
import net.minecraft.world.phys.Vec3 import net.minecraft.world.phys.Vec3
@ -155,69 +152,59 @@ class SpellDatum<T : Any> private constructor(val payload: T) {
if (keys.size != 1) if (keys.size != 1)
throw IllegalArgumentException("Expected exactly one kv pair: $nbt") throw IllegalArgumentException("Expected exactly one kv pair: $nbt")
return when (val key = keys.iterator().next()) { val out = "".asTextComponent
TAG_DOUBLE -> TextComponent( when (val key = keys.iterator().next()) {
String.format( TAG_DOUBLE -> out += String.format(
"%.4f", "%.4f",
nbt.getDouble(TAG_DOUBLE) nbt.getDouble(TAG_DOUBLE)
) ).green
).withStyle(ChatFormatting.GREEN)
TAG_VEC3 -> { TAG_VEC3 -> {
val vec = vecFromNBT(nbt.getLongArray(key)) val vec = vecFromNBT(nbt.getLongArray(key))
// the focus color is really more red, but we don't want to show an error-y color // the focus color is really more red, but we don't want to show an error-y color
TextComponent( out += String.format(
String.format(
"(%.2f, %.2f, %.2f)", "(%.2f, %.2f, %.2f)",
vec.x, vec.x,
vec.y, vec.y,
vec.z vec.z
) ).lightPurple
).withStyle(ChatFormatting.LIGHT_PURPLE)
} }
TAG_LIST -> { TAG_LIST -> {
val out = TextComponent("[").withStyle(ChatFormatting.WHITE) out += "[".white
val arr = nbt.getList(key, Tag.TAG_COMPOUND) val arr = nbt.getList(key, Tag.TAG_COMPOUND)
for ((i, subtag) in arr.withIndex()) { for ((i, subtag) in arr.withIndex()) {
// this is safe because otherwise we wouldn't have been able to get the list before out += displayFromNBT(subtag.asCompound)
out.append(displayFromNBT(subtag as CompoundTag))
if (i != arr.lastIndex) { if (i != arr.lastIndex) {
out.append(", ") out += ", ".white
} }
} }
out.append("]") out += "]".white
} }
TAG_WIDGET -> { TAG_WIDGET -> {
val widget = Widget.valueOf(nbt.getString(key)) val widget = Widget.valueOf(nbt.getString(key))
if (widget == Widget.GARBAGE) TextComponent("arimfexendrapuse").withStyle( out += if (widget == Widget.GARBAGE)
ChatFormatting.DARK_GRAY, "arimfexendrapuse".darkGray.obfuscated
ChatFormatting.OBFUSCATED else
) widget.toString().darkPurple
// use dark purple instead of pink, so that vec3 can be pink instead of error red
else TextComponent(widget.toString()).withStyle(ChatFormatting.DARK_PURPLE)
} }
TAG_PATTERN -> { TAG_PATTERN -> {
val pat = HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN)) val pat = HexPattern.fromNBT(nbt.getCompound(TAG_PATTERN))
var angleDesc = pat.anglesSignature() var angleDesc = pat.anglesSignature()
if (angleDesc.isNotBlank()) angleDesc = " $angleDesc"; if (angleDesc.isNotBlank()) angleDesc = " $angleDesc";
val out = TextComponent("HexPattern(").withStyle(ChatFormatting.GOLD) out += "HexPattern(".gold
out.append(TextComponent("${pat.startDir}$angleDesc").withStyle(ChatFormatting.WHITE)) out += "${pat.startDir}$angleDesc".white
out.append(")") out += ")".gold
} }
TAG_ENTITY -> { 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 // handle pre-0.5.0 foci not having the tag
try { out += Component.Serializer.fromJson(json)?.aqua ?: "hexcasting.spelldata.entity.whoknows".asTranslatedComponent.white
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)
}
} }
else -> throw IllegalArgumentException("Unknown key $key: $nbt") else -> throw IllegalArgumentException("Unknown key $key: $nbt")
} }
return out
} }
// Set of legal types to go in a spell // Set of legal types to go in a spell

View file

@ -16,7 +16,6 @@ import at.petrak.hexcasting.api.utils.*
import at.petrak.hexcasting.xplat.IXplatAbstractions import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.Tag import net.minecraft.nbt.Tag
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel import net.minecraft.server.level.ServerLevel
import net.minecraft.world.phys.Vec3 import net.minecraft.world.phys.Vec3
@ -236,13 +235,7 @@ class CastingHarness private constructor(
} }
} }
fun generateDescs(): List<Component> { fun generateDescs() = stack.map(SpellDatum<*>::display)
val descs = ArrayList<Component>(this.stack.size)
for (datum in this.stack) {
descs.add(datum.display())
}
return descs
}
/** /**
* Return the functional update represented by the current state (for use with `copy`) * Return the functional update represented by the current state (for use with `copy`)

View file

@ -7,10 +7,10 @@ import at.petrak.hexcasting.api.mod.HexStatistics
import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.ParticleSpray
import at.petrak.hexcasting.api.spell.RenderedSpell import at.petrak.hexcasting.api.spell.RenderedSpell
import at.petrak.hexcasting.api.spell.mishaps.Mishap 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.HexItems
import at.petrak.hexcasting.common.lib.HexSounds import at.petrak.hexcasting.common.lib.HexSounds
import net.minecraft.Util import net.minecraft.Util
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.sounds.SoundSource import net.minecraft.sounds.SoundSource
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
@ -25,7 +25,7 @@ sealed class OperatorSideEffect {
data class RequiredEnlightenment(val awardStat: Boolean) : OperatorSideEffect() { data class RequiredEnlightenment(val awardStat: Boolean) : OperatorSideEffect() {
override fun performEffect(harness: CastingHarness): Boolean { override fun performEffect(harness: CastingHarness): Boolean {
harness.ctx.caster.sendMessage( harness.ctx.caster.sendMessage(
TranslatableComponent("hexcasting.message.cant_great_spell"), "hexcasting.message.cant_great_spell".asTranslatedComponent,
Util.NIL_UUID Util.NIL_UUID
) )
@ -53,7 +53,7 @@ sealed class OperatorSideEffect {
val leftoverMana = harness.withdrawMana(this.amount, overcastOk) val leftoverMana = harness.withdrawMana(this.amount, overcastOk)
if (leftoverMana > 0 && !overcastOk) { if (leftoverMana > 0 && !overcastOk) {
harness.ctx.caster.sendMessage( harness.ctx.caster.sendMessage(
TranslatableComponent("hexcasting.message.cant_overcast"), "hexcasting.message.cant_overcast".asTranslatedComponent,
Util.NIL_UUID Util.NIL_UUID
) )
} }

View file

@ -1,19 +1,22 @@
package at.petrak.hexcasting.api.spell.mishaps package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer 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.ParticleSpray
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.casting.ResolvedPatternType import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType
import at.petrak.hexcasting.api.spell.math.HexPattern 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.common.lib.HexItems
import at.petrak.hexcasting.mixin.accessor.AccessorLivingEntity import at.petrak.hexcasting.mixin.accessor.AccessorLivingEntity
import net.minecraft.ChatFormatting
import net.minecraft.Util import net.minecraft.Util
import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component 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.resources.ResourceLocation
import net.minecraft.world.InteractionHand
import net.minecraft.world.damagesource.DamageSource import net.minecraft.world.damagesource.DamageSource
import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.item.ItemEntity
@ -40,6 +43,8 @@ sealed class Mishap : Throwable() {
abstract fun errorMessage(ctx: CastingContext, errorCtx: Context): Component abstract fun errorMessage(ctx: CastingContext, errorCtx: Context): Component
// Useful helper functions
protected fun dyeColor(color: DyeColor): FrozenColorizer = protected fun dyeColor(color: DyeColor): FrozenColorizer =
FrozenColorizer( FrozenColorizer(
ItemStack(HexItems.DYE_COLORIZERS[color]!!), ItemStack(HexItems.DYE_COLORIZERS[color]!!),
@ -47,11 +52,35 @@ sealed class Mishap : Throwable() {
) )
protected fun error(stub: String, vararg args: Any): Component = 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 = protected fun actionName(action: ResourceLocation?): Component =
TranslatableComponent("hexcasting.spell.${action ?: "unknown"}") "hexcasting.spell.${action ?: "unknown"}".asTranslatedComponent.lightPurple.obfuscated
.setStyle(Style.EMPTY.withColor(ChatFormatting.LIGHT_PURPLE).withUnderlined(true))
protected fun yeetHeldItemsTowards(ctx: CastingContext, targetPos: Vec3) {
// Knock the player's items out of their hands
val items = mutableListOf<ItemStack>()
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) { protected fun yeetItem(stack: ItemStack, ctx: CastingContext, delta: Vec3) {
val entity = ItemEntity( val entity = ItemEntity(
@ -66,6 +95,10 @@ sealed class Mishap : Throwable() {
ctx.world.addWithUUID(entity) 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?) data class Context(val pattern: HexPattern, val action: ResourceLocation?)
companion object { companion object {

View file

@ -5,7 +5,6 @@ import at.petrak.hexcasting.api.misc.HexDamageSources
import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.ParticleSpray
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 net.minecraft.network.chat.Component
import net.minecraft.world.entity.npc.Villager import net.minecraft.world.entity.npc.Villager
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
@ -14,14 +13,13 @@ class MishapAlreadyBrainswept(val villager: Villager) : Mishap() {
dyeColor(DyeColor.GREEN) dyeColor(DyeColor.GREEN)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
villager.hurt(HexDamageSources.overcastDamageFrom(ctx.caster), villager.health) trulyHurt(villager, HexDamageSources.overcastDamageFrom(ctx.caster), villager.health)
} }
override fun particleSpray(ctx: CastingContext): ParticleSpray { override fun particleSpray(ctx: CastingContext) =
return ParticleSpray.burst(villager.eyePosition, 1.0) ParticleSpray.burst(villager.eyePosition, 1.0)
}
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("already_brainswept") error("already_brainswept")
} }

View file

@ -4,9 +4,9 @@ import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.ParticleSpray import at.petrak.hexcasting.api.spell.ParticleSpray
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.utils.asTranslatedComponent
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.level.Explosion import net.minecraft.world.level.Explosion
import net.minecraft.world.phys.Vec3 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) 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 { override fun particleSpray(ctx: CastingContext) =
return ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0) ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0)
}
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
val bs = ctx.world.getBlockState(this.pos) error("bad_block", expected, this.pos.toShortString(), blockAtPos(ctx, this.pos))
return error("bad_block", expected, this.pos.toShortString(), bs.block.name)
}
companion object { companion object {
@JvmStatic @JvmStatic
fun of(pos: BlockPos, stub: String): MishapBadBlock { 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)
} }
} }
} }

View file

@ -6,7 +6,6 @@ import at.petrak.hexcasting.api.spell.ParticleSpray
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 net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.npc.Villager import net.minecraft.world.entity.npc.Villager
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.phys.Vec3 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) return ParticleSpray.burst(Vec3.atCenterOf(pos), 1.0)
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
val bs = ctx.world.getBlockState(this.pos) error("bad_brainsweep", blockAtPos(ctx, this.pos))
return error("bad_brainsweep", bs.block.name)
}
} }

View file

@ -3,8 +3,8 @@ package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.misc.FrozenColorizer
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.utils.asTranslatedComponent
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.item.ItemEntity
import net.minecraft.world.item.DyeColor 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) 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 { override fun errorMessage(ctx: CastingContext, errorCtx: Context) = if (item.item.isEmpty)
return if (item.item.isEmpty) error("no_item", actionName(errorCtx.action), wanted)
error("no_item", actionName(errorCtx.action), wanted) else
else error("bad_item", actionName(errorCtx.action), wanted, item.item.count, item.item.displayName)
error("bad_item", actionName(errorCtx.action), wanted, item.item.count, item.item.displayName)
}
companion object { companion object {
@JvmStatic @JvmStatic
fun of(item: ItemEntity, stub: String): MishapBadItem { 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)
} }
} }
} }

View file

@ -1,10 +1,10 @@
package at.petrak.hexcasting.api.spell.mishaps 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.Component
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.InteractionHand import net.minecraft.world.InteractionHand
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
@ -14,24 +14,18 @@ class MishapBadOffhandItem(val item: ItemStack, val hand: InteractionHand, val w
dyeColor(DyeColor.BROWN) dyeColor(DyeColor.BROWN)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
val item = ctx.caster.getItemInHand(hand).copy() yeetHeldItem(ctx, hand)
ctx.caster.setItemInHand(hand, ItemStack.EMPTY.copy())
val delta = ctx.caster.lookAngle.scale(0.5)
yeetItem(item, ctx, delta)
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { override fun errorMessage(ctx: CastingContext, errorCtx: Context) = if (item.isEmpty)
if (item.isEmpty) error("no_item.offhand", actionName(errorCtx.action), wanted)
return error("no_item.offhand", actionName(errorCtx.action), wanted) else
error("bad_item.offhand", actionName(errorCtx.action), wanted, item.count, item.displayName)
return error("bad_item.offhand", actionName(errorCtx.action), wanted, item.count, item.displayName)
}
companion object { companion object {
@JvmStatic @JvmStatic
fun of(item: ItemStack, hand: InteractionHand, stub: String, vararg args: Any): MishapBadOffhandItem { 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))
} }
} }
} }

View file

@ -4,7 +4,6 @@ import at.petrak.hexcasting.api.misc.FrozenColorizer
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.casting.ResolvedPatternType import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType
import net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
class MishapDisallowedSpell : Mishap() { class MishapDisallowedSpell : Mishap() {
@ -17,6 +16,6 @@ class MishapDisallowedSpell : Mishap() {
// NO-OP // NO-OP
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("disallowed", actionName(errorCtx.action)) error("disallowed", actionName(errorCtx.action))
} }

View file

@ -5,8 +5,8 @@ import at.petrak.hexcasting.api.misc.HexDamageSources
import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.spell.Widget
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.Component
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.phys.Vec3 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) trulyHurt(ctx.caster, HexDamageSources.OVERCAST, ctx.caster.health / 2)
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
return error("divide_by_zero.$suffix", operand1, operand2) error("divide_by_zero.$suffix", operand1, operand2)
}
companion object { companion object {
private const val PREFIX = "hexcasting.mishap.divide_by_zero" 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 { fun tan(angle: Double): MishapDivideByZero {
val translatedAngle = translate(angle) val translatedAngle = translate(angle)
return MishapDivideByZero( return MishapDivideByZero(
TranslatableComponent("$PREFIX.sin", translatedAngle), "$PREFIX.sin".asTranslatedComponent(translatedAngle),
TranslatableComponent("$PREFIX.cos", translatedAngle) "$PREFIX.cos".asTranslatedComponent(translatedAngle)
) )
} }
@JvmStatic @JvmStatic
val zero val zero
get() = TranslatableComponent("$PREFIX.zero") get() = "$PREFIX.zero".asTranslatedComponent
@JvmStatic @JvmStatic
val zerothPower val zerothPower
get() = TranslatableComponent("$PREFIX.zero.power") get() = "$PREFIX.zero.power".asTranslatedComponent
@JvmStatic @JvmStatic
val zeroVector val zeroVector
get() = TranslatableComponent("$PREFIX.zero.vec") get() = "$PREFIX.zero.vec".asTranslatedComponent
@JvmStatic @JvmStatic
fun powerOf(power: Component) = TranslatableComponent("$PREFIX.power", power) fun powerOf(power: Component) = "$PREFIX.power".asTranslatedComponent(power)
@JvmStatic @JvmStatic
fun powerOf(datum: Any) = when (datum) { fun powerOf(datum: Any) = when (datum) {

View file

@ -1,36 +1,19 @@
package at.petrak.hexcasting.api.spell.mishaps 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.entity.Entity
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.ItemStack
class MishapEntityTooFarAway(val entity: Entity) : Mishap() { class MishapEntityTooFarAway(val entity: Entity) : Mishap() {
override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer = override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer =
dyeColor(DyeColor.PINK) dyeColor(DyeColor.PINK)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
// Knock the player's items out of their hands yeetHeldItemsTowards(ctx, entity.position())
val items = mutableListOf<ItemStack>()
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)
}
} }
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)) error("entity_too_far", SpellDatum.make(entity).display(), actionName(errorCtx.action))
} }

View file

@ -3,7 +3,6 @@ package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.misc.FrozenColorizer
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 net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
class MishapError(val exception: Exception) : Mishap() { class MishapError(val exception: Exception) : Mishap() {
@ -11,8 +10,9 @@ class MishapError(val exception: Exception) : Mishap() {
dyeColor(DyeColor.BLACK) dyeColor(DyeColor.BLACK)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
// NO-OP
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("unknown", actionName(errorCtx.action), exception) error("unknown", actionName(errorCtx.action), exception)
} }

View file

@ -1,9 +1,8 @@
package at.petrak.hexcasting.api.spell.mishaps 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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 import net.minecraft.world.item.DyeColor
class MishapEvalTooDeep : Mishap() { class MishapEvalTooDeep : Mishap() {
@ -14,6 +13,6 @@ class MishapEvalTooDeep : Mishap() {
ctx.caster.airSupply -= 290 ctx.caster.airSupply -= 290
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("eval_too_deep") error("eval_too_deep")
} }

View file

@ -1,36 +1,19 @@
package at.petrak.hexcasting.api.spell.mishaps package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.entity.Entity
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.ItemStack
class MishapImmuneEntity(val entity: Entity) : Mishap() { class MishapImmuneEntity(val entity: Entity) : Mishap() {
override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer = override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer =
dyeColor(DyeColor.BLUE) dyeColor(DyeColor.BLUE)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
val items = mutableListOf<ItemStack>() yeetHeldItemsTowards(ctx, entity.position())
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)
}
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component { override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
return error("immune_entity", actionName(errorCtx.action), entity.displayName) error("immune_entity", actionName(errorCtx.action), entity.displayName)
}
} }

View file

@ -6,8 +6,8 @@ import at.petrak.hexcasting.api.spell.SpellList
import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.spell.Widget
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.math.HexPattern 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.Component
import net.minecraft.network.chat.TranslatableComponent
import net.minecraft.world.entity.Entity import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.item.ItemEntity import net.minecraft.world.entity.item.ItemEntity
@ -33,14 +33,9 @@ class MishapInvalidIota(
stack[stack.size - 1 - reverseIdx] = SpellDatum.make(Widget.GARBAGE) stack[stack.size - 1 - reverseIdx] = SpellDatum.make(Widget.GARBAGE)
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error( error("invalid_value", actionName(errorCtx.action), expected, reverseIdx,
"invalid_value", perpetrator.display())
actionName(errorCtx.action),
expected,
reverseIdx,
perpetrator.display()
)
companion object { companion object {
@JvmStatic @JvmStatic
@ -60,7 +55,7 @@ class MishapInvalidIota(
else -> "unknown" else -> "unknown"
} }
return MishapInvalidIota(perpetrator, reverseIdx, TranslatableComponent(key)) return MishapInvalidIota(perpetrator, reverseIdx, key.asTranslatedComponent)
} }
} }
} }

View file

@ -5,7 +5,6 @@ import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.Widget import at.petrak.hexcasting.api.spell.Widget
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType import at.petrak.hexcasting.api.spell.casting.ResolvedPatternType
import net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
class MishapInvalidPattern : Mishap() { class MishapInvalidPattern : Mishap() {
@ -18,6 +17,6 @@ class MishapInvalidPattern : Mishap() {
stack.add(SpellDatum.make(Widget.GARBAGE)) stack.add(SpellDatum.make(Widget.GARBAGE))
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("invalid_pattern") error("invalid_pattern")
} }

View file

@ -1,10 +1,9 @@
package at.petrak.hexcasting.api.spell.mishaps 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.misc.FrozenColorizer
import net.minecraft.Util import net.minecraft.Util
import net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
/** /**
@ -15,10 +14,11 @@ class MishapInvalidSpellDatumType(val perpetrator: Any) : Mishap() {
dyeColor(DyeColor.BLACK) dyeColor(DyeColor.BLACK)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
// Send it twice, just to make it clear
val msg = this.errorMessage(ctx, errorCtx) val msg = this.errorMessage(ctx, errorCtx)
ctx.caster.sendMessage(msg, Util.NIL_UUID) 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) error("invalid_spell_datum_type", this.perpetrator.toString(), this.perpetrator.javaClass.typeName)
} }

View file

@ -1,10 +1,9 @@
package at.petrak.hexcasting.api.spell.mishaps 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 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.resources.ResourceLocation
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
@ -16,7 +15,7 @@ class MishapLocationInWrongDimension(val properDimension: ResourceLocation) : Mi
stack.add(SpellDatum.make(Widget.GARBAGE)) stack.add(SpellDatum.make(Widget.GARBAGE))
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("wrong_dimension", actionName(errorCtx.action!!), properDimension.toString(), error("wrong_dimension", actionName(errorCtx.action), properDimension.toString(),
ctx.world.dimension().location().toString()) ctx.world.dimension().location().toString())
} }

View file

@ -1,13 +1,9 @@
package at.petrak.hexcasting.api.spell.mishaps package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext 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.DyeColor
import net.minecraft.world.item.ItemStack
import net.minecraft.world.phys.Vec3 import net.minecraft.world.phys.Vec3
class MishapLocationTooFarAway(val location: Vec3, val type: String = "too_far") : Mishap() { 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) dyeColor(DyeColor.MAGENTA)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
// Knock the player's items out of their hands yeetHeldItemsTowards(ctx, location)
val items = mutableListOf<ItemStack>()
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)
}
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("location_$type", SpellDatum.make(location).display(), actionName(errorCtx.action!!)) error("location_$type", SpellDatum.make(location).display(), actionName(errorCtx.action))
} }

View file

@ -1,10 +1,9 @@
package at.petrak.hexcasting.api.spell.mishaps 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.misc.FrozenColorizer
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
class MishapNoAkashicRecord(val pos: BlockPos) : Mishap() { class MishapNoAkashicRecord(val pos: BlockPos) : Mishap() {
@ -15,6 +14,6 @@ class MishapNoAkashicRecord(val pos: BlockPos) : Mishap() {
ctx.caster.giveExperiencePoints(-100) 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()) error("no_akashic_record", pos.toShortString())
} }

View file

@ -3,7 +3,6 @@ package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.misc.FrozenColorizer
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 net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
class MishapNoSpellCircle : Mishap() { class MishapNoSpellCircle : Mishap() {
@ -14,6 +13,6 @@ class MishapNoSpellCircle : Mishap() {
ctx.caster.inventory.dropAll() 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)) error("no_spell_circle", actionName(errorCtx.action))
} }

View file

@ -16,6 +16,6 @@ class MishapNotEnoughArgs(val expected: Int, val got: Int) : Mishap() {
stack.add(SpellDatum.make(Widget.GARBAGE)) 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) error("not_enough_args", actionName(errorCtx.action), expected, got)
} }

View file

@ -15,11 +15,10 @@ class MishapOthersName(val other: Player) : Mishap() {
dyeColor(DyeColor.BLACK) dyeColor(DyeColor.BLACK)
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
val effect = MobEffectInstance(MobEffects.BLINDNESS, 20 * 60) ctx.caster.addEffect(MobEffectInstance(MobEffects.BLINDNESS, 20 * 60))
ctx.caster.addEffect(effect)
} }
override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error("others_name", other.name) error("others_name", other.name)
companion object { companion object {

View file

@ -14,6 +14,6 @@ class MishapTooManyCloseParens : Mishap() {
stack.add(SpellDatum.make(errorCtx.pattern)) 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") error("too_many_close_parens")
} }

View file

@ -3,9 +3,9 @@ package at.petrak.hexcasting.api.spell.mishaps
import at.petrak.hexcasting.api.misc.FrozenColorizer import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.DatumType import at.petrak.hexcasting.api.spell.DatumType
import at.petrak.hexcasting.api.spell.SpellDatum 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.Widget
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import net.minecraft.network.chat.Component
import net.minecraft.world.item.DyeColor import net.minecraft.world.item.DyeColor
/** /**
@ -19,13 +19,17 @@ class MishapUnescapedValue(
override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) { override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList<SpellDatum<*>>) {
val idx = stack.indexOfLast { it.getType() == DatumType.LIST } val idx = stack.indexOfLast { it.getType() == DatumType.LIST }
if (idx != -1) if (idx != -1) {
stack[idx] = SpellDatum.make(Widget.GARBAGE) 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 = override fun errorMessage(ctx: CastingContext, errorCtx: Context) =
error( error("unescaped", perpetrator.display())
"unescaped",
perpetrator.display()
)
} }

View file

@ -3,9 +3,11 @@ package at.petrak.hexcasting.api.utils
import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.math.HexCoord import at.petrak.hexcasting.api.spell.math.HexCoord
import net.minecraft.ChatFormatting
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.ListTag import net.minecraft.nbt.ListTag
import net.minecraft.nbt.LongArrayTag import net.minecraft.nbt.LongArrayTag
import net.minecraft.network.chat.*
import net.minecraft.world.InteractionHand import net.minecraft.world.InteractionHand
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
import net.minecraft.world.phys.Vec2 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()) 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<SpellDatum<*>>.serializeToNBT(): ListTag { fun Iterable<SpellDatum<*>>.serializeToNBT(): ListTag {
val tag = ListTag() val tag = ListTag()
for (elt in this) for (elt in this)

View file

@ -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.HexCoord
import at.petrak.hexcasting.api.spell.math.HexDir import at.petrak.hexcasting.api.spell.math.HexDir
import at.petrak.hexcasting.api.spell.math.HexPattern 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.api.utils.otherHand
import at.petrak.hexcasting.client.drawPatternFromPoints import at.petrak.hexcasting.client.drawPatternFromPoints
import at.petrak.hexcasting.client.drawSpot 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.renderer.GameRenderer
import net.minecraft.client.resources.sounds.SimpleSoundInstance import net.minecraft.client.resources.sounds.SimpleSoundInstance
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TextComponent
import net.minecraft.sounds.SoundSource import net.minecraft.sounds.SoundSource
import net.minecraft.util.Mth import net.minecraft.util.Mth
import net.minecraft.world.InteractionHand import net.minecraft.world.InteractionHand
@ -38,7 +38,7 @@ class GuiSpellcasting(
private val handOpenedWith: InteractionHand, private val handOpenedWith: InteractionHand,
private var patterns: MutableList<ResolvedPattern>, private var patterns: MutableList<ResolvedPattern>,
private var stackDescs: List<Component> private var stackDescs: List<Component>
) : Screen(TextComponent("")) { ) : Screen("gui.hexcasting.spellcasting".asTranslatedComponent) {
private var drawState: PatternDrawState = PatternDrawState.BetweenPatterns private var drawState: PatternDrawState = PatternDrawState.BetweenPatterns
private val usedSpots: MutableSet<HexCoord> = HashSet() private val usedSpots: MutableSet<HexCoord> = HashSet()

View file

@ -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.casting.SpellContinuation
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota
import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs 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.abs
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -20,7 +20,7 @@ object OpLastNToList : Operator {
throw MishapInvalidIota( throw MishapInvalidIota(
datum, datum,
0, 0,
TranslatableComponent("hexcasting.mishap.invalid_value.int.between", 0, stack.size) "hexcasting.mishap.invalid_value.int.between".asTranslatedComponent(0, stack.size)
) )
} }
val output = mutableListOf<SpellDatum<*>>() val output = mutableListOf<SpellDatum<*>>()

View file

@ -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.casting.CastingContext
import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota 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 import kotlin.math.acos
object OpArcCos : ConstManaOperator { object OpArcCos : ConstManaOperator {
@ -19,7 +19,7 @@ object OpArcCos : ConstManaOperator {
throw MishapInvalidIota( throw MishapInvalidIota(
SpellDatum.make(value), SpellDatum.make(value),
0, 0,
TranslatableComponent("hexcasting.mishap.invalid_value.double.between", -1, 1) "hexcasting.mishap.invalid_value.double.between".asTranslatedComponent(-1, 1)
) )
return acos(value).asSpellResult return acos(value).asSpellResult
} }

View file

@ -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.casting.CastingContext
import at.petrak.hexcasting.api.spell.getChecked import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota 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 import kotlin.math.asin
object OpArcSin : ConstManaOperator { object OpArcSin : ConstManaOperator {
@ -19,7 +19,7 @@ object OpArcSin : ConstManaOperator {
throw MishapInvalidIota( throw MishapInvalidIota(
SpellDatum.make(value), SpellDatum.make(value),
0, 0,
TranslatableComponent("hexcasting.mishap.invalid_value.double.between", -1, 1) "hexcasting.mishap.invalid_value.double.between".asTranslatedComponent(-1, 1)
) )
return asin(value).asSpellResult return asin(value).asSpellResult
} }

View file

@ -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.getChecked
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota
import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.utils.asTranslatedComponent
import it.unimi.dsi.fastutil.ints.IntArrayList import it.unimi.dsi.fastutil.ints.IntArrayList
import net.minecraft.network.chat.TranslatableComponent
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.ln import kotlin.math.ln
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -32,7 +32,7 @@ object OpAlwinfyHasAscendedToABeingOfPureMath : Operator {
throw MishapInvalidIota( throw MishapInvalidIota(
stack.last(), stack.last(),
0, 0,
TranslatableComponent("hexcasting.mishap.invalid_value.int", 0) "hexcasting.mishap.invalid_value.int".asTranslatedComponent(0)
) )
stack.removeLast() stack.removeLast()
val code = codeDouble.roundToInt() val code = codeDouble.roundToInt()

View file

@ -1,11 +1,11 @@
package at.petrak.hexcasting.common.casting.operators.stack package at.petrak.hexcasting.common.casting.operators.stack
import at.petrak.hexcasting.api.spell.ConstManaOperator 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota 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.abs
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -20,7 +20,7 @@ object OpDuplicateN : ConstManaOperator {
throw MishapInvalidIota( throw MishapInvalidIota(
args[1], args[1],
0, 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() val count = countDouble.roundToInt()

View file

@ -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.OperationResult
import at.petrak.hexcasting.api.spell.Operator 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.SpellDatum
import at.petrak.hexcasting.api.spell.casting.CastingContext import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.casting.SpellContinuation 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.MishapInvalidIota
import at.petrak.hexcasting.api.spell.mishaps.MishapNotEnoughArgs 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.abs
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -28,7 +28,7 @@ object OpFisherman : Operator {
throw MishapInvalidIota( throw MishapInvalidIota(
datum, datum,
0, 0,
TranslatableComponent("hexcasting.mishap.invalid_value.int.between", 1, stack.size) "hexcasting.mishap.invalid_value.int.between".asTranslatedComponent(1, stack.size)
) )
} }

View file

@ -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.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)", "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": "Hexcasting Research",
"advancement.hexcasting:root.desc": "Find a concentrated form of media growing deep beneath the earth.", "advancement.hexcasting:root.desc": "Find a concentrated form of media growing deep beneath the earth.",
"advancement.hexcasting:enlightenment": "Achieve Enlightenment", "advancement.hexcasting:enlightenment": "Achieve Enlightenment",