Skip to content

Commit dd29ab3

Browse files
committed
small ContainerInventoryScreen cleanup, a bit more to do
1 parent dbdb3f2 commit dd29ab3

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/main/java/meteordevelopment/meteorclient/gui/screens/ContainerInventoryScreen.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
import net.minecraft.component.type.BundleContentsComponent;
1414
import net.minecraft.entity.player.PlayerInventory;
1515
import net.minecraft.item.BundleItem;
16-
import net.minecraft.item.Item;
1716
import net.minecraft.item.ItemStack;
18-
import net.minecraft.item.tooltip.TooltipType;
1917
import net.minecraft.text.Text;
2018
import net.minecraft.util.Identifier;
19+
import net.minecraft.util.math.MathHelper;
20+
import org.lwjgl.glfw.GLFW;
2121

2222
import java.util.ArrayList;
23+
import java.util.Collections;
2324
import java.util.List;
2425

2526
import static meteordevelopment.meteorclient.MeteorClient.mc;
@@ -35,13 +36,11 @@ public class ContainerInventoryScreen extends Screen {
3536
private final List<ItemStack> containerItems;
3637
private final PlayerInventory playerInventory;
3738
private final int containerRows;
38-
private final Item.TooltipContext tooltipContext;
3939
private int x, y;
4040

4141
public ContainerInventoryScreen(ItemStack containerItem) {
4242
super(containerItem.getName());
4343
this.playerInventory = mc.player.getInventory();
44-
this.tooltipContext = Item.TooltipContext.create(mc.world);
4544

4645
this.containerItems = new ArrayList<>();
4746
if (containerItem.getItem() instanceof BundleItem) {
@@ -52,14 +51,10 @@ public ContainerInventoryScreen(ItemStack containerItem) {
5251
} else {
5352
ItemStack[] tempItems = new ItemStack[64];
5453
Utils.getItemsInContainerItem(containerItem, tempItems);
55-
for (ItemStack stack : tempItems) {
56-
if (stack != null && !stack.isEmpty()) {
57-
containerItems.add(stack);
58-
}
59-
}
54+
Collections.addAll(containerItems, tempItems);
6055
}
6156

62-
this.containerRows = Math.max(1, (containerItems.size() + 8) / 9);
57+
this.containerRows = Math.max(1, MathHelper.ceilDiv(containerItems.size(), 9));
6358
}
6459

6560
@Override
@@ -77,13 +72,15 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
7772
int baseY = y + 18;
7873
int playerY = baseY + containerRows * SLOT_SIZE + 20;
7974

75+
// drawing the slot textures
8076
for (int row = 0; row < containerRows + 4; row++) {
8177
for (int col = 0; col < 9; col++) {
8278
int slotY = row < containerRows ? baseY + row * SLOT_SIZE : playerY + (row - containerRows) * SLOT_SIZE;
8379
context.drawGuiTexture(RenderPipelines.GUI_TEXTURED, SLOT_TEXTURE, baseX + col * SLOT_SIZE, slotY, SLOT_SIZE, SLOT_SIZE);
8480
}
8581
}
8682

83+
// drawing the container items
8784
for (int i = 0; i < containerItems.size(); i++) {
8885
ItemStack item = containerItems.get(i);
8986
if (!item.isEmpty()) {
@@ -94,6 +91,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
9491
}
9592
}
9693

94+
// drawing your inventory items
9795
for (int row = 0; row < 4; row++) {
9896
for (int col = 0; col < 9; col++) {
9997
int slotIndex = row < 3 ? 9 + row * 9 + col : col;
@@ -107,6 +105,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
107105
}
108106
}
109107

108+
// drawing title headers
110109
context.getMatrices().pushMatrix();
111110
context.getMatrices().translate((float)x, (float)y);
112111
if (textRenderer != null) {
@@ -118,24 +117,48 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
118117
if (mouseX >= baseX && mouseX < baseX + 9 * SLOT_SIZE) {
119118
int col = (mouseX - baseX) / SLOT_SIZE;
120119

120+
// tooltips for the container items
121121
if (mouseY >= baseY && mouseY < baseY + containerRows * SLOT_SIZE) {
122122
int index = ((mouseY - baseY) / SLOT_SIZE) * 9 + col;
123123
if (index < containerItems.size()) {
124124
ItemStack item = containerItems.get(index);
125125
if (!item.isEmpty()) {
126-
context.drawTooltip(textRenderer, item.getTooltip(tooltipContext, mc.player, TooltipType.BASIC), item.getTooltipData(), mouseX, mouseY);
126+
context.drawTooltip(textRenderer, getTooltipFromItem(mc, item), item.getTooltipData(), mouseX, mouseY);
127127
}
128128
}
129-
} else if (mouseY >= playerY && mouseY < playerY + 4 * SLOT_SIZE) {
129+
}
130+
131+
// tooltips for your inventory items
132+
else if (mouseY >= playerY && mouseY < playerY + 4 * SLOT_SIZE) {
130133
int row = (mouseY - playerY) / SLOT_SIZE;
131134
int slotIndex = row < 3 ? 9 + row * 9 + col : col;
132135
if (slotIndex < playerInventory.size()) {
133136
ItemStack item = playerInventory.getStack(slotIndex);
134137
if (!item.isEmpty()) {
135-
context.drawTooltip(textRenderer, item.getTooltip(tooltipContext, mc.player, TooltipType.BASIC), item.getTooltipData(), mouseX, mouseY);
138+
context.drawTooltip(textRenderer, getTooltipFromItem(mc, item), item.getTooltipData(), mouseX, mouseY);
136139
}
137140
}
138141
}
139142
}
140143
}
144+
145+
@Override
146+
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
147+
if (keyCode == GLFW.GLFW_KEY_ESCAPE || mc.options.inventoryKey.matchesKey(keyCode, scanCode)) {
148+
close();
149+
return true;
150+
}
151+
152+
return false;
153+
}
154+
155+
@Override
156+
public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
157+
if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
158+
close();
159+
return true;
160+
}
161+
162+
return false;
163+
}
141164
}

src/main/java/meteordevelopment/meteorclient/utils/render/PeekScreen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
7474
close();
7575
return true;
7676
}
77+
7778
return false;
7879
}
7980

0 commit comments

Comments
 (0)