| 1 | package thunder.hack.utility;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.google.gson.JsonParser;
|
| 6 | import com.mojang.blaze3d.platform.NativeImage;
|
| 7 | import net.fabricmc.loader.api.metadata.Person;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import org.jetbrains.annotations.NotNull;
|
| 12 | import thunder.hack.ThunderHack;
|
| 13 | import thunder.hack.gui.font.Texture;
|
| 14 | import thunder.hack.utility.math.MathUtility;
|
| 15 |
|
| 16 | import java.io.*;
|
| 17 | import java.net.URI;
|
| 18 | import java.net.URL;
|
| 19 | import java.net.URLConnection;
|
| 20 | import java.nio.charset.StandardCharsets;
|
| 21 | import java.util.ArrayList;
|
| 22 | import java.util.Enumeration;
|
| 23 | import java.util.List;
|
| 24 | import java.util.Objects;
|
| 25 | import java.util.concurrent.atomic.AtomicReference;
|
| 26 | import java.util.jar.JarFile;
|
| 27 | import java.util.jar.Manifest;
|
| 28 |
|
| 29 | import static thunder.hack.core.manager.client.ConfigManager.IMAGES_FOLDER;
|
| 30 | import static thunder.hack.features.modules.Module.mc;
|
| 31 |
|
| 32 | public final class ThunderUtility {
|
| 33 | public static List<String> changeLog = new ArrayList<>();
|
| 34 | public static List<String> starGazer = new ArrayList<>();
|
| 35 |
|
| 36 | public static @NotNull String getAuthors() {
|
| 37 | List<String> names = ThunderHack.MOD_META.getAuthors()
|
| 38 | .stream()
|
| 39 | .map(Person::getName)
|
| 40 | .toList();
|
| 41 |
|
| 42 | return String.join(", ", names);
|
| 43 | }
|
| 44 |
|
| 45 | public static String solveName(String notSolved) {
|
| 46 | AtomicReference<String> mb = new AtomicReference<>("FATAL ERROR");
|
| 47 | Objects.requireNonNull(mc.getConnection()).getListedOnlinePlayers().forEach(player -> {
|
| 48 | if (notSolved.contains(player.getProfile().name())) {
|
| 49 | mb.set(player.getProfile().name());
|
| 50 | }
|
| 51 | });
|
| 52 |
|
| 53 | return mb.get();
|
| 54 | }
|
| 55 |
|
| 56 | public static Identifier registerDynamicTexture(String path, DynamicTexture texture) {
|
| 57 | if (texture == null) {
|
| 58 | return null;
|
| 59 | }
|
| 60 |
|
| 61 | Identifier id = new Texture(path).getId();
|
| 62 | mc.execute(() -> mc.getTextureManager().register(id, texture));
|
| 63 | return id;
|
| 64 | }
|
| 65 |
|
| 66 | public static Identifier getCustomImg(String name) throws IOException {
|
| 67 | try (FileInputStream stream = new FileInputStream(IMAGES_FOLDER + "/" + name + ".png")) {
|
| 68 | return registerDynamicTexture("th-" + name + "-" + (int) MathUtility.random(0, 1000), new DynamicTexture(() -> "th_" + name, NativeImage.read(stream)));
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | public static void syncVersion() {
|
| 73 | try {
|
| 74 | try (BufferedReader reader = openRemoteReader("https://raw.githubusercontent.com/ulybaka1337/THRecodeImprovedUtil/main/syncVersion121.txt")) {
|
| 75 | if (!reader.readLine().equals(ThunderHack.VERSION))
|
| 76 | ThunderHack.isOutdated = true;
|
| 77 | }
|
| 78 | } catch (Exception ignored) {
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | private static BufferedReader openRemoteReader(String url) throws IOException {
|
| 83 | URLConnection connection = URI.create(url).toURL().openConnection();
|
| 84 | connection.setConnectTimeout(5000);
|
| 85 | connection.setReadTimeout(5000);
|
| 86 | connection.setUseCaches(false);
|
| 87 | connection.setRequestProperty("User-Agent", "MelaHack/" + ThunderHack.VERSION);
|
| 88 | return new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
| 89 | }
|
| 90 |
|
| 91 | public static void parseStarGazer() {
|
| 92 | List<String> starGazers = new ArrayList<>();
|
| 93 |
|
| 94 | try {
|
| 95 | for (int page = 1; page <= 3; page++) {
|
| 96 | StringBuilder response = new StringBuilder();
|
| 97 | String inputLine;
|
| 98 |
|
| 99 | try (BufferedReader in = openRemoteReader("https://api.github.com/repos/Pan4ur/ThunderHack-Recode/stargazers?per_page=100&page=" + page)) {
|
| 100 | while ((inputLine = in.readLine()) != null) {
|
| 101 | response.append(inputLine);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | JsonArray jsonArray = JsonParser.parseString(response.toString()).getAsJsonArray();
|
| 106 | for (int i = 0; i < jsonArray.size(); i++) {
|
| 107 | JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
|
| 108 | starGazers.add(jsonObject.getAsJsonPrimitive("login").getAsString());
|
| 109 | }
|
| 110 |
|
| 111 | Thread.sleep(1500);
|
| 112 | }
|
| 113 |
|
| 114 | starGazer = starGazers;
|
| 115 | } catch (Exception ignored) {
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public static void syncContributors() {
|
| 120 | try (BufferedReader in = openRemoteReader("https://raw.githubusercontent.com/ulybaka1337/THRecodeImprovedUtil/main/thTeam.txt")) {
|
| 121 | String inputLine;
|
| 122 | int i = 0;
|
| 123 | while ((inputLine = in.readLine()) != null) {
|
| 124 | ThunderHack.contributors[i] = inputLine.trim();
|
| 125 | i++;
|
| 126 | }
|
| 127 | } catch (Exception ignored) {
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | public static String readManifestField(String fieldName) {
|
| 132 | try {
|
| 133 | Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
|
| 134 | while (en.hasMoreElements()) {
|
| 135 | try {
|
| 136 | URL url = en.nextElement();
|
| 137 | InputStream is = url.openStream();
|
| 138 | if (is != null) {
|
| 139 | String s = new Manifest(is).getMainAttributes().getValue(fieldName);
|
| 140 | if (s != null)
|
| 141 | return s;
|
| 142 | }
|
| 143 | } catch (Exception ignored) {
|
| 144 | }
|
| 145 | }
|
| 146 | } catch (Exception ignored) {
|
| 147 | }
|
| 148 | return "0";
|
| 149 | }
|
| 150 |
|
| 151 |
|
| 152 | public static void parseCommits() {
|
| 153 | try (BufferedReader in = openRemoteReader("https://api.github.com/repos/ulybaka1337/ThunderHack-Recode-Improved/commits?per_page=50")) {
|
| 154 | changeLog.add("Latest Commits [Recode Improved; Date: " + ThunderHack.BUILD_DATE + "; GitHash:" + ThunderHack.GITHUB_HASH + "]");
|
| 155 | changeLog.add("\n");
|
| 156 |
|
| 157 | String inputLine;
|
| 158 | while ((inputLine = in.readLine()) != null) {
|
| 159 | JsonArray jsonArray = JsonParser.parseString(inputLine).getAsJsonArray();
|
| 160 |
|
| 161 | for (int i = 0; i < jsonArray.size(); i++) {
|
| 162 | JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
|
| 163 | JsonObject commitObject = jsonObject.getAsJsonObject("commit");
|
| 164 | JsonObject authorObject = commitObject.getAsJsonObject("author");
|
| 165 |
|
| 166 | String name = authorObject.get("name").getAsString().replace("\n", "");
|
| 167 | String date = authorObject.get("date").getAsString().replace("\n", "");
|
| 168 | String info = commitObject.get("message").getAsString().replace("\n", "");
|
| 169 |
|
| 170 | if (name.contains("ImgBot") || info.startsWith("Merge") || info.startsWith("Revert")) {
|
| 171 | continue;
|
| 172 | }
|
| 173 |
|
| 174 | String formattedDate = ChatFormatting.GRAY + date.split("T")[0] + ChatFormatting.RESET;
|
| 175 | String formattedName = "@" + ChatFormatting.RED + name + ChatFormatting.RESET;
|
| 176 |
|
| 177 | changeLog.add("- " + info + " [" + formattedDate + "] (" + formattedName + ")");
|
| 178 | }
|
| 179 | }
|
| 180 | } catch (Exception ignored) {
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|