Compare commits

...

3 Commits
main ... grief

Author SHA1 Message Date
Etzelia 83c94e0448
Merge branch 'master' of git.etztech.xyz:Minecraft/MineAlert into grief
Signed-off-by: Etzelia <etzelia@hotmail.com>
2020-08-02 13:57:26 -05:00
Etzelia a731758f12
Add OPEN_URL to spigot message
Signed-off-by: Etzelia <etzelia@hotmail.com>
2020-08-01 21:09:58 -05:00
Etzelia b7375b0794
Add grief alert
Signed-off-by: Etzelia <etzelia@hotmail.com>
2020-08-01 21:01:51 -05:00
7 changed files with 166 additions and 13 deletions

View File

@ -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 -->

View File

@ -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);
}

View File

@ -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() {

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -161,7 +161,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",

View File

@ -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"