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/1
grief^2
Etzelia 2020-08-02 17:40:05 +02:00 committed by ZeroHD
parent d7e9ce7925
commit f667672823
4 changed files with 66 additions and 23 deletions

View File

@ -3,7 +3,7 @@
<groupId>xyz.etztech</groupId>
<artifactId>MineAlert</artifactId>
<!-- Version is used in plugin.yml -->
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>
<!-- Plugin Information -->

View File

@ -3,7 +3,7 @@ package xyz.etztech.minealert;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.minealert.commands.MainCommand;
import xyz.etztech.minealert.listeners.BlockBreakListener;
import xyz.etztech.minealert.listeners.OreAlertListener;
import java.util.logging.Logger;
@ -19,7 +19,7 @@ public class MineAlert extends JavaPlugin {
if (isEnabled()) {
new MainCommand(this);
new BlockBreakListener(this);
new OreAlertListener(this);
}
}

View File

@ -13,6 +13,7 @@ import xyz.etztech.embed.Embed;
import xyz.etztech.minealert.Color;
import xyz.etztech.minealert.Lang;
import xyz.etztech.minealert.MineAlert;
import xyz.etztech.minealert.listeners.OreAlertListener;
public class MainCommand implements CommandExecutor {
@ -43,6 +44,9 @@ public class MainCommand implements CommandExecutor {
case "webhook":
webhook(commandSender);
break;
case "status":
status(commandSender);
break;
default:
Lang.UNKNOWN_COMMAND.sms(commandSender);
break;
@ -57,6 +61,7 @@ public class MainCommand implements CommandExecutor {
.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 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())
.create();
commandSender.spigot().sendMessage(message);
@ -67,6 +72,19 @@ public class MainCommand implements CommandExecutor {
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) {
String webhook = this.plugin.getConfig().getString("webhook", "");
if ("".equals(webhook)) {
@ -86,6 +104,5 @@ public class MainCommand implements CommandExecutor {
Lang.WEBHOOK_FAILED.sms(commandSender);
}
});
}
}

View File

@ -12,6 +12,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import xyz.etztech.Javacord;
import xyz.etztech.Webhook;
import xyz.etztech.embed.Author;
@ -24,14 +25,14 @@ import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
public class BlockBreakListener implements Listener {
public class OreAlertListener implements Listener {
private final MineAlert plugin;
private final HashSet<Location> cache = new HashSet<>();
private final Map<Player, List<BlockEvent>> map = new HashMap<>();
private final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
private static final HashSet<Location> cache = new HashSet<>();
private static final Map<Player, List<BlockEvent>> map = new HashMap<>();
private static final Queue<BlockEvent> queue = new ConcurrentLinkedQueue<>();
public BlockBreakListener(MineAlert plugin) {
public OreAlertListener(MineAlert plugin) {
this.plugin = plugin;
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, this::task);
@ -57,19 +58,32 @@ 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() {
while (this.plugin.isEnabled()) {
BlockEvent event = this.queue.poll();
if (event != null) {
if (cache.contains(event.getLocation())) continue;
cache.add(event.getLocation());
for (String s: this.plugin.getConfig().getConfigurationSection("ore.blocks").getKeys(false)) {
if (Lang.getMaterialKey(event.getMaterial()).equals(s)) {
addStrike(event);
if (event.isParent()) {
check(event);
}
break;
if (isMaterialTracked(event.getMaterial())) {
addStrike(event);
if (event.isParent()) {
check(event);
}
}
}
@ -90,7 +104,7 @@ public class BlockBreakListener implements Listener {
"ore.start"
);
int each = this.plugin.getConfigIntFallback(
5,
1,
String.format("ore.blocks.%s.each", blockKey),
"ore.each"
);
@ -110,12 +124,12 @@ public class BlockBreakListener implements Listener {
double alert = (double) strikes / start;
if (alert == 1) {
alert(event, strikes, true);
sendAlert(event, strikes, true);
} else if (alert > 1) {
if (alert % ping == 0) {
alert(event, strikes, true);
} else if (alert % each == 0) {
alert(event, strikes, false);
if (strikes % ping == 0) {
sendAlert(event, strikes, true);
} else if (strikes % each == 0) {
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(),
event.getPlayer().getName(), strikes, Lang.getMaterialName(event.getMaterial()));
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 {