Fix the bounding box region

This commit is contained in:
Logan Saso
2025-10-04 22:03:40 -07:00
parent 6f9ae25a7b
commit 2b2f2fd556
3 changed files with 82 additions and 9 deletions

View File

@@ -5,6 +5,9 @@ import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import loganintech.regionforcefield.RegionForcefieldPlugin;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@@ -45,6 +48,8 @@ public class ForcefieldCommand implements CommandExecutor, TabCompleter {
return handleStatus(sender);
case "info":
return handleInfo(sender);
case "test":
return handleTest(sender);
case "help":
sendHelp(sender);
return true;
@@ -177,12 +182,51 @@ public class ForcefieldCommand implements CommandExecutor, TabCompleter {
return true;
}
private boolean handleTest(@NotNull CommandSender sender) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command can only be used by players.");
return true;
}
if (!sender.hasPermission("regionforcefield.debug")) {
sender.sendMessage(ChatColor.RED + "You don't have permission to use this command.");
return true;
}
Player player = (Player) sender;
// Place test blocks in a small box around the player
sender.sendMessage(ChatColor.YELLOW + "Placing test blocks around you...");
Location loc = player.getLocation();
BlockData glassData = Material.PURPLE_STAINED_GLASS_PANE.createBlockData();
int count = 0;
for (int x = -2; x <= 2; x++) {
for (int z = -2; z <= 2; z++) {
if (x == 0 && z == 0) continue; // Skip player location
Location testLoc = loc.clone().add(x, 0, z);
if (testLoc.getBlock().getType() == Material.AIR) {
player.sendBlockChange(testLoc, glassData);
count++;
}
}
}
sender.sendMessage(ChatColor.GREEN + "Placed " + count + " test blocks around you.");
sender.sendMessage(ChatColor.GRAY + "These are client-side only and will disappear on relog.");
return true;
}
private void sendHelp(@NotNull CommandSender sender) {
sender.sendMessage(ChatColor.GOLD + "=== RegionForcefield Commands ===");
sender.sendMessage(ChatColor.YELLOW + "/forcefield debug " + ChatColor.GRAY + "- Toggle debug mode");
sender.sendMessage(ChatColor.YELLOW + "/forcefield reload " + ChatColor.GRAY + "- Reload configuration");
sender.sendMessage(ChatColor.YELLOW + "/forcefield status " + ChatColor.GRAY + "- Show plugin status");
sender.sendMessage(ChatColor.YELLOW + "/forcefield info " + ChatColor.GRAY + "- Show region information");
sender.sendMessage(ChatColor.YELLOW + "/forcefield test " + ChatColor.GRAY + "- Test block rendering");
sender.sendMessage(ChatColor.YELLOW + "/forcefield help " + ChatColor.GRAY + "- Show this help message");
}
@@ -208,7 +252,7 @@ public class ForcefieldCommand implements CommandExecutor, TabCompleter {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
List<String> subcommands = Arrays.asList("debug", "reload", "status", "info", "help");
List<String> subcommands = Arrays.asList("debug", "reload", "status", "info", "test", "help");
String input = args[0].toLowerCase();
for (String subcommand : subcommands) {

View File

@@ -286,6 +286,7 @@ public class ForcefieldRenderer {
private void placeBlock(@NotNull Player player, @NotNull World world,
double x, double y, double z, @NotNull Set<Location> blocks) {
if (!plugin.getConfig().getBoolean("render-blocks", true)) {
plugin.debug("Skipping block render (render-blocks is false)");
return;
}
@@ -295,6 +296,11 @@ public class ForcefieldRenderer {
if (location.getBlock().getType() == Material.AIR) {
player.sendBlockChange(location, glassBlockData);
blocks.add(location);
plugin.debug("Placed block at " + location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ() +
" for player " + player.getName() + " (material: " + glassBlockData.getMaterial() + ")");
} else {
plugin.debug("Skipped block at " + location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ() +
" - not air (is " + location.getBlock().getType() + ")");
}
}

View File

@@ -77,6 +77,7 @@ public class ForcefieldUpdateTask extends BukkitRunnable {
/**
* Checks if a region is near enough to a player to render.
* Calculates distance to the nearest point on the region's bounding box.
*
* @param player the player
* @param region the region
@@ -88,18 +89,40 @@ public class ForcefieldUpdateTask extends BukkitRunnable {
double playerY = player.getLocation().getY();
double playerZ = player.getLocation().getZ();
// Get region's center (approximate)
double regionCenterX = (region.getMinimumPoint().x() + region.getMaximumPoint().x()) / 2.0;
double regionCenterY = (region.getMinimumPoint().y() + region.getMaximumPoint().y()) / 2.0;
double regionCenterZ = (region.getMinimumPoint().z() + region.getMaximumPoint().z()) / 2.0;
// Get region bounds
int minX = region.getMinimumPoint().x();
int minY = region.getMinimumPoint().y();
int minZ = region.getMinimumPoint().z();
int maxX = region.getMaximumPoint().x();
int maxY = region.getMaximumPoint().y();
int maxZ = region.getMaximumPoint().z();
// Calculate distance
// Find the closest point on the region's bounding box to the player
double closestX = clamp(playerX, minX, maxX);
double closestY = clamp(playerY, minY, maxY);
double closestZ = clamp(playerZ, minZ, maxZ);
// Calculate distance from player to the closest point on the region
double distance = Math.sqrt(
Math.pow(playerX - regionCenterX, 2) +
Math.pow(playerY - regionCenterY, 2) +
Math.pow(playerZ - regionCenterZ, 2)
Math.pow(playerX - closestX, 2) +
Math.pow(playerY - closestY, 2) +
Math.pow(playerZ - closestZ, 2)
);
plugin.debug("Player " + player.getName() + " distance to region: " + String.format("%.1f", distance) + " blocks");
return distance <= maxRenderDistance;
}
/**
* Clamps a value between a minimum and maximum.
*
* @param value the value to clamp
* @param min the minimum value
* @param max the maximum value
* @return the clamped value
*/
private double clamp(double value, double min, double max) {
return Math.max(min, Math.min(max, value));
}
}