From 89235873ee8fff9e04fba55a7001fbce344e3552 Mon Sep 17 00:00:00 2001 From: Dianliang233 Date: Sat, 30 Mar 2024 23:12:29 +0800 Subject: [PATCH] Add support for items in inventory --- src/client/java/wiki/mc/rtfw/FTFWClient.java | 28 ++++++++------ .../mc/rtfw/mixin/HandledScreenMixin.java | 37 +++++++++++++++++++ src/client/resources/fabric.mod.json | 7 +++- src/client/resources/rtfw.client.mixins.json | 11 ++++++ 4 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/client/java/wiki/mc/rtfw/mixin/HandledScreenMixin.java create mode 100644 src/client/resources/rtfw.client.mixins.json diff --git a/src/client/java/wiki/mc/rtfw/FTFWClient.java b/src/client/java/wiki/mc/rtfw/FTFWClient.java index 33ef4e21..e5f36174 100644 --- a/src/client/java/wiki/mc/rtfw/FTFWClient.java +++ b/src/client/java/wiki/mc/rtfw/FTFWClient.java @@ -18,6 +18,12 @@ import java.nio.charset.StandardCharsets; public class FTFWClient implements ClientModInitializer { + public static KeyBinding readKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.rtfw.open", // The translation key of the keybinding's name + InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. + GLFW.GLFW_KEY_SEMICOLON, // The keycode of the key + "category.rtfw.rtfw" // The translation key of the keybinding's category. + )); + public static @Nullable URI buildUri(String pageName, String language) { String solvedLanguage = null; if (language.startsWith("de_")) { @@ -60,35 +66,35 @@ public class FTFWClient implements ClientModInitializer { var client = MinecraftClient.getInstance(); var hit = client.crosshairTarget; + if (hit == null) return null; + switch (hit.getType()) { case MISS: break; case BLOCK: var blockHit = (BlockHitResult) hit; var blockPos = blockHit.getBlockPos(); - var blockState = client.world.getBlockState(blockPos); - var block = blockState.getBlock(); - return block.getName().getString(); + if (client.world != null) { + var blockState = client.world.getBlockState(blockPos); + var block = blockState.getBlock(); + return block.getName().getString(); + } + break; case ENTITY: var entityHit = (EntityHitResult) hit; var entity = entityHit.getEntity(); return entity.getName().getString(); } + return null; } @Override public void onInitializeClient() { - var readKey = KeyBindingHelper.registerKeyBinding(new KeyBinding( - "key.rtfw.open", // The translation key of the keybinding's name - InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse. - GLFW.GLFW_KEY_SEMICOLON, // The keycode of the key - "category.rtfw.rtfw" // The translation key of the keybinding's category. - )); - ClientTickEvents.END_CLIENT_TICK.register(client -> { while (readKey.wasPressed()) { - var pageName = getPageNameByRaycast(); + String pageName = getPageNameByRaycast(); + if (pageName != null) { var uri = buildUri(pageName, client.options.language); Util.getOperatingSystem().open(uri); diff --git a/src/client/java/wiki/mc/rtfw/mixin/HandledScreenMixin.java b/src/client/java/wiki/mc/rtfw/mixin/HandledScreenMixin.java new file mode 100644 index 00000000..a95d3e5a --- /dev/null +++ b/src/client/java/wiki/mc/rtfw/mixin/HandledScreenMixin.java @@ -0,0 +1,37 @@ +package wiki.mc.rtfw.mixin; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.screen.slot.Slot; +import net.minecraft.util.Util; +import org.jetbrains.annotations.Nullable; +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.CallbackInfoReturnable; +import wiki.mc.rtfw.FTFWClient; + +import java.net.URI; + +@Mixin(HandledScreen.class) +public class HandledScreenMixin { + @Shadow + @Nullable + protected Slot focusedSlot; + + @Inject(method = "keyPressed", at = @At("HEAD")) + public void keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { + if (FTFWClient.readKey.matchesKey(keyCode, scanCode)) { + Slot slot = this.focusedSlot; + if (slot != null && slot.hasStack()) { + String pageName = slot.getStack().getItem().getName().getString(); + String language = MinecraftClient.getInstance().getLanguageManager().getLanguage(); + URI uri = FTFWClient.buildUri(pageName, language); + if (uri != null) { + Util.getOperatingSystem().open(uri); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/resources/fabric.mod.json b/src/client/resources/fabric.mod.json index 1c84ec2e..f904976d 100644 --- a/src/client/resources/fabric.mod.json +++ b/src/client/resources/fabric.mod.json @@ -19,7 +19,12 @@ "wiki.mc.rtfw.FTFWClient" ] }, - "mixins": [], + "mixins": [ + { + "config": "rtfw.client.mixins.json", + "environment": "client" + } + ], "depends": { "fabricloader": ">=0.15.7", "minecraft": "~1.20.4", diff --git a/src/client/resources/rtfw.client.mixins.json b/src/client/resources/rtfw.client.mixins.json new file mode 100644 index 00000000..e43a540a --- /dev/null +++ b/src/client/resources/rtfw.client.mixins.json @@ -0,0 +1,11 @@ +{ + "required": true, + "package": "wiki.mc.rtfw.mixin", + "compatibilityLevel": "JAVA_17", + "client": [ + "HandledScreenMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file