Skip to content

Commit

Permalink
Raiiiiii
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Nov 13, 2024
1 parent 673e796 commit f4c0394
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 47 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
- Fixed a rare issue causing spells to fail when inserting map entries.
- Fixed an error in the serialization of running spells which has surfaced for the second time due to poor documentation. (@StellarWitch7)
- The offending function has been renamed and documentation clarifying its purpose has been added.
- Made the Folding Distortion support iteration over map fragments. (@StellarWitch7)
- Added a Creative-only variant of Knot which has infinite mana. Possible texture improvements pending. (@StellarWitch7)
- Fixed an issue where two registries with the same identifier were being loaded, causing issues on Neoforge. (@StellarWitch7)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deps.yarn=1.21+build.2
loader_version=0.15.11

# Mod Properties
mod_version=2.0.0-beta.1b
mod_version=2.0.0-beta.2
maven_group=dev.enjarai
archives_base_name=trickster

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public void tick() {
}
} catch (BlunderException blunder) {
error = Optional.of(blunder.createMessage()
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")"));
} catch (Exception e) {
error = Optional.of(Text.literal("Uncaught exception in spell: " + e.getMessage())
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")"));
}

error.ifPresent(e -> stack.set(ModComponents.SPELL_CORE, slot.fail(e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public void tick() {
}
} catch (BlunderException blunder) {
error = Optional.of(blunder.createMessage()
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")"));
} catch (Exception e) {
error = Optional.of(Text.literal("Uncaught exception in spell: " + e.getMessage())
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")"));
}

error.ifPresent(e -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean onRemoved(ServerWorld world, BlockPos pos, ItemStack stack) {

if (comp != null
&& !(comp.executor() instanceof ErroredSpellExecutor)
&& !comp.executor().getCurrentState().isDelayed()
&& !comp.executor().getDeepestState().isDelayed()
&& world.random.nextBoolean()) {
world.createExplosion(null, pos.getX(), pos.getY(), pos.getZ(), 5f, true, ExplosionSourceType.BLOCK);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ public void executeOffhand() {
ModCriteria.NAN_NUMBER.trigger((ServerPlayerEntity) player());

player().sendMessage(blunder.createMessage()
.append(" (").append(spell.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(spell.getDeepestState().formatStackTrace()).append(")"));
} catch (Exception e) {
player().sendMessage(Text.literal("Uncaught exception in spell: " + e.getMessage())
.append(" (").append(spell.getCurrentState().formatStackTrace()).append(")"));
.append(" (").append(spell.getDeepestState().formatStackTrace()).append(")"));
}

sendMessage(new Replace(result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.function.BiFunction;

public sealed interface SpellInstruction permits Fragment, EnterScopeInstruction, ExitScopeInstruction {
public static final Endec<Stack<SpellInstruction>> STACK_ENDEC = SerializedSpellInstruction.ENDEC.listOf().xmap((l) -> {
public static final Endec<Stack<SpellInstruction>> STACK_ENDEC = SerializedSpellInstruction.ENDEC.listOf().xmap(l -> {
var s = new Stack<SpellInstruction>();
s.addAll(l.stream().map(SerializedSpellInstruction::toDeserialized).toList());
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public SpellQueueResult queueAndCast(SpellSource source, SpellPart spell, List<F
(index, executor3) -> { });
return new SpellQueueResult(isDone.get()
? SpellQueueResult.Type.QUEUED_DONE
: SpellQueueResult.Type.QUEUED_STILL_RUNNING, executor.getCurrentState());
: SpellQueueResult.Type.QUEUED_STILL_RUNNING, executor.getDeepestState());
}
}
}

return new SpellQueueResult(SpellQueueResult.Type.NOT_QUEUED, executor.getCurrentState());
return new SpellQueueResult(SpellQueueResult.Type.NOT_QUEUED, executor.getDeepestState());
}

@Override
Expand Down Expand Up @@ -116,7 +116,7 @@ private boolean tryRun(SpellSource source, Int2ObjectMap.Entry<SpellExecutor> en
}
} catch (BlunderException blunder) {
var message = blunder.createMessage()
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")");
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")");

if (blunder instanceof NaNBlunder)
source.getPlayer().ifPresent(ModCriteria.NAN_NUMBER::trigger);
Expand All @@ -126,7 +126,7 @@ private boolean tryRun(SpellSource source, Int2ObjectMap.Entry<SpellExecutor> en
errorCallback.callTheBack(entry.getIntKey(), executor);
} catch (Exception e) {
var message = Text.literal("Uncaught exception in spell: " + e.getMessage())
.append(" (").append(executor.getCurrentState().formatStackTrace()).append(")");
.append(" (").append(executor.getDeepestState().formatStackTrace()).append(")");

Trickster.LOGGER.warn("Uncaught error in spell:", e);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class AtomicSpellExecutor implements SpellExecutor {
public static final StructEndec<AtomicSpellExecutor> ENDEC = StructEndecBuilder.of(
SpellPart.ENDEC.fieldOf("root", AtomicSpellExecutor::spell),
SpellPart.ENDEC.fieldOf("root", e -> e.root),
SpellInstruction.STACK_ENDEC.fieldOf("instructions", e -> e.instructions),
Fragment.ENDEC.listOf().fieldOf("inputs", e -> e.inputs),
Endec.INT.listOf().fieldOf("scope", e -> e.scope),
Expand Down Expand Up @@ -131,7 +131,7 @@ public int getLastRunExecutions() {
}

@Override
public ExecutionState getCurrentState() {
public ExecutionState getDeepestState() {
return state;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

public class DefaultSpellExecutor implements SpellExecutor {
public static final StructEndec<DefaultSpellExecutor> ENDEC = StructEndecBuilder.of(
SpellPart.ENDEC.fieldOf("root", DefaultSpellExecutor::spell),
SpellPart.ENDEC.fieldOf("root", e -> e.root),
SpellInstruction.STACK_ENDEC.fieldOf("instructions", e -> e.instructions),
Fragment.ENDEC.listOf().fieldOf("inputs", e -> e.inputs),
Endec.INT.listOf().fieldOf("scope", e -> e.scope),
ExecutionState.ENDEC.fieldOf("state", DefaultSpellExecutor::getCurrentState),
ExecutionState.ENDEC.fieldOf("state", DefaultSpellExecutor::getDeepestState),
EndecTomfoolery.safeOptionalOf(SpellExecutor.ENDEC).optionalFieldOf("child", e -> e.child, Optional.empty()),
EndecTomfoolery.safeOptionalOf(Fragment.ENDEC).optionalFieldOf("override_return_value", e -> e.overrideReturnValue, Optional.empty()),
DefaultSpellExecutor::new
Expand Down Expand Up @@ -180,7 +180,7 @@ public Optional<Fragment> run(SpellContext ctx) throws BlunderException {
}
}

protected Optional<Fragment> runChild(SpellContext ctx) {
private Optional<Fragment> runChild(SpellContext ctx) {
var result = child.flatMap(c -> c.run(ctx.source(), ctx.data()));

if (result.isPresent()) {
Expand All @@ -191,7 +191,7 @@ protected Optional<Fragment> runChild(SpellContext ctx) {
return result;
}

protected static SpellExecutor makeExecutor(SpellContext context, SpellInstruction inst, List<Fragment> args) throws BlunderException {
private static SpellExecutor makeExecutor(SpellContext context, SpellInstruction inst, List<Fragment> args) throws BlunderException {
return inst.makeFork(context, args);
}

Expand All @@ -201,8 +201,8 @@ public int getLastRunExecutions() {
}

@Override
public ExecutionState getCurrentState() {
return child.map(SpellExecutor::getCurrentState).orElse(state);
public ExecutionState getDeepestState() {
return child.map(SpellExecutor::getDeepestState).orElse(state);
}

//TODO: add way to turn this and all children into a SpellPart using MiscUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public int getLastRunExecutions() {
}

@Override
public ExecutionState getCurrentState() {
public ExecutionState getDeepestState() {
return new ExecutionState(List.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

public class FoldingSpellExecutor implements SpellExecutor {
public static final StructEndec<FoldingSpellExecutor> ENDEC = new BackwardCompatibleStructEndec<>(StructEndecBuilder.of(
ExecutionState.ENDEC.fieldOf("state", FoldingSpellExecutor::getCurrentState),
SpellPart.ENDEC.fieldOf("executable", FoldingSpellExecutor::spell),
ExecutionState.ENDEC.fieldOf("state", e -> e.state),
SpellPart.ENDEC.fieldOf("executable", e -> e.executable),
Fragment.ENDEC.fieldOf("last_result", e -> e.lastResult),
EndecTomfoolery.stackOf(Fragment.ENDEC).fieldOf("values", e -> e.values),
EndecTomfoolery.stackOf(Fragment.ENDEC).fieldOf("keys", e -> e.keys),
Fragment.ENDEC.fieldOf("previous", e -> e.previous),
EndecTomfoolery.safeOptionalOf(SpellExecutor.ENDEC).fieldOf("child", e -> e.child),
FoldingSpellExecutor::new
), StructEndecBuilder.of( // 2.0.0-beta.1 compat
ExecutionState.ENDEC.fieldOf("state", FoldingSpellExecutor::getCurrentState),
), StructEndecBuilder.of( // <=2.0.0-beta.1 compat
ExecutionState.ENDEC.fieldOf("state", FoldingSpellExecutor::getDeepestState),
SpellPart.ENDEC.fieldOf("executable", FoldingSpellExecutor::spell),
ListFragment.ENDEC.fieldOf("list", e -> (ListFragment) e.previous),
EndecTomfoolery.stackOf(Fragment.ENDEC).fieldOf("elements", executor -> executor.values),
Expand Down Expand Up @@ -65,7 +65,7 @@ private FoldingSpellExecutor(ExecutionState state, SpellPart executable, Fragmen
}

public FoldingSpellExecutor(SpellContext ctx, SpellPart executable, Fragment result, Stack<Fragment> values, Stack<Fragment> keys, Fragment previous) {
this(ctx.state(), executable, result, values, keys, previous, Optional.empty());
this(ctx.state().recurseOrThrow(List.of()), executable, result, values, keys, previous, Optional.empty());

if (values.size() != keys.size())
throw new IllegalStateException("FoldingSpellExecutor requires that the `values` and `keys` stack be of equal length!");
Expand Down Expand Up @@ -145,7 +145,7 @@ public int getLastRunExecutions() {
}

@Override
public ExecutionState getCurrentState() {
return child.map(SpellExecutor::getCurrentState).orElse(state);
public ExecutionState getDeepestState() {
return child.map(SpellExecutor::getDeepestState).orElse(state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ default Fragment singleTickRun(SpellContext context) throws BlunderException {
return run(context.source()).orElseThrow(ExecutionLimitReachedBlunder::new);
} catch (Exception e) {
context.state().getStacktrace().clear();
context.state().getStacktrace().addAll(getCurrentState().getStacktrace());
context.state().getStacktrace().addAll(getDeepestState().getStacktrace());
throw e;
}
}
Expand Down Expand Up @@ -70,7 +70,13 @@ default Optional<Fragment> run(SpellSource source) throws BlunderException {
*/
Optional<Fragment> run(SpellContext ctx) throws BlunderException;

/**
* @return the spell's executions in the last tick, or its child's if applicable.
*/
int getLastRunExecutions();

ExecutionState getCurrentState();
/**
* @return the spell's ExecutionState, or its child's if applicable.
*/
ExecutionState getDeepestState();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public class TryCatchSpellExecutor implements SpellExecutor {
TryCatchSpellExecutor::new
);

protected final SpellExecutor trySpell;
protected final SpellExecutor catchSpell;
protected boolean catching = false;
protected int lastRunExecutions;
private final SpellExecutor trySpell;
private final SpellExecutor catchSpell;
private boolean catching = false;

protected TryCatchSpellExecutor(SpellExecutor trySpell, SpellExecutor catchSpell, boolean catching) {
private TryCatchSpellExecutor(SpellExecutor trySpell, SpellExecutor catchSpell, boolean catching) {
this.trySpell = trySpell;
this.catchSpell = catchSpell;
this.catching = catching;
Expand All @@ -50,8 +49,6 @@ public SpellPart spell() {

@Override
public Optional<Fragment> run(SpellSource source, TickData data) throws BlunderException {
lastRunExecutions = 0;

if (catching)
return catchSpell.run(source);

Expand All @@ -74,11 +71,11 @@ public int getLastRunExecutions() {
}

@Override
public ExecutionState getCurrentState() {
return child().getCurrentState();
public ExecutionState getDeepestState() {
return child().getDeepestState();
}

protected SpellExecutor child() {
private SpellExecutor child() {
return catching ? catchSpell : trySpell;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,32 @@ letting it run concurrently with this spell. The used spell slot is returned, or

;;;;;

<|page-title@lavender:book_components|title=Note: Collections|>Collections are fragments which contain other fragments and may be accessed using a specific key.
Lists are collections where the key is a whole number between zero and the size of the list, exclusive.
Maps are also collections, though their keys may be any value and aren't automatically determined by order of insertion.

;;;;;

<|glyph@trickster:templates|trick-id=trickster:fold,title=Folding Distortion|>

spell, any[], any -> any[]
spell, collection, any -> any

---

For each item in the given list, execute the given spell, passing the given fragment to the first iteration.
For each entry in the collection, execute the given spell, with the given fragment as the first result.

;;;;;

Each iteration receives four arguments:

---

any, any, number, any[]
any, any, any, collection

---

Where the first argument is the result of the last iteration, the second is the current item, the third is its index in the given list,
and the fourth is the given list.
Where the first argument is the result of the last iteration, the second is the current value, the third is its key,
and the fourth is the given collection.

;;;;;

Expand Down

0 comments on commit f4c0394

Please sign in to comment.