Skip to content

Commit e18b491

Browse files
authored
chore: Merge v4.0.0
v4.0.0 - Rewrite entire library
2 parents 44535c8 + c7aa07d commit e18b491

38 files changed

+1060
-1224
lines changed

LICENSE

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
MIT License
2+
13
Copyright (c) 2021 Joshua Sing <[email protected]>
24

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.
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ A simple update checking library for Minecraft Plugins.
88
![GitHub release (latest by date)](https://img.shields.io/github/v/release/HyperaOfficial/UpdateLib?color=%232155cc&&label=Version&&style=for-the-badge)
99
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/HyperaOfficial/UpdateLib?color=%232155cc&&style=for-the-badge)
1010

11-
Click **[here](https://docs.hypera.dev/docs/updatelib)** to view documentation and usage.<br>
11+
Click **[here](https://wiki.hypera.dev/updatelib/)** to view documentation and usage.<br>
1212
Click **[here](https://github.com/HyperaOfficial/UpdateLib/blob/main/LICENSE)** to view the license.

SECURITY.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
| Version | Supported |
66
| :------ | -----------------: |
7-
| 3.x.x | :white_check_mark: |
8-
| 2.0.x | :white_check_mark: |
9-
| < 2.0 | :x: |
7+
| 4.x.x | :white_check_mark: |
8+
| < 4.0 | :x: |
109

1110

1211
## Reporting a Vulnerability

pom.xml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>dev.hypera</groupId>
2424
<artifactId>UpdateLib</artifactId>
25-
<version>3.1.2</version>
25+
<version>4.0.0</version>
2626
<packaging>jar</packaging>
2727

2828
<name>UpdateLib</name>
@@ -31,8 +31,8 @@
3131

3232
<licenses>
3333
<license>
34-
<name>OpenBSD License</name>
35-
<url>https://cvsweb.openbsd.org/src/share/misc/license.template?rev=HEAD</url>
34+
<name>MIT License</name>
35+
<url>https://opensource.org/licenses/MIT</url>
3636
</license>
3737
</licenses>
3838

@@ -97,6 +97,18 @@
9797
</execution>
9898
</executions>
9999
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-gpg-plugin</artifactId>
103+
<version>3.0.1</version>
104+
<executions>
105+
<execution>
106+
<goals>
107+
<goal>sign</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
</plugin>
100112
<plugin>
101113
<groupId>org.apache.maven.plugins</groupId>
102114
<artifactId>maven-deploy-plugin</artifactId>
@@ -126,6 +138,20 @@
126138
</exclusion>
127139
</exclusions>
128140
</dependency>
141+
142+
<!-- SemVer4J -->
143+
<dependency>
144+
<groupId>com.vdurmont</groupId>
145+
<artifactId>semver4j</artifactId>
146+
<version>3.1.0</version>
147+
</dependency>
148+
149+
<!-- JetBrains Annotations -->
150+
<dependency>
151+
<groupId>org.jetbrains</groupId>
152+
<artifactId>annotations</artifactId>
153+
<version>22.0.0</version>
154+
</dependency>
129155
</dependencies>
130156

131157
</project>
Lines changed: 84 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,140 @@
11
/*
2+
* UpdateLib - A simple update checking library for Minecraft Plugins.
23
* Copyright (c) 2021 Joshua Sing <[email protected]>
34
*
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.
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
711
*
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.
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
1522
*/
1623

1724
package dev.hypera.updatelib;
1825

19-
import dev.hypera.updatelib.annotations.RunAsync;
20-
import dev.hypera.updatelib.checkers.UpdateChecker;
21-
import dev.hypera.updatelib.data.CheckData;
26+
import dev.hypera.updatelib.comparators.IVersionComparator;
27+
import dev.hypera.updatelib.exceptions.UpdateLibException;
2228
import dev.hypera.updatelib.objects.UpdateStatus;
23-
29+
import dev.hypera.updatelib.objects.enums.Status;
30+
import dev.hypera.updatelib.resolvers.IVersionResolver;
31+
import java.time.Instant;
2432
import java.util.Timer;
2533
import java.util.TimerTask;
34+
import java.util.concurrent.CompletableFuture;
2635
import java.util.function.Consumer;
36+
import org.jetbrains.annotations.ApiStatus.Internal;
2737

38+
/**
39+
* UpdateLib main class
40+
*
41+
* @author Joshua Sing <[email protected]>
42+
*/
2843
public class UpdateLib {
2944

30-
private final static String VERSION = "3.1.2"; // Current UpdateLib version.
45+
private static final String VERSION = "4.0.0";
3146

3247
private final long resourceId;
3348
private final String currentVersion;
34-
private final int connectionTimeout;
35-
36-
private final UpdateChecker updateChecker;
37-
38-
private final Consumer<UpdateStatus> completeAction;
39-
private final Consumer<Throwable> errorHandler;
49+
private final int timeout;
50+
private final IVersionResolver versionResolver;
51+
private final IVersionComparator versionComparator;
52+
private final Consumer<UpdateStatus> statusHandler;
4053

41-
private UpdateStatus lastStatus = null;
54+
private UpdateStatus lastStatus = UpdateStatus.DEFAULT;
4255
private long lastCheck = 0L;
4356

44-
/**
45-
* UpdateLib Constructor - Used internally by UpdateLib.
46-
*
47-
* @param resourceId Resource ID.
48-
* @param currentVersion Current version of the Resource.
49-
* @param repeatingChecksEnabled If UpdateLib should check for updates periodically.
50-
* @param interval How often UpdateLib should check for updates.
51-
* @param connectionTimeout The amount of milliseconds UpdateLib should wait before timing out on requests.
52-
* @param updateChecker {@link UpdateChecker} to be used by UpdateLib to check for updates.
53-
* @param completeAction Action to run after UpdateLib has checked for an update.
54-
* @param errorHandler Consumer to run if UpdateLib encounters an exception.
55-
*/
56-
protected UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long interval, int connectionTimeout, UpdateChecker updateChecker, Consumer<UpdateStatus> completeAction, Consumer<Throwable> errorHandler) {
57+
@Internal
58+
protected UpdateLib(long resourceId, String currentVersion, int timeout, boolean repeatingChecks, long interval, IVersionResolver versionResolver, IVersionComparator versionComparator, Consumer<UpdateStatus> statusHandler) {
5759
this.resourceId = resourceId;
5860
this.currentVersion = currentVersion;
59-
this.connectionTimeout = connectionTimeout;
60-
this.updateChecker = updateChecker;
61-
this.completeAction = completeAction;
62-
this.errorHandler = errorHandler;
61+
this.timeout = timeout;
62+
this.versionResolver = versionResolver;
63+
this.versionComparator = versionComparator;
64+
this.statusHandler = statusHandler;
6365

64-
Thread thread = new Thread(this::checkNow);
65-
thread.setName("UpdateLib-" + thread.getId());
66-
thread.start();
66+
check();
6767

68-
if(repeatingChecksEnabled) {
68+
if (repeatingChecks) {
6969
new Timer().schedule(new TimerTask() {
7070
@Override
7171
public void run() {
72-
checkNow();
72+
check();
7373
}
7474
}, interval);
7575
}
7676
}
7777

78+
/**
79+
* Create a new {@link UpdateLibBuilder} instance.
80+
* @return New {@link UpdateLibBuilder} instance.
81+
*/
82+
public static UpdateLibBuilder builder() {
83+
return UpdateLibBuilder.create();
84+
}
7885

7986
/**
80-
* Start an update check. It's recommended to only run this asynchronously as it may take time to fetch data from
81-
* the API.
82-
*
83-
* @return Response - Instance of {@link UpdateStatus}
84-
* @since 2.0.0-SNAPSHOT
87+
* Get the current version of UpdateLib.
88+
* @return Current UpdateLib version.
8589
*/
86-
@RunAsync
87-
public UpdateStatus checkNow() {
88-
try {
89-
lastStatus = updateChecker.check(new CheckData(resourceId, currentVersion, connectionTimeout));
90-
lastCheck = System.currentTimeMillis();
91-
if(null != completeAction)
92-
completeAction.accept(lastStatus);
93-
} catch (Exception ex) {
94-
if(null == errorHandler)
95-
ex.printStackTrace();
96-
else
97-
errorHandler.accept(ex);
98-
}
90+
public static String getVersion() {
91+
return VERSION;
92+
}
9993

100-
return lastStatus;
94+
/**
95+
* Checks for an update.
96+
* @return {@link CompletableFuture<UpdateStatus>}
97+
*/
98+
public CompletableFuture<UpdateStatus> check() {
99+
return CompletableFuture.supplyAsync(() -> {
100+
try {
101+
String distributedVersion = versionResolver.getVersion(this, resourceId);
102+
Status comparison = versionComparator.compareVersions(currentVersion, distributedVersion);
103+
104+
lastStatus = new UpdateStatus(currentVersion, distributedVersion, comparison);
105+
lastCheck = Instant.now().toEpochMilli();
106+
107+
statusHandler.accept(lastStatus);
108+
return lastStatus;
109+
} catch (UpdateLibException ex) {
110+
throw new IllegalStateException(ex);
111+
}
112+
});
101113
}
102114

103115
/**
104-
* Get last stored {@link UpdateStatus}. If UpdateLib hasn't checked for an update or the last check failed, this
105-
* will return null.
106-
*
107-
* @return Last stored {@link UpdateStatus}
108-
* @since 3.0.0-SNAPSHOT
116+
* Get the last update status.
117+
* @return Last update status.
109118
*/
110119
public UpdateStatus getLastStatus() {
111120
return lastStatus;
112121
}
113122

114123
/**
115-
* Get the last time UpdateLib checked for updates.
116-
*
117-
* @return Last check time in milliseconds.
118-
* @since 2.0.0-SNAPSHOT
124+
* Get the last time UpdateLib checked for an update.
125+
* @return Last check time.
119126
*/
120127
public long getLastCheck() {
121128
return lastCheck;
122129
}
123130

124131
/**
125-
* Get the current version of UpdateLib.
126-
*
127-
* @return Current version of UpdateLib.
128-
* @since 2.1.0-SNAPSHOT
132+
* Get the http connection timeout.
133+
* @return Connection timeout.
129134
*/
130-
public static String getVersion() {
131-
return VERSION;
135+
@Internal
136+
public int getTimeout() {
137+
return timeout;
132138
}
133139

134140
}

0 commit comments

Comments
 (0)