Add grief alert (#2)
Merge branch 'master' into grief Merge branch 'master' of git.etztech.xyz:Minecraft/MineAlert into grief Signed-off-by: Etzelia <etzelia@hotmail.com> Add OPEN_URL to spigot message Signed-off-by: Etzelia <etzelia@hotmail.com> Add grief alert Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.etztech.xyz/Minecraft/MineAlert/pulls/2 Reviewed-by: ZeroHD <joey@ahines.net>master
parent
117a9c5607
commit
c159eea8fd
2
pom.xml
2
pom.xml
|
@ -3,7 +3,7 @@
|
|||
<groupId>xyz.etztech</groupId>
|
||||
<artifactId>MineAlert</artifactId>
|
||||
<!-- Version is used in plugin.yml -->
|
||||
<version>0.0.2</version>
|
||||
<version>0.0.3</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<!-- Plugin Information -->
|
||||
|
|
|
@ -5,16 +5,16 @@ import net.md_5.bungee.api.chat.TextComponent;
|
|||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
|
||||
public enum Color {
|
||||
DEFAULT("#AAAAAA"),
|
||||
ERROR("#F14668"),
|
||||
INFO("#3298DC"),
|
||||
PRIMARY("#3273DC");
|
||||
public class Color {
|
||||
public static Color DEFAULT = new Color("#AAAAAA");
|
||||
public static Color ERROR = new Color("#F14668");
|
||||
public static Color INFO = new Color("#3298DC");
|
||||
public static Color PRIMARY = new Color("#3273DC");
|
||||
|
||||
private final String hex;
|
||||
private final ChatColor chatColor;
|
||||
|
||||
Color(String hex) {
|
||||
public Color(String hex) {
|
||||
this.hex = 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),
|
||||
UNKNOWN_COMMAND("This command wasn't recognized.", Color.ERROR),
|
||||
PLUGIN_RELOADED("MineAlert reloaded.", Color.INFO),
|
||||
ALERT("%s has found %d %s veins.", Color.DEFAULT),
|
||||
NOT_ENOUGH_ARGS("%s requires %d arguments.", Color.ERROR),
|
||||
WEBHOOK_FAILED("Could not send webhook.", 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 Color color;
|
||||
|
@ -21,8 +25,8 @@ public enum Lang {
|
|||
this.color = color;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
public String getMessage(Object ...args) {
|
||||
return String.format(this.message, args);
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
|
|
|
@ -3,6 +3,7 @@ package xyz.etztech.minealert;
|
|||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import xyz.etztech.minealert.commands.MainCommand;
|
||||
import xyz.etztech.minealert.listeners.GriefAlertListener;
|
||||
import xyz.etztech.minealert.listeners.OreAlertListener;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
@ -19,6 +20,7 @@ public class MineAlert extends JavaPlugin {
|
|||
|
||||
if (isEnabled()) {
|
||||
new MainCommand(this);
|
||||
new GriefAlertListener(this);
|
||||
new OreAlertListener(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
package xyz.etztech.minealert.listeners;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
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) {
|
||||
StringBuilder extra = new StringBuilder();
|
||||
if (dates.size() == threshold) {
|
||||
extra.append(" 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("...");
|
||||
}
|
||||
ComponentBuilder builder = new ComponentBuilder()
|
||||
.append(alert + extra.toString()).color(color.getChatColor());
|
||||
if (!"".equals(usernameURL)) {
|
||||
builder.event(new ClickEvent(ClickEvent.Action.OPEN_URL, usernameURL));
|
||||
}
|
||||
sendAlert(builder.create());
|
||||
}
|
||||
}
|
||||
|
||||
public void sendAlert(BaseComponent[] message) {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (player.hasPermission("minealert.alert")) {
|
||||
player.spigot().sendMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -164,7 +164,7 @@ public class OreAlertListener implements Listener {
|
|||
}
|
||||
|
||||
private void sendAlert(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()));
|
||||
String hexColor = this.plugin.getConfigStringFallback(
|
||||
"#AAAAAA",
|
||||
|
|
|
@ -5,6 +5,19 @@ webhook: ''
|
|||
# Can use {username} as a placeholder for the player's 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
|
||||
ore:
|
||||
# Radius to search around block for similar blocks in the "vein"
|
||||
|
|
Loading…
Reference in New Issue