diff --git a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java index d542e8e..dea8c98 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java @@ -11,6 +11,7 @@ import xyz.etztech.minecraftmanager.listeners.BlockBreakListener; import xyz.etztech.minecraftmanager.listeners.CommandPreprocessListener; import xyz.etztech.minecraftmanager.listeners.SessionListener; import xyz.etztech.minecraftmanager.objects.Application; +import xyz.etztech.minecraftmanager.objects.OreAlert; import xyz.etztech.minecraftmanager.objects.Question; import xyz.etztech.minecraftmanager.objects.Rules; @@ -33,6 +34,7 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { private static List diamonds = new ArrayList<>(); private static Rules rules; private static List banOptions; + private static OreAlert oreAlert; // API private static boolean logOverride = false; @@ -87,6 +89,8 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { CommandPreprocessListener commandPreprocessListener = new CommandPreprocessListener(this); getServer().getPluginManager().registerEvents(commandPreprocessListener, this); + oreAlert = new OreAlert(this); + Bukkit.getConsoleSender().sendMessage("MinecraftManager has started successfully."); } } @@ -190,6 +194,10 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { return logOverride; } + public OreAlert getOreAlert() { + return oreAlert; + } + @Override public void logOverride(boolean override) { logOverride = override; diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java index 3711f37..a82838c 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java @@ -18,11 +18,9 @@ import java.util.List; public class BlockBreakListener implements Listener { private MinecraftManager plugin; - private OreAlert oreAlert; public BlockBreakListener(MinecraftManager minecraftManager) { this.plugin = minecraftManager; - this.oreAlert = new OreAlert(); } @EventHandler @@ -34,21 +32,22 @@ public class BlockBreakListener implements Listener { if (Material.DIAMOND_ORE == block.getType()) { if (MinecraftManager.addDiamond(getLocationString(block))) { //plugin.log("[OreAlert]: " + event.getPlayer().getName()); - oreAlert.addStrike(player.getUniqueId()); - oreAlert.purge(player.getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); + plugin.getOreAlert().addStrike(player.getUniqueId()); + plugin.getOreAlert().purge(player.getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); - int alertable = oreAlert.getStrikes(player.getUniqueId()); + int alertable = plugin.getOreAlert().getStrikes(player.getUniqueId()); + + double start = (double) alertable / plugin.getConfig().getInt("orealert.notify.start", 5); // Start - if (alertable / plugin.getConfig().getInt("orealert.notify.start", 5) == 1) { - // TODO Ping - } else { - // Alert - if (alertable % plugin.getConfig().getInt("orealert.notify.each", 1) == 0) { - // TODO Alert - } + if (start == 1) { + plugin.getOreAlert().alert(player.getName(), alertable, true); + } else if (start > 1) { // Ping if (alertable % plugin.getConfig().getInt("orealert.notify.ping", 5) == 0) { - // TODO Ping + plugin.getOreAlert().alert(player.getName(), alertable, true); + // Alert + } else if (alertable % plugin.getConfig().getInt("orealert.notify.each", 1) == 0) { + plugin.getOreAlert().alert(player.getName(), alertable, false); } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java index df3e98d..258ccb5 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java +++ b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java @@ -1,19 +1,21 @@ package xyz.etztech.minecraftmanager.objects; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.UUID; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import xyz.etztech.core.web.CoreWeb; +import xyz.etztech.minecraftmanager.MinecraftManager; + +import java.util.*; public class OreAlert { - private HashMap> players; + private Map> players = new HashMap<>(); - public HashMap> getPlayers() { - return players; - } + private MinecraftManager plugin; - public void setPlayers(HashMap> players) { - this.players = players; + public OreAlert(MinecraftManager plugin) { + this.plugin = plugin; } public void addStrike(UUID uuid) { @@ -26,6 +28,30 @@ public class OreAlert { return players.getOrDefault(uuid, new ArrayList<>()).size(); } + public void alert(String username, int strikes, boolean ping) { + // has found diamond ore veins. + String message = username + " has found " + strikes + " diamond ore veins."; + + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.hasPermission("minecraftmanager.orealert")) { + player.sendMessage(ChatColor.AQUA + message); + } + } + + // Webhook + String webhook = plugin.getConfig().getString("orealert.notify.webhook", ""); + if (StringUtils.isNotEmpty(webhook)) { + String content = message; + if (ping) { + content = "@here " + message; + } + Map data = new HashMap<>(); + data.put("username", "OreAlert"); + data.put("content", content); + CoreWeb.asyncPost(plugin, webhook, data); + } + } + public void purge(UUID uuid, int purge) { Date cutoff = new Date(); // Cutoff to minutes, subtract purge, back to milliseconds diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 309528b..f496671 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -85,3 +85,5 @@ orealert: each: 1 # After the initial alert, how many should be found in addition before more pings? ping: 5 + # Webhook to send to + webhook: ''