Files
src
build.gradle
Code
4 175
src/main/java/com/kodari/orescanner/OreScannerPlugin.java
package com.kodari.orescanner;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public final class OreScannerPlugin extends JavaPlugin implements CommandExecutor {
private Set<String> oreTypes;
private int scanRadius;
private int maximumRadius;
@Override
public void onEnable() {
saveDefaultConfig();
maximumRadius = Math.max(1, getConfig().getInt("maximum-radius", 24));
scanRadius = Math.max(1, Math.min(maximumRadius, getConfig().getInt("scan-radius", 12)));
oreTypes = new HashSet<>();
for (String ore : getConfig().getStringList("ores")) {
oreTypes.add(ore.toUpperCase(Locale.ROOT));
}
getCommand("orescanner").setExecutor(this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(ChatColor.RED + "Only players can scan for ores.");
return true;
}
if (!player.hasPermission("orescanner.use")) {
player.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
return true;
}
if (args.length > 1) {
player.sendMessage(ChatColor.RED + "Usage: /orescanner [radius]");
return true;
}
int radius = scanRadius;
if (args.length == 1) {
try {
radius = Integer.parseInt(args[0]);
} catch (NumberFormatException exception) {
player.sendMessage(ChatColor.RED + "Radius must be a whole number.");
return true;
}
if (radius < 1 || radius > maximumRadius) {
player.sendMessage(ChatColor.RED + "Radius must be between 1 and " + maximumRadius + ".");
return true;
}
}
scan(player, radius);
return true;
}
private void scan(Player player, int radius) {
Location origin = player.getLocation();
World world = player.getWorld();
int centerX = origin.getBlockX();
int centerY = origin.getBlockY();
int centerZ = origin.getBlockZ();
Map<String, Integer> counts = new TreeMap<>();
Map<String, Double> nearestDistances = new HashMap<>();
Map<String, String> nearestCoordinates = new HashMap<>();
for (int x = centerX - radius; x <= centerX + radius; x++) {
for (int y = Math.max(0, centerY - radius); y <= Math.min(world.getMaxHeight() - 1, centerY + radius); y++) {
for (int z = centerZ - radius; z <= centerZ + radius; z++) {
if (!world.isChunkLoaded(x >> 4, z >> 4)) {
continue;
}
String type = world.getBlockAt(x, y, z).getType().name();
if (!oreTypes.contains(type)) {
continue;
}
counts.merge(type, 1, Integer::sum);
double dx = x + 0.5 - origin.getX();
double dy = y + 0.5 - origin.getY();
double dz = z + 0.5 - origin.getZ();
double distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared < nearestDistances.getOrDefault(type, Double.MAX_VALUE)) {
nearestDistances.put(type, distanceSquared);
nearestCoordinates.put(type, x + ", " + y + ", " + z);
}
}
}
}
if (counts.isEmpty()) {
player.sendMessage(ChatColor.YELLOW + "No configured ores found within " + radius + " blocks.");
return;
}
player.sendMessage(ChatColor.GOLD + "Ore scan results within " + radius + " blocks:");
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
player.sendMessage(ChatColor.YELLOW + entry.getKey() + ChatColor.GRAY + " x" + entry.getValue()
+ ChatColor.DARK_GRAY + " — nearest: " + ChatColor.WHITE + nearestCoordinates.get(entry.getKey()));
}
}
}