Skip to content

Commit 2228533

Browse files
committed
UpdateLib 2.0.0-SNAPSHOT
0 parents  commit 2228533

File tree

8 files changed

+453
-0
lines changed

8 files changed

+453
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
*.iml
3+
target/

LICENSE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2021 Joshua Sing <[email protected]>
2+
3+
Permission to use, copy, modify, and distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

pom.xml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2021 Joshua Sing <[email protected]>
4+
~
5+
~ Permission to use, copy, modify, and distribute this software for any
6+
~ purpose with or without fee is hereby granted, provided that the above
7+
~ copyright notice and this permission notice appear in all copies.
8+
~
9+
~ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
~ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
~ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
~ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
~ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
~ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
~ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>dev.hypera</groupId>
24+
<artifactId>UpdateLib</artifactId>
25+
<version>2.0.0-SNAPSHOT</version>
26+
<packaging>jar</packaging>
27+
28+
<name>UpdateLib</name>
29+
<description>A simple update library for SpigotMC resources.</description>
30+
<url>https://github.com/HyperaOfficial/UpdateLib</url>
31+
32+
<licenses>
33+
<license>
34+
<name>GNU General Public License</name>
35+
<url>https://www.gnu.org/licenses/gpl-3.0.html</url>
36+
</license>
37+
</licenses>
38+
39+
<developers>
40+
<developer>
41+
<name>Joshua Sing</name>
42+
<email>[email protected]</email>
43+
</developer>
44+
</developers>
45+
46+
<scm>
47+
<connection>scm:git:git://github.com/HyperaOfficial/UpdateLib.git</connection>
48+
<developerConnection>scm:git:ssh://github.com:HyperaOfficial/UpdateLib.git</developerConnection>
49+
<url>http://github.com/HyperaOfficial/UpdateLib/tree/main</url>
50+
</scm>
51+
52+
<properties>
53+
<java.version>1.8</java.version>
54+
</properties>
55+
56+
<build>
57+
<defaultGoal>clean package</defaultGoal>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<version>3.8.1</version>
63+
<configuration>
64+
<source>${java.version}</source>
65+
<target>${java.version}</target>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-shade-plugin</artifactId>
71+
<version>3.2.4</version>
72+
<executions>
73+
<execution>
74+
<phase>package</phase>
75+
<goals>
76+
<goal>shade</goal>
77+
</goals>
78+
<configuration>
79+
<shadeTestJar>false</shadeTestJar>
80+
</configuration>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
<dependencies>
88+
<dependency>
89+
<groupId>com.googlecode.json-simple</groupId>
90+
<artifactId>json-simple</artifactId>
91+
<version>1.1.1</version>
92+
<scope>compile</scope>
93+
<exclusions>
94+
<exclusion>
95+
<groupId>junit</groupId>
96+
<artifactId>junit</artifactId>
97+
</exclusion>
98+
</exclusions>
99+
</dependency>
100+
</dependencies>
101+
102+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib;
18+
19+
import dev.hypera.updatelib.internal.UpdateLib;
20+
21+
public class UpdateLibBuilder {
22+
23+
private final long resourceId;
24+
private final String currentVersion;
25+
26+
private boolean repeatingChecksEnabled = true;
27+
private long checkInterval = 2 * (60 * (60 * 1000)); // 2 Hours
28+
private int connectionTimeout = 5000; // 5 Seconds
29+
30+
private UpdateLibBuilder(String currentVersion, long resourceId) {
31+
this.currentVersion = currentVersion;
32+
this.resourceId = resourceId;
33+
}
34+
35+
/**
36+
* Creates a new instance of {@link UpdateLibBuilder}.
37+
*
38+
* @param currentVersion Current version of the resource.
39+
* @param resourceId SpigotMC Resource Id.
40+
* @return Instance of {@link UpdateLibBuilder}
41+
*/
42+
public static UpdateLibBuilder create(String currentVersion, long resourceId) {
43+
return new UpdateLibBuilder(currentVersion, resourceId);
44+
}
45+
46+
/**
47+
* Should UpdateLib keep checking for updates? (Time defined by checkInterval)
48+
*
49+
* @param enabled Repeating checks enabled.
50+
* @see #setCheckInterval(long)
51+
*/
52+
public void setRepeatingChecksEnabled(boolean enabled) {
53+
this.repeatingChecksEnabled = enabled;
54+
}
55+
56+
/**
57+
* How often should UpdateLib check for updates? (Only works if repeatingChecksEnabled is true)
58+
*
59+
* @param interval Interval in milliseconds.
60+
* @see #setRepeatingChecksEnabled(boolean)
61+
*/
62+
public void setCheckInterval(long interval) {
63+
this.checkInterval = interval;
64+
}
65+
66+
/**
67+
* After how many milliseconds should we timeout the request to SpigotMC's API?
68+
*
69+
* @param timeout Timeout in milliseconds.
70+
*/
71+
public void setConnectionTimeout(int timeout) {
72+
this.connectionTimeout = timeout;
73+
}
74+
75+
/**
76+
* Builds a new instance of {@link UpdateLib}.
77+
*
78+
* @return Instance of {@link UpdateLib}
79+
*/
80+
public UpdateLib build() {
81+
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout);
82+
}
83+
84+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib.internal;
18+
19+
import dev.hypera.updatelib.internal.tasks.UpdateChecker;
20+
import dev.hypera.updatelib.internal.tasks.UpdateTask;
21+
22+
import java.util.Timer;
23+
24+
public class UpdateLib {
25+
26+
private final long resourceId;
27+
private final String currentVersion;
28+
private final int connectionTimeout;
29+
30+
private long lastCheck = 0L;
31+
private UpdateResponse lastResponse = null;
32+
33+
public UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long checkInterval, int connectionTimeout) {
34+
this.resourceId = resourceId;
35+
this.currentVersion = currentVersion;
36+
this.connectionTimeout = connectionTimeout;
37+
38+
Thread thread = new Thread(() -> {
39+
try {
40+
checkNow();
41+
} catch (Exception ex) {
42+
ex.printStackTrace();
43+
}
44+
});
45+
46+
thread.setName("UpdateLib-" + thread.getId());
47+
thread.start();
48+
49+
if(repeatingChecksEnabled) {
50+
new Timer().schedule(new UpdateTask(resourceId, currentVersion, connectionTimeout, this), checkInterval);
51+
}
52+
}
53+
54+
/**
55+
* Checks for a update now.
56+
*
57+
* @return Response (instance of {@link UpdateResponse}).
58+
* @throws Exception Any errors that occurred while checking.
59+
*/
60+
public UpdateResponse checkNow() throws Exception {
61+
lastCheck = System.currentTimeMillis();
62+
lastResponse = new UpdateChecker().check(resourceId, currentVersion, connectionTimeout);
63+
return lastResponse;
64+
}
65+
66+
/**
67+
* Get last {@link UpdateResponse} stored - If UpdateLib hasn't checked for updates yet, this will return null.
68+
*
69+
* @return Last {@link UpdateResponse} stored.
70+
*/
71+
public UpdateResponse getLastResponse() {
72+
return lastResponse;
73+
}
74+
75+
/**
76+
* Get the last time UpdateLib checked for an update.
77+
* @return Last time in milliseconds.
78+
*/
79+
public long getLastCheckTime() {
80+
return lastCheck;
81+
}
82+
83+
/**
84+
* Set last check time. - Used internally by UpdateLib.
85+
*
86+
* @param lastCheck Last check time in milliseconds.
87+
*/
88+
public void setLastCheck(long lastCheck) {
89+
this.lastCheck = lastCheck;
90+
}
91+
92+
/**
93+
* Set last response. - Used internally by UpdateLib.
94+
*
95+
* @param lastResponse Last response.
96+
*/
97+
public void setLastResponse(UpdateResponse lastResponse) {
98+
this.lastResponse = lastResponse;
99+
}
100+
101+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib.internal;
18+
19+
public class UpdateResponse {
20+
21+
private final boolean updateAvailable;
22+
private final String currentVersion;
23+
private final String spigotVersion;
24+
25+
public UpdateResponse(boolean updateAvailable, String currentVersion, String spigotVersion) {
26+
this.updateAvailable = updateAvailable;
27+
this.currentVersion = currentVersion;
28+
this.spigotVersion = spigotVersion;
29+
}
30+
31+
public boolean isUpdateAvailable() {
32+
return updateAvailable;
33+
}
34+
35+
public String getCurrentVersion() {
36+
return currentVersion;
37+
}
38+
39+
public String getSpigotVersion() {
40+
return spigotVersion;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)