90 lines
2.5 KiB
Java
90 lines
2.5 KiB
Java
package xyz.etztech.minealert;
|
|
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import xyz.etztech.minealert.commands.MainCommand;
|
|
import xyz.etztech.minealert.listeners.BlockBreakListener;
|
|
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLConnection;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.logging.Logger;
|
|
|
|
public class MineAlert extends JavaPlugin {
|
|
|
|
private static MineAlert instance;
|
|
private final Logger log = Logger.getLogger( "Minecraft" );
|
|
|
|
public void onEnable() {
|
|
instance = this;
|
|
saveDefaultConfig();
|
|
reloadConfig();
|
|
|
|
if (isEnabled()) {
|
|
new MainCommand(this);
|
|
new BlockBreakListener(this);
|
|
}
|
|
}
|
|
|
|
public void log(String message) {
|
|
log.info( "[MineAlert]: " + message );
|
|
}
|
|
|
|
public static MineAlert getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* @param def The default if no paths resolve
|
|
* @param path Config paths to check, from specific -> fallback
|
|
* @return The resolved String value
|
|
*/
|
|
public String getConfigStringFallback(String def, String ...path) {
|
|
String fallback = "";
|
|
for (String p : path) {
|
|
fallback = getConfig().getString(p, fallback);
|
|
if (!"".equals(fallback)) {
|
|
return fallback;
|
|
}
|
|
}
|
|
return def;
|
|
}
|
|
|
|
/**
|
|
* @param def The default if no paths resolve
|
|
* @param path Config paths to check, from specific -> fallback
|
|
* @return The resolved Int value
|
|
*/
|
|
public int getConfigIntFallback(int def, String ...path) {
|
|
int fallback = 0;
|
|
for (String p : path) {
|
|
fallback = getConfig().getInt(p, fallback);
|
|
if (fallback != 0) {
|
|
return fallback;
|
|
}
|
|
}
|
|
return def;
|
|
}
|
|
|
|
public static void sendWebhook(String webhook, String embed) throws Exception {
|
|
URL url = new URL(webhook);
|
|
URLConnection con = url.openConnection();
|
|
HttpURLConnection http = (HttpURLConnection) con;
|
|
http.setRequestMethod("POST");
|
|
http.setDoOutput(true);
|
|
|
|
byte[] out = embed.getBytes(StandardCharsets.UTF_8);
|
|
int length = out.length;
|
|
http.setFixedLengthStreamingMode(length);
|
|
http.setRequestProperty("Content-Type", "application/json; utf-8");
|
|
http.setRequestProperty("User-Agent", "MineAlert Agent");
|
|
|
|
try (OutputStream os = http.getOutputStream()) {
|
|
os.write(out, 0, out.length);
|
|
}
|
|
}
|
|
}
|
|
|