diff --git a/core/pom.xml b/core/pom.xml index e90b7c7..f326ae1 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -68,6 +68,11 @@ 4.8.1 provided + + com.squareup.okhttp3 + okhttp + 4.9.0 + @@ -112,6 +117,10 @@ net.kyori com.andre601.oneversionremake.core.dependencies.adventure + + okhttp3 + com.andre601.oneversionremake.core.dependencies.okhttp + diff --git a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java index 32c7e7a..b504951 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java @@ -47,7 +47,7 @@ public OneVersionRemake(PluginCore pluginCore){ this.protocolVersionResolver = new ProtocolVersionResolver(this, pluginCore.getPath()); this.parser = new Parser(this); - start(); + load(); } public ProxyLogger getProxyLogger(){ @@ -128,7 +128,7 @@ public List getPlayers(Class clazz, List lines, List } } - private void start(){ + private void load(){ loadVersion(); printBanner(); @@ -139,19 +139,34 @@ private void start(){ return; } - if(!loadFile()) - return; + if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ + if(getProtocolVersionResolver().loadFile()){ + getProxyLogger().info("Updated versions.json!"); + enable(); + }else{ + getProxyLogger().warn("Unable to update versions.json! Check previous lines for warnings and errors."); + } + }else{ + if(getProtocolVersionResolver().setupConfigurate()){ + getProxyLogger().info("Loaded versions.json!"); + enable(); + }else{ + getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings."); + } + } + } + private void enable(){ List protocols = configHandler.getIntList("Protocol", "Versions"); boolean versionsSet; if(protocols.isEmpty()){ printWarning(); - + versionsSet = false; }else{ getProxyLogger().info("Loaded the following Protocol Version(s):"); getProxyLogger().info(getProtocolVersionResolver().getFriendlyNames(protocols, false)); - + versionsSet = true; } @@ -212,25 +227,4 @@ private void loadVersion(){ version = "UNKNOWN"; } } - - private boolean loadFile(){ - if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ - getProxyLogger().info("Fetching latest versions.json from GitHub.com..."); - if(getProtocolVersionResolver().loadFile()){ - getProxyLogger().info("Updated versions.json!"); - return true; - }else{ - getProxyLogger().warn("Unable to update versions.json! Check previous lines for errors and warnings."); - } - }else{ - if(getProtocolVersionResolver().setupConfigurate()){ - getProxyLogger().info("Loaded versions.json!"); - return true; - }else{ - getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings."); - } - } - - return false; - } } diff --git a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java index 6f7c3eb..3291911 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java @@ -20,17 +20,14 @@ import com.andre601.oneversionremake.core.OneVersionRemake; import com.andre601.oneversionremake.core.interfaces.ProxyLogger; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.gson.GsonConfigurationLoader; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -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.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -40,23 +37,20 @@ import java.util.stream.Stream; public class ProtocolVersionResolver{ - private final OneVersionRemake core; - private final HttpClient client = HttpClient.newHttpClient(); + private final OkHttpClient CLIENT = new OkHttpClient(); private final ProxyLogger logger; private final Path file; - private final File path; + private final Path path; private ConfigurationNode node = null; public ProtocolVersionResolver(OneVersionRemake core, Path path){ - this.core = core; - this.logger = core.getProxyLogger(); this.file = path.resolve("versions.json"); - this.path = path.toFile(); + this.path = path; } public boolean hasFile(){ @@ -64,23 +58,12 @@ public boolean hasFile(){ } public boolean loadFile(){ - if(!path.isDirectory() && !path.mkdirs()){ + if(!path.toFile().isDirectory() && !path.toFile().mkdirs()){ logger.warn("Could not create folder for plugin!"); return false; } - try(InputStream is = updateCache()){ - if(is == null){ - return false; - } - - Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING); - }catch(IOException ex){ - logger.warn("Unable to create versions.json! Encountered IOException.", ex); - return false; - } - - return setupConfigurate(); + return updateCache(); } public boolean setupConfigurate(){ @@ -98,57 +81,55 @@ public boolean setupConfigurate(){ return true; } - public InputStream updateCache(){ - try{ - HttpRequest request = HttpRequest.newBuilder() - .GET() - .header("User-Agent", "OneVersionRemake") - .uri(new URI("https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json")) - .build(); - - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); - - if(response.statusCode() != 200){ - String CONNECTION_ERR = "Unable to establish connection! Status-Code %d (%s)"; - - switch(response.statusCode()){ + public boolean updateCache(){ + String url = "https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json"; + Request request = new Request.Builder() + .url(url) + .addHeader("User-Agent", "OneVersionRemake") + .build(); + + try(Response response = CLIENT.newCall(request).execute()){ + if(!response.isSuccessful()){ + logger.warn(String.format( + "Unable to establish connection! Status-Code %d (%s) received!", + response.code(), + response.message() + )); + switch(response.code()){ case 404: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Site not available")); - logger.warn("Please report this to the Developer!"); - return null; - - case 429: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Rate Limited")); - logger.warn("You connect too many times in a short period. Please delay any further restarts."); - return null; - - case 500: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Internal Server Error")); - logger.warn("The Server (GitHub) had an unexpected error when handling the request. Try again later."); - return null; - - default: - logger.warn("Encountered unknown Response code " + response.statusCode()); - logger.warn("Inform the developer about this on their Discord. This is NOT a bug however!"); - return null; + logger.warn(String.format( + "The requested site (%s) does not exist. Please report this to the developer on Discord!", + url + )); + break; + + case 429: + logger.warn("Encountered a Rate Limit. Please delay any future Proxy Restarts to avoid this."); + break; + + case 500: + logger.warn("The Site (GitHub.com) encountered an error when handling the request. Try again later..."); + break; + + default: + logger.warn("This is an unknown error by the plugin! Please report this to the developer on Discord!"); + break; } + return false; } - return response.body(); - }catch(Exception ex){ - if(ex instanceof URISyntaxException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! URI was invalid!"); - }else - if(ex instanceof IOException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was non-successful!"); - }else - if(ex instanceof InterruptedException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was interrupted!"); - }else{ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Received unknown Exception!", ex); + ResponseBody body = response.body(); + if(body == null){ + logger.warn("GitHub.com returned an invalid/empty body!"); + return false; } - return null; + Files.copy(body.byteStream(), file, StandardCopyOption.REPLACE_EXISTING); + + return setupConfigurate(); + }catch(IOException ex){ + logger.warn("Encountered IOException while performing a request!", ex); + return false; } } diff --git a/pom.xml b/pom.xml index 31c9a99..dd2d09c 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 3.8.1 + 3.8.2 Only allow specific client versions on your Network. 11