From bed3b002b0a85109ae8f27a0bfc99a1bb2cf6421 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Tue, 1 Oct 2019 21:21:20 +0200 Subject: [PATCH] Fix OreAlert (#15) --- pom.xml | 2 +- .../minecraftmanager/MinecraftManager.java | 12 +- .../listeners/BlockBreakListener.java | 67 +---------- .../minecraftmanager/objects/OreAlert.java | 108 ++++++++++++++++++ 4 files changed, 112 insertions(+), 77 deletions(-) diff --git a/pom.xml b/pom.xml index 4c63696..a829446 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ org.spigotmc spigot-api - 1.14.3-R0.1-SNAPSHOT + 1.14.4-R0.1-SNAPSHOT provided diff --git a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java index dea8c98..314d9ed 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java @@ -31,7 +31,6 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { private static Map applyMode = new HashMap<>(); private static Map applyQuestion = new HashMap<>(); private static Map applications = new HashMap<>(); - private static List diamonds = new ArrayList<>(); private static Rules rules; private static List banOptions; private static OreAlert oreAlert; @@ -90,13 +89,14 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { getServer().getPluginManager().registerEvents(commandPreprocessListener, this); oreAlert = new OreAlert(this); + Bukkit.getScheduler().runTaskAsynchronously(this, () -> oreAlert.monitor()); Bukkit.getConsoleSender().sendMessage("MinecraftManager has started successfully."); } } public void onDisable() { - + oreAlert.setRunning(false); } public void loadConfig() { @@ -158,14 +158,6 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { applications.put(uuid, application); } - public static boolean addDiamond(String location) { - if (diamonds.contains(location)) { - return false; - } - diamonds.add(location); - return true; - } - public static Rules getRules() { return rules; } diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java index a82838c..15eb06f 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java @@ -1,20 +1,14 @@ package xyz.etztech.minecraftmanager.listeners; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; 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.player.PlayerLoginEvent; -import xyz.etztech.minecraftmanager.MCMAPI; import xyz.etztech.minecraftmanager.MinecraftManager; import xyz.etztech.minecraftmanager.objects.OreAlert; -import java.util.ArrayList; -import java.util.List; - public class BlockBreakListener implements Listener { private MinecraftManager plugin; @@ -30,68 +24,9 @@ public class BlockBreakListener implements Listener { Block block = event.getBlock(); Player player = event.getPlayer(); if (Material.DIAMOND_ORE == block.getType()) { - if (MinecraftManager.addDiamond(getLocationString(block))) { - //plugin.log("[OreAlert]: " + event.getPlayer().getName()); - plugin.getOreAlert().addStrike(player.getUniqueId()); - plugin.getOreAlert().purge(player.getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); - - int alertable = plugin.getOreAlert().getStrikes(player.getUniqueId()); - - double start = (double) alertable / plugin.getConfig().getInt("orealert.notify.start", 5); - // Start - 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) { - 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); - } - } - - } - - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - for (Block radiusBlock : getBlocks(block, 5)) { - if (Material.DIAMOND_ORE == radiusBlock.getType()) { - MinecraftManager.addDiamond(getLocationString(radiusBlock)); - } - } - }); - + plugin.getOreAlert().getQueue().add(new OreAlert.Strike(player, block)); } } } - private List getBlocks(Block start, int radius) { - if (radius < 0) { - return new ArrayList(0); - } - int iterations = (radius * 2) + 1; - List blocks = new ArrayList(iterations * iterations * iterations); - for (int x = -radius; x <= radius; x++) { - for (int y = -radius; y <= radius; y++) { - for (int z = -radius; z <= radius; z++) { - blocks.add(start.getRelative(x, y, z)); - } - } - } - return blocks; - } - - private String getLocationString(Block block) { - int X = block.getX(); - int Y = block.getY(); - int Z = block.getZ(); - - String x = String.valueOf(X); - String y = String.valueOf(Y); - String z = String.valueOf(Z); - - return x + y + z; - } - - } diff --git a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java index 258ccb5..9b40c5c 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java +++ b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java @@ -3,6 +3,8 @@ package xyz.etztech.minecraftmanager.objects; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import xyz.etztech.core.web.CoreWeb; import xyz.etztech.minecraftmanager.MinecraftManager; @@ -11,6 +13,9 @@ import java.util.*; public class OreAlert { private Map> players = new HashMap<>(); + private List diamonds = new ArrayList<>(); + private List queue = new ArrayList<>(); + private boolean running; private MinecraftManager plugin; @@ -18,6 +23,18 @@ public class OreAlert { this.plugin = plugin; } + public List getQueue() { + return this.queue; + } + + public boolean isRunning() { + return running; + } + + public void setRunning(boolean running) { + this.running = running; + } + public void addStrike(UUID uuid) { ArrayList strikes = players.getOrDefault(uuid, new ArrayList<>()); strikes.add(new Date()); @@ -61,6 +78,97 @@ public class OreAlert { players.put(uuid, purged); } + public boolean addDiamond(String location) { + if (diamonds.contains(location)) { + return false; + } + diamonds.add(location); + return true; + } + public void monitor() { + Strike strike; + running = true; + while (running) { + if (queue.size() > 0) { + strike = queue.get(0); + if (addDiamond(getLocationString(strike.getBlock()))) { + addStrike(strike.getPlayer().getUniqueId()); + purge(strike.getPlayer().getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); + + int alertable = getStrikes(strike.getPlayer().getUniqueId()); + + double start = (double) alertable / plugin.getConfig().getInt("orealert.notify.start", 5); + // Start + if (start == 1) { + alert(strike.getPlayer().getName(), alertable, true); + } else if (start > 1) { + // Ping + if (alertable % plugin.getConfig().getInt("orealert.notify.ping", 5) == 0) { + alert(strike.getPlayer().getName(), alertable, true); + // Alert + } else if (alertable % plugin.getConfig().getInt("orealert.notify.each", 1) == 0) { + alert(strike.getPlayer().getName(), alertable, false); + } + } + + for (Block radiusBlock : getBlocks(strike.getBlock(), 5)) { + if (Material.DIAMOND_ORE == radiusBlock.getType()) { + addDiamond(getLocationString(radiusBlock)); + } + } + + } + queue.remove(0); + } + } + } + + private List getBlocks(Block start, int radius) { + if (radius < 0) { + return new ArrayList<>(0); + } + int iterations = (radius * 2) + 1; + List blocks = new ArrayList(iterations * iterations * iterations); + for (int x = -radius; x <= radius; x++) { + for (int y = -radius; y <= radius; y++) { + for (int z = -radius; z <= radius; z++) { + blocks.add(start.getRelative(x, y, z)); + } + } + } + return blocks; + } + + private String getLocationString(Block block) { + int X = block.getX(); + int Y = block.getY(); + int Z = block.getZ(); + + String x = String.valueOf(X); + String y = String.valueOf(Y); + String z = String.valueOf(Z); + + return x + y + z; + } + + public static class Strike { + private Player player; + private Block block; + + public Strike(Player player, Block block) { + this.player = player; + this.block = block; + } + + public Player getPlayer() { + return player; + } + + public Block getBlock() { + return block; + } + } + }