|
| 1 | +package cc.woverflow.crashpatch.mixin; |
| 2 | + |
| 3 | +import cc.woverflow.crashpatch.CrashPatch; |
| 4 | +import cc.woverflow.crashpatch.hooks.MinecraftHook; |
| 5 | +import gg.essential.universal.ChatColor; |
| 6 | +import gg.essential.universal.UDesktop; |
| 7 | +import net.minecraft.client.gui.FontRenderer; |
| 8 | +import net.minecraft.client.gui.GuiScreen; |
| 9 | +import net.minecraft.client.multiplayer.GuiConnecting; |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 13 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 14 | + |
| 15 | +import java.awt.*; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.URI; |
| 18 | + |
| 19 | +@Mixin(GuiConnecting.class) |
| 20 | +public class MixinGuiConnecting extends GuiScreen { |
| 21 | + |
| 22 | + @Inject(method = "drawScreen", at = @At("TAIL")) |
| 23 | + private void drawWarningText(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) { |
| 24 | + if (((MinecraftHook) mc).hasRecoveredFromCrash()) { |
| 25 | + drawSplitCenteredString(getText(), width / 2, 5, Color.WHITE.getRGB()); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private String getText() { |
| 30 | + return ChatColor.RED + "If Minecraft is stuck on this screen, please force close the game" + (CrashPatch.INSTANCE.isSkyclient() ? " and go to https://discord.gg/eh7tNFezct for support" : "") + "."; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { |
| 35 | + super.mouseClicked(mouseX, mouseY, mouseButton); |
| 36 | + if (((MinecraftHook) mc).hasRecoveredFromCrash()) { |
| 37 | + if (mouseButton == 0) { |
| 38 | + String[] list = wrapFormattedStringToWidth(getText(), width).split("\n"); |
| 39 | + int width = -1; |
| 40 | + for (String text : list) { |
| 41 | + width = Math.max(width, fontRendererObj.getStringWidth(text)); |
| 42 | + } |
| 43 | + int left = (this.width / 2) - width / 2; |
| 44 | + if ((width == -1 || (left < mouseX && left + width > mouseX)) && (mouseY > 5 && mouseY < 15 + ((list.length - 1) * (fontRendererObj.FONT_HEIGHT + 2)))) { |
| 45 | + UDesktop.browse(URI.create("https://discord.gg/eh7tNFezct")); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void drawSplitCenteredString(String text, int x, int y, int color) { |
| 52 | + for (String line : wrapFormattedStringToWidth(text, width).split("\n")) { |
| 53 | + drawCenteredString(fontRendererObj, line, x, y, color); |
| 54 | + y += fontRendererObj.FONT_HEIGHT + 2; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public String wrapFormattedStringToWidth(String str, int wrapWidth) |
| 59 | + { |
| 60 | + int i = this.sizeStringToWidth(str, wrapWidth); |
| 61 | + |
| 62 | + if (str.length() <= i) |
| 63 | + { |
| 64 | + return str; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + String s = str.substring(0, i); |
| 69 | + char c0 = str.charAt(i); |
| 70 | + boolean flag = c0 == 32 || c0 == 10; |
| 71 | + String s1 = FontRenderer.getFormatFromString(s) + str.substring(i + (flag ? 1 : 0)); |
| 72 | + return s + "\n" + this.wrapFormattedStringToWidth(s1, wrapWidth); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private int sizeStringToWidth(String str, int wrapWidth) |
| 77 | + { |
| 78 | + int i = str.length(); |
| 79 | + int j = 0; |
| 80 | + int k = 0; |
| 81 | + int l = -1; |
| 82 | + |
| 83 | + for (boolean flag = false; k < i; ++k) |
| 84 | + { |
| 85 | + char c0 = str.charAt(k); |
| 86 | + |
| 87 | + switch (c0) |
| 88 | + { |
| 89 | + case '\n': |
| 90 | + --k; |
| 91 | + break; |
| 92 | + case ' ': |
| 93 | + l = k; |
| 94 | + default: |
| 95 | + j += fontRendererObj.getCharWidth(c0); |
| 96 | + |
| 97 | + if (flag) |
| 98 | + { |
| 99 | + ++j; |
| 100 | + } |
| 101 | + |
| 102 | + break; |
| 103 | + case '\u00a7': |
| 104 | + |
| 105 | + if (k < i - 1) |
| 106 | + { |
| 107 | + ++k; |
| 108 | + char c1 = str.charAt(k); |
| 109 | + |
| 110 | + if (c1 != 108 && c1 != 76) |
| 111 | + { |
| 112 | + if (c1 == 114 || c1 == 82 || isFormatColor(c1)) |
| 113 | + { |
| 114 | + flag = false; |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + flag = true; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (c0 == 10) |
| 125 | + { |
| 126 | + ++k; |
| 127 | + l = k; |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + if (j > wrapWidth) |
| 132 | + { |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return k != i && l != -1 && l < k ? l : k; |
| 138 | + } |
| 139 | + |
| 140 | + private static boolean isFormatColor(char colorChar) |
| 141 | + { |
| 142 | + return colorChar >= 48 && colorChar <= 57 || colorChar >= 97 && colorChar <= 102 || colorChar >= 65 && colorChar <= 70; |
| 143 | + } |
| 144 | +} |
0 commit comments