Merge branch 'master' of git.etztech.xyz:Minecraft/MineAlert into grief

Signed-off-by: Etzelia <etzelia@hotmail.com>
grief
Etzelia 2020-08-02 13:57:26 -05:00
commit 83c94e0448
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
3 changed files with 65 additions and 22 deletions

View File

@ -3,8 +3,8 @@ 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.GriefAlertListener;
import xyz.etztech.minealert.listeners.OreAlertListener;
import java.util.logging.Logger;
@ -20,8 +20,8 @@ public class MineAlert extends JavaPlugin {
if (isEnabled()) {
new MainCommand(this);
new BlockBreakListener(this);
new GriefAlertListener(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.ORE_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 {