|
10 | 10 | import org.spongepowered.asm.mixin.Mixin; |
11 | 11 | import org.spongepowered.asm.mixin.Unique; |
12 | 12 | import org.spongepowered.asm.mixin.injection.At; |
13 | | -import org.spongepowered.asm.mixin.injection.Inject; |
14 | | -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
15 | 13 |
|
16 | | -import net.fabricmc.fabric.api.client.screen.v1.Screens; |
17 | | -import net.minecraft.client.gui.screen.Screen; |
| 14 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 15 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 16 | + |
18 | 17 | import net.minecraft.client.gui.screen.StatsScreen; |
19 | 18 | import net.minecraft.client.gui.widget.ButtonWidget; |
20 | | -import net.minecraft.client.gui.widget.ClickableWidget; |
21 | | -import net.minecraft.client.resource.language.I18n; |
| 19 | +import net.minecraft.client.gui.widget.DirectionalLayoutWidget; |
| 20 | +import net.minecraft.client.gui.widget.Widget; |
22 | 21 | import net.minecraft.text.Text; |
23 | 22 | import net.wurstclient.WurstClient; |
24 | 23 |
|
25 | 24 | @Mixin(StatsScreen.class) |
26 | | -public abstract class StatsScreenMixin extends Screen |
| 25 | +public class StatsScreenMixin |
27 | 26 | { |
28 | | - @Unique |
29 | | - private ButtonWidget toggleWurstButton; |
30 | | - |
31 | | - private StatsScreenMixin(WurstClient wurst, Text title) |
| 27 | + @WrapOperation(at = @At(value = "INVOKE", |
| 28 | + target = "Lnet/minecraft/client/gui/widget/DirectionalLayoutWidget;add(Lnet/minecraft/client/gui/widget/Widget;)Lnet/minecraft/client/gui/widget/Widget;", |
| 29 | + ordinal = 4), method = "createButtons()V") |
| 30 | + private <T extends Widget> T onCreateDoneButton( |
| 31 | + DirectionalLayoutWidget layout, T doneWidget, Operation<T> original) |
32 | 32 | { |
33 | | - super(title); |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * Adds the hidden "Enable/Disable Wurst" button on the Statistics screen. |
38 | | - */ |
39 | | - @Inject(at = @At("TAIL"), method = "createButtons()V") |
40 | | - private void onCreateButtons(CallbackInfo ci) |
41 | | - { |
42 | | - if(WurstClient.INSTANCE.getOtfs().disableOtf.shouldHideEnableButton()) |
43 | | - return; |
| 33 | + if(!(doneWidget instanceof ButtonWidget doneButton)) |
| 34 | + throw new IllegalStateException( |
| 35 | + "The done button in the statistics screen somehow isn't a button"); |
44 | 36 |
|
45 | | - toggleWurstButton = ButtonWidget |
46 | | - .builder(Text.literal(""), this::toggleWurst).width(150).build(); |
| 37 | + if(WurstClient.INSTANCE.getOtfs().disableOtf.shouldHideEnableButton()) |
| 38 | + return original.call(layout, doneButton); |
47 | 39 |
|
48 | | - ClickableWidget doneButton = getDoneButton(); |
49 | | - doneButton.setX(width / 2 + 2); |
50 | 40 | doneButton.setWidth(150); |
51 | 41 |
|
52 | | - toggleWurstButton.setPosition(width / 2 - 152, doneButton.getY()); |
53 | | - |
54 | | - updateWurstButtonText(toggleWurstButton); |
55 | | - addDrawableChild(toggleWurstButton); |
56 | | - } |
57 | | - |
58 | | - @Unique |
59 | | - private ClickableWidget getDoneButton() |
60 | | - { |
61 | | - for(ClickableWidget button : Screens.getButtons(this)) |
62 | | - if(button.getMessage().getString() |
63 | | - .equals(I18n.translate("gui.done"))) |
64 | | - return button; |
65 | | - |
66 | | - throw new IllegalStateException( |
67 | | - "Can't find the done button on the statistics screen."); |
| 42 | + DirectionalLayoutWidget subLayout = |
| 43 | + layout.add(DirectionalLayoutWidget.horizontal()).spacing(5); |
| 44 | + subLayout.add(ButtonWidget.builder(getButtonText(), this::toggleWurst) |
| 45 | + .width(150).build()); |
| 46 | + return original.call(subLayout, doneButton); |
68 | 47 | } |
69 | 48 |
|
70 | 49 | @Unique |
71 | 50 | private void toggleWurst(ButtonWidget button) |
72 | 51 | { |
73 | 52 | WurstClient wurst = WurstClient.INSTANCE; |
74 | 53 | wurst.setEnabled(!wurst.isEnabled()); |
75 | | - |
76 | | - updateWurstButtonText(button); |
| 54 | + button.setMessage(getButtonText()); |
77 | 55 | } |
78 | 56 |
|
79 | 57 | @Unique |
80 | | - private void updateWurstButtonText(ButtonWidget button) |
| 58 | + private Text getButtonText() |
81 | 59 | { |
82 | 60 | WurstClient wurst = WurstClient.INSTANCE; |
83 | 61 | String text = (wurst.isEnabled() ? "Disable" : "Enable") + " Wurst"; |
84 | | - button.setMessage(Text.literal(text)); |
| 62 | + return Text.literal(text); |
85 | 63 | } |
86 | 64 | } |
0 commit comments