Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Sep 13, 2020
1 parent 4429a46 commit 4eb348a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/main/java/the_fireplace/audiobook/Audiobook.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
21 changes: 10 additions & 11 deletions src/main/java/the_fireplace/audiobook/AudiobookLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> pages = Lists.newArrayListWithCapacity(contents.getPageCount());
for(int i=0;i<contents.getPageCount();i++)
pages.add(contents.getPage(i).getString());
playPauseBook(pages);
playBook(pages);
}

public static void playPauseBook(@Nullable List<String> 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<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/audiobook/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"key.audiobook.read_book": "Read/Cancel Audiobook"
"key.audiobook.read_book": "Read Audiobook",
"key.audiobook.stop_book": "Cancel Narration"
}

0 comments on commit 4eb348a

Please sign in to comment.