From 412e9ba11c38f1188fed9d4f31850ff8067f6052 Mon Sep 17 00:00:00 2001 From: The_Fireplace Date: Sun, 13 Sep 2020 18:41:29 -0500 Subject: [PATCH] Play books without opening the book GUI --- .../audiobook/AudiobookLogic.java | 2 +- .../audiobook/mixin/MinecraftClientMixin.java | 34 +++++++++++++++++++ src/main/resources/audiobook.mixins.json | 3 +- src/main/resources/fabric.mod.json | 3 -- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java diff --git a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java index 0b438aa..84d62c4 100644 --- a/src/main/java/the_fireplace/audiobook/AudiobookLogic.java +++ b/src/main/java/the_fireplace/audiobook/AudiobookLogic.java @@ -10,7 +10,7 @@ public class AudiobookLogic { public static void playBook(ItemStack stack) { - playBook(stack.getTag() != null ? BookScreen.readPages(stack.getTag()) : null); + playBook(stack.hasTag() && stack.getTag() != null ? BookScreen.readPages(stack.getTag()) : null); } public static void playBook(BookScreen.Contents contents) { diff --git a/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java b/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java new file mode 100644 index 0000000..e2b6fb1 --- /dev/null +++ b/src/main/java/the_fireplace/audiobook/mixin/MinecraftClientMixin.java @@ -0,0 +1,34 @@ +package the_fireplace.audiobook.mixin; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ingame.BookScreen; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.item.BookItem; +import net.minecraft.util.Hand; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import the_fireplace.audiobook.Audiobook; +import the_fireplace.audiobook.AudiobookLogic; + +import javax.annotation.Nullable; + +@Mixin(MinecraftClient.class) +public abstract class MinecraftClientMixin { + + @Shadow @Nullable public ClientPlayerEntity player; + + @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 BookItem && player.getStackInHand(Hand.MAIN_HAND).hasTag() && player.getStackInHand(Hand.MAIN_HAND).getTag().contains("pages") && !BookScreen.readPages(player.getStackInHand(Hand.MAIN_HAND).getTag()).isEmpty()) + AudiobookLogic.playBook(player.getStackInHand(Hand.MAIN_HAND)); + else if(player.getStackInHand(Hand.OFF_HAND).getItem() instanceof BookItem) + AudiobookLogic.playBook(player.getStackInHand(Hand.OFF_HAND)); + } else if(Audiobook.stopAudiobookKey.isPressed()) + AudiobookLogic.stopNarration(); + } +} diff --git a/src/main/resources/audiobook.mixins.json b/src/main/resources/audiobook.mixins.json index 867f359..2bcd5fc 100644 --- a/src/main/resources/audiobook.mixins.json +++ b/src/main/resources/audiobook.mixins.json @@ -4,7 +4,8 @@ "package": "the_fireplace.audiobook.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ - "BookScreenMixin" + "BookScreenMixin", + "MinecraftClientMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 605cf0e..133520f 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -21,9 +21,6 @@ "entrypoints": { "client": [ "the_fireplace.audiobook.Audiobook" - ], - "modmenu": [ - "the_fireplace.audiobook.ModMenuIntegration" ] }, "mixins": [