Skip to content

Commit

Permalink
Merge pull request #84 from Andre601/feature/3.8.1-add-metrics-to-vel…
Browse files Browse the repository at this point in the history
…ocity

Add Metrics back to Velocity module
  • Loading branch information
Andre601 authored Jul 8, 2021
2 parents dca1a3e + 5bffa13 commit a071ba5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> protocols = configHandler.getIntList("Protocol", "Versions");
boolean versionsSet;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand All @@ -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;
Expand All @@ -86,7 +85,7 @@ public boolean loadFile(){

public boolean setupConfigurate(){
GsonConfigurationLoader loader = GsonConfigurationLoader.builder()
.file(file)
.file(file.toFile())
.build();

try{
Expand All @@ -110,19 +109,45 @@ public InputStream updateCache(){
HttpResponse<InputStream> 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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<plugin.version>3.8.0</plugin.version>
<plugin.version>3.8.1</plugin.version>
<plugin.description>Only allow specific client versions on your Network.</plugin.description>

<maven.compiler.target>11</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -82,11 +88,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()));

}

Expand Down

0 comments on commit a071ba5

Please sign in to comment.