Fix alert logic and add status (#1)
Typo Signed-off-by: Etzelia <etzelia@hotmail.com> Wording Signed-off-by: Etzelia <etzelia@hotmail.com> Add BlockPlaceListener and utility methods Signed-off-by: Etzelia <etzelia@hotmail.com> Fix some notification logic. Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.etztech.xyz/Minecraft/MineAlert/pulls/1grief^2
parent
d7e9ce7925
commit
f667672823
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.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<!-- Plugin Information -->
|
<!-- Plugin Information -->
|
||||||
|
|
|
@ -3,7 +3,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.OreAlertListener;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class MineAlert extends JavaPlugin {
|
||||||
|
|
||||||
if (isEnabled()) {
|
if (isEnabled()) {
|
||||||
new MainCommand(this);
|
new MainCommand(this);
|
||||||
new BlockBreakListener(this);
|
new OreAlertListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ import xyz.etztech.embed.Embed;
|
||||||
import xyz.etztech.minealert.Color;
|
import xyz.etztech.minealert.Color;
|
||||||
import xyz.etztech.minealert.Lang;
|
import xyz.etztech.minealert.Lang;
|
||||||
import xyz.etztech.minealert.MineAlert;
|
import xyz.etztech.minealert.MineAlert;
|
||||||
|
import xyz.etztech.minealert.listeners.OreAlertListener;
|
||||||
|
|
||||||
public class MainCommand implements CommandExecutor {
|
public class MainCommand implements CommandExecutor {
|
||||||
|
|
||||||
|
@ -43,6 +44,9 @@ public class MainCommand implements CommandExecutor {
|
||||||
case "webhook":
|
case "webhook":
|
||||||
webhook(commandSender);
|
webhook(commandSender);
|
||||||
break;
|
break;
|
||||||
|
case "status":
|
||||||
|
status(commandSender);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Lang.UNKNOWN_COMMAND.sms(commandSender);
|
Lang.UNKNOWN_COMMAND.sms(commandSender);
|
||||||
break;
|
break;
|
||||||
|
@ -57,6 +61,7 @@ public class MainCommand implements CommandExecutor {
|
||||||
.append(String.format("===== MineAlert v%s =====", version)).color(Color.PRIMARY.getChatColor())
|
.append(String.format("===== MineAlert v%s =====", version)).color(Color.PRIMARY.getChatColor())
|
||||||
.append("\n/minealert help - Show this message").color(Color.INFO.getChatColor())
|
.append("\n/minealert help - Show this message").color(Color.INFO.getChatColor())
|
||||||
.append("\n/minealert reload - Reload the config").color(Color.INFO.getChatColor())
|
.append("\n/minealert reload - Reload the config").color(Color.INFO.getChatColor())
|
||||||
|
.append("\n/minealert status - Check the cache, map, and queue").color(Color.INFO.getChatColor())
|
||||||
.append("\n/minealert webhook - Test the global webhook").color(Color.INFO.getChatColor())
|
.append("\n/minealert webhook - Test the global webhook").color(Color.INFO.getChatColor())
|
||||||
.create();
|
.create();
|
||||||
commandSender.spigot().sendMessage(message);
|
commandSender.spigot().sendMessage(message);
|
||||||
|
@ -67,6 +72,19 @@ public class MainCommand implements CommandExecutor {
|
||||||
Lang.PLUGIN_RELOADED.sms(commandSender);
|
Lang.PLUGIN_RELOADED.sms(commandSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void status(CommandSender commandSender) {
|
||||||
|
BaseComponent[] message = new ComponentBuilder()
|
||||||
|
.append("===== MineAlert Status =====").color(Color.PRIMARY.getChatColor())
|
||||||
|
.append("\nCache: ").color(Color.PRIMARY.getChatColor())
|
||||||
|
.append(String.format("%d", OreAlertListener.getCache().size())).color(Color.INFO.getChatColor())
|
||||||
|
.append("\nMap: ").color(Color.PRIMARY.getChatColor())
|
||||||
|
.append(String.format("%d", OreAlertListener.getMap().size())).color(Color.INFO.getChatColor())
|
||||||
|
.append("\nQueue: ").color(Color.PRIMARY.getChatColor())
|
||||||
|
.append(String.format("%d", OreAlertListener.getQueue().size())).color(Color.INFO.getChatColor())
|
||||||
|
.create();
|
||||||
|
commandSender.spigot().sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
private void webhook(CommandSender commandSender) {
|
private void webhook(CommandSender commandSender) {
|
||||||
String webhook = this.plugin.getConfig().getString("webhook", "");
|
String webhook = this.plugin.getConfig().getString("webhook", "");
|
||||||
if ("".equals(webhook)) {
|
if ("".equals(webhook)) {
|
||||||
|
@ -86,6 +104,5 @@ public class MainCommand implements CommandExecutor {
|
||||||
Lang.WEBHOOK_FAILED.sms(commandSender);
|
Lang.WEBHOOK_FAILED.sms(commandSender);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
import xyz.etztech.Javacord;
|
import xyz.etztech.Javacord;
|
||||||
import xyz.etztech.Webhook;
|
import xyz.etztech.Webhook;
|
||||||
import xyz.etztech.embed.Author;
|
import xyz.etztech.embed.Author;
|
||||||
|
@ -24,14 +25,14 @@ import java.time.OffsetDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class BlockBreakListener implements Listener {
|
public class OreAlertListener implements Listener {
|
||||||
|
|
||||||
private final MineAlert plugin;
|
private final MineAlert plugin;
|
||||||
private final HashSet<Location> cache = new HashSet<>();
|
private static final HashSet<Location> cache = new HashSet<>();
|
||||||
private final Map<Player, List<BlockEvent>> map = new HashMap<>();
|
private static final Map<Player, List<BlockEvent>> map = new HashMap<>();
|
||||||
private final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
|
private static final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
public BlockBreakListener(MineAlert plugin) {
|
public OreAlertListener(MineAlert plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, this::task);
|
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, this::task);
|
||||||
|
@ -57,20 +58,33 @@ public class BlockBreakListener implements Listener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockPlace(BlockPlaceEvent event) {
|
||||||
|
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
||||||
|
if (isMaterialTracked(event.getBlock().getType())) cache.add(event.getBlock().getLocation());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMaterialTracked(Material material) {
|
||||||
|
for (String s: this.plugin.getConfig().getConfigurationSection("ore.blocks").getKeys(false)) {
|
||||||
|
if (Lang.getMaterialKey(material).equals(s)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void task() {
|
private void task() {
|
||||||
while (this.plugin.isEnabled()) {
|
while (this.plugin.isEnabled()) {
|
||||||
BlockEvent event = this.queue.poll();
|
BlockEvent event = this.queue.poll();
|
||||||
if (event != null) {
|
if (event != null) {
|
||||||
if (cache.contains(event.getLocation())) continue;
|
if (cache.contains(event.getLocation())) continue;
|
||||||
cache.add(event.getLocation());
|
cache.add(event.getLocation());
|
||||||
for (String s: this.plugin.getConfig().getConfigurationSection("ore.blocks").getKeys(false)) {
|
if (isMaterialTracked(event.getMaterial())) {
|
||||||
if (Lang.getMaterialKey(event.getMaterial()).equals(s)) {
|
|
||||||
addStrike(event);
|
addStrike(event);
|
||||||
if (event.isParent()) {
|
if (event.isParent()) {
|
||||||
check(event);
|
check(event);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +104,7 @@ public class BlockBreakListener implements Listener {
|
||||||
"ore.start"
|
"ore.start"
|
||||||
);
|
);
|
||||||
int each = this.plugin.getConfigIntFallback(
|
int each = this.plugin.getConfigIntFallback(
|
||||||
5,
|
1,
|
||||||
String.format("ore.blocks.%s.each", blockKey),
|
String.format("ore.blocks.%s.each", blockKey),
|
||||||
"ore.each"
|
"ore.each"
|
||||||
);
|
);
|
||||||
|
@ -110,12 +124,12 @@ public class BlockBreakListener implements Listener {
|
||||||
|
|
||||||
double alert = (double) strikes / start;
|
double alert = (double) strikes / start;
|
||||||
if (alert == 1) {
|
if (alert == 1) {
|
||||||
alert(event, strikes, true);
|
sendAlert(event, strikes, true);
|
||||||
} else if (alert > 1) {
|
} else if (alert > 1) {
|
||||||
if (alert % ping == 0) {
|
if (strikes % ping == 0) {
|
||||||
alert(event, strikes, true);
|
sendAlert(event, strikes, true);
|
||||||
} else if (alert % each == 0) {
|
} else if (strikes % each == 0) {
|
||||||
alert(event, strikes, false);
|
sendAlert(event, strikes, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,7 +160,7 @@ public class BlockBreakListener implements Listener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void alert(BlockEvent event, int strikes, boolean ping) {
|
private void sendAlert(BlockEvent event, int strikes, boolean ping) {
|
||||||
String message = String.format(Lang.ALERT.getMessage(),
|
String message = String.format(Lang.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(
|
||||||
|
@ -198,6 +212,18 @@ public class BlockBreakListener implements Listener {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashSet<Location> getCache() {
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<Player, List<BlockEvent>> getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Queue<BlockEvent> getQueue() {
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BlockEvent {
|
class BlockEvent {
|
Loading…
Reference in New Issue