Skip to content

Commit

Permalink
Document the API somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Jan 12, 2022
1 parent 00ab406 commit e1c4a24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 40 additions & 2 deletions src/main/java/io/github/ladysnake/blabber/Blabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -38,18 +41,53 @@ public static Identifier id(String path) {
return new Identifier(MOD_ID, path);
}

/**
* Starts a dialogue
*
* <p>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}.
*
* <p>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<? extends DialogueAction> codec) {
Registry.register(BlabberRegistrar.ACTION_REGISTRY, actionId, codec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import java.util.List;

public class CutsceneDialogueScreen extends HandledScreen<DialogueScreenHandler> {
public class BlabberDialogueScreen extends HandledScreen<DialogueScreenHandler> {
public static final int MIN_RENDER_Y = 40;
public static final int TITLE_GAP = 20;
public static final int CHOICE_GAP = 5;
Expand All @@ -42,7 +42,7 @@ public class CutsceneDialogueScreen extends HandledScreen<DialogueScreenHandler>
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerDialogueTracker> KEY = ComponentRegistry.getOrCreate(Blabber.id("dialogue_tracker"), PlayerDialogueTracker.class);

Expand Down Expand Up @@ -61,8 +63,8 @@ public void endDialogue() {
}
}

public DialogueStateMachine getCurrentDialogue() {
return this.currentDialogue;
public Optional<DialogueStateMachine> getCurrentDialogue() {
return Optional.ofNullable(this.currentDialogue);
}

@Override
Expand Down

0 comments on commit e1c4a24

Please sign in to comment.