From 38e1a8ac2c1d2c8579c9365a768f8cc56e5af1d1 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Mon, 12 Nov 2018 14:59:06 -0600 Subject: [PATCH] Added classes Need to add API to Django project for alerts/pings --- .../listeners/BlockBreakListener.java | 46 ++++++++++++++----- .../minecraftmanager/objects/OreAlert.java | 40 ++++++++++++++++ src/main/resources/config.yml | 13 +++++- 3 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java diff --git a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java index 1566226..3711f37 100644 --- a/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java +++ b/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java @@ -10,6 +10,7 @@ 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; @@ -17,28 +18,51 @@ import java.util.List; public class BlockBreakListener implements Listener { private MinecraftManager plugin; + private OreAlert oreAlert; public BlockBreakListener(MinecraftManager minecraftManager) { this.plugin = minecraftManager; + this.oreAlert = new OreAlert(); } @EventHandler public void onBlockBreak(BlockBreakEvent event) { - Block block = event.getBlock(); - if (Material.DIAMOND_ORE == block.getType()) { - if (MinecraftManager.addDiamond(getLocationString(block))) { - plugin.log("[OreAlert]: " + event.getPlayer().getName()); - } + if (plugin.getConfig().getBoolean("orealert.enabled", true)) { + 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()); + oreAlert.addStrike(player.getUniqueId()); + oreAlert.purge(player.getUniqueId(), plugin.getConfig().getInt("orealert.purge", 30)); - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - for (Block radiusBlock : getBlocks(block, 5)) { - if (Material.DIAMOND_ORE == radiusBlock.getType()) { - MinecraftManager.addDiamond(getLocationString(radiusBlock)); + int alertable = oreAlert.getStrikes(player.getUniqueId()); + // Start + if (alertable / plugin.getConfig().getInt("orealert.notify.start", 5) == 1) { + // TODO Ping + } else { + // Alert + if (alertable % plugin.getConfig().getInt("orealert.notify.each", 1) == 0) { + // TODO Alert + } + // Ping + if (alertable % plugin.getConfig().getInt("orealert.notify.ping", 5) == 0) { + // TODO Ping + } } - } - }); + } + + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + for (Block radiusBlock : getBlocks(block, 5)) { + if (Material.DIAMOND_ORE == radiusBlock.getType()) { + MinecraftManager.addDiamond(getLocationString(radiusBlock)); + } + } + }); + + } } } diff --git a/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java new file mode 100644 index 0000000..df3e98d --- /dev/null +++ b/src/main/java/xyz/etztech/minecraftmanager/objects/OreAlert.java @@ -0,0 +1,40 @@ +package xyz.etztech.minecraftmanager.objects; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.UUID; + +public class OreAlert { + private HashMap> players; + + public HashMap> getPlayers() { + return players; + } + + public void setPlayers(HashMap> players) { + this.players = players; + } + + public void addStrike(UUID uuid) { + ArrayList strikes = players.getOrDefault(uuid, new ArrayList<>()); + strikes.add(new Date()); + players.put(uuid, strikes); + } + + public int getStrikes(UUID uuid) { + return players.getOrDefault(uuid, new ArrayList<>()).size(); + } + + public void purge(UUID uuid, int purge) { + 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); + } + + + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 652e83d..309528b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -73,4 +73,15 @@ django: # MCM API password - defined in your Django settings api: "Testing1" - +# OreAlert, for pinging based on diamond ore strikes +orealert: + enabled: true + # How long until we purge a node strike, in minutes + purge: 30 + notify: + # How many veins found within the above purge minutes to notify + start: 5 + # After the initial alert, how many should be found in addition before more alerts? + each: 1 + # After the initial alert, how many should be found in addition before more pings? + ping: 5