Skip to content

Commit

Permalink
Refactor for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Jul 20, 2021
1 parent 1b178f0 commit b13a60a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/main/java/the_fireplace/audiobook/Audiobook.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/the_fireplace/audiobook/AudiobookLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -34,4 +39,21 @@ public static void playBook(@Nullable List<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Boolean> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,20 +15,22 @@

import javax.annotation.Nullable;

@Environment(EnvType.CLIENT)
@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 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();
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"fabricloader": ">=0.7.4",
"fabric": "*",
"fabric-key-binding-api-v1": "*",
"minecraft": ">=1.14.4"
"minecraft": ">=1.14.4 <1.17"
}
}

0 comments on commit b13a60a

Please sign in to comment.