Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Initial stab at an early update checker (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
TehNut committed Sep 12, 2016
1 parent 2d32649 commit 89c8ff6
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ curseforge {
changelog = project.hasProperty('changelog') ? project.changelog : ''
releaseType = 'release'
addGameVersion '1.9'
addGameVersion '1.9.4'
addGameVersion '1.10'
addGameVersion '1.10.2'

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tehnut/launchgui/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ConfigHandler {

public static boolean enableUpdateChecker;
public static boolean disableContinueButtonIfUpdate;
public static boolean checkUpdateEarly;
public static String updateGuiLines;
public static String updateCheckerUrl;
public static String updateInformationButtonText;
Expand Down Expand Up @@ -79,6 +80,7 @@ public static void syncConfig() {
config.addCustomCategoryComment(category + ".button", "Settings related to the shown buttons.");
config.addCustomCategoryComment(category + ".internal", "Settings for the internal checking that the GUI does.");
config.addCustomCategoryComment(category + ".information", "Information to provide to players.");
checkUpdateEarly = config.getBoolean("checkUpdateEarly", category, true, "Checks for a pack update during the Pre-Initialization phase instead of when the main menu displays.\nThis will open a new window that always displays on top. It will pause loading until closed.");
enableUpdateChecker = config.getBoolean("enableUpdateChecker", category + ".internal", false, "Enables the update checker.");
disableContinueButtonIfUpdate = config.getBoolean("disableContinueButtonIfUpdate", category + ".internal", false, "Disable the Continue button if there is a pack update available.");
updateGuiLines = config.getString("updateGuiLines", category + ".information", "Click the information button below to find out more!", "Information to display to your players whenever a new update is available.\n" +
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/tehnut/launchgui/LaunchGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import tehnut.launchgui.gui.javafx.UpdateWindow;
import tehnut.launchgui.utils.EventHandler;
import tehnut.launchgui.utils.Utils;

Expand All @@ -25,9 +26,13 @@ public class LaunchGui {
public void preInit(FMLPreInitializationEvent event) {
ConfigHandler.init(new File(event.getModConfigurationDirectory(), "LaunchGui.cfg"));

if (ConfigHandler.enableUpdateChecker)
if (ConfigHandler.enableUpdateChecker) {
remoteVersion = Utils.getRemoteVersion();

if (ConfigHandler.checkUpdateEarly && Utils.hasUpdate())
UpdateWindow.initWindow();
}

if (ConfigHandler.enableNoticeGui)
remoteText = Utils.getRemoteText();

Expand Down
117 changes: 117 additions & 0 deletions src/main/java/tehnut/launchgui/gui/javafx/UpdateWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package tehnut.launchgui.gui.javafx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import net.minecraft.client.resources.I18n;
import tehnut.launchgui.ConfigHandler;
import tehnut.launchgui.utils.LogHelper;
import tehnut.launchgui.utils.Utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class UpdateWindow extends Application {

private Stage window;

public static void initWindow() {
launch();
}

@Override
public void start(final Stage primaryStage) {
this.window = primaryStage;

primaryStage.setTitle(ConfigHandler.modpackName + " Update Checker");
primaryStage.setWidth(700);
primaryStage.setHeight(500);
primaryStage.setAlwaysOnTop(true);
primaryStage.setResizable(false);

BorderPane border = new BorderPane();
border.setPadding(new Insets(25, 25, 25, 25));

VBox titleBox = new VBox(getTitle(), new Separator());
titleBox.setPadding(new Insets(5, 5, 5, 5));
titleBox.setSpacing(10);
border.setTop(titleBox);

VBox bodyBox = new VBox(new VBox(getBodyText()), new Separator());
bodyBox.setPadding(new Insets(5, 5, 5, 5));
bodyBox.setSpacing(10);
border.setCenter(bodyBox);

HBox buttonBox = new HBox(getButtons());
buttonBox.setPadding(new Insets(5, 5, 5, 5));
buttonBox.setSpacing(10);
border.setBottom(buttonBox);

primaryStage.setScene(new Scene(border));
primaryStage.show();
}

private Node[] getBodyText() {
List<Node> nodes = new ArrayList<Node>();

String[] toDraw = Utils.replaceTextCodes(ConfigHandler.updateGuiLines).split("\n");
for (String draw : toDraw) {
String wrapped = draw.replaceAll("(.{55})", "$1\n");
String[] cutWrapped = wrapped.split("\n");

for (String cut : cutWrapped) {
Label label = new Label(cut);
label.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
nodes.add(label);
}
}

return nodes.toArray(new Node[nodes.size()]);
}

private Node[] getButtons() {
Button acknowledgeButton = new Button(ConfigHandler.continueButtonText);
acknowledgeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
window.close();
}
});
acknowledgeButton.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));

Button informationButton = new Button(ConfigHandler.updateInformationButtonText);
informationButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
Utils.browse(new URI(ConfigHandler.updateInformationUrl));
} catch (Exception e) {
LogHelper.error("Failed to load the page at " + ConfigHandler.updateInformationUrl + "!");
e.printStackTrace();
}
}
});
informationButton.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));

return new Node[] { acknowledgeButton, informationButton };
}

private Node getTitle() {
Label title = new Label(I18n.format("gui.launchgui.update.avail"));
title.setFont(Font.font("Tahoma", FontWeight.NORMAL, 25));
return title;
}
}
2 changes: 1 addition & 1 deletion src/main/java/tehnut/launchgui/utils/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EventHandler {
public void openMainMenu(GuiOpenEvent event) {
if (shouldLoadGUI) {
if (event.getGui() instanceof GuiMainMenu) {
if (Utils.hasUpdate()) {
if (Utils.hasUpdate() && !ConfigHandler.checkUpdateEarly) {
event.setGui(new GuiUpdate());
shouldLoadGUI = false;
return;
Expand Down

0 comments on commit 89c8ff6

Please sign in to comment.