Fix OreAlert (#15)

up
Etzelia 2019-10-01 21:21:20 +02:00 committed by Gitea
parent 42169b64d9
commit bed3b002b0
4 changed files with 112 additions and 77 deletions

View File

@ -31,7 +31,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.14.3-R0.1-SNAPSHOT</version> <version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -31,7 +31,6 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
private static Map<String, Boolean> applyMode = new HashMap<>(); private static Map<String, Boolean> applyMode = new HashMap<>();
private static Map<String, Question> applyQuestion = new HashMap<>(); private static Map<String, Question> applyQuestion = new HashMap<>();
private static Map<String, Application> applications = new HashMap<>(); private static Map<String, Application> applications = new HashMap<>();
private static List<String> diamonds = new ArrayList<>();
private static Rules rules; private static Rules rules;
private static List<String> banOptions; private static List<String> banOptions;
private static OreAlert oreAlert; private static OreAlert oreAlert;
@ -90,13 +89,14 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
getServer().getPluginManager().registerEvents(commandPreprocessListener, this); getServer().getPluginManager().registerEvents(commandPreprocessListener, this);
oreAlert = new OreAlert(this); oreAlert = new OreAlert(this);
Bukkit.getScheduler().runTaskAsynchronously(this, () -> oreAlert.monitor());
Bukkit.getConsoleSender().sendMessage("MinecraftManager has started successfully."); Bukkit.getConsoleSender().sendMessage("MinecraftManager has started successfully.");
} }
} }
public void onDisable() { public void onDisable() {
oreAlert.setRunning(false);
} }
public void loadConfig() { public void loadConfig() {
@ -158,14 +158,6 @@ public class MinecraftManager extends JavaPlugin implements IMinecraftManager {
applications.put(uuid, application); 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() { public static Rules getRules() {
return rules; return rules;
} }

View File

@ -1,20 +1,14 @@
package xyz.etztech.minecraftmanager.listeners; package xyz.etztech.minecraftmanager.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; 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.MinecraftManager;
import xyz.etztech.minecraftmanager.objects.OreAlert; import xyz.etztech.minecraftmanager.objects.OreAlert;
import java.util.ArrayList;
import java.util.List;
public class BlockBreakListener implements Listener { public class BlockBreakListener implements Listener {
private MinecraftManager plugin; private MinecraftManager plugin;
@ -30,68 +24,9 @@ public class BlockBreakListener implements Listener {
Block block = event.getBlock(); Block block = event.getBlock();
Player player = event.getPlayer(); Player player = event.getPlayer();
if (Material.DIAMOND_ORE == block.getType()) { if (Material.DIAMOND_ORE == block.getType()) {
if (MinecraftManager.addDiamond(getLocationString(block))) { plugin.getOreAlert().getQueue().add(new OreAlert.Strike(player, 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));
}
}
});
} }
} }
} }
private List<Block> getBlocks(Block start, int radius) {
if (radius < 0) {
return new ArrayList<Block>(0);
}
int iterations = (radius * 2) + 1;
List<Block> blocks = new ArrayList<Block>(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;
}
} }

View File

@ -3,6 +3,8 @@ package xyz.etztech.minecraftmanager.objects;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import xyz.etztech.core.web.CoreWeb; import xyz.etztech.core.web.CoreWeb;
import xyz.etztech.minecraftmanager.MinecraftManager; import xyz.etztech.minecraftmanager.MinecraftManager;
@ -11,6 +13,9 @@ import java.util.*;
public class OreAlert { public class OreAlert {
private Map<UUID, ArrayList<Date>> players = new HashMap<>(); private Map<UUID, ArrayList<Date>> players = new HashMap<>();
private List<String> diamonds = new ArrayList<>();
private List<Strike> queue = new ArrayList<>();
private boolean running;
private MinecraftManager plugin; private MinecraftManager plugin;
@ -18,6 +23,18 @@ public class OreAlert {
this.plugin = plugin; this.plugin = plugin;
} }
public List<Strike> getQueue() {
return this.queue;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public void addStrike(UUID uuid) { public void addStrike(UUID uuid) {
ArrayList<Date> strikes = players.getOrDefault(uuid, new ArrayList<>()); ArrayList<Date> strikes = players.getOrDefault(uuid, new ArrayList<>());
strikes.add(new Date()); strikes.add(new Date());
@ -61,6 +78,97 @@ public class OreAlert {
players.put(uuid, purged); 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<Block> getBlocks(Block start, int radius) {
if (radius < 0) {
return new ArrayList<>(0);
}
int iterations = (radius * 2) + 1;
List<Block> blocks = new ArrayList<Block>(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;
}
}
} }