Skip to content

Commit

Permalink
Merge pull request #29 from ServerJars/fix/new-urls-and-options
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Fran authored Jul 30, 2022
2 parents 0564d64 + 505f6c4 commit f7acb62
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 134 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ buildNumber.properties
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
.idea/**/compiler.xml
cmake-build-*/
.idea/**/mongoSettings.xml
.idea
*.iws
out/
.idea_modules/
Expand Down Expand Up @@ -77,4 +79,5 @@ bin/
*.pom.xml
modules/
plugins/
!gradle-wrapper.jar
!gradle-wrapper.jar
.fleet/
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.songoda'
version '3.1.0'
version '3.2.0'

repositories {
mavenLocal()
Expand Down
192 changes: 100 additions & 92 deletions src/main/java/com/songoda/serverjars/ServerJars.java

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions src/main/java/com/songoda/serverjars/handlers/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,68 @@ public class ConfigHandler {

@Getter
@Setter
private boolean skipConfigCreation = false, debug = false;
private boolean skipConfigCreation = false, debug = false, useHttps = true, useVersionCli = false, useTypeCli = false, useCategoryCli = false, useHomeDirCli = false;

@Getter
@Setter
private String apiDomain = "serverjars.com";

public void init() {
try {
if(!this.skipConfigCreation && !ServerJars.CFG_FILE.exists()) { // Create the config if it doesn't exist, and we aren't skipping it
ServerJars.CFG_FILE.createNewFile();
}

this.load();
} catch (IOException e) {
e.printStackTrace();
}
}

public void load() throws IOException {
if(ServerJars.CFG_FILE.exists()){
String type = this.getType(), version = this.getVersion();
String category = this.getCategory(), type = this.getType(), version = this.getVersion();
boolean useHomeDirectory = this.useHomeDirectory();
properties.load(new FileInputStream(ServerJars.CFG_FILE));
if(this.isSkipConfigCreation()){
this.setType(type);

if(useVersionCli) { // To give priority to the cli
this.setVersion(version);
}

if(useCategoryCli){ // To give priority to the cli
this.setCategory(category);
}

if(useTypeCli) { // To give priority to the cli
this.setType(type);
}

if(useHomeDirCli){ // To give priority to the cli
this.setUseHomeDirectory(useHomeDirectory);
}
}
}

public void reset() {
String category = this.getCategory(), type = this.getType(), version = this.getVersion();
boolean useHomeDir = this.useHomeDirectory();
properties.clear();
this.setType(this.getType());
this.setVersion(this.getVersion());
this.setUseHomeDirectory(this.useHomeDirectory());
this.setCategory(useCategoryCli ? category : this.getCategory());
this.setType(useTypeCli ? type : this.getType());
this.setVersion(useVersionCli ? version : this.getVersion());
this.setUseHomeDirectory(useHomeDirCli ? useHomeDir : this.useHomeDirectory());
}

public void save() throws IOException{
properties.store(new FileOutputStream(ServerJars.CFG_FILE), HEADER);
}

public String getCategory() {
return properties.getProperty("category", "servers");
}

public void setCategory(String category){
properties.setProperty("category", category);
}

public String getType(){
return properties.getProperty("type", "paper");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,31 @@ public void run(String data) {
case "skipConfigCreation":
ServerJars.config.setSkipConfigCreation(Utils.bool(value));
break;
case "serverCategory":
ServerJars.config.setCategory(value);
ServerJars.config.setUseCategoryCli(true);
break;
case "serverType":
ServerJars.config.setType(value);
ServerJars.config.setUseTypeCli(true);
break;
case "version":
ServerJars.config.setVersion(value);
ServerJars.config.setUseVersionCli(true);
break;
case "useHomeDirectory":
ServerJars.config.setUseHomeDirectory(Utils.bool(value));
ServerJars.config.setUseHomeDirCli(true);
break;
case "debug":
ServerJars.config.setDebug(Utils.bool(value));
break;
case "apiDomain":
ServerJars.config.setApiDomain(value);
break;
case "useHttps":
ServerJars.config.setUseHttps(Utils.bool(value));
break;
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/songoda/serverjars/utils/OnlineRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.google.gson.JsonParser;
import lombok.Getter;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
Expand All @@ -26,17 +26,18 @@ public OnlineRequest(String url) {
}

public Response connect() throws IOException {
long time = this.timeCachedResponse.getOrDefault(this.getUrl(), 0L);
if(!this.cachedResponse.containsKey(this.getUrl()) || (time == 0L || (System.currentTimeMillis() - time) < 15000L)) {
String key = this.getUrl();
long time = this.timeCachedResponse.getOrDefault(key, 0L);
if(!this.cachedResponse.containsKey(key) || (time == 0L || (System.currentTimeMillis() - time) < 15000L)) {
URL url = new URL(this.getUrl());
HttpsURLConnection connection = ((HttpsURLConnection) url.openConnection());
HttpURLConnection connection = ((HttpURLConnection) url.openConnection());
Response response = new Response(connection);
this.cachedResponse.put(this.getUrl(), response);
this.timeCachedResponse.put(this.getUrl(), System.currentTimeMillis());
this.cachedResponse.put(key, response);
this.timeCachedResponse.put(key, System.currentTimeMillis());
return response;
}

return this.cachedResponse.get(this.getUrl());
return this.cachedResponse.get(key);
}

public static class Response {
Expand All @@ -53,7 +54,7 @@ public static class Response {
@Getter
private final InputStream inputStream;

public Response(HttpsURLConnection connection) throws IOException {
public Response(HttpURLConnection connection) throws IOException {
this.statusCode = connection.getResponseCode();
this.statusMessage = connection.getResponseMessage();
this.inputStream = connection.getInputStream();
Expand Down
Loading

0 comments on commit f7acb62

Please sign in to comment.