Skip to content

Commit

Permalink
feat: warn users during arena creation if the pos1/pos2 overlaps with…
Browse files Browse the repository at this point in the history
… another arena bounding box

Closes #371
  • Loading branch information
Misat11 committed Jun 21, 2024
1 parent 0ba3bab commit ccdea59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public boolean cmd(Player player, String action, String[] args) {
} else if (action.equalsIgnoreCase("spec")) {
response = setSpecSpawn(player.getLocation());
} else if (action.equalsIgnoreCase("pos1")) {
response = setPos1(player.getLocation());
response = setPos1(player.getLocation(), args.length > 0 && "force".equalsIgnoreCase(args[0]));
} else if (action.equalsIgnoreCase("pos2")) {
response = setPos2(player.getLocation());
response = setPos2(player.getLocation(), args.length > 0 && "force".equalsIgnoreCase(args[0]));
} else if (action.equalsIgnoreCase("pausecountdown")) {
if (args.length >= 1) {
response = setPauseCountdown(Integer.parseInt(args[0]));
Expand Down Expand Up @@ -891,7 +891,7 @@ public String setSpecSpawn(Location loc) {
.replace("%yaw%", Float.toString(loc.getYaw())).replace("%pitch%", Float.toString(loc.getPitch()));
}

public String setPos1(Location loc) {
public String setPos1(Location loc, boolean force) {
if (game.getWorld() == null) {
game.setWorld(loc.getWorld());
}
Expand All @@ -903,13 +903,27 @@ public String setPos1(Location loc) {
return i18n("admin_command_pos1_pos2_difference_must_be_higher");
}
}

if (!force) {
for (String gameN : Main.getGameNames()) {
Game game1 = Main.getGame(gameN);
if (game1 == game) {
continue;
}

if (game.getPos2() != null && arenaOverlaps(game1.getPos1(), game1.getPos2(), loc, game.getPos2()) || game.getPos2() == null && isInArea(game1.getPos1(), game1.getPos2(), loc)) {
return i18n("admin_arena_overlaps").replace("%arena%", game.getName()).replace("%position%", "pos1");
}
}
}

game.setPos1(loc);
return i18n("admin_command_pos1_setted").replace("%arena%", game.getName())
.replace("%x%", Integer.toString(loc.getBlockX())).replace("%y%", Integer.toString(loc.getBlockY()))
.replace("%z%", Integer.toString(loc.getBlockZ()));
}

public String setPos2(Location loc) {
public String setPos2(Location loc, boolean force) {
if (game.getWorld() == null) {
game.setWorld(loc.getWorld());
}
Expand All @@ -921,6 +935,20 @@ public String setPos2(Location loc) {
return i18n("admin_command_pos1_pos2_difference_must_be_higher");
}
}

if (!force) {
for (String gameN : Main.getGameNames()) {
Game game1 = Main.getGame(gameN);
if (game1 == game) {
continue;
}

if (game.getPos1() != null && arenaOverlaps(game1.getPos1(), game1.getPos2(), game.getPos1(), loc) || game.getPos1() == null && isInArea(game1.getPos1(), game1.getPos2(), loc)) {
return i18n("admin_arena_overlaps").replace("%arena%", game.getName()).replace("%position%", "pos2");
}
}
}

game.setPos2(loc);
return i18n("admin_command_pos2_setted").replace("%arena%", game.getName())
.replace("%x%", Integer.toString(loc.getBlockX())).replace("%y%", Integer.toString(loc.getBlockY()))
Expand Down Expand Up @@ -956,6 +984,32 @@ public static boolean isInArea(Location l, Location p1, Location p2) {
&& max.getY() >= l.getY() && max.getZ() >= l.getZ());
}

public static boolean arenaOverlaps(Location l1, Location l2, Location p1, Location p2) {
if (!p1.getWorld().equals(l1.getWorld())) {
return false;
}

double minLX = Math.min(l1.getX(), l2.getX());
double minLY = Math.min(l1.getY(), l2.getY());
double minLZ = Math.min(l1.getZ(), l2.getZ());
double maxLX = Math.max(l1.getX(), l2.getX());
double maxLY = Math.max(l1.getY(), l2.getY());
double maxLZ = Math.max(l1.getZ(), l2.getZ());

double minPX = Math.min(p1.getX(), p2.getX());
double minPY = Math.min(p1.getY(), p2.getY());
double minPZ = Math.min(p1.getZ(), p2.getZ());
double maxPX = Math.max(p1.getX(), p2.getX());
double maxPY = Math.max(p1.getY(), p2.getY());
double maxPZ = Math.max(p1.getZ(), p2.getZ());

// From https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/util/BoundingBox.java#699
// The method in Bukkit is too new for us
return minLX < maxPX && maxLX > minPX
&& minLY < maxPY && maxLY > minPY
&& minLZ < maxPZ && maxLZ > minPZ;
}

public static boolean isChunkInArea(Chunk l, Location p1, Location p2) {
if (!p1.getWorld().equals(l.getWorld())) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/main/resources/languages/language_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,5 @@ game_ended_too_early: "&cThe game ends too early. No one wins."
help_bw_cheatin_give: "/bw cheatIn <game> give <resource> <amount> <player> - &cGives player specified amount of resource"
help_bw_cheatin_kill: "/bw cheatIn <game> kill <player> - &cKills specified player"
help_bw_cheatin_destroybed: "/bw cheatIn <game> destroybed <team> - &cDestroys bed of the specific team"
help_bw_cheatin_destroyallbeds: "/bw cheatIn <game> destroyallbeds - &cDestroys all beds in the game"
help_bw_cheatin_destroyallbeds: "/bw cheatIn <game> destroyallbeds - &cDestroys all beds in the game"
admin_arena_overlaps: "&cThe chosen location causes the arena to overlap with another arena, which can lead to errors! Please choose a different location. If you insist on using this location, execute &7/bw admin %arena% %position% force"

0 comments on commit ccdea59

Please sign in to comment.