Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.json.simple.parser.JSONParser;

import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -36,6 +36,9 @@ public class LegacyMinecraftPinger extends JavaPlugin {

private boolean firstPing = true;

private boolean lastRequestFailed;
private long lastFailedRequestTime;

@Override
public void onEnable() {
plugin = this;
Expand Down Expand Up @@ -83,6 +86,9 @@ public void onEnable() {
LMPConfig.generateConfigOption("settings.force-server-uuid.value", "");
LMPConfig.generateConfigOption("settings.force-server-uuid.info", "Allows a server owner to force the UUID the server. This is recommend once you receive a key meaning your UUID won't change if any of your details do. YOUR SERVER MUST HAVE A VALID KEY FOR THE APPROPRIATE UUID TO USE THIS SETTING.");

LMPConfig.generateConfigOption("settings.http.waitAfterFail", true);
LMPConfig.generateConfigOption("settings.http.waitPeriodMinutes", 60);

LMPConfig.generateConfigOption("flags.BetaEvolutions.enabled", false);
LMPConfig.generateConfigOption("flags.BetaEvolutions.info", "Enabled this if your server runs Beta Evolutions");
LMPConfig.generateConfigOption("flags.MineOnline.enabled", true);
Expand All @@ -109,13 +115,22 @@ public void onEnable() {
}
}


taskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
if (lastRequestFailed && LMPConfig.getConfigBoolean("settings.http.waitAfterFail", true))
{
long timeDiffMinutes = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - lastFailedRequestTime);
int waitPeriod = LMPConfig.getConfigInteger("settings.http.waitPeriodMinutes", 60);
if (timeDiffMinutes < waitPeriod)
return;
}

final String jsonData = generateJsonData().toJSONString();
final String apiURL = LMPConfig.getConfigString("url");
Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, () -> {
//Post code directly copied from: https://github.com/codieradical/MineOnlineBroadcast-Bukkit/blob/master/src/MineOnlineBroadcast.java
// tweaked slightly by moderator_man for wait-after-fail functionality
HttpURLConnection connection = null;
int responseCode = 0;
try {
URL url = new URL(apiURL);
connection = (HttpURLConnection) url.openConnection();
Expand Down Expand Up @@ -174,28 +189,56 @@ public void onEnable() {


} catch (Exception e) {
lastRequestFailed = true;
lastFailedRequestTime = System.currentTimeMillis();

plugin.logger(Level.INFO, "Malformed JSON response after ping returned normal status code: " + e + ": " + e.getMessage());
return;
}

responseCode = tryGetResponseCode(connection);

rd.close();
} catch (Exception e) {
responseCode = tryGetResponseCode(connection);
lastRequestFailed = true;
lastFailedRequestTime = System.currentTimeMillis();

plugin.logger(Level.WARNING, "An error occurred when attempting to ping: " + e + ": " + e.getMessage());
if (this.LMPConfig.getConfigBoolean("debug")) {
plugin.logger(Level.WARNING, "Ping Object: " + jsonData);
}
} finally {
if (connection != null)
{
if (responseCode == 0)
plugin.logger(Level.WARNING, "Ping request didn't properly set the responseCode variable. Weird.");

if (responseCode == 200)
{
lastRequestFailed = false;
lastFailedRequestTime = 0;
}
connection.disconnect();
}
}
}, 0L);
}, 20, 20L * LMPConfig.getConfigInteger("pingTime", 45));
}

}, 20, 20 * Integer.valueOf(String.valueOf(LMPConfig.getConfigOption("pingTime", 45))));

private int tryGetResponseCode(HttpURLConnection connection)
{
if (connection == null)
return 500;

try
{
return connection.getResponseCode();
} catch (Exception ex) {
return 500;
}
}


public void verifyUUIDString() {
if (LMPConfig.getConfigBoolean("settings.force-server-uuid.enabled")) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public interface ConfigurationFile {

public Integer getConfigInteger(String key);

public Integer getConfigInteger(String key, Integer defaultValue);

public Long getConfigLong(String key);

public Double getConfigDouble(String key);

public Boolean getConfigBoolean(String key);

public Boolean getConfigBoolean(String key, Boolean defaultValue);

// public void writeConfigurationFile();

public void generateConfigOption(String key, Object defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public Integer getConfigInteger(String key) {
return Integer.valueOf(getConfigString(key));
}

@Override
public Integer getConfigInteger(String key, Integer defaultValue)
{
String cfgValueRaw = getConfigString(key);
if (cfgValueRaw == null || cfgValueRaw.isEmpty())
return defaultValue;
return Integer.valueOf(cfgValueRaw);
}

public Long getConfigLong(String key) {
return Long.valueOf(getConfigString(key));
}
Expand All @@ -57,6 +66,14 @@ public Boolean getConfigBoolean(String key) {
return Boolean.valueOf(getConfigString(key));
}

public Boolean getConfigBoolean(String key, Boolean defaultValue)
{
String cfgValueRaw = getConfigString(key);
if (cfgValueRaw == null || cfgValueRaw.isEmpty() || !(cfgValueRaw.equalsIgnoreCase("true") || cfgValueRaw.equalsIgnoreCase("false")))
return defaultValue;
return Boolean.valueOf(cfgValueRaw);
}

@Override
public void writeConfigurationFile() {
if (fileChanged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public Integer getConfigInteger(String key) {
return Integer.valueOf(getConfigString(key));
}

@Override
public Integer getConfigInteger(String key, Integer defaultValue)
{
String cfgValueRaw = getConfigString(key);
if (cfgValueRaw == null || cfgValueRaw.isEmpty())
return defaultValue;
return Integer.valueOf(cfgValueRaw);
}

public Long getConfigLong(String key) {
return Long.valueOf(getConfigString(key));
}
Expand All @@ -73,6 +82,14 @@ public Boolean getConfigBoolean(String key) {
return Boolean.valueOf(getConfigString(key));
}

public Boolean getConfigBoolean(String key, Boolean defaultValue)
{
String cfgValueRaw = getConfigString(key);
if (cfgValueRaw == null || cfgValueRaw.isEmpty() || !(cfgValueRaw.equalsIgnoreCase("true") || cfgValueRaw.equalsIgnoreCase("false")))
return defaultValue;
return Boolean.valueOf(cfgValueRaw);
}


//Getters End

Expand Down