Skip to content

Commit

Permalink
Add support for items in inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Dianliang233 committed Mar 30, 2024
1 parent b002c6e commit 8923587
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/client/java/wiki/mc/rtfw/FTFWClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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_")) {
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions src/client/java/wiki/mc/rtfw/mixin/HandledScreenMixin.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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);
}
}
}
}
}
7 changes: 6 additions & 1 deletion src/client/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/client/resources/rtfw.client.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"required": true,
"package": "wiki.mc.rtfw.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"HandledScreenMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 8923587

Please sign in to comment.