Skip to content

Commit

Permalink
Ensure starting state action runs as expected
Browse files Browse the repository at this point in the history
(cherry picked from commit 3b3bdb2)
  • Loading branch information
Pyrofab committed May 1, 2024
1 parent e7f7917 commit ce6950f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
------------------------------------------------------
Version 1.5.1-mc1.20.1
------------------------------------------------------
**Fixes**
- Actions for the starting state now run as expected when a dialogue is started

------------------------------------------------------
Version 1.5.0-mc1.20.1
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx2G
java_version=17

# Mod Properties
mod_version = 1.5.0-mc1.20.1
mod_version = 1.5.1-mc1.20.1
maven_group = org.ladysnake
archives_base_name = blabber

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,24 @@ public static PlayerDialogueTracker get(PlayerEntity player) {
public void startDialogue(Identifier id, @Nullable Entity interlocutor) throws CommandSyntaxException {
DialogueTemplate template = DialogueRegistry.getOrEmpty(id)
.orElseThrow(() -> new IllegalArgumentException("Unknown dialogue " + id));
this.startDialogue0(
DialogueStateMachine currentDialogue = this.startDialogue0(
id,
template,
template.start(),
interlocutor
);
currentDialogue.getStartAction().ifPresent(a -> a.action().handle((ServerPlayerEntity) this.player, interlocutor));
}

private void startDialogue0(Identifier id, DialogueTemplate template, @Nullable String start, @Nullable Entity interlocutor) throws CommandSyntaxException {
private DialogueStateMachine startDialogue0(Identifier id, DialogueTemplate template, @Nullable String start, @Nullable Entity interlocutor) throws CommandSyntaxException {
ServerPlayerEntity serverPlayer = ((ServerPlayerEntity) this.player);
this.interlocutor = interlocutor;
try {
DialogueTemplate parsedTemplate = template.parseText(CommandDialogueAction.getSource(serverPlayer), serverPlayer);
this.currentDialogue = new DialogueStateMachine(id, parsedTemplate, start);
this.updateConditions(serverPlayer, this.currentDialogue);
this.openDialogueScreen();
return this.currentDialogue;
} catch (CommandSyntaxException e) {
this.interlocutor = null;
throw e;
Expand Down Expand Up @@ -109,7 +111,7 @@ public void updateDialogue() {
this.endDialogue();

DialogueRegistry.getOrEmpty(oldDialogue.getId())
.ifPresent(template -> this.tryStartDialogue(
.ifPresent(template -> this.tryResumeDialogue(
oldDialogue.getId(),
template,
oldDialogue.getCurrentStateKey(),
Expand Down Expand Up @@ -157,7 +159,7 @@ public void serverTick() {
} else {
interlocutor = null;
}
tryStartDialogue(saved.dialogueId(), saved.template(), saved.selectedState(), interlocutor);
tryResumeDialogue(saved.dialogueId(), saved.template(), saved.selectedState(), interlocutor);
}
this.resumptionAttempts = 0;
this.deserializedState = null;
Expand Down Expand Up @@ -185,11 +187,11 @@ public void serverTick() {
}
}

private void tryStartDialogue(Identifier id, DialogueTemplate template, String selectedState, Entity interlocutor) {
private void tryResumeDialogue(Identifier id, DialogueTemplate template, String selectedState, Entity interlocutor) {
try {
this.startDialogue0(id, template, selectedState, interlocutor);
} catch (CommandSyntaxException e) {
Blabber.LOGGER.error("(Blabber) Failed to load dialogue template " + id, e);
Blabber.LOGGER.error("(Blabber) Failed to load dialogue template {}", id, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public boolean isAvailable(int choice) {
return this.conditionalChoices.getOrDefault(this.currentStateKey, Int2BooleanMaps.EMPTY_MAP).getOrDefault(choice, true);
}

public Optional<InstancedDialogueAction<?>> getStartAction() {
return this.getStates().get(this.template.start()).action();
}

/**
* @throws IllegalStateException if making an invalid choice
*/
Expand Down

0 comments on commit ce6950f

Please sign in to comment.