diff --git a/src/main/java/the_fireplace/audiobook/Audiobook.java b/src/main/java/the_fireplace/audiobook/Audiobook.java index 231ec6d..fb36e54 100644 --- a/src/main/java/the_fireplace/audiobook/Audiobook.java +++ b/src/main/java/the_fireplace/audiobook/Audiobook.java @@ -1,23 +1,20 @@ package the_fireplace.audiobook; -import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; -import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.options.KeyBinding; +import net.minecraft.client.util.InputUtil; import java.awt.event.KeyEvent; public class Audiobook implements ClientModInitializer { public static final String MODID = "audiobook"; - public static ModConfig config; public static KeyBinding audiobookKey; + public static KeyBinding stopAudiobookKey; @Override public void onInitializeClient() { - AutoConfig.register(ModConfig.class, JanksonConfigSerializer::new); - config = AutoConfig.getConfigHolder(ModConfig.class).getConfig(); - audiobookKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.audiobook.read_book", KeyEvent.VK_H, "key.categories.misc")); + stopAudiobookKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.audiobook.stop_book", InputUtil.UNKNOWN_KEY.getCode(), "key.categories.misc")); } } diff --git a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java index 772ac31..0b438aa 100644 --- a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java +++ b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java @@ -9,28 +9,27 @@ import java.util.List; public class AudiobookLogic { - private static boolean playing = false; - public static void playPauseBook(ItemStack stack) { - playPauseBook(stack.getTag() != null ? BookScreen.readPages(stack.getTag()) : null); + public static void playBook(ItemStack stack) { + playBook(stack.getTag() != null ? BookScreen.readPages(stack.getTag()) : null); } - public static void playPauseBook(BookScreen.Contents contents) { + public static void playBook(BookScreen.Contents contents) { List pages = Lists.newArrayListWithCapacity(contents.getPageCount()); for(int i=0;i pages) { - if(playing && Narrator.getNarrator().active()) { - playing = false; - Narrator.getNarrator().clear(); - } else if(pages != null && !pages.isEmpty()) { - playing = true; + public static void playBook(@Nullable List pages) { + if(pages != null && !pages.isEmpty()) { StringBuilder output = new StringBuilder(); for(String page: pages) output.append(" ").append(page); Narrator.getNarrator().say(output.toString(), true); } } + + public static void stopNarration() { + Narrator.getNarrator().clear(); + } } diff --git a/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java b/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java index 6ca5930..e28351e 100644 --- a/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java +++ b/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java @@ -15,8 +15,11 @@ public abstract class BookScreenMixin { @Inject(at = @At(value="HEAD"), method = "keyPressed", cancellable = true) private void keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable infoReturnable) { - if(Audiobook.audiobookKey.isPressed()) { - AudiobookLogic.playPauseBook(contents); + if(Audiobook.audiobookKey.matchesKey(keyCode, scanCode)) { + AudiobookLogic.playBook(contents); + infoReturnable.setReturnValue(true); + } else if(Audiobook.stopAudiobookKey.matchesKey(keyCode, scanCode)) { + AudiobookLogic.stopNarration(); infoReturnable.setReturnValue(true); } } diff --git a/src/main/resources/assets/audiobook/lang/en_us.json b/src/main/resources/assets/audiobook/lang/en_us.json index 284c524..b425880 100644 --- a/src/main/resources/assets/audiobook/lang/en_us.json +++ b/src/main/resources/assets/audiobook/lang/en_us.json @@ -1,3 +1,4 @@ { - "key.audiobook.read_book": "Read/Cancel Audiobook" + "key.audiobook.read_book": "Read Audiobook", + "key.audiobook.stop_book": "Cancel Narration" }