| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.MessageBox;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.text2speech.Narrator;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.client.gui.components.toasts.SystemToast;
|
| 10 | import net.minecraft.client.gui.components.toasts.ToastManager;
|
| 11 | import net.minecraft.client.main.SilentInitException;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.sounds.SoundSource;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class GameNarrator {
|
| 19 | public static final Component NO_TITLE = CommonComponents.EMPTY;
|
| 20 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 21 | private final Minecraft minecraft;
|
| 22 | private final Narrator narrator = Narrator.getNarrator();
|
| 23 |
|
| 24 | public GameNarrator(final Minecraft minecraft) {
|
| 25 | this.minecraft = minecraft;
|
| 26 | }
|
| 27 |
|
| 28 | public void sayChatQueued(final Component message) {
|
| 29 | if (this.getStatus().shouldNarrateChat()) {
|
| 30 | this.narrateNotInterruptingMessage(message);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public void saySystemChatQueued(final Component message) {
|
| 35 | if (this.getStatus().shouldNarrateSystemOrChat()) {
|
| 36 | this.narrateNotInterruptingMessage(message);
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public void saySystemQueued(final Component message) {
|
| 41 | if (this.getStatus().shouldNarrateSystem()) {
|
| 42 | this.narrateNotInterruptingMessage(message);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | private void narrateNotInterruptingMessage(final Component message) {
|
| 47 | String messageString = message.getString();
|
| 48 | if (!messageString.isEmpty()) {
|
| 49 | this.logNarratedMessage(messageString);
|
| 50 | this.narrateMessage(messageString, false);
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | public void saySystemNow(final Component message) {
|
| 55 | this.saySystemNow(message.getString());
|
| 56 | }
|
| 57 |
|
| 58 | public void saySystemNow(final String message) {
|
| 59 | if (this.getStatus().shouldNarrateSystem() && !message.isEmpty()) {
|
| 60 | this.logNarratedMessage(message);
|
| 61 | if (this.narrator.active()) {
|
| 62 | this.narrator.clear();
|
| 63 | this.narrateMessage(message, true);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | private void narrateMessage(final String message, final boolean interrupt) {
|
| 69 | this.narrator.say(message, interrupt, this.minecraft.options.getFinalSoundSourceVolume(SoundSource.VOICE));
|
| 70 | }
|
| 71 |
|
| 72 | private NarratorStatus getStatus() {
|
| 73 | return this.minecraft.options.narrator().get();
|
| 74 | }
|
| 75 |
|
| 76 | private void logNarratedMessage(final String message) {
|
| 77 | if (SharedConstants.IS_RUNNING_IN_IDE) {
|
| 78 | LOGGER.debug("Narrating: {}", message.replaceAll("\n", "\\\\n"));
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | public void updateNarratorStatus(final NarratorStatus status) {
|
| 83 | this.clear();
|
| 84 | this.narrateMessage(Component.translatable("options.narrator").append(" : ").append(status.getName()).getString(), true);
|
| 85 | ToastManager toastManager = Minecraft.getInstance().gui.toastManager();
|
| 86 | if (this.narrator.active()) {
|
| 87 | if (status == NarratorStatus.OFF) {
|
| 88 | SystemToast.addOrUpdate(toastManager, SystemToast.SystemToastId.NARRATOR_TOGGLE, Component.translatable("narrator.toast.disabled"), null);
|
| 89 | } else {
|
| 90 | SystemToast.addOrUpdate(toastManager, SystemToast.SystemToastId.NARRATOR_TOGGLE, Component.translatable("narrator.toast.enabled"), status.getName());
|
| 91 | }
|
| 92 | } else {
|
| 93 | SystemToast.addOrUpdate(
|
| 94 | toastManager,
|
| 95 | SystemToast.SystemToastId.NARRATOR_TOGGLE,
|
| 96 | Component.translatable("narrator.toast.disabled"),
|
| 97 | Component.translatable("options.narrator.notavailable")
|
| 98 | );
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | public boolean isActive() {
|
| 103 | return this.narrator.active();
|
| 104 | }
|
| 105 |
|
| 106 | public void clear() {
|
| 107 | if (this.getStatus() != NarratorStatus.OFF && this.narrator.active()) {
|
| 108 | this.narrator.clear();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | public void destroy() {
|
| 113 | this.narrator.destroy();
|
| 114 | }
|
| 115 |
|
| 116 | public void checkStatus(final boolean requiredActive) {
|
| 117 | if (requiredActive
|
| 118 | && !this.isActive()
|
| 119 | && !MessageBox.errorWithContinue(
|
| 120 | "Failed to initialize text-to-speech library. Do you want to continue?\nIf this problem persists, please report it at bugs.mojang.com"
|
| 121 | )) {
|
| 122 | throw new GameNarrator.NarratorInitException("Narrator library is not active");
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | @Environment(EnvType.CLIENT)
|
| 127 | public static class NarratorInitException extends SilentInitException {
|
| 128 | public NarratorInitException(final String message) {
|
| 129 | super(message);
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|