From 03f2ade02ab77039c9cc435644279c49e5932add Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Thu, 8 Jul 2021 15:58:23 +0200 Subject: [PATCH 1/2] Add Metrics back to Velocity module --- .../core/OneVersionRemake.java | 39 +++++++------ .../core/proxy/ProtocolVersionResolver.java | 57 +++++++++++++------ .../velocity/VelocityCore.java | 2 +- .../velocity/VelocityCore.java | 15 +++-- 4 files changed, 75 insertions(+), 38 deletions(-) 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 094d036..32c7e7a 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java @@ -139,22 +139,8 @@ private void start(){ return; } - if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ - getProxyLogger().info("Fetching latest versions.json from GitHub..."); - if(getProtocolVersionResolver().loadFile()){ - getProxyLogger().info("Updated versions.json!"); - }else{ - getProxyLogger().warn("Couldn't update versions.json! Check previous lines for any errors and warnings."); - return; - } - }else{ - if(getProtocolVersionResolver().setupConfigurate()){ - getProxyLogger().info("Loaded versions.json!"); - }else{ - getProxyLogger().warn("Couldn't load versions.json! Check previous lines for any errors and warnings."); - return; - } - } + if(!loadFile()) + return; List protocols = configHandler.getIntList("Protocol", "Versions"); boolean versionsSet; @@ -226,4 +212,25 @@ 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 fa5abcb..6f7c3eb 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 @@ -45,7 +45,7 @@ public class ProtocolVersionResolver{ private final ProxyLogger logger; - private final File file; + private final Path file; private final File path; private ConfigurationNode node = null; @@ -55,12 +55,12 @@ public ProtocolVersionResolver(OneVersionRemake core, Path path){ this.logger = core.getProxyLogger(); - this.file = new File(path.toFile(), "versions.json"); + this.file = path.resolve("versions.json"); this.path = path.toFile(); } public boolean hasFile(){ - return file.exists(); + return file.toFile().exists(); } public boolean loadFile(){ @@ -71,11 +71,10 @@ public boolean loadFile(){ try(InputStream is = updateCache()){ if(is == null){ - logger.warn("Unable to create versions.json! InputStream was null."); return false; } - Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING); }catch(IOException ex){ logger.warn("Unable to create versions.json! Encountered IOException.", ex); return false; @@ -86,7 +85,7 @@ public boolean loadFile(){ public boolean setupConfigurate(){ GsonConfigurationLoader loader = GsonConfigurationLoader.builder() - .file(file) + .file(file.toFile()) .build(); try{ @@ -110,19 +109,45 @@ public InputStream updateCache(){ HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); if(response.statusCode() != 200){ - logger.warn("Unable to establish connection! Site responded with non-successfull status-code " + response.statusCode() + "!"); - return null; + String CONNECTION_ERR = "Unable to establish connection! Status-Code %d (%s)"; + + switch(response.statusCode()){ + 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; + } } return response.body(); - }catch(URISyntaxException ex){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! URI was invalid!"); - return null; - }catch(IOException ex){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was non-successful!"); - return null; - }catch(InterruptedException ex){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was interrupted!"); + }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); + } + return null; } } diff --git a/velocity-legacy/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java b/velocity-legacy/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java index ecc6654..3531a32 100644 --- a/velocity-legacy/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java +++ b/velocity-legacy/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java @@ -57,9 +57,9 @@ public class VelocityCore implements PluginCore{ @Inject public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory factory){ this.logger = new VelocityLogger(LoggerFactory.getLogger("OneVersionRemake")); + this.proxy = proxy; this.path = path; - this.factory = factory; } diff --git a/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java b/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java index 72f2325..5912994 100644 --- a/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java +++ b/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java @@ -37,6 +37,8 @@ import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.server.ServerPing; +import org.bstats.charts.DrilldownPie; +import org.bstats.velocity.Metrics; import org.slf4j.LoggerFactory; import java.nio.file.Path; @@ -48,13 +50,17 @@ public class VelocityCore implements PluginCore{ private final ProxyServer proxy; private final Path path; + private final Metrics.Factory factory; + private OneVersionRemake core; - @Inject // TODO: Re-Add Metrics.Factory factory once Velocity 3 supports it - public VelocityCore(ProxyServer proxy, @DataDirectory Path path){ + @Inject + public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory factory){ this.logger = new VelocityLogger(LoggerFactory.getLogger("OneVersionRemake")); + this.proxy = proxy; this.path = path; + this.factory = factory; } @Subscribe @@ -83,10 +89,9 @@ public void loadEventListeners(){ @Override public void loadMetrics(){ getProxyLogger().info("Metrics currently not available. Skipping setup..."); - // TODO: Re-Add Metrics once available for Velocity 2 - //Metrics metrics = factory.make(this, 10341); + Metrics metrics = factory.make(this, 10341); - //metrics.addCustomChart(new DrilldownPie("allowed_protocols", () -> core.getPieMap())); + metrics.addCustomChart(new DrilldownPie("allowed_protocols", () -> core.getPieMap())); } From 5bffa137e20b0c56c210a585ba80bb693a089e52 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Thu, 8 Jul 2021 16:02:22 +0200 Subject: [PATCH 2/2] Bump version and remove misleading text --- pom.xml | 2 +- .../com/andre601/oneversionremake/velocity/VelocityCore.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c055484..31c9a99 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 3.8.0 + 3.8.1 Only allow specific client versions on your Network. 11 diff --git a/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java b/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java index 5912994..6048e11 100644 --- a/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java +++ b/velocity/src/main/java/com/andre601/oneversionremake/velocity/VelocityCore.java @@ -88,7 +88,6 @@ public void loadEventListeners(){ @Override public void loadMetrics(){ - getProxyLogger().info("Metrics currently not available. Skipping setup..."); Metrics metrics = factory.make(this, 10341); metrics.addCustomChart(new DrilldownPie("allowed_protocols", () -> core.getPieMap()));