Added classes

Need to add API to Django project for alerts/pings
master
Etzelia 2018-11-12 14:59:06 -06:00
parent f06f771697
commit 38e1a8ac2c
3 changed files with 87 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import xyz.etztech.minecraftmanager.MCMAPI; import xyz.etztech.minecraftmanager.MCMAPI;
import xyz.etztech.minecraftmanager.MinecraftManager; import xyz.etztech.minecraftmanager.MinecraftManager;
import xyz.etztech.minecraftmanager.objects.OreAlert;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -17,28 +18,51 @@ import java.util.List;
public class BlockBreakListener implements Listener { public class BlockBreakListener implements Listener {
private MinecraftManager plugin; private MinecraftManager plugin;
private OreAlert oreAlert;
public BlockBreakListener(MinecraftManager minecraftManager) { public BlockBreakListener(MinecraftManager minecraftManager) {
this.plugin = minecraftManager; this.plugin = minecraftManager;
this.oreAlert = new OreAlert();
} }
@EventHandler @EventHandler
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
Block block = event.getBlock(); if (plugin.getConfig().getBoolean("orealert.enabled", true)) {
if (Material.DIAMOND_ORE == block.getType()) { Block block = event.getBlock();
if (MinecraftManager.addDiamond(getLocationString(block))) { Player player = event.getPlayer();
plugin.log("[OreAlert]: " + event.getPlayer().getName()); 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, () -> { int alertable = oreAlert.getStrikes(player.getUniqueId());
for (Block radiusBlock : getBlocks(block, 5)) { // Start
if (Material.DIAMOND_ORE == radiusBlock.getType()) { if (alertable / plugin.getConfig().getInt("orealert.notify.start", 5) == 1) {
MinecraftManager.addDiamond(getLocationString(radiusBlock)); // 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));
}
}
});
}
} }
} }

View File

@ -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<UUID, ArrayList<Date>> players;
public HashMap<UUID, ArrayList<Date>> getPlayers() {
return players;
}
public void setPlayers(HashMap<UUID, ArrayList<Date>> players) {
this.players = players;
}
public void addStrike(UUID uuid) {
ArrayList<Date> 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<Date> purged = players.getOrDefault(uuid, new ArrayList<>());
purged.removeIf(date -> date.before(cutoff));
players.put(uuid, purged);
}
}

View File

@ -73,4 +73,15 @@ django:
# MCM API password - defined in your Django settings # MCM API password - defined in your Django settings
api: "Testing1" 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