MineAlert/src/main/java/xyz/etztech/minealert/MineAlert.java

151 lines
5.2 KiB
Java

package xyz.etztech.minealert;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.Javacord;
import xyz.etztech.Webhook;
import xyz.etztech.embed.Author;
import xyz.etztech.embed.Embed;
import xyz.etztech.embed.Field;
import xyz.etztech.minealert.commands.AlertMute;
import xyz.etztech.minealert.commands.MainCommand;
import xyz.etztech.minealert.listeners.GriefAlertListener;
import xyz.etztech.minealert.listeners.OreAlertListener;
import java.time.OffsetDateTime;
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 GriefAlertListener(this);
new OreAlertListener(this);
new AlertMute(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;
}
/**
* @param player Player to check
* @param muteType Type of alert to check fo
* @return True if the player ignores the alert, false otherwise
*/
static public Boolean hasIgnoreAlertPerm(Player player, MuteType muteType) {
return player.hasPermission("minealert.ignore") || player.hasPermission(muteType.getMetadataValue());
}
/**
* @param baseURL Template URL string
* @param player Player that generated the alert
* @param location Location of the alert
* @return Formatted alert URL
*/
static public String formatAlertURL(String baseURL, Player player, Location location) {
return baseURL.replaceAll("\\{username}", player.getName())
.replaceAll("\\{x}", String.valueOf(location.getBlockX()))
.replaceAll("\\{y}", String.valueOf(location.getBlockY()))
.replaceAll("\\{z}", String.valueOf(location.getBlockZ()))
.replaceAll("\\{world_name}", location.getWorld().getName());
}
/**
* @param location Location to format
* @return Location string
*/
static public String formattedLocation(Location location) {
return String.format("%d %d %d", location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
/**
* @param color Color of the embed
* @param player Player the alert is for
* @param location Location of the alert
* @param message Alert message
* @param baseURL Template url
* @return Alert Embed
*/
static public Embed buildAlertEmbed(Color color, Player player, Location location, String message, String baseURL) {
String usernameURL = formatAlertURL(baseURL, player, location);
return new Embed()
.color(color.getInt())
.description(Javacord.escapeFormat(message))
.timestamp(OffsetDateTime.now())
.author(new Author(player.getName(),
!"".equals(usernameURL) ? usernameURL : "",
String.format("https://minotar.net/helm/%s/100.png", player.getName()),
""))
.addField(new Field("Location", String.format("`%s`", MineAlert.formattedLocation(location))))
.addField(new Field("World", String.format("`%s`", location.getWorld().getName())));
}
/**
* @param webhookURL URL to send the webhook to
* @param color Color of the embed
* @param player Player the alert is for
* @param location Location of the alert
* @param message Alert message
* @param baseURL Template url
*/
public void sendWebhook(String webhookURL, Color color, Player player, Location location, String message, String baseURL) {
this.getServer().getScheduler().runTaskAsynchronously(this, () -> {
Embed embed = buildAlertEmbed(color, player, location, message, baseURL);
try {
Javacord.sendWebhook(webhookURL, new Webhook("@here", embed));
} catch (Exception e) {
this.log(Lang.WEBHOOK_FAILED.getMessage());
}
});
}
}