From b13a60a5c17d9b87f0b11da30cc08bf30a9661c0 Mon Sep 17 00:00:00 2001 From: The_Fireplace Date: Tue, 20 Jul 2021 17:55:43 -0500 Subject: [PATCH] Refactor for better readability --- .../the_fireplace/audiobook/Audiobook.java | 5 +++- .../audiobook/AudiobookLogic.java | 24 ++++++++++++++++++- .../audiobook/mixin/BookScreenMixin.java | 7 ++++-- .../audiobook/mixin/MinecraftClientMixin.java | 17 ++++++------- src/main/resources/fabric.mod.json | 2 +- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/the_fireplace/audiobook/Audiobook.java b/src/main/java/the_fireplace/audiobook/Audiobook.java index 5425f07..bd14fdc 100644 --- a/src/main/java/the_fireplace/audiobook/Audiobook.java +++ b/src/main/java/the_fireplace/audiobook/Audiobook.java @@ -1,12 +1,15 @@ package the_fireplace.audiobook; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.minecraft.client.options.KeyBinding; import java.awt.event.KeyEvent; -public class Audiobook implements ClientModInitializer { +@Environment(EnvType.CLIENT) +public final class Audiobook implements ClientModInitializer { public static final String MODID = "audiobook"; public static KeyBinding audiobookKey; public static KeyBinding stopAudiobookKey; diff --git a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java index f3c95b1..cc25c03 100644 --- a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java +++ b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java @@ -2,13 +2,18 @@ import com.google.common.collect.Lists; import com.mojang.text2speech.Narrator; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.gui.screen.ingame.BookScreen; import net.minecraft.item.ItemStack; +import net.minecraft.item.WritableBookItem; +import net.minecraft.item.WrittenBookItem; import javax.annotation.Nullable; import java.util.List; -public class AudiobookLogic { +@Environment(EnvType.CLIENT) +public final class AudiobookLogic { public static void playBook(ItemStack stack) { playBook(stack.hasTag() && stack.getTag() != null ? BookScreen.Contents.create(stack) : null); } @@ -34,4 +39,21 @@ public static void playBook(@Nullable List pages) { public static void stopNarration() { Narrator.getNarrator().clear(); } + + public static boolean isReadableBook(ItemStack stack) { + return isReadable(stack) && isBook(stack); + } + + private static boolean isReadable(ItemStack stack) { + if (!stack.hasTag()) { + return false; + } + assert stack.getTag() != null; + + return !BookScreen.readPages(stack.getTag()).isEmpty(); + } + + private static boolean isBook(ItemStack stack) { + return stack.getItem() instanceof WrittenBookItem || stack.getItem() instanceof WritableBookItem; + } } diff --git a/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java b/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java index e28351e..b6d5e45 100644 --- a/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java +++ b/src/main/java/the_fireplace/audiobook/mixin/BookScreenMixin.java @@ -1,5 +1,7 @@ package the_fireplace.audiobook.mixin; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.gui.screen.ingame.BookScreen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -9,16 +11,17 @@ import the_fireplace.audiobook.Audiobook; import the_fireplace.audiobook.AudiobookLogic; +@Environment(EnvType.CLIENT) @Mixin(BookScreen.class) public abstract class BookScreenMixin { @Shadow private BookScreen.Contents contents; @Inject(at = @At(value="HEAD"), method = "keyPressed", cancellable = true) private void keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable infoReturnable) { - if(Audiobook.audiobookKey.matchesKey(keyCode, scanCode)) { + if (Audiobook.audiobookKey.matchesKey(keyCode, scanCode)) { AudiobookLogic.playBook(contents); infoReturnable.setReturnValue(true); - } else if(Audiobook.stopAudiobookKey.matchesKey(keyCode, scanCode)) { + } else if (Audiobook.stopAudiobookKey.matchesKey(keyCode, scanCode)) { AudiobookLogic.stopNarration(); infoReturnable.setReturnValue(true); } diff --git a/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java b/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java index 48fee40..c88f555 100644 --- a/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java +++ b/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java @@ -1,10 +1,9 @@ package the_fireplace.audiobook.mixin; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.ingame.BookScreen; import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.item.WritableBookItem; -import net.minecraft.item.WrittenBookItem; import net.minecraft.util.Hand; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -16,6 +15,7 @@ import javax.annotation.Nullable; +@Environment(EnvType.CLIENT) @Mixin(MinecraftClient.class) public abstract class MinecraftClientMixin { @@ -23,13 +23,14 @@ public abstract class MinecraftClientMixin { @Inject(at = @At(value="HEAD"), method = "handleInputEvents") private void handleInputEvents(CallbackInfo info) { - if(Audiobook.audiobookKey.isPressed() && player != null) { - //noinspection ConstantConditions - if((player.getStackInHand(Hand.MAIN_HAND).getItem() instanceof WrittenBookItem || player.getStackInHand(Hand.MAIN_HAND).getItem() instanceof WritableBookItem) && player.getStackInHand(Hand.MAIN_HAND).hasTag() && !BookScreen.readPages(player.getStackInHand(Hand.MAIN_HAND).getTag()).isEmpty()) + if (Audiobook.audiobookKey.isPressed() && player != null) { + if (AudiobookLogic.isReadableBook(player.getStackInHand(Hand.MAIN_HAND))) { AudiobookLogic.playBook(player.getStackInHand(Hand.MAIN_HAND)); - else if(player.getStackInHand(Hand.OFF_HAND).getItem() instanceof WrittenBookItem || player.getStackInHand(Hand.OFF_HAND).getItem() instanceof WritableBookItem) + } else if (AudiobookLogic.isReadableBook(player.getStackInHand(Hand.OFF_HAND))) { AudiobookLogic.playBook(player.getStackInHand(Hand.OFF_HAND)); - } else if(Audiobook.stopAudiobookKey.isPressed()) + } + } else if(Audiobook.stopAudiobookKey.isPressed()) { AudiobookLogic.stopNarration(); + } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 0838997..2b0c047 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -31,6 +31,6 @@ "fabricloader": ">=0.7.4", "fabric": "*", "fabric-key-binding-api-v1": "*", - "minecraft": ">=1.14.4" + "minecraft": ">=1.14.4 <1.17" } }