Skip to content

Commit

Permalink
Play books without opening the book GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Sep 13, 2020
1 parent 4eb348a commit 412e9ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/the_fireplace/audiobook/AudiobookLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 2 additions & 1 deletion src/main/resources/audiobook.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"package": "the_fireplace.audiobook.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"BookScreenMixin"
"BookScreenMixin",
"MinecraftClientMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
"entrypoints": {
"client": [
"the_fireplace.audiobook.Audiobook"
],
"modmenu": [
"the_fireplace.audiobook.ModMenuIntegration"
]
},
"mixins": [
Expand Down

0 comments on commit 412e9ba

Please sign in to comment.