Skip to content

Commit

Permalink
feat: create update check
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGmG committed Jun 11, 2023
1 parent 5352405 commit 3732633
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.bryang.chatlab.listener.PlayerChatListener;
import me.bryang.chatlab.listener.PlayerRegistryListener;
import me.bryang.chatlab.listener.ServerLoadListener;
import org.bukkit.event.Listener;
import team.unnamed.inject.AbstractModule;

Expand All @@ -13,6 +14,7 @@ public void configure() {
.asSet()
.to(PlayerRegistryListener.class)
.to(PlayerChatListener.class)
.to(ServerLoadListener.class)
.singleton();
}
}
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 plugin/src/main/java/me/bryang/chatlab/update/UpdateChecker.java
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;
}



}

0 comments on commit 3732633

Please sign in to comment.