From 35e6b3f7667bea26194037969e7b5d8e9fff4da7 Mon Sep 17 00:00:00 2001 From: "petrak@" Date: Mon, 15 May 2023 12:19:53 -0500 Subject: [PATCH] spell circle error handling is a bit sus --- .../circles/BlockEntityAbstractImpetus.java | 42 ++++++++++++------- .../api/casting/eval/env/CircleCastEnv.java | 15 +++++++ .../common/blocks/circles/BlockSlate.java | 8 ++-- .../hexcasting/lang/en_us.flatten.json5 | 2 + 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/circles/BlockEntityAbstractImpetus.java b/Common/src/main/java/at/petrak/hexcasting/api/casting/circles/BlockEntityAbstractImpetus.java index d4bd60f2..e11b8f73 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/circles/BlockEntityAbstractImpetus.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/circles/BlockEntityAbstractImpetus.java @@ -79,16 +79,17 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen public void clearError() { this.errorMsg = null; this.errorDisplay = null; + this.sync(); } public void postError(Component error, ItemStack display) { this.errorMsg = error; this.errorDisplay = display; + this.sync(); } public void postMishap(Component mishapDisplay) { - this.errorMsg = mishapDisplay; - this.errorDisplay = new ItemStack(Items.MUSIC_DISC_11); + this.postError(mishapDisplay, new ItemStack(Items.MUSIC_DISC_11)); } //region execution @@ -98,7 +99,7 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen return; this.setChanged(); - + var state = this.getExecutionState(); if (state == null) { return; @@ -109,15 +110,14 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen if (!shouldContinue) { this.endExecution(); this.executionState = null; - } - else + } else this.level.scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), state.getTickSpeed()); } - + public void endExecution() { if (this.executionState == null) return; - + this.executionState.endExecution(this); } @@ -143,7 +143,7 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen return; // TODO: error here? if (this.level.isClientSide) return; // TODO: error here? - + if (this.executionState != null) { return; } @@ -151,12 +151,14 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen if (this.executionState == null) return; - + var serverLevel = (ServerLevel) this.level; - - serverLevel.scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), this.executionState.getTickSpeed()); - - serverLevel.setBlockAndUpdate(this.getBlockPos(), this.getBlockState().setValue(BlockCircleComponent.ENERGIZED, true)); + + serverLevel.scheduleTick(this.getBlockPos(), this.getBlockState().getBlock(), + this.executionState.getTickSpeed()); + + serverLevel.setBlockAndUpdate(this.getBlockPos(), + this.getBlockState().setValue(BlockCircleComponent.ENERGIZED, true)); } @Contract(pure = true) @@ -263,11 +265,19 @@ public abstract class BlockEntityAbstractImpetus extends HexBlockEntity implemen this.lazyExecutionState = null; } - if (tag.contains(TAG_MEDIA, Tag.TAG_INT)) { - this.media = tag.getInt(TAG_MEDIA); - } else if (tag.contains(TAG_MEDIA, Tag.TAG_LONG)) { + if (tag.contains(TAG_MEDIA, Tag.TAG_LONG)) { this.media = tag.getLong(TAG_MEDIA); } + + if (tag.contains(TAG_ERROR_MSG, Tag.TAG_STRING) && tag.contains(TAG_ERROR_DISPLAY, Tag.TAG_COMPOUND)) { + var msg = Component.Serializer.fromJson(tag.getString(TAG_ERROR_MSG)); + var display = ItemStack.of(tag.getCompound(TAG_ERROR_DISPLAY)); + this.errorMsg = msg; + this.errorDisplay = display; + } else { + this.errorMsg = null; + this.errorDisplay = null; + } } public void applyScryingLensOverlay(List> lines, diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java index f05391d8..ae642014 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/CircleCastEnv.java @@ -7,6 +7,7 @@ import at.petrak.hexcasting.api.casting.circles.CircleExecutionState; import at.petrak.hexcasting.api.casting.eval.CastResult; import at.petrak.hexcasting.api.casting.eval.CastingEnvironment; import at.petrak.hexcasting.api.casting.eval.MishapEnvironment; +import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect; import at.petrak.hexcasting.api.pigment.FrozenPigment; import at.petrak.hexcasting.common.lib.HexItems; import net.minecraft.Util; @@ -63,6 +64,20 @@ public class CircleCastEnv extends CastingEnvironment { var soundPos = this.execState.currentPos; this.world.playSound(null, soundPos, sound, SoundSource.PLAYERS, 1f, 1f); } + + // TODO: this is gonna bite us in the bum someday + // we check whether we should cut the execution in BlockSlate, but post the mishap here; + // although everything should be pretty immutable here it's something to keep in mind + // classic time-of-check/time-of-use + var imp = this.getImpetus(); + if (imp != null) { + for (var sideEffect : result.getSideEffects()) { + if (sideEffect instanceof OperatorSideEffect.DoMishap doMishap) { + var msg = doMishap.getMishap().errorMessageWithName(this, doMishap.getErrorCtx()); + imp.postMishap(msg); + } + } + } } @Override diff --git a/Common/src/main/java/at/petrak/hexcasting/common/blocks/circles/BlockSlate.java b/Common/src/main/java/at/petrak/hexcasting/common/blocks/circles/BlockSlate.java index c0937fa6..e28c72ca 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/blocks/circles/BlockSlate.java +++ b/Common/src/main/java/at/petrak/hexcasting/common/blocks/circles/BlockSlate.java @@ -94,9 +94,11 @@ public class BlockSlate extends BlockCircleComponent implements EntityBlock, Sim var vm = new CastingVM(imageIn, env); var result = vm.queueExecuteAndWrapIota(new PatternIota(pattern), world); - // TODO: make mishaps actually halt execution - - return new ControlFlow.Continue(vm.getImage(), exitDirs.toList()); + if (result.getResolutionType().getSuccess()) { + return new ControlFlow.Continue(vm.getImage(), exitDirs.toList()); + } else { + return new ControlFlow.Stop(); + } } @Override diff --git a/Common/src/main/resources/assets/hexcasting/lang/en_us.flatten.json5 b/Common/src/main/resources/assets/hexcasting/lang/en_us.flatten.json5 index 38b9311d..620860cf 100644 --- a/Common/src/main/resources/assets/hexcasting/lang/en_us.flatten.json5 +++ b/Common/src/main/resources/assets/hexcasting/lang/en_us.flatten.json5 @@ -286,6 +286,8 @@ "brainsweep.min_level": "Level %s or higher", "brainsweep.level": "Level %s", "brainsweep.product": "Mindless Body", + + "media": "%d dust" }, // ^ tooltip spelldata: {