44import com .google .gson .JsonParser ;
55import net .fabricmc .loader .api .FabricLoader ;
66import net .i_no_am .viewmodel .Global ;
7- import net .i_no_am .viewmodel .ViewModel ;
7+ import net .minecraft .client .gui .screen .ConfirmScreen ;
8+ import net .minecraft .text .Text ;
9+ import net .minecraft .util .Formatting ;
10+ import net .minecraft .util .Util ;
811
9- import java .io .BufferedReader ;
10- import java .io .InputStreamReader ;
11- import java .net .HttpURLConnection ;
12- import java .net .URL ;
12+ import java .net .URI ;
13+ import java .net .http .HttpClient ;
14+ import java .net .http .HttpRequest ;
15+ import java .net .http .HttpResponse ;
16+ import java .time .Duration ;
17+ import java .util .HashMap ;
18+ import java .util .Map ;
1319
1420public class Version implements Global {
1521
16- private static final String REPO = "https://api.github.com/repos/I-No-oNe/View-Model/releases/latest" ;
22+ private static final Map <String , Double > versionCache = new HashMap <>();
23+ private final String api ;
24+ private final String download ;
25+ private static boolean bl = false ;
26+ private final double version ;
1727
18- public static void checkUpdates () {
28+ /**
29+ @param api The link to the github repo api.
30+ @param download The link to the download page.
31+ ***/
32+
33+ public Version (String api , String download ) throws Exception {
34+ this .api = api ;
35+ this .download = download ;
36+ this .version = getVApi ();
37+ }
38+
39+ public static Version create (String apiLink , String downloadLink ) {
1940 try {
20- String latestVersion = getLatestVersionFromGitHub ();
21- if (!latestVersion .equals (getModVersion ())) {
22- ViewModel .isOutdated = true ;
41+ return new Version (apiLink , downloadLink );
42+ } catch (Exception e ) {
43+ throw new RuntimeException (e );
44+ }
45+ }
46+
47+ public void notifyUpdate (boolean printVersions ) {
48+ if (!bl && mc .currentScreen == null && mc .player != null && !isUpdated ()) {
49+ if (printVersions ) {
50+ System .out .println ("Versions: \n Current Version: " + getSelf () + "\n " + "Online Version: " + getApi ());
2351 }
24- } catch (Exception ignored ) {}
52+ mc .setScreen (new ConfirmScreen (confirmed -> {
53+ if (confirmed ) {
54+ Util .getOperatingSystem ().open (URI .create (download ));
55+ mc .player .closeScreen ();
56+ }
57+ mc .player .closeScreen ();
58+ }, Text .of (Formatting .RED + "You are using an outdated version of View Model" ), Text .of ("Please download the latest version from " + Formatting .GREEN + "Modrinth" ), Text .of ("Download" ), Text .of ("Continue playing" )));
59+ bl = true ;
60+ }
61+ }
62+
63+ private boolean isUpdated () {
64+ return getSelf () >= getApi ();
65+ }
66+
67+ private double getApi () {
68+ return version ;
2569 }
2670
27- private static String getLatestVersionFromGitHub () throws Exception {
28- HttpURLConnection connection = (HttpURLConnection ) new URL (Version .REPO ).openConnection ();
71+ private static double getSelf () {
72+ String versionString = FabricLoader .getInstance ().getModContainer (modId ).orElseThrow (() -> new RuntimeException (modId + " Isn't loaded" )).getMetadata ().getVersion ().getFriendlyString ();
73+ return parseVersion (versionString );
74+ }
2975
30- connection . setRequestMethod ( "GET" );
31- connection . setRequestProperty ( "Accept" , "application/vnd.github.v3+json " );
76+ private static double parseVersion ( String version ) {
77+ String [] parts = version . split ( "- " );
3278
33- BufferedReader in = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
34- StringBuilder response = new StringBuilder ();
35- String inputLine ;
36- while ((inputLine = in .readLine ()) != null ) {
37- response .append (inputLine );
79+ for (String part : parts ) {
80+ if (part .matches ("\\ d+\\ .\\ d+\\ .\\ d+" )) {
81+ String [] versionNumbers = part .split ("\\ ." );
82+ double parsedVersion = Double .parseDouble (versionNumbers [0 ] + "." + versionNumbers [1 ]);
83+ return parsedVersion * 10 ;
84+ } else if (part .matches ("\\ d+\\ .\\ d+" )) return Double .parseDouble (part );
3885 }
39- in .close ();
4086
41- JsonObject jsonResponse = JsonParser .parseString (response .toString ()).getAsJsonObject ();
42- return jsonResponse .get ("tag_name" ).getAsString ();
87+ return 0.0 ;
4388 }
4489
90+ private double getVApi () throws Exception {
91+ if (versionCache .containsKey (api )) {
92+ return versionCache .get (api );
93+ }
4594
46- public static String getModVersion () {
47- String fullVersionString = FabricLoader . getInstance ().getModContainer ( modId ). get (). getMetadata (). getVersion (). getFriendlyString ();
48- String [] parts = fullVersionString . split ( "-" );
49- for ( String part : parts ) {
50- if ( part . matches ( " \\ d+ \\ . \\ d+" )) {
51- return part ;
52- }
95+ HttpClient client = HttpClient . newHttpClient ();
96+ HttpRequest request = HttpRequest . newBuilder ().uri ( URI . create ( api )). timeout ( Duration . ofSeconds ( 600 )). header ( "Accept" , "application/vnd.github.v3+json" ). build ();
97+
98+ HttpResponse < String > response = client . send ( request , HttpResponse . BodyHandlers . ofString ());
99+
100+ if ( response . statusCode () != 200 ) {
101+ throw new RuntimeException ( "Failed to fetch latest version: " + response . statusCode ());
53102 }
54- return "Unknown" ;
103+
104+ JsonObject jsonResponse = JsonParser .parseString (response .body ()).getAsJsonObject ();
105+ String versionString = jsonResponse .get ("tag_name" ).getAsString ();
106+
107+ double parsedVersion = parseVersion (versionString );
108+ versionCache .put (api , parsedVersion );
109+ return parsedVersion ;
55110 }
56- }
111+ }
0 commit comments