From 155c467ab94cdfb42a8718d4d501012806d6c399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E7=84=B0?= Date: Sun, 14 Jun 2026 23:26:20 +0800 Subject: [PATCH] Fix stale entity iotas read from holders --- .../at/petrak/hexcasting/api/casting/ActionUtils.kt | 11 ++++++----- .../hexcasting/api/casting/iota/EntityIota.java | 3 +-- .../api/casting/mishaps/MishapInternalException.kt | 2 +- .../hexcasting/common/casting/actions/rw/OpRead.kt | 3 ++- .../common/casting/actions/rw/OpTheCoolerRead.kt | 3 ++- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/ActionUtils.kt b/Common/src/main/java/at/petrak/hexcasting/api/casting/ActionUtils.kt index d67e0693b4..2255286893 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/ActionUtils.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/ActionUtils.kt @@ -4,6 +4,7 @@ package at.petrak.hexcasting.api.casting import at.petrak.hexcasting.api.casting.iota.* import at.petrak.hexcasting.api.casting.math.HexPattern +import at.petrak.hexcasting.api.casting.mishaps.MishapEntityNotFound import at.petrak.hexcasting.api.casting.mishaps.MishapInvalidIota import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs import at.petrak.hexcasting.api.utils.asTranslatedComponent @@ -37,7 +38,7 @@ fun List.getDouble(idx: Int, argc: Int = 0): Double { fun List.getEntity(level: ServerLevel, idx: Int, argc: Int = 0): Entity { val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) } if (x is EntityIota) { - return x.getEntity(level) + return x.getEntity(level) ?: throw MishapEntityNotFound(x.entityId, x.entityName) } else { throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity") } @@ -84,7 +85,7 @@ fun List.getBool(idx: Int, argc: Int = 0): Boolean { fun List.getItemEntity(level: ServerLevel, idx: Int, argc: Int = 0): ItemEntity { val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) } if (x is EntityIota) { - val e = x.getEntity(level) + val e = x.getEntity(level) ?: throw MishapEntityNotFound(x.entityId, x.entityName) if (e is ItemEntity) return e } @@ -94,7 +95,7 @@ fun List.getItemEntity(level: ServerLevel, idx: Int, argc: Int = 0): ItemE fun List.getPlayer(level: ServerLevel, idx: Int, argc: Int = 0): ServerPlayer { val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) } if (x is EntityIota) { - val e = x.getEntity(level) + val e = x.getEntity(level) ?: throw MishapEntityNotFound(x.entityId, x.entityName) if (e is ServerPlayer) return e } @@ -104,7 +105,7 @@ fun List.getPlayer(level: ServerLevel, idx: Int, argc: Int = 0): ServerPla fun List.getMob(level: ServerLevel, idx: Int, argc: Int = 0): Mob { val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) } if (x is EntityIota) { - val e = x.getEntity(level) + val e = x.getEntity(level) ?: throw MishapEntityNotFound(x.entityId, x.entityName) if (e is Mob) return e } @@ -114,7 +115,7 @@ fun List.getMob(level: ServerLevel, idx: Int, argc: Int = 0): Mob { fun List.getLivingEntityButNotArmorStand(level: ServerLevel, idx: Int, argc: Int = 0): LivingEntity { val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) } if (x is EntityIota) { - val e = x.getEntity(level) + val e = x.getEntity(level) ?: throw MishapEntityNotFound(x.entityId, x.entityName) if (e is LivingEntity && e !is ArmorStand) return e } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/iota/EntityIota.java b/Common/src/main/java/at/petrak/hexcasting/api/casting/iota/EntityIota.java index 47d0c769ae..dadf90cfd0 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/iota/EntityIota.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/iota/EntityIota.java @@ -21,7 +21,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.lang.ref.WeakReference; import java.util.Optional; import java.util.UUID; @@ -44,7 +43,7 @@ public UUID getEntityId() { return entityId; } - public Entity getEntity(ServerLevel level) { + public @Nullable Entity getEntity(ServerLevel level) { return level.getEntity(entityId); } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/mishaps/MishapInternalException.kt b/Common/src/main/java/at/petrak/hexcasting/api/casting/mishaps/MishapInternalException.kt index 7301c01127..098326f1f0 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/mishaps/MishapInternalException.kt +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/mishaps/MishapInternalException.kt @@ -14,5 +14,5 @@ class MishapInternalException(val exception: Exception) : Mishap() { } override fun errorMessage(ctx: CastingEnvironment, errorCtx: Context) = - error("unknown", exception) + error("unknown", exception.toString()) } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpRead.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpRead.kt index 0c8087b4b3..9ae097d882 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpRead.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpRead.kt @@ -4,6 +4,7 @@ import at.petrak.hexcasting.api.casting.castables.ConstMediaAction import at.petrak.hexcasting.api.casting.eval.CastingEnvironment import at.petrak.hexcasting.api.casting.iota.Iota import at.petrak.hexcasting.api.casting.mishaps.MishapBadOffhandItem +import at.petrak.hexcasting.api.utils.validateIota import at.petrak.hexcasting.xplat.IXplatAbstractions object OpRead : ConstMediaAction { @@ -28,6 +29,6 @@ object OpRead : ConstMediaAction { ?: datumHolder.emptyIota() ?: throw MishapBadOffhandItem.of(handStack, "iota.read") - return listOf(datum) + return listOf(validateIota(datum, env.world)) } } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpTheCoolerRead.kt b/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpTheCoolerRead.kt index 060ebe9d77..dfbaf4675c 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpTheCoolerRead.kt +++ b/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/rw/OpTheCoolerRead.kt @@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.casting.eval.CastingEnvironment import at.petrak.hexcasting.api.casting.getEntity import at.petrak.hexcasting.api.casting.iota.Iota import at.petrak.hexcasting.api.casting.mishaps.MishapBadEntity +import at.petrak.hexcasting.api.utils.validateIota import at.petrak.hexcasting.xplat.IXplatAbstractions object OpTheCoolerRead : ConstMediaAction { @@ -24,6 +25,6 @@ object OpTheCoolerRead : ConstMediaAction { val datum = datumHolder.readIota() ?: datumHolder.emptyIota() ?: throw MishapBadEntity.of(target, "iota.read") - return listOf(datum) + return listOf(validateIota(datum, env.world)) } }