Add BlockPlaceListener and utility methods

Signed-off-by: Etzelia <etzelia@hotmail.com>
bugs
Etzelia 2020-08-01 15:05:57 -05:00
parent e6fffae053
commit 461a057eff
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
4 changed files with 59 additions and 16 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 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 (isMaterialTrcked(event.getBlock().getType())) cache.add(event.getBlock().getLocation());
});
}
private boolean isMaterialTrcked(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 (isMaterialTrcked(event.getMaterial())) {
addStrike(event);
if (event.isParent()) {
check(event);
}
}
}
@ -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 {