Skip to content

Commit 26aee1e

Browse files
committed
Add an item's base repair-level its tooltip
1 parent 2fa10a2 commit 26aee1e

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

.github/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ which is located at `.minecraft/config/civmodern.properties`.
2626
- Better toggle-sneak (bound to `]` by default). Will automatically deactivate itself if you manually `SHIFT` sneak, or
2727
if you enter water, or start swimming, sprinting, elytra gliding, or creative flying.
2828

29+
- Shows an item's base repair level by default in advanced-tooltips mode.
30+
2931
## Removed
3032

3133
- Radar has been removed. Why? Because it's duplicated effort. [CombatRadar](https://modrinth.com/mod/combatradar) and

src/main/java/sh/okx/civmodern/mod/CivModernConfig.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class CivModernConfig {
1919
public static @NotNull File CONFIG_DIR = null, CONFIG_FILE = null; // Will be set during mod init
2020

2121
public static int compactedItemColour;
22+
public static boolean showItemRepairLevel;
2223

2324
public static boolean iceRoadPitchCardinalEnabled;
2425
public static boolean iceRoadYawCardinalEnabled;
@@ -63,6 +64,7 @@ public static void parse(
6364
final @NotNull Properties properties
6465
) {
6566
compactedItemColour = Integer.parseInt(properties.getProperty("compacted_colour", "16777048"));
67+
showItemRepairLevel = Boolean.parseBoolean(properties.getProperty("show_repair_level", "true"));
6668

6769
iceRoadPitchCardinalEnabled = Boolean.parseBoolean(properties.getProperty("ice_road_cardinal_pitch", "true"));
6870
iceRoadYawCardinalEnabled = Boolean.parseBoolean(properties.getProperty("ice_road_cardinal_yaw", "true"));
@@ -74,6 +76,7 @@ public static void parse(
7476
public static void save() {
7577
final var properties = new Properties();
7678
properties.setProperty("compacted_colour", Integer.toString(compactedItemColour));
79+
properties.setProperty("show_repair_level", Boolean.toString(showItemRepairLevel));
7780

7881
properties.setProperty("ice_road_cardinal_pitch", Boolean.toString(iceRoadPitchCardinalEnabled));
7982
properties.setProperty("ice_road_cardinal_yaw", Boolean.toString(iceRoadYawCardinalEnabled));

src/main/java/sh/okx/civmodern/mod/gui/screen/CompactedConfigScreen.java src/main/java/sh/okx/civmodern/mod/gui/screen/ItemConfigScreen.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import org.lwjgl.glfw.GLFW;
1919
import sh.okx.civmodern.mod.CivModernConfig;
2020
import sh.okx.civmodern.mod.ColourProvider;
21+
import sh.okx.civmodern.mod.gui.widget.CyclicButton;
2122
import sh.okx.civmodern.mod.gui.widget.HsbColourPicker;
2223
import sh.okx.civmodern.mod.gui.widget.ImageButton;
2324

24-
public class CompactedConfigScreen extends Screen {
25+
public class ItemConfigScreen extends Screen {
2526
private static final DecimalFormat FORMAT = new DecimalFormat("##%");
2627

2728
private static final ItemStack ITEM; static {
@@ -44,8 +45,8 @@ public class CompactedConfigScreen extends Screen {
4445
private final Screen parent;
4546
private HsbColourPicker picker;
4647

47-
public CompactedConfigScreen(Screen parent) {
48-
super(Component.translatable("civmodern.screen.compacted.title"));
48+
public ItemConfigScreen(Screen parent) {
49+
super(Component.translatable("civmodern.screen.items.title"));
4950
this.parent = parent;
5051
}
5152

@@ -91,6 +92,17 @@ protected void init() {
9192

9293
addRenderableWidget(picker = hsb);
9394

95+
addRenderableWidget(new CyclicButton(
96+
this.width / 2 - 80,
97+
this.height / 6 + 72,
98+
160,
99+
Button.DEFAULT_HEIGHT,
100+
CivModernConfig.showItemRepairLevel ? 0 : 1,
101+
(button) -> CivModernConfig.showItemRepairLevel = button.getIndex() == 0,
102+
Component.translatable("civmodern.screen.items.repair.enabled"),
103+
Component.translatable("civmodern.screen.items.repair.disabled")
104+
));
105+
94106
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> {
95107
CivModernConfig.save();
96108
minecraft.setScreen(parent);

src/main/java/sh/okx/civmodern/mod/gui/screen/MainConfigScreen.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public MainConfigScreen() {
1414
protected void init() {
1515
int col0 = this.width / 2 - 150 / 2;
1616
addRenderableWidget(Button.builder(Component.translatable("civmodern.screen.main.compacted"), button -> {
17-
minecraft.setScreen(new CompactedConfigScreen(this));
17+
minecraft.setScreen(new ItemConfigScreen(this));
1818
}).pos(col0, this.height / 6).size(150, 20).build());
1919
addRenderableWidget(Button.builder(Component.translatable("civmodern.screen.main.ice"), button -> {
2020
minecraft.setScreen(new IceRoadConfigScreen(this));

src/main/java/sh/okx/civmodern/mod/mixins/ItemStackMixin.java

+65
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package sh.okx.civmodern.mod.mixins;
22

3+
import java.util.List;
34
import net.minecraft.core.component.DataComponentMap;
45
import net.minecraft.core.component.DataComponents;
56
import net.minecraft.network.chat.Component;
67
import net.minecraft.network.chat.Style;
78
import net.minecraft.world.item.ItemStack;
89
import net.minecraft.world.item.component.ItemLore;
10+
import org.jetbrains.annotations.NotNull;
911
import org.spongepowered.asm.mixin.Mixin;
1012
import org.spongepowered.asm.mixin.Shadow;
1113
import org.spongepowered.asm.mixin.Unique;
14+
import org.spongepowered.asm.mixin.injection.At;
15+
import org.spongepowered.asm.mixin.injection.Inject;
16+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
17+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
18+
import sh.okx.civmodern.mod.CivModernConfig;
1219
import sh.okx.civmodern.mod.features.ExtendedItemStack;
1320

1421
@Mixin(ItemStack.class)
1522
public abstract class ItemStackMixin implements ExtendedItemStack {
1623
@Shadow
1724
public abstract DataComponentMap getComponents();
1825

26+
// ============================================================
27+
// Compacted item detection
28+
// ============================================================
29+
1930
@Unique
2031
private Boolean civmodern$isCompacted = null;
2132

@@ -51,4 +62,58 @@ public boolean isMarkedAsCompacted() {
5162
}
5263
return false;
5364
}
65+
66+
// ============================================================
67+
// Showing an item's repair level
68+
//
69+
// @ModifyVariable seems to have some difficulty finding the List<Component> local variable, likewise with local
70+
// capture and @Local. Anything that works shows red lines in the IDE, and anything that the IDE thinks should work
71+
// throws while testing. This may just be an issue with the Minecraft Development plugin. Nonetheless, I've done it
72+
// in a roundabout way to ensure stability.
73+
// ============================================================
74+
75+
@Unique
76+
private List<Component> civmodern$tooltipLines = null;
77+
78+
@ModifyVariable(
79+
method = "getTooltipLines",
80+
at = @At("STORE")
81+
)
82+
private @NotNull List<Component> civmodern$captureTooltipLines(
83+
final @NotNull List<Component> tooltipLines
84+
) {
85+
return this.civmodern$tooltipLines = tooltipLines;
86+
}
87+
88+
@Inject(
89+
method = "getTooltipLines",
90+
at = @At(
91+
value = "INVOKE",
92+
target = "Lnet/minecraft/world/item/ItemStack;isDamaged()Z",
93+
shift = At.Shift.BEFORE
94+
)
95+
)
96+
private void civmodern$showRepairLevel(
97+
final @NotNull CallbackInfoReturnable<List<Component>> cir
98+
) {
99+
if (CivModernConfig.showItemRepairLevel) {
100+
final int repairCost = getComponents().getOrDefault(DataComponents.REPAIR_COST, 0);
101+
if (repairCost > 0) {
102+
this.civmodern$tooltipLines.add(Component.translatable(
103+
"civmodern.repair.level",
104+
Integer.toString(repairCost)
105+
));
106+
}
107+
}
108+
}
109+
110+
@Inject(
111+
method = "getTooltipLines",
112+
at = @At("RETURN")
113+
)
114+
private void civmodern$releaseTooltipLines(
115+
final @NotNull CallbackInfoReturnable<List<Component>> cir
116+
) {
117+
this.civmodern$tooltipLines = null;
118+
}
54119
}

src/main/resources/assets/civmodern/lang/en_us.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"civmodern.screen.main.radar": "Radar Settings",
1212
"civmodern.screen.main.ice": "Ice Road Macro",
1313
"civmodern.screen.main.compacted": "Compacted Item Settings",
14-
"civmodern.screen.compacted.title": "Civ Modern Config (Compacted Items)",
14+
"civmodern.screen.items.title": "Civ Modern Config (Items)",
1515
"civmodern.screen.ice.title": "Civ Modern Config (Ice Road Macro)",
1616
"civmodern.screen.ice.cardinal.pitch.enable": "Snap pitch: Enabled",
1717
"civmodern.screen.ice.cardinal.pitch.disable": "Snap pitch: Disabled",
@@ -23,5 +23,6 @@
2323
"civmodern.screen.ice.stop.disable": "Stop on low food: Disabled",
2424
"civmodern.radar.enter" : "§r%s §eappeared at §b[%s§b]",
2525
"civmodern.radar.hover" : "Click to highlight position\nControl click to add waypoint",
26-
"civmodern.radar.leave" : "§r%s §edisappeared at §b[%s§b]"
26+
"civmodern.radar.leave" : "§r%s §edisappeared at §b[%s§b]",
27+
"civmodern.repair.level": "§fRepair level: %s"
2728
}

0 commit comments

Comments
 (0)