|
1 | 1 | package sh.okx.civmodern.mod.mixins;
|
2 | 2 |
|
| 3 | +import java.util.List; |
3 | 4 | import net.minecraft.core.component.DataComponentMap;
|
4 | 5 | import net.minecraft.core.component.DataComponents;
|
5 | 6 | import net.minecraft.network.chat.Component;
|
6 | 7 | import net.minecraft.network.chat.Style;
|
7 | 8 | import net.minecraft.world.item.ItemStack;
|
8 | 9 | import net.minecraft.world.item.component.ItemLore;
|
| 10 | +import org.jetbrains.annotations.NotNull; |
9 | 11 | import org.spongepowered.asm.mixin.Mixin;
|
10 | 12 | import org.spongepowered.asm.mixin.Shadow;
|
11 | 13 | 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; |
12 | 19 | import sh.okx.civmodern.mod.features.ExtendedItemStack;
|
13 | 20 |
|
14 | 21 | @Mixin(ItemStack.class)
|
15 | 22 | public abstract class ItemStackMixin implements ExtendedItemStack {
|
16 | 23 | @Shadow
|
17 | 24 | public abstract DataComponentMap getComponents();
|
18 | 25 |
|
| 26 | + // ============================================================ |
| 27 | + // Compacted item detection |
| 28 | + // ============================================================ |
| 29 | + |
19 | 30 | @Unique
|
20 | 31 | private Boolean civmodern$isCompacted = null;
|
21 | 32 |
|
@@ -51,4 +62,58 @@ public boolean isMarkedAsCompacted() {
|
51 | 62 | }
|
52 | 63 | return false;
|
53 | 64 | }
|
| 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 | + } |
54 | 119 | }
|
0 commit comments