use of org.bleachhack.util.collections.ImmutablePairList in project BleachHack by BleachDrinker420.
the class UpdateScreen method init.
public void init() {
super.init();
int wd = Math.min(width / 2 - 30, 175);
addWindow(new Window(width / 2 - wd, height / 16, width / 2 + wd, height - height / 16, String.format("BleachHack Update [%s -> %s]", BleachHack.VERSION, updateJson.get("name").getAsString()), new ItemStack(Items.MAGENTA_GLAZED_TERRACOTTA)));
int w = getWindow(0).x2 - getWindow(0).x1;
int h = getWindow(0).y2 - getWindow(0).y1;
getWindow(0).addWidget(new WindowTextWidget("A new BleachHack update is available.", true, WindowTextWidget.TextAlign.MIDDLE, 1.5f, w / 2, 18, 0xe0e0e0));
getWindow(0).addWidget(new WindowBoxWidget(3, 50, w - 3, h - 23));
ImmutablePairList<String, Boolean> changelog = new ImmutablePairList<>();
if (updateJson.has("changelog") && updateJson.get("changelog").isJsonArray()) {
for (JsonElement je : updateJson.get("changelog").getAsJsonArray()) {
if (je.isJsonPrimitive()) {
String string = je.getAsString();
if (string.charAt(0) == '-')
string = "\u00a77-\u00a7r" + string.substring(1);
List<StringVisitable> wrapped = client.textRenderer.getTextHandler().wrapLines(string, w - 32, Style.EMPTY);
for (int i = 0; i < wrapped.size(); i++) changelog.add(wrapped.get(i).getString(), i == 0);
}
}
} else {
changelog.add("Could not find changelog.", false);
}
changelogWidgets.clear();
changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget(updateJson.get("name").getAsString(), true, WindowTextWidget.TextAlign.MIDDLE, 2.5f, w / 2, 58, 0xe0e0e0)));
for (int i = 0; i < changelog.size(); i++) {
if (changelog.get(i).getValue()) {
changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget("*", true, 10, 87 + i * 10, 0xa0a0a0)));
}
changelogWidgets.add(getWindow(0).addWidget(new WindowTextWidget(changelog.get(i).getKey(), true, 19, 85 + i * 10, 0xe0e0e0)));
}
scrollbar = getWindow(0).addWidget(new WindowScrollbarWidget(w - 14, 51, 37 + changelog.size() * 10, h - 75, 0));
getWindow(0).addWidget(new WindowButtonWidget(3, h - 21, w / 2 - 2, h - 3, "Website", () -> Util.getOperatingSystem().open(URI.create("https://bleachhack.org/"))));
getWindow(0).addWidget(new WindowButtonWidget(w / 2 + 2, h - 21, w - 3, h - 3, "Update", () -> {
try {
JsonObject installerJson = updateJson.get("installer").getAsJsonObject();
if (installerJson.has("os") && installerJson.get("os").isJsonPrimitive() && !System.getProperty("os.name").startsWith(installerJson.get("os").getAsString())) {
updateResult = "Updater doesn't support your OS!";
selectWindow(1);
return;
}
File modpath = new File(FabricLoader.getInstance().getModContainer("bleachhack").get().getOrigin().getPaths().get(0).toUri());
if (!modpath.isFile()) {
updateResult = "Invalid mod path!";
selectWindow(1);
return;
}
String link = installerJson.get("link").getAsString();
String name = link.replaceFirst("^.*\\/", "");
File installerFile = new File(System.getProperty("java.io.tmpdir"), name);
BleachLogger.logger.info("\n> Installer path: " + installerFile + "\n> Installer URL: " + link + "\n> Installer file name: " + name + "\n> Regular File: " + Files.isRegularFile(installerFile.toPath()) + "\n> File Length: " + installerFile.length());
if (!Files.isRegularFile(installerFile.toPath()) || installerFile.length() <= 1024L) {
FileUtils.copyURLToFile(new URL(link), installerFile);
}
String execCommand = link.endsWith(".jar") ? "java -jar " : "cmd /c start ";
Runtime.getRuntime().exec(execCommand + installerFile.getAbsolutePath() + " " + modpath + " " + installerJson.get("url").getAsString());
client.scheduleStop();
} catch (Exception e) {
updateResult = "Unknown error!";
selectWindow(1);
e.printStackTrace();
}
}));
wd = Math.min(width / 2 - 20, 90);
addWindow(new Window(width / 2 - wd, height / 2 - 15, width / 2 + wd, height / 2 + 15, "Error updating!", new ItemStack(Items.RED_BANNER), true));
getWindow(1).addWidget(new WindowTextWidget("", true, WindowTextWidget.TextAlign.MIDDLE, wd, 16, 0xc05050).withRenderEvent((wg, ms, wx, wy) -> ((WindowTextWidget) wg).setText(new LiteralText(updateResult))));
}
Aggregations