From 7a63f21c789db608ece676edf71eccb928d67c37 Mon Sep 17 00:00:00 2001 From: ZeroHD Date: Sun, 19 Jul 2020 17:13:28 +0200 Subject: [PATCH] Allow the blocks OreAlert tracks to be configured (#21) Allow OreAlert tracked blocks to be configured + Each block is tracked separately + Set api-version to 1.16 to allow for new material support + Bumped spigot api version to 1.16 Co-authored-by: Joey Hines Reviewed-on: https://git.etztech.xyz/MMS/MinecraftManagerPlugin/pulls/21 Reviewed-by: Etzelia --- pom.xml | 2 +- .../minecraftmanager/MinecraftManager.java | 34 ++++++++++++++----- .../listeners/BlockBreakListener.java | 21 +++++++----- .../minecraftmanager/objects/OreAlert.java | 27 ++++++++------- .../objects/PlayerOreStrikeList.java | 33 ++++++++++++++++++ src/main/resources/config.yml | 3 ++ src/main/resources/plugin.yml | 2 ++ 7 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 src/main/java/xyz/etztech/minecraftmanager/objects/PlayerOreStrikeList.java diff --git a/pom.xml b/pom.xml index a829446..1cb6492 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ org.spigotmc spigot-api - 1.14.4-R0.1-SNAPSHOT + 1.16.1-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..76c0623 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java +++ b/src/main/java/xyz/etztech/minecraftmanager/MinecraftManager.java @@ -1,6 +1,8 @@ package xyz.etztech.minecraftmanager; import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -16,10 +18,7 @@ import xyz.etztech.minecraftmanager.objects.Question; import xyz.etztech.minecraftmanager.objects.Rules; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Logger; public class MinecraftManager extends JavaPlugin implements IMinecraftManager { @@ -31,7 +30,7 @@ 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 Map> oreStrikes = new HashMap<>(); private static Rules rules; private static List banOptions; private static OreAlert oreAlert; @@ -113,6 +112,11 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { } rules = new Rules(config); banOptions = config.getStringList("ban.options"); + oreStrikes.clear(); + + for (String block_type: config.getStringList("orealert.blocks")) { + oreStrikes.put(block_type, new LinkedList<>()); + } } @@ -158,14 +162,26 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager { applications.put(uuid, application); } - public static boolean addDiamond(String location) { - if (diamonds.contains(location)) { + public static boolean addStrike(Material material, String location) { + List strikeList = oreStrikes.get(material.toString()); + + if (strikeList.contains(location)) { return false; } - diamonds.add(location); - return true; + else { + strikeList.add(location); + return true; + } } + public static boolean isTracked(Material material) { + String blockType = material.toString(); + List strikeList = oreStrikes.get(blockType); + + return strikeList != null; + } + + 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 7f54d1e..3c14004 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java @@ -2,6 +2,7 @@ package xyz.etztech.minecraftmanager.listeners; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -24,33 +25,35 @@ public class BlockBreakListener implements Listener { if (plugin.getConfig().getBoolean("orealert.enabled", true)) { Block block = event.getBlock(); + Material blockMaterial = event.getBlock().getBlockData().getMaterial(); Player player = event.getPlayer(); - if (Material.DIAMOND_ORE == block.getType()) { - if (MinecraftManager.addDiamond(getLocationString(block))) { + + if (MinecraftManager.isTracked(blockMaterial)) { + if (MinecraftManager.addStrike(blockMaterial, getLocationString(block))) { //plugin.log("[OreAlert]: " + event.getPlayer().getName()); - plugin.getOreAlert().addStrike(player.getUniqueId()); + plugin.getOreAlert().addStrike(player.getUniqueId(), blockMaterial); plugin.getOreAlert().purge(player.getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); - int alertable = plugin.getOreAlert().getStrikes(player.getUniqueId()); + int alertable = plugin.getOreAlert().getStrikes(player.getUniqueId(), blockMaterial); double start = (double) alertable / plugin.getConfig().getInt("orealert.notify.start", 5); // Start if (start == 1) { - plugin.getOreAlert().alert(player.getName(), alertable, true); + plugin.getOreAlert().alert(player.getName(), blockMaterial, alertable, true); } else if (start > 1) { // Ping if (alertable % plugin.getConfig().getInt("orealert.notify.ping", 5) == 0) { - plugin.getOreAlert().alert(player.getName(), alertable, true); + plugin.getOreAlert().alert(player.getName(), blockMaterial, alertable, true); // Alert } else if (alertable % plugin.getConfig().getInt("orealert.notify.each", 1) == 0) { - plugin.getOreAlert().alert(player.getName(), alertable, false); + plugin.getOreAlert().alert(player.getName(), blockMaterial, alertable, false); } } } for (Block radiusBlock : getBlocks(block, 5)) { - if (Material.DIAMOND_ORE == radiusBlock.getType()) { - MinecraftManager.addDiamond(getLocationString(radiusBlock)); + if (MinecraftManager.isTracked(radiusBlock.getType())) { + MinecraftManager.addStrike(radiusBlock.getType(), getLocationString(radiusBlock)); } } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java index 258ccb5..29d5265 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; @@ -10,7 +12,7 @@ import xyz.etztech.minecraftmanager.MinecraftManager; import java.util.*; public class OreAlert { - private Map> players = new HashMap<>(); + private Map players = new HashMap<>(); private MinecraftManager plugin; @@ -18,19 +20,21 @@ public class OreAlert { this.plugin = plugin; } - public void addStrike(UUID uuid) { - ArrayList strikes = players.getOrDefault(uuid, new ArrayList<>()); - strikes.add(new Date()); - players.put(uuid, strikes); + public void addStrike(UUID uuid, Material material) { + PlayerOreStrikeList playerStrikes = players.getOrDefault(uuid, new PlayerOreStrikeList()); + + playerStrikes.addStrike(material); + + players.put(uuid, playerStrikes); } - public int getStrikes(UUID uuid) { - return players.getOrDefault(uuid, new ArrayList<>()).size(); + public int getStrikes(UUID uuid, Material material) { + return players.getOrDefault(uuid, new PlayerOreStrikeList()).strikeCount(material); } - public void alert(String username, int strikes, boolean ping) { + public void alert(String username, Material material, int strikes, boolean ping) { // has found diamond ore veins. - String message = username + " has found " + strikes + " diamond ore veins."; + String message = username + " has found " + strikes + " " + material.name() + " veins."; for (Player player : Bukkit.getOnlinePlayers()) { if (player.hasPermission("minecraftmanager.orealert")) { @@ -56,9 +60,8 @@ public class OreAlert { Date cutoff = new Date(); // Cutoff to minutes, subtract purge, back to milliseconds cutoff.setTime(((cutoff.getTime()/1000/60) - purge)*60*1000); - ArrayList purged = players.getOrDefault(uuid, new ArrayList<>()); - purged.removeIf(date -> date.before(cutoff)); - players.put(uuid, purged); + PlayerOreStrikeList strikeList = players.getOrDefault(uuid, new PlayerOreStrikeList()); + strikeList.purge(cutoff); } diff --git a/src/main/java/xyz/etztech/minecraftmanager/objects/PlayerOreStrikeList.java b/src/main/java/xyz/etztech/minecraftmanager/objects/PlayerOreStrikeList.java new file mode 100644 index 0000000..f1b71e9 --- /dev/null +++ b/src/main/java/xyz/etztech/minecraftmanager/objects/PlayerOreStrikeList.java @@ -0,0 +1,33 @@ +package xyz.etztech.minecraftmanager.objects; + +import org.bukkit.Material; + +import java.util.*; + +public class PlayerOreStrikeList { + private Map> strikes = new HashMap<>(); + + public PlayerOreStrikeList() { + } + + public void addStrike(Material material) { + ArrayList oreStrikes = getStrikes(material); + oreStrikes.add(new Date()); + strikes.put(material, oreStrikes); + } + + public int strikeCount(Material material) { + return getStrikes(material).size(); + } + + public ArrayList getStrikes(Material material) { + return strikes.getOrDefault(material, new ArrayList<>()); + } + + public void purge(Date cutoff) { + strikes.forEach((block, list) -> { + list.removeIf(date -> date.before(cutoff)); + strikes.put(block, list); + }); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f496671..7c61245 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -76,6 +76,9 @@ django: # OreAlert, for pinging based on diamond ore strikes orealert: enabled: true + blocks: + - diamond_ore + - ancient_debris # How long until we purge a node strike, in minutes purge: 30 notify: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3469daa..cb0a970 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,6 +4,8 @@ description: ${description} author: ${author} website: ${url} main: ${mainClass} +api-version: 1.16 + commands: minecraftmanager: description: Base MCM command