diff --git a/gradle.properties b/gradle.properties index 4dcf449..ca03819 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.12.12 # Mod Properties - mod_version = 0.1.0-SNAPSHOT + mod_version = 0.1.0 maven_group = io.github.ladysnake archives_base_name = blabber diff --git a/src/main/java/io/github/ladysnake/blabber/Blabber.java b/src/main/java/io/github/ladysnake/blabber/Blabber.java index 1a0f779..5d28946 100644 --- a/src/main/java/io/github/ladysnake/blabber/Blabber.java +++ b/src/main/java/io/github/ladysnake/blabber/Blabber.java @@ -21,14 +21,17 @@ import io.github.ladysnake.blabber.impl.common.BlabberCommand; import io.github.ladysnake.blabber.impl.common.BlabberRegistrar; import io.github.ladysnake.blabber.impl.common.CommandDialogueAction; +import io.github.ladysnake.blabber.impl.common.DialogueStateMachine; import io.github.ladysnake.blabber.impl.common.PlayerDialogueTracker; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.Nullable; public final class Blabber implements ModInitializer { public static final String MOD_ID = "blabber"; @@ -38,18 +41,53 @@ public static Identifier id(String path) { return new Identifier(MOD_ID, path); } + /** + * Starts a dialogue + * + *

This operation closes the player's {@linkplain PlayerEntity#currentScreenHandler current screen handler}, + * if any, and opens a new dialogue screen instead. + * + * @param player the player for whom to initiate a dialogue + * @param id the identifier for the dialogue + */ public static void startDialogue(ServerPlayerEntity player, Identifier id) { PlayerDialogueTracker.get(player).startDialogue(id); } - public static void endDialogue(ServerPlayerEntity player) { - PlayerDialogueTracker.get(player).endDialogue(); + /** + * Ends the current dialogue if its id equals {@code expectedDialogue}. + * + *

If the identifiers match, the current dialogue will be ended no matter + * its state. + * + * @param player the player for whom to end the current dialogue + * @param expectedDialogue the identifier being compared to the current dialogue, or {@code null} to end any ongoing dialogue + */ + public static void endDialogue(ServerPlayerEntity player, @Nullable Identifier expectedDialogue) { + Identifier currentDialogueId = PlayerDialogueTracker.get(player).getCurrentDialogue().map(DialogueStateMachine::getId).orElse(null); + if (currentDialogueId != null && (expectedDialogue == null || expectedDialogue.equals(currentDialogueId))) { + PlayerDialogueTracker.get(player).endDialogue(); + } } + /** + * Register a basic {@link DialogueAction} to handle dialogue choices. + * + * @param actionId the identifier used to reference the action in dialogue definition files + * @param action the action to run when triggered by a player + * @see #registerAction(Identifier, Codec) + */ public static void registerAction(Identifier actionId, DialogueAction action) { registerAction(actionId, Codec.unit(action)); } + /** + * Register a configurable {@link DialogueAction} to handle dialogue choices. + * + * @param actionId the identifier used to reference the action in dialogue definition files + * @param codec a codec for deserializing dialogue actions using the given value + * @see #registerAction(Identifier, DialogueAction) + */ public static void registerAction(Identifier actionId, Codec codec) { Registry.register(BlabberRegistrar.ACTION_REGISTRY, actionId, codec); } diff --git a/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberClient.java b/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberClient.java index 43014f8..354625f 100644 --- a/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberClient.java +++ b/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberClient.java @@ -28,7 +28,7 @@ public final class BlabberClient implements ClientModInitializer { @Override public void onInitializeClient() { - ScreenRegistry.register(BlabberRegistrar.DIALOGUE_SCREEN_HANDLER, CutsceneDialogueScreen::new); + ScreenRegistry.register(BlabberRegistrar.DIALOGUE_SCREEN_HANDLER, BlabberDialogueScreen::new); } public static void sendDialogueActionMessage(int choice) { diff --git a/src/main/java/io/github/ladysnake/blabber/impl/client/CutsceneDialogueScreen.java b/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberDialogueScreen.java similarity index 97% rename from src/main/java/io/github/ladysnake/blabber/impl/client/CutsceneDialogueScreen.java rename to src/main/java/io/github/ladysnake/blabber/impl/client/BlabberDialogueScreen.java index 95961c2..086186d 100644 --- a/src/main/java/io/github/ladysnake/blabber/impl/client/CutsceneDialogueScreen.java +++ b/src/main/java/io/github/ladysnake/blabber/impl/client/BlabberDialogueScreen.java @@ -33,7 +33,7 @@ import java.util.List; -public class CutsceneDialogueScreen extends HandledScreen { +public class BlabberDialogueScreen extends HandledScreen { public static final int MIN_RENDER_Y = 40; public static final int TITLE_GAP = 20; public static final int CHOICE_GAP = 5; @@ -42,7 +42,7 @@ public class CutsceneDialogueScreen extends HandledScreen private int selectedChoice; private boolean hoveringChoice; - public CutsceneDialogueScreen(DialogueScreenHandler handler, PlayerInventory inventory, Text title) { + public BlabberDialogueScreen(DialogueScreenHandler handler, PlayerInventory inventory, Text title) { super(handler, inventory, title); } diff --git a/src/main/java/io/github/ladysnake/blabber/impl/common/PlayerDialogueTracker.java b/src/main/java/io/github/ladysnake/blabber/impl/common/PlayerDialogueTracker.java index 6529fd1..9f95685 100644 --- a/src/main/java/io/github/ladysnake/blabber/impl/common/PlayerDialogueTracker.java +++ b/src/main/java/io/github/ladysnake/blabber/impl/common/PlayerDialogueTracker.java @@ -29,6 +29,8 @@ import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import java.util.Optional; + public final class PlayerDialogueTracker implements ServerTickingComponent { public static final ComponentKey KEY = ComponentRegistry.getOrCreate(Blabber.id("dialogue_tracker"), PlayerDialogueTracker.class); @@ -61,8 +63,8 @@ public void endDialogue() { } } - public DialogueStateMachine getCurrentDialogue() { - return this.currentDialogue; + public Optional getCurrentDialogue() { + return Optional.ofNullable(this.currentDialogue); } @Override