parent
d7e9ce7925
commit
b7375b0794
2
pom.xml
2
pom.xml
|
@ -3,7 +3,7 @@
|
||||||
<groupId>xyz.etztech</groupId>
|
<groupId>xyz.etztech</groupId>
|
||||||
<artifactId>MineAlert</artifactId>
|
<artifactId>MineAlert</artifactId>
|
||||||
<!-- Version is used in plugin.yml -->
|
<!-- Version is used in plugin.yml -->
|
||||||
<version>0.0.1</version>
|
<version>0.0.3</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<!-- Plugin Information -->
|
<!-- Plugin Information -->
|
||||||
|
|
|
@ -5,16 +5,16 @@ import net.md_5.bungee.api.chat.TextComponent;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
|
|
||||||
public enum Color {
|
public class Color {
|
||||||
DEFAULT("#AAAAAA"),
|
public static Color DEFAULT = new Color("#AAAAAA");
|
||||||
ERROR("#F14668"),
|
public static Color ERROR = new Color("#F14668");
|
||||||
INFO("#3298DC"),
|
public static Color INFO = new Color("#3298DC");
|
||||||
PRIMARY("#3273DC");
|
public static Color PRIMARY = new Color("#3273DC");
|
||||||
|
|
||||||
private final String hex;
|
private final String hex;
|
||||||
private final ChatColor chatColor;
|
private final ChatColor chatColor;
|
||||||
|
|
||||||
Color(String hex) {
|
public Color(String hex) {
|
||||||
this.hex = hex;
|
this.hex = hex;
|
||||||
this.chatColor = ChatColor.of(hex);
|
this.chatColor = ChatColor.of(hex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,13 @@ public enum Lang {
|
||||||
NO_PERMISSION("You don't have permission to do that.", Color.ERROR),
|
NO_PERMISSION("You don't have permission to do that.", Color.ERROR),
|
||||||
UNKNOWN_COMMAND("This command wasn't recognized.", Color.ERROR),
|
UNKNOWN_COMMAND("This command wasn't recognized.", Color.ERROR),
|
||||||
PLUGIN_RELOADED("MineAlert reloaded.", Color.INFO),
|
PLUGIN_RELOADED("MineAlert reloaded.", Color.INFO),
|
||||||
ALERT("%s has found %d %s veins.", Color.DEFAULT),
|
WEBHOOK_FAILED("Could not send webhook.", Color.ERROR),
|
||||||
NOT_ENOUGH_ARGS("%s requires %d arguments.", Color.ERROR),
|
|
||||||
WEBHOOK_FAILED("Could not send webhook.", Color.ERROR);
|
ORE_ALERT("%s has found %d %s veins.", Color.DEFAULT),
|
||||||
|
|
||||||
|
IGNITE_ALERT("%s started a fire.", Color.DEFAULT),
|
||||||
|
TNT_ALERT("%s placed TnT.", Color.DEFAULT),
|
||||||
|
LAVA_ALERT("%s poured lava.", Color.DEFAULT);
|
||||||
|
|
||||||
private final String message;
|
private final String message;
|
||||||
private final Color color;
|
private final Color color;
|
||||||
|
@ -21,8 +25,8 @@ public enum Lang {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMessage() {
|
public String getMessage(Object ...args) {
|
||||||
return this.message;
|
return String.format(this.message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color getColor() {
|
public Color getColor() {
|
||||||
|
|
|
@ -4,6 +4,7 @@ package xyz.etztech.minealert;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import xyz.etztech.minealert.commands.MainCommand;
|
import xyz.etztech.minealert.commands.MainCommand;
|
||||||
import xyz.etztech.minealert.listeners.BlockBreakListener;
|
import xyz.etztech.minealert.listeners.BlockBreakListener;
|
||||||
|
import xyz.etztech.minealert.listeners.GriefAlertListener;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -20,6 +21,7 @@ public class MineAlert extends JavaPlugin {
|
||||||
if (isEnabled()) {
|
if (isEnabled()) {
|
||||||
new MainCommand(this);
|
new MainCommand(this);
|
||||||
new BlockBreakListener(this);
|
new BlockBreakListener(this);
|
||||||
|
new GriefAlertListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -147,7 +147,7 @@ public class BlockBreakListener implements Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void alert(BlockEvent event, int strikes, boolean ping) {
|
private void alert(BlockEvent event, int strikes, boolean ping) {
|
||||||
String message = String.format(Lang.ALERT.getMessage(),
|
String message = String.format(Lang.ORE_ALERT.getMessage(),
|
||||||
event.getPlayer().getName(), strikes, Lang.getMaterialName(event.getMaterial()));
|
event.getPlayer().getName(), strikes, Lang.getMaterialName(event.getMaterial()));
|
||||||
String hexColor = this.plugin.getConfigStringFallback(
|
String hexColor = this.plugin.getConfigStringFallback(
|
||||||
"#AAAAAA",
|
"#AAAAAA",
|
||||||
|
|
|
@ -0,0 +1,124 @@
|
||||||
|
package xyz.etztech.minealert.listeners;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.BlockIgniteEvent;
|
||||||
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
|
import org.bukkit.event.player.PlayerBucketEmptyEvent;
|
||||||
|
import xyz.etztech.Javacord;
|
||||||
|
import xyz.etztech.Webhook;
|
||||||
|
import xyz.etztech.embed.Author;
|
||||||
|
import xyz.etztech.embed.Embed;
|
||||||
|
import xyz.etztech.minealert.Color;
|
||||||
|
import xyz.etztech.minealert.Lang;
|
||||||
|
import xyz.etztech.minealert.MineAlert;
|
||||||
|
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class GriefAlertListener implements Listener {
|
||||||
|
private final MineAlert plugin;
|
||||||
|
private final Map<String, List<Date>> map = new HashMap<>();
|
||||||
|
|
||||||
|
public GriefAlertListener(MineAlert plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
|
||||||
|
this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, this::purge, 0, 20 * 60 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void purge() {
|
||||||
|
Date now = Calendar.getInstance().getTime();
|
||||||
|
int purge = 1000 * 60 * this.plugin.getConfig().getInt("grief.reset", 10);
|
||||||
|
for (Iterator<List<Date>> it = map.values().iterator(); it.hasNext(); ) {
|
||||||
|
List<Date> dates = it.next();
|
||||||
|
dates.removeIf(date -> new Date(date.getTime() + purge).before(now));
|
||||||
|
if (dates.size() == 0) it.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAlert(String playerName, Lang lang) {
|
||||||
|
String alert = lang.getMessage(playerName);
|
||||||
|
purge();
|
||||||
|
List<Date> dates = map.getOrDefault(alert, new ArrayList<>());
|
||||||
|
dates.add(new Date());
|
||||||
|
map.put(alert, dates);
|
||||||
|
|
||||||
|
Color color = new Color(plugin.getConfigStringFallback(
|
||||||
|
"#FFA500",
|
||||||
|
"grief.color"
|
||||||
|
));
|
||||||
|
String usernameURL = this.plugin.getConfigStringFallback(
|
||||||
|
"",
|
||||||
|
"grief.url",
|
||||||
|
"url"
|
||||||
|
).replaceAll("\\{username}", playerName);
|
||||||
|
|
||||||
|
int threshold = this.plugin.getConfig().getInt("grief.threshold", 5);
|
||||||
|
if (dates.size() < threshold) {
|
||||||
|
sendAlert(alert, color);
|
||||||
|
} else if (dates.size() == threshold) {
|
||||||
|
StringBuilder extra = new StringBuilder(" Suppressing more alerts for a while");
|
||||||
|
String webhook = this.plugin.getConfigStringFallback(
|
||||||
|
"",
|
||||||
|
"grief.webhook",
|
||||||
|
"webhook"
|
||||||
|
);
|
||||||
|
if (!"".equals(webhook)) {
|
||||||
|
extra.append(" and pinging Discord");
|
||||||
|
Embed embed = new Embed()
|
||||||
|
.color(color.getInt())
|
||||||
|
.description(alert)
|
||||||
|
.timestamp(OffsetDateTime.now())
|
||||||
|
.author(new Author(Javacord.escapeFormat(playerName),
|
||||||
|
!"".equals(usernameURL) ? usernameURL : "",
|
||||||
|
String.format("https://minotar.net/helm/%s/100.png", playerName),
|
||||||
|
""));
|
||||||
|
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
||||||
|
try {
|
||||||
|
Javacord.sendWebhook(webhook, new Webhook("@here", embed));
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.plugin.log(Lang.WEBHOOK_FAILED.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
extra.append("...");
|
||||||
|
sendAlert(alert + extra.toString(), color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendAlert(String alert, Color color) {
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
if (player.hasPermission("minealert.alert")) {
|
||||||
|
color.sms(player, alert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockIgnite(BlockIgniteEvent event) {
|
||||||
|
if (event.getPlayer() != null &&
|
||||||
|
(event.getCause() == BlockIgniteEvent.IgniteCause.FLINT_AND_STEEL ||
|
||||||
|
event.getCause() == BlockIgniteEvent.IgniteCause.FIREBALL)) {
|
||||||
|
addAlert(event.getPlayer().getName(), Lang.IGNITE_ALERT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockPlace(BlockPlaceEvent event) {
|
||||||
|
if(event.getBlockPlaced().getType() == Material.TNT) {
|
||||||
|
addAlert(event.getPlayer().getName(), Lang.TNT_ALERT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBucketEmpty(PlayerBucketEmptyEvent event) {
|
||||||
|
if(event.getBucket() == Material.LAVA_BUCKET) {
|
||||||
|
addAlert(event.getPlayer().getName(), Lang.LAVA_ALERT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,6 +5,19 @@ webhook: ''
|
||||||
# Can use {username} as a placeholder for the player's username
|
# Can use {username} as a placeholder for the player's username
|
||||||
url: 'https://website.com/{username}'
|
url: 'https://website.com/{username}'
|
||||||
|
|
||||||
|
# GriefAlert
|
||||||
|
grief:
|
||||||
|
# How many alerts before temporarily muting
|
||||||
|
threshold: 5
|
||||||
|
# How long before un-muting (in minutes)
|
||||||
|
reset: 10
|
||||||
|
# Discord webhook
|
||||||
|
webhook: ''
|
||||||
|
# Webhook color
|
||||||
|
color: ''
|
||||||
|
# Override
|
||||||
|
url: ''
|
||||||
|
|
||||||
# OreAlert
|
# OreAlert
|
||||||
ore:
|
ore:
|
||||||
# Radius to search around block for similar blocks in the "vein"
|
# Radius to search around block for similar blocks in the "vein"
|
||||||
|
|
Loading…
Reference in New Issue