From ce6950f561c93e18159a76e11d223f93ebb1321b Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Thu, 2 May 2024 00:59:39 +0200 Subject: [PATCH] Ensure starting state action runs as expected (cherry picked from commit 3b3bdb2222e661e3da3e8c5b4d48d89f782eef5e) --- changelog.md | 6 ++++++ gradle.properties | 2 +- .../blabber/impl/common/PlayerDialogueTracker.java | 14 ++++++++------ .../impl/common/machine/DialogueStateMachine.java | 4 ++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index d0b0f489..86a1b1bf 100644 --- a/changelog.md +++ b/changelog.md @@ -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 ------------------------------------------------------ diff --git a/gradle.properties b/gradle.properties index 358344fb..0aa1a52e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java b/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java index da7a4f44..9b037f1e 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java @@ -63,15 +63,16 @@ 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 { @@ -79,6 +80,7 @@ private void startDialogue0(Identifier id, DialogueTemplate template, @Nullable 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; @@ -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(), @@ -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; @@ -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); } } diff --git a/src/main/java/org/ladysnake/blabber/impl/common/machine/DialogueStateMachine.java b/src/main/java/org/ladysnake/blabber/impl/common/machine/DialogueStateMachine.java index 70aca76b..6f971f2f 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/machine/DialogueStateMachine.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/machine/DialogueStateMachine.java @@ -179,6 +179,10 @@ public boolean isAvailable(int choice) { return this.conditionalChoices.getOrDefault(this.currentStateKey, Int2BooleanMaps.EMPTY_MAP).getOrDefault(choice, true); } + public Optional> getStartAction() { + return this.getStates().get(this.template.start()).action(); + } + /** * @throws IllegalStateException if making an invalid choice */