forked from DotRacel/JByteMod-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
69 deletions.
There are no files selected for viewing
65 changes: 0 additions & 65 deletions
65
src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/main/java/de/xbrowniecodez/jbytemod/utils/update/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package de.xbrowniecodez.jbytemod.utils.update; | ||
|
||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
import javax.swing.*; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import de.xbrowniecodez.jbytemod.utils.update.objects.Version; | ||
import de.xbrowniecodez.jbytemod.utils.update.ui.UpdateDialogFrame; | ||
import lombok.SneakyThrows; | ||
import me.grax.jbytemod.JByteMod; | ||
|
||
public class UpdateChecker { | ||
|
||
public UpdateChecker() { | ||
JByteMod.LOGGER.log("Checking for updates..."); | ||
JsonObject releaseInfo = fetchLatestReleaseInfo(); | ||
if (releaseInfo != null) { | ||
Version latestVersion = new Version(releaseInfo.get("name").getAsString()); | ||
String changelog = releaseInfo.get("body").getAsString(); | ||
if(latestVersion.isNewer(JByteMod.version)) | ||
showUpdateDialog(String.valueOf(latestVersion), changelog); | ||
} | ||
} | ||
|
||
|
||
@SneakyThrows | ||
private JsonObject fetchLatestReleaseInfo() { | ||
URL url = new URL("https://api.github.com/repos/xBrownieCodezV2/JByteMod-Remastered/releases/latest"); | ||
URLConnection connection = url.openConnection(); | ||
|
||
try (InputStream inputStream = connection.getInputStream(); | ||
InputStreamReader reader = new InputStreamReader(inputStream)) { | ||
return JsonParser.parseReader(reader).getAsJsonObject(); | ||
} | ||
} | ||
|
||
private void showUpdateDialog(String latestVersion, String changelog) { | ||
SwingUtilities.invokeLater(() -> { | ||
JFrame updateDialogFrame = new UpdateDialogFrame(latestVersion, changelog); | ||
updateDialogFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); | ||
updateDialogFrame.setSize(600, 500); | ||
updateDialogFrame.setLocationRelativeTo(null); | ||
updateDialogFrame.setVisible(true); | ||
}); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/de/xbrowniecodez/jbytemod/utils/update/objects/Version.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package de.xbrowniecodez.jbytemod.utils.update.objects; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public final class Version { | ||
private final int majorVersion; | ||
private final int minorVersion; | ||
|
||
public Version(int majorVersion, int minorVersion) { | ||
this.majorVersion = majorVersion; | ||
this.minorVersion = minorVersion; | ||
} | ||
|
||
public Version(String version) { | ||
String[] split = version.split("\\."); | ||
this.majorVersion = Integer.parseInt(split[0]); | ||
this.minorVersion = Integer.parseInt(split[1]); | ||
} | ||
|
||
/** | ||
* Compares this version to another version. | ||
* | ||
* @param version The version to compare to. | ||
* @return true if the version is greater than the other version, false otherwise. | ||
*/ | ||
public boolean isNewer(Version version) { | ||
return (majorVersion > version.majorVersion) | ||
|| (minorVersion > version.minorVersion); | ||
} | ||
|
||
public String toString() { | ||
return String.format("%d.%d", majorVersion, minorVersion); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/de/xbrowniecodez/jbytemod/utils/update/ui/UpdateDialogFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package de.xbrowniecodez.jbytemod.utils.update.ui; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.net.URI; | ||
|
||
public class UpdateDialogFrame extends JFrame { | ||
public UpdateDialogFrame(String latestVersion, String changelog) { | ||
JLabel label = new JLabel(String.format("Version %s is available, would you like to download it?", latestVersion)); | ||
label.setBounds(10, 10, 400, 20); | ||
|
||
JTextArea textArea = new JTextArea(); | ||
textArea.setEditable(false); | ||
textArea.setLineWrap(true); | ||
textArea.setWrapStyleWord(true); | ||
textArea.setText(changelog); | ||
JScrollPane scrollPane = new JScrollPane(textArea); | ||
scrollPane.setBounds(10, 40, 570, 370); | ||
|
||
JButton yesButton = new JButton("Yes"); | ||
yesButton.setBounds(10, 420, 80, 30); | ||
yesButton.addActionListener(e -> { | ||
openDownloadLink(latestVersion); | ||
dispose(); | ||
}); | ||
|
||
JButton noButton = new JButton("No"); | ||
noButton.setBounds(100, 420, 80, 30); | ||
noButton.addActionListener(e -> dispose()); | ||
|
||
add(label); | ||
add(scrollPane); | ||
add(yesButton); | ||
add(noButton); | ||
setLayout(null); | ||
} | ||
|
||
@SneakyThrows | ||
private void openDownloadLink(String version) { | ||
URI downloadUri = new URI(String.format( | ||
"https://github.com/xBrownieCodezV2/JByteMod-Remastered/releases/download/%s/JByteMod-Remastered-%s.jar", | ||
version, version)); | ||
|
||
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { | ||
Desktop.getDesktop().browse(downloadUri); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters