MinecraftManagerPlugin/src/main/java/xyz/etztech/minecraftmanager/listeners/BlockBreakListener.java

98 lines
3.5 KiB
Java

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;
public BlockBreakListener(MinecraftManager minecraftManager) {
this.plugin = minecraftManager;
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
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());
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;
}
}