Skip to content

Commit ace0863

Browse files
committed
Add GuiItemInfoEvent
1 parent 39f748c commit ace0863

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023-2025 Fox2Code
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package com.fox2code.foxloader.event.client;
25+
26+
import net.minecraft.client.gui.GuiScreen;
27+
import net.minecraft.common.item.ItemStack;
28+
29+
import java.util.ArrayList;
30+
import java.util.Collections;
31+
import java.util.List;
32+
33+
public final class GuiItemInfoEvent extends GuiScreenEvent {
34+
private final ItemStack itemStack;
35+
private List<String> description;
36+
37+
public GuiItemInfoEvent(GuiScreen guiScreen, ItemStack itemStack, List<String> description) {
38+
super(guiScreen);
39+
this.itemStack = itemStack;
40+
this.description = description;
41+
}
42+
43+
public ItemStack getItemStack() {
44+
return this.itemStack;
45+
}
46+
47+
public List<String> getDescription() {
48+
return this.description;
49+
}
50+
51+
public void setDescription(List<String> description) {
52+
this.description = description == null ?
53+
Collections.emptyList() : description;
54+
}
55+
56+
public void addDescriptionLine(String line) {
57+
try {
58+
this.description.add(line);
59+
} catch (UnsupportedOperationException e) {
60+
this.description = new ArrayList<>(this.description);
61+
this.description.add(line);
62+
}
63+
}
64+
65+
public void addDescriptionLine(int index, String line) {
66+
int size = this.description.size();
67+
if (index < 0 || index > size) {
68+
throw new IndexOutOfBoundsException("Index: " + index);
69+
}
70+
try {
71+
this.description.add(index, line);
72+
} catch (UnsupportedOperationException e) {
73+
this.description = new ArrayList<>(this.description);
74+
this.description.add(index, line);
75+
}
76+
}
77+
78+
public boolean removeDescriptionLine(String line) {
79+
try {
80+
return this.description.remove(line);
81+
} catch (UnsupportedOperationException e) {
82+
this.description = new ArrayList<>(this.description);
83+
return this.description.remove(line);
84+
}
85+
}
86+
}

loader/src/main/java/com/fox2code/foxloader/internal/InternalScreenHooks.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,31 @@
2424
package com.fox2code.foxloader.internal;
2525

2626
import com.fox2code.foxevents.EventHolder;
27+
import com.fox2code.foxloader.event.client.GuiItemInfoEvent;
2728
import com.fox2code.foxloader.event.client.GuiScreenInitEvent;
2829
import net.minecraft.client.gui.GuiElement;
2930
import net.minecraft.client.gui.GuiScreen;
31+
import net.minecraft.common.item.ItemStack;
3032

3133
import java.util.List;
3234

3335
public final class InternalScreenHooks {
3436
private static final EventHolder<GuiScreenInitEvent> GUI_SCREEN_INIT_EVENT =
3537
EventHolder.getHolderFromEvent(GuiScreenInitEvent.class);
38+
private static final EventHolder<GuiItemInfoEvent> GUI_ITEM_INFO_EVENT =
39+
EventHolder.getHolderFromEvent(GuiItemInfoEvent.class);
3640

3741
private InternalScreenHooks() {}
3842

3943
public static void onGuiScreenInitHook(GuiScreen guiScreen, List<GuiElement> controlList) {
4044
if (GUI_SCREEN_INIT_EVENT.isEmpty()) return;
4145
GUI_SCREEN_INIT_EVENT.callEvent(new GuiScreenInitEvent(guiScreen, controlList));
4246
}
47+
48+
public static List<String> onGuiGetItemInfoHook(List<String> description, GuiScreen guiScreen, ItemStack itemStack) {
49+
if (GUI_ITEM_INFO_EVENT.isEmpty()) return description;
50+
GuiItemInfoEvent guiItemInfoEvent = new GuiItemInfoEvent(guiScreen, itemStack, description);
51+
GUI_ITEM_INFO_EVENT.callEvent(guiItemInfoEvent);
52+
return guiItemInfoEvent.getDescription();
53+
}
4354
}

patching/src/main/java/com/fox2code/foxloader/patching/game/GuiScreenPatch.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.objectweb.asm.tree.*;
2929

3030
public final class GuiScreenPatch extends GamePatch {
31+
private static final String ItemStack = "net/minecraft/common/item/ItemStack";
3132
private static final String Minecraft = "net/minecraft/client/Minecraft";
3233
private static final String GuiDebug = "net/minecraft/client/gui/GuiDebug";
3334
private static final String GuiScreen = "net/minecraft/client/gui/GuiScreen";
@@ -108,6 +109,18 @@ private static void patchGuiScreen(ClassNode classNode) {
108109
insnList.add(new MethodInsnNode(INVOKESTATIC, InternalScreenHooks,
109110
"onGuiScreenInitHook", "(L" + GuiScreen + ";Ljava/util/List;)V"));
110111
TransformerUtils.insertToEndOfCode(setWorldAndResolution, insnList);
112+
MethodNode getItemInfo = TransformerUtils.getMethod(classNode, "getItemInfo");
113+
for (AbstractInsnNode abstractInsnNode : getItemInfo.instructions) {
114+
if (abstractInsnNode.getOpcode() == ARETURN && abstractInsnNode.getPrevious().getOpcode() != ACONST_NULL) {
115+
InsnList itemDescHook = new InsnList();
116+
itemDescHook.add(new VarInsnNode(ALOAD, 0));
117+
itemDescHook.add(new VarInsnNode(ALOAD, 1));
118+
itemDescHook.add(new MethodInsnNode(INVOKESTATIC, InternalScreenHooks, "onGuiGetItemInfoHook",
119+
"(Ljava/util/List;L" + GuiScreen + ";L" + ItemStack + ";)Ljava/util/List;", false));
120+
getItemInfo.instructions.insertBefore(abstractInsnNode, itemDescHook);
121+
break;
122+
}
123+
}
111124
}
112125

113126
private void patchGuiContainer(ClassNode classNode) {

0 commit comments

Comments
 (0)