1
mirror of https://github.com/Shiewk/Widgets.git synced 2026-04-28 11:34:17 +02:00

Add Inventory Widget (#3)

This commit is contained in:
Shy
2025-08-23 11:00:37 +02:00
parent cd8c354610
commit 154fb46f12
5 changed files with 262 additions and 17 deletions
@@ -70,6 +70,7 @@ public class WidgetsModClient implements ClientModInitializer {
WidgetManager.register(new BiomeWidget(Identifier.of(WidgetsMod.MOD_ID, "biome")));
WidgetManager.register(new SpeedWidget(Identifier.of(WidgetsMod.MOD_ID, "speed")));
WidgetManager.register(new ArmorHudWidget(Identifier.of(WidgetsMod.MOD_ID, "armor")));
WidgetManager.register(new InventoryWidget(Identifier.of(WidgetsMod.MOD_ID, "inventory")));
ComboWidget comboWidget = new ComboWidget(Identifier.of(WidgetsMod.MOD_ID, "combo"));
WidgetManager.register(comboWidget);
@@ -0,0 +1,218 @@
package de.shiewk.widgets.widgets;
import de.shiewk.widgets.WidgetSettings;
import de.shiewk.widgets.widgets.settings.EnumWidgetSetting;
import de.shiewk.widgets.widgets.settings.IntSliderWidgetSetting;
import de.shiewk.widgets.widgets.settings.RGBAColorWidgetSetting;
import de.shiewk.widgets.widgets.settings.ToggleWidgetSetting;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import java.util.List;
import static net.minecraft.text.Text.translatable;
public class InventoryWidget extends ResizableWidget {
private static final Identifier VANILLA_INVENTORY = Identifier.of("widgets", "textures/vanilla_inventory.png");
private static final Identifier TEXTURE_PACK_INVENTORY = Identifier.ofVanilla("textures/gui/container/inventory.png");
public enum InventoryMode {
VANILLA,
TEXTURE_PACK,
TRANSPARENT(true),
GRID(true),
BOXES(true);
public final boolean canDisableHotbar;
InventoryMode(){
this(false);
}
InventoryMode(boolean canDisableHotbar){
this.canDisableHotbar = canDisableHotbar;
}
public Text display() {
return translatable("widgets.widgets.inventory.mode." + name().toLowerCase());
}
}
public InventoryWidget(Identifier id) {
super(id, List.of(
new EnumWidgetSetting<>("mode", translatable("widgets.widgets.inventory.mode"), InventoryMode.class, InventoryMode.TEXTURE_PACK, InventoryMode::display),
new ToggleWidgetSetting("show_hotbar", translatable("widgets.widgets.inventory.showHotbar"), true),
new ToggleWidgetSetting("rainbow_grid", translatable("widgets.widgets.inventory.rainbowGrid"), false),
new IntSliderWidgetSetting("grid_rainbow_speed", translatable("widgets.widgets.common.rainbow.speed"), 1, 3, 10),
new RGBAColorWidgetSetting("grid_color", translatable("widgets.widgets.inventory.gridColor"), 0, 0, 0, 255),
new ToggleWidgetSetting("rainbow_boxes", translatable("widgets.widgets.inventory.rainbowBoxes"), false),
new IntSliderWidgetSetting("box_rainbow_speed", translatable("widgets.widgets.common.rainbow.speed"), 1, 3, 10),
new RGBAColorWidgetSetting("box_color", translatable("widgets.widgets.inventory.boxColor"), 80, 80, 80, 128)
));
getSettings().optionById("rainbow_grid").setShowCondition(() -> this.mode == InventoryMode.GRID);
getSettings().optionById("grid_rainbow_speed").setShowCondition(() -> this.mode == InventoryMode.GRID && this.rainbowGrid);
getSettings().optionById("grid_color").setShowCondition(() -> this.mode == InventoryMode.GRID && !this.rainbowGrid);
getSettings().optionById("rainbow_boxes").setShowCondition(() -> this.mode == InventoryMode.BOXES);
getSettings().optionById("box_rainbow_speed").setShowCondition(() -> this.mode == InventoryMode.BOXES && this.rainbowBoxes);
getSettings().optionById("box_color").setShowCondition(() -> this.mode == InventoryMode.BOXES && !this.rainbowBoxes);
getSettings().optionById("show_hotbar").setShowCondition(() -> this.mode.canDisableHotbar);
}
private InventoryMode mode = InventoryMode.TEXTURE_PACK;
private PlayerInventory inventory;
private boolean rainbowGrid = false;
private int gridColor = 0xff000000;
private int gridRainbowSpeed = 3;
private boolean rainbowBoxes = false;
private int boxColor = 0xff000000;
private int boxRainbowSpeed = 3;
private boolean showHotbar = false;
@Override
public void renderScaled(DrawContext context, long measuringTimeNano, TextRenderer textRenderer, int posX, int posY) {
drawBackground(context, measuringTimeNano, posX, posY);
if (inventory != null){
drawItems(context, textRenderer, switch (mode){
case VANILLA, TEXTURE_PACK -> posX+8;
case GRID -> posX+1;
case TRANSPARENT, BOXES -> posX;
}, switch (mode){
case VANILLA, TEXTURE_PACK -> posY+9;
case GRID -> posY+1;
case TRANSPARENT, BOXES -> posY;
});
}
}
private void drawBackground(DrawContext context, long mt, int posX, int posY) {
switch (mode){
case VANILLA -> context.drawTexture(RenderPipelines.GUI_TEXTURED, VANILLA_INVENTORY, posX, posY, 0, 0, 176, 91, 176, 91);
case TEXTURE_PACK -> {
context.enableScissor(posX, posY, posX + width(), posY + 6);
context.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE_PACK_INVENTORY, posX, posY, 0, 0, 256, 256, 256, 256);
context.disableScissor();
context.enableScissor(posX, posY + 6, posX + width(), posY + height());
context.drawTexture(RenderPipelines.GUI_TEXTURED, TEXTURE_PACK_INVENTORY, posX, posY - 75, 0, 0, 256, 256, 256, 256);
context.disableScissor();
}
case GRID -> {
int gridColor = rainbowGrid ? BasicTextWidget.rainbowColor(mt, gridRainbowSpeed) : this.gridColor;
context.drawHorizontalLine(posX, posX+width(), posY, gridColor);
context.drawHorizontalLine(posX, posX+width(), posY + 18, gridColor);
context.drawHorizontalLine(posX, posX+width(), posY + 36, gridColor);
context.drawHorizontalLine(posX, posX+width(), posY + 54, gridColor);
if (showHotbar){
context.drawHorizontalLine(posX, posX+width(), posY + 58, gridColor);
context.drawHorizontalLine(posX, posX+width(), posY + 76, gridColor);
}
context.drawVerticalLine(posX, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*2, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*3, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*4, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*5, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*6, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*7, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*8, posY, posY+height(), gridColor);
context.drawVerticalLine(posX+18*9, posY, posY+height(), gridColor);
}
case BOXES -> {
int boxColor = rainbowBoxes ? ((BasicTextWidget.rainbowColor(mt, boxRainbowSpeed) & 0xffffff) | (this.boxColor & 0xff000000)) : this.boxColor;
for (int ry = 0; ry < 4; ry++) {
if (ry == 0 && !showHotbar) continue;
for (int rx = 0; rx < 9; rx++) {
int itemY = ry == 0 ? posY + 58 : posY + (ry-1) * 18;
int itemX = posX + rx * 18;
context.fill(itemX, itemY, itemX + 16, itemY + 16, boxColor);
}
}
}
}
}
private void drawItems(DrawContext context, TextRenderer textRenderer, int posX, int posY) {
for (int ry = 0; ry < 4; ry++) {
if (ry == 0 && !showHotbar) continue;
for (int rx = 0; rx < 9; rx++) {
int slot = ry * 9 + rx;
ItemStack stack = inventory.getStack(slot);
if (stack.isEmpty()) continue;
int itemY = ry == 0 ? posY + 58 : posY + (ry-1) * 18;
int itemX = posX + rx * 18;
context.drawItem(stack, itemX, itemY);
context.drawStackOverlay(textRenderer, stack, itemX, itemY);
}
}
}
@Override
public void tick() {
ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player != null) {
PlayerInventory clientPlayerEntityInventory = player.getInventory();
if (clientPlayerEntityInventory != null) inventory = clientPlayerEntityInventory;
}
}
@Override
public Text getName() {
return translatable("widgets.widgets.inventory");
}
@Override
public Text getDescription() {
return translatable("widgets.widgets.inventory.description");
}
@Override
public int width() {
return switch (mode){
case VANILLA, TEXTURE_PACK -> 176;
case TRANSPARENT, BOXES -> 160;
case GRID -> 162;
};
}
@Override
public int height() {
return switch (mode){
case VANILLA, TEXTURE_PACK -> 91;
case TRANSPARENT, BOXES -> showHotbar ? 74 : 53;
case GRID -> showHotbar ? 76 : 55;
};
}
@Override
public void onSettingsChanged(WidgetSettings settings) {
super.onSettingsChanged(settings);
this.mode = ((InventoryMode) ((EnumWidgetSetting<?>) settings.optionById("mode")).getValue());
this.rainbowGrid = ((ToggleWidgetSetting) settings.optionById("rainbow_grid")).getValue();
this.gridRainbowSpeed = ((IntSliderWidgetSetting) settings.optionById("grid_rainbow_speed")).getValue();
this.gridColor = ((RGBAColorWidgetSetting) settings.optionById("grid_color")).getColor();
this.rainbowBoxes = ((ToggleWidgetSetting) settings.optionById("rainbow_boxes")).getValue();
this.boxRainbowSpeed = ((IntSliderWidgetSetting) settings.optionById("box_rainbow_speed")).getValue();
this.boxColor = ((RGBAColorWidgetSetting) settings.optionById("box_color")).getColor();
this.showHotbar = ((ToggleWidgetSetting) settings.optionById("show_hotbar")).getValue() || !mode.canDisableHotbar;
}
}
@@ -51,6 +51,10 @@
"widgets.widgets.clock.showSeconds": "Sekunden anzeigen",
"widgets.widgets.clock.weekFormat": "Wochentagformat",
"widgets.widgets.clock.weekFormat.none": "Nicht anzeigen",
"widgets.widgets.combo": "Combo",
"widgets.widgets.combo.combo": "Combo: %s",
"widgets.widgets.combo.description": "Zeigt deine momentane Combo an",
"widgets.widgets.combo.displayThreshold": "Minimum sichtbar",
"widgets.widgets.common.hideInSingleplayer": "In Einzelspielerwelten verbergen",
"widgets.widgets.common.rainbow": "Regenbogen-Text",
"widgets.widgets.common.rainbow.speed": "Regenbogengeschwindigkeit",
@@ -72,6 +76,16 @@
"widgets.widgets.cps.right": "Zeigt/Zählt Rechts-Klicks",
"widgets.widgets.fps": "FPS",
"widgets.widgets.fps.description": "Zeigt deine aktuellen FPS an.",
"widgets.widgets.inventory": "Inventar",
"widgets.widgets.inventory.description": "Zeigt dein Inventar.",
"widgets.widgets.inventory.gridColor": "Farbe des Gitters",
"widgets.widgets.inventory.mode": "Modus",
"widgets.widgets.inventory.mode.boxes": "Zellen",
"widgets.widgets.inventory.mode.grid": "Gitter",
"widgets.widgets.inventory.mode.texture_pack": "Texturenpaket",
"widgets.widgets.inventory.mode.transparent": "Transparent",
"widgets.widgets.inventory.mode.vanilla": "Standard",
"widgets.widgets.inventory.rainbowGrid": "Regenbogengitter",
"widgets.widgets.keystrokes": "Keystrokes",
"widgets.widgets.keystrokes.colorBackgroundPressed": "Hintergrundfarbe (Taste gedrückt)",
"widgets.widgets.keystrokes.colorBackgroundUnpressed": "Hintergrundfarbe (Taste nicht gedrückt)",
@@ -117,8 +131,7 @@
"widgets.widgets.tps.description": "Zeigt die TPS im Einzelspielermodus an oder schätzt die TPS im Mehrspielermodus",
"widgets.widgets.tps.dynamicColor": "Farbe dynamisch anzeigen",
"widgets.widgets.tps.tps": "%s TPS",
"widgets.widgets.combo": "Combo",
"widgets.widgets.combo.description": "Zeigt deine momentane Combo an",
"widgets.widgets.combo.combo": "Combo: %s",
"widgets.widgets.combo.displayThreshold": "Minimum sichtbar"
"widgets.widgets.inventory.boxColor": "Zellenfarbe",
"widgets.widgets.inventory.rainbowBoxes": "Regenbogenzellen",
"widgets.widgets.inventory.showHotbar": "Schnellzugriffsleiste anzeigen"
}
@@ -43,7 +43,7 @@
"widgets.widgets.clock": "Clock/Date",
"widgets.widgets.clock.dateFormat": "Date format",
"widgets.widgets.clock.dateFormat.none": "No date",
"widgets.widgets.clock.description": "Shows the current time and/or date",
"widgets.widgets.clock.description": "Shows the current time and/or date.",
"widgets.widgets.clock.hourFormat": "Hour Format",
"widgets.widgets.clock.hourFormat.24hour": "Military Time",
"widgets.widgets.clock.hourFormat.am_pm": "AM/PM",
@@ -51,13 +51,17 @@
"widgets.widgets.clock.showSeconds": "Show seconds",
"widgets.widgets.clock.weekFormat": "Day of week format",
"widgets.widgets.clock.weekFormat.none": "Don't show",
"widgets.widgets.combo": "Combo",
"widgets.widgets.combo.combo": "Combo: %s",
"widgets.widgets.combo.description": "Shows your current Combo.",
"widgets.widgets.combo.displayThreshold": "Display Threshold",
"widgets.widgets.common.hideInSingleplayer": "Hide in singleplayer worlds",
"widgets.widgets.common.rainbow": "Rainbow text",
"widgets.widgets.common.rainbow.speed": "Rainbow speed",
"widgets.widgets.common.showLabel": "Show label",
"widgets.widgets.common.sizePercent": "Widget size (%)",
"widgets.widgets.coordinates": "Coordinates",
"widgets.widgets.coordinates.description": "Shows your current coordinates",
"widgets.widgets.coordinates.description": "Shows your current coordinates.",
"widgets.widgets.coordinates.showX": "Show X coordinate:",
"widgets.widgets.coordinates.showY": "Show Y coordinate:",
"widgets.widgets.coordinates.showZ": "Show Z coordinate:",
@@ -66,12 +70,24 @@
"widgets.widgets.cps.appearance.pipe": "Split (Pipe)",
"widgets.widgets.cps.appearance.slash": "Split (Slash)",
"widgets.widgets.cps.appearance.unified": "Unified",
"widgets.widgets.cps.description": "Shows your clicks per second",
"widgets.widgets.cps.description": "Shows your clicks per second.",
"widgets.widgets.cps.left": "Display/Count left clicks",
"widgets.widgets.cps.middle": "Display/Count middle clicks",
"widgets.widgets.cps.right": "Display/Count right clicks",
"widgets.widgets.fps": "FPS",
"widgets.widgets.fps.description": "Shows your current FPS.",
"widgets.widgets.inventory": "Inventory",
"widgets.widgets.inventory.boxColor": "Box color",
"widgets.widgets.inventory.description": "Shows your inventory on screen.",
"widgets.widgets.inventory.gridColor": "Grid color",
"widgets.widgets.inventory.mode": "Mode",
"widgets.widgets.inventory.mode.boxes": "Boxes",
"widgets.widgets.inventory.mode.grid": "Grid",
"widgets.widgets.inventory.mode.texture_pack": "Texture pack",
"widgets.widgets.inventory.mode.transparent": "Transparent",
"widgets.widgets.inventory.mode.vanilla": "Vanilla",
"widgets.widgets.inventory.rainbowBoxes": "Rainbow boxes",
"widgets.widgets.inventory.rainbowGrid": "Rainbow Grid",
"widgets.widgets.keystrokes": "Keystrokes",
"widgets.widgets.keystrokes.colorBackgroundPressed": "Background color (key pressed)",
"widgets.widgets.keystrokes.colorBackgroundUnpressed": "Background color (key not pressed)",
@@ -84,18 +100,18 @@
"widgets.widgets.memory.showPercentage": "Show percentage",
"widgets.widgets.memory.withLabel": "Memory: %s",
"widgets.widgets.ping": "Ping",
"widgets.widgets.ping.description": "Shows your latency to the server",
"widgets.widgets.ping.description": "Shows your latency to the server.",
"widgets.widgets.ping.dynamicColor": "Dynamic Color",
"widgets.widgets.plaintext": "Text",
"widgets.widgets.plaintext.description": "Input a text to display on screen",
"widgets.widgets.plaintext.description": "Input a text to display on screen.",
"widgets.widgets.plaintext.initial": "Change this in the widget settings",
"widgets.widgets.plaintext.placeholder": "Type your text here...",
"widgets.widgets.plaintext.text": "Text",
"widgets.widgets.playerCount": "Player count",
"widgets.widgets.playerCount.description": "Shows the number of players online. May not be accurate on all servers (especially those that spawn fake players in the tab list)",
"widgets.widgets.playerCount.description": "Shows the number of players online. May not be accurate on all servers (especially those that spawn fake players in the tab list).",
"widgets.widgets.playerCount.online": "%s online",
"widgets.widgets.playtime": "Playtime",
"widgets.widgets.playtime.description": "Shows the current session's uptime",
"widgets.widgets.playtime.description": "Shows the current session's uptime.",
"widgets.widgets.playtime.labelStyle": "Label Style",
"widgets.widgets.playtime.labelStyle.none": "No label",
"widgets.widgets.playtime.labelStyle.played": "Time played",
@@ -103,7 +119,7 @@
"widgets.widgets.playtime.played": "%s played",
"widgets.widgets.playtime.playtime": "Playtime: %s",
"widgets.widgets.serverIP": "Server IP",
"widgets.widgets.serverIP.description": "Shows the server address",
"widgets.widgets.serverIP.description": "Shows the server address.",
"widgets.widgets.serverIP.dynamicWidth": "Dynamically set widget width",
"widgets.widgets.speed": "Speed",
"widgets.widgets.speed.description": "Shows the speed you are moving at.",
@@ -114,11 +130,8 @@
"widgets.widgets.speed.withY": "With Y-axis velocity",
"widgets.widgets.speed.withZ": "With Z-axis velocity",
"widgets.widgets.tps": "TPS",
"widgets.widgets.tps.description": "Shows the current TPS when in singleplayer or estimates server TPS when in multiplayer",
"widgets.widgets.tps.description": "Shows the current TPS when in singleplayer and estimates server TPS when in multiplayer.",
"widgets.widgets.tps.dynamicColor": "Dynamic Color",
"widgets.widgets.tps.tps": "%s TPS",
"widgets.widgets.combo": "Combo",
"widgets.widgets.combo.description": "Shows your current Combo",
"widgets.widgets.combo.combo": "Combo: %s",
"widgets.widgets.combo.displayThreshold": "Display Threshold"
"widgets.widgets.inventory.showHotbar": "Show hotbar"
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 643 B