-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
plugin/src/main/java/me/bryang/chatlab/listener/ServerLoadListener.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,25 @@ | ||
package me.bryang.chatlab.listener; | ||
|
||
import me.bryang.chatlab.update.UpdateChecker; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.server.ServerLoadEvent; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class ServerLoadListener implements Listener { | ||
|
||
@Inject | ||
private UpdateChecker updateChecker; | ||
|
||
@EventHandler | ||
public void onLoad(ServerLoadEvent event){ | ||
|
||
if (updateChecker.enabledInAsync()){ | ||
return; | ||
} | ||
|
||
updateChecker.checkUpdate(); | ||
} | ||
|
||
} |
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
5 changes: 5 additions & 0 deletions
5
plugin/src/main/java/me/bryang/chatlab/update/UpdateAnnouncementType.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,5 @@ | ||
package me.bryang.chatlab.update; | ||
|
||
public enum UpdateAnnouncementType { | ||
SERVER, CONSOLE, BOTH; | ||
} |
129 changes: 129 additions & 0 deletions
129
plugin/src/main/java/me/bryang/chatlab/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,129 @@ | ||
package me.bryang.chatlab.update; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import me.bryang.chatlab.ChatLab; | ||
import me.bryang.chatlab.configuration.ConfigurationContainer; | ||
import me.bryang.chatlab.configuration.section.RootSection; | ||
import org.slf4j.Logger; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
@Singleton | ||
public class UpdateChecker { | ||
|
||
@Inject | ||
private ChatLab plugin; | ||
|
||
@Inject | ||
private ConfigurationContainer<RootSection> configContainer; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
private boolean isSent; | ||
private boolean updated; | ||
private String lastVersion; | ||
private UpdateAnnouncementType updateAnnouncementType; | ||
private final HttpClient httpClient = HttpClient.newHttpClient(); | ||
|
||
|
||
public void init() { | ||
|
||
|
||
RootSection rootSection = configContainer.get(); | ||
AtomicReference<JsonObject> jsonObject = new AtomicReference<>(); | ||
|
||
URI uri; | ||
try { | ||
uri = new URI("https://api.github.com/repos/DevBlook/ChatLab/releases/latest"); | ||
|
||
} catch (URISyntaxException exception) { | ||
exception.fillInStackTrace(); | ||
return; | ||
} | ||
|
||
HttpRequest request = HttpRequest | ||
.newBuilder() | ||
.uri(uri) | ||
.header("accept", "application/vnd.github+json") | ||
.GET() | ||
.build(); | ||
|
||
|
||
httpClient | ||
.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenAccept(action -> { | ||
|
||
JsonElement jsonElement = JsonParser.parseString(action.body()); | ||
|
||
jsonObject.set(jsonElement.getAsJsonObject()); | ||
|
||
String selectedLatestVersion = jsonObject.get().get("tag_name").getAsString(); | ||
|
||
lastVersion = selectedLatestVersion; | ||
updated = selectedLatestVersion.equalsIgnoreCase(plugin.getPluginMeta().getVersion()); | ||
|
||
try { | ||
|
||
updateAnnouncementType = UpdateAnnouncementType.valueOf( | ||
rootSection.settings.updateAnnouncementType.toUpperCase()); | ||
|
||
} catch (IllegalArgumentException illegalArgumentException) { | ||
updateAnnouncementType = UpdateAnnouncementType.CONSOLE; | ||
|
||
} | ||
|
||
if (!enabledInAsync()) { | ||
return; | ||
} | ||
|
||
checkUpdate(); | ||
|
||
|
||
|
||
}); | ||
isSent = true; | ||
} | ||
|
||
public void checkUpdate(){ | ||
|
||
|
||
if (!announcementPresent(UpdateAnnouncementType.CONSOLE)){ | ||
return; | ||
} | ||
|
||
if (!isUpdated()) { | ||
logger.info("The plugin has a new update. New version:" + lastVersion()); | ||
logger.info("Download here: https://github.com/devblook/chatlab/releases/latest"); | ||
|
||
|
||
} | ||
|
||
} | ||
public String lastVersion() { | ||
return lastVersion; | ||
} | ||
|
||
public boolean isUpdated() { | ||
return updated; | ||
} | ||
|
||
public boolean announcementPresent(UpdateAnnouncementType selectedAnnouncementType) { | ||
return (selectedAnnouncementType == updateAnnouncementType || updateAnnouncementType == UpdateAnnouncementType.BOTH); | ||
} | ||
public boolean enabledInAsync(){ | ||
return isSent; | ||
} | ||
|
||
|
||
|
||
} |