Skip to content

Commit

Permalink
Improve error when a predicate is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed May 1, 2024
1 parent 3b3bdb2 commit f2808c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ public void serverTick() {
}
}

ChoiceAvailabilityPacket update = this.updateConditions(serverPlayer, this.currentDialogue);
try {
ChoiceAvailabilityPacket update = this.updateConditions(serverPlayer, this.currentDialogue);

if (update != null) {
ServerPlayNetworking.send(serverPlayer, update);
if (update != null) {
ServerPlayNetworking.send(serverPlayer, update);
}
} catch (CommandSyntaxException e) {
throw new IllegalStateException("Error while updating dialogue conditions", e);
}
}
}
Expand All @@ -191,7 +195,7 @@ private void tryResumeDialogue(Identifier id, DialogueTemplate template, String
}
}

private @Nullable ChoiceAvailabilityPacket updateConditions(ServerPlayerEntity player, DialogueStateMachine currentDialogue) {
private @Nullable ChoiceAvailabilityPacket updateConditions(ServerPlayerEntity player, DialogueStateMachine currentDialogue) throws CommandSyntaxException {
if (currentDialogue.hasConditions()) {
return currentDialogue.updateConditions(new LootContext.Builder(
new LootContextParameterSet.Builder(player.getServerWorld())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.ladysnake.blabber.impl.common.machine;

import com.google.common.collect.ImmutableList;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
import it.unimi.dsi.fastutil.ints.Int2BooleanMaps;
import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
Expand Down Expand Up @@ -51,6 +53,7 @@
import java.util.stream.IntStream;

public final class DialogueStateMachine {
private static final DynamicCommandExceptionType INVALID_PREDICATE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("blabber:commands.dialogue.start.predicate.invalid", String.valueOf(id)));

private final Identifier id;
private final DialogueTemplate template;
Expand Down Expand Up @@ -130,7 +133,7 @@ public boolean hasConditions() {
return !this.conditionalChoices.isEmpty();
}

public @Nullable ChoiceAvailabilityPacket updateConditions(LootContext context) {
public @Nullable ChoiceAvailabilityPacket updateConditions(LootContext context) throws CommandSyntaxException {
ChoiceAvailabilityPacket ret = null;
for (Map.Entry<String, Int2BooleanMap> conditionalState : this.conditionalChoices.entrySet()) {
List<DialogueChoice> availableChoices = getStates().get(conditionalState.getKey()).choices();
Expand All @@ -139,7 +142,7 @@ public boolean hasConditions() {
LootCondition condition = context.getWorld().getServer().getLootManager().getElement(
LootDataType.PREDICATES, predicateId
);
if (condition == null) throw new IllegalStateException("Could not find predicate " + predicateId);
if (condition == null) throw INVALID_PREDICATE_EXCEPTION.create(predicateId);
boolean testResult = runTest(condition, context);
if (testResult != conditionalChoice.setValue(testResult)) {
if (ret == null) ret = new ChoiceAvailabilityPacket();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/blabber/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"blabber:commands.dialogue.start.invalid": "There is no dialogue with type \"%s\"",
"blabber:commands.dialogue.start.predicate.invalid": "There is no predicate with type \"%s\"",
"blabber:commands.dialogue.start.success": "Initiated dialogue \"%s\" for %s",
"blabber:dialogue.instructions": "Hover or press [%s]/[%s] to select a choice, then click or press [%s] to confirm it",
"blabber:dialogue.locked_choice": "This choice has unfulfilled prerequisites",
Expand Down

0 comments on commit f2808c2

Please sign in to comment.