Adds /metropolis-plot-go command to teleport to a plot.

This commit is contained in:
2026-09-27 04:59:16 -07:00
parent b6b30621f0
commit da46d3e4b9
11 changed files with 379 additions and 29 deletions

View File

@@ -1237,11 +1237,17 @@ public class MetropolisPlugin extends JavaPlugin {
public String teleportPlayerToPlot(Player player, Plot plot) {
Location loc = plot.getViableSpawnLocation(world);
if (loc != null) {
player.teleport(loc);
if (loc == null) {
return "No safe teleport location exists in plot " + plot.getRegionName() + ".";
}
return player.teleport(loc) ? null : "Unable to teleport to plot " + plot.getRegionName() + ".";
}
return null;
public List<Plot> getReservationsForPlayer(UUID playerId) {
if (regionManager == null) {
return List.of();
}
return PlotReservationLookup.forPlayer(regionManager.getRegions().values(), playerId);
}
public boolean homeExists(UUID playerId, int homeNumber) {

View File

@@ -0,0 +1,72 @@
/*
This file is part of Metropolis.
Metropolis is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Metropolis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Metropolis. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
*/
package com.majinnaibu.bukkitplugins.metropolis;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
public final class PlotReservationLookup {
private PlotReservationLookup() {}
public static List<Plot> forPlayer(Iterable<ProtectedRegion> regions, UUID playerId) {
List<Plot> reservations = new ArrayList<>();
for (ProtectedRegion region : regions) {
if (!(region instanceof ProtectedCuboidRegion) || !isReservation(region)) {
continue;
}
if (region.getOwners().getUniqueIds().contains(playerId)
|| region.getMembers().getUniqueIds().contains(playerId)) {
reservations.add(Plot.get(region));
}
}
reservations.sort(Comparator.comparing(Plot::getRegionName));
return List.copyOf(reservations);
}
public static Plot findByName(List<Plot> reservations, String requestedName) {
String prefixedName = requestedName.startsWith("r_") ? requestedName : "r_" + requestedName;
for (Plot reservation : reservations) {
if (reservation.getRegionName().equals(requestedName)
|| reservation.getRegionName().equals(prefixedName)) {
return reservation;
}
}
return null;
}
private static boolean isReservation(ProtectedRegion region) {
String regionId = region.getId();
if (regionId.startsWith("r_")) {
return true;
}
if (regionId.startsWith("h_")
|| regionId.equalsIgnoreCase("City")
|| regionId.equalsIgnoreCase("Spawn")) {
return false;
}
return region.getFlag(Flags.LAVA_FLOW) == StateFlag.State.DENY
&& region.getFlag(Flags.SNOW_FALL) == StateFlag.State.DENY;
}
}

View File

@@ -19,6 +19,7 @@ package com.majinnaibu.bukkitplugins.metropolis.commands;
import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin;
import com.majinnaibu.bukkitplugins.metropolis.Plot;
import com.majinnaibu.bukkitplugins.metropolis.PlotReservationLookup;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@@ -34,23 +35,85 @@ public class MetropolisPlotGoCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = null;
Plot plot = null;
if (sender instanceof Player) {
player = (Player) sender;
}
if (args.length == 1 && player != null) {
plot = _plugin.getPlot(args[0]);
} else if (args.length >= 2) {
player = _plugin.getPlayer(args[1]);
plot = _plugin.getPlot(args[0]);
} else {
if (args.length > 2) {
sender.sendMessage(
"Usage: /metropolis-plot-go [playerName] or /metropolis-plot-go <reservationName> [playerName]");
return false;
}
if (plot == null || player == null) {
Player player;
String requestedReservation = null;
if (args.length == 0) {
if (!(sender instanceof Player senderPlayer)) {
sender.sendMessage("Console must specify an online player name.");
return false;
}
player = senderPlayer;
} else if (args.length == 1) {
Player namedPlayer = _plugin.getPlayer(args[0]);
if (namedPlayer != null) {
player = namedPlayer;
} else if (sender instanceof Player senderPlayer) {
player = senderPlayer;
requestedReservation = args[0];
} else {
sender.sendMessage("No online player named " + args[0] + ".");
return false;
}
} else {
requestedReservation = args[0];
player = _plugin.getPlayer(args[1]);
if (player == null) {
sender.sendMessage("No online player named " + args[1] + ".");
return false;
}
}
boolean canManagePlots = sender.hasPermission("metropolis.plot.go");
if (player != sender && !canManagePlots) {
sender.sendMessage("You do not have permission to teleport another player.");
return false;
}
var reservations = _plugin.getReservationsForPlayer(player.getUniqueId());
Plot plot;
if (requestedReservation == null) {
if (reservations.isEmpty()) {
sender.sendMessage(
player.getName() + " is not a member or owner of a Metropolis reservation.");
return false;
}
if (reservations.size() > 1) {
String names =
reservations.stream()
.map(Plot::getRegionName)
.reduce((first, second) -> first + ", " + second)
.orElse("");
sender.sendMessage(
player.getName()
+ " belongs to multiple reservations: "
+ names
+ ". Specify one with /metropolis-plot-go <reservationName> [playerName].");
return false;
}
plot = reservations.get(0);
} else {
plot = PlotReservationLookup.findByName(reservations, requestedReservation);
if (plot == null && canManagePlots) {
plot = _plugin.getPlot(requestedReservation);
}
if (plot == null) {
sender.sendMessage(
player.getName()
+ " is not a member or owner of reservation "
+ requestedReservation
+ ".");
return false;
}
}
if (player != sender && !canManagePlots) {
sender.sendMessage("You do not have permission to teleport another player.");
return false;
}
@@ -60,6 +123,11 @@ public class MetropolisPlotGoCommand implements CommandExecutor {
return false;
}
String destination = "reservation " + plot.getRegionName();
sender.sendMessage("Teleported " + player.getName() + " to " + destination + ".");
if (player != sender) {
player.sendMessage("You were teleported to " + destination + ".");
}
return true;
}
}

View File

@@ -40,9 +40,8 @@ commands:
permission: metropolis.plot.reserve
usage: /metropolis-plot-reserve <name> <minX> <minY> <minZ> <maxX> <maxY> <maxZ>
metropolis-plot-go:
description: This command teleports the user or another player to a plot.
permission: metropolis.plot.go
usage: /metropolis-plot-go <plotName> [playerName]
description: Teleports the sender or an authorized target to a reservation.
usage: /metropolis-plot-go [playerName] or /metropolis-plot-go <reservationName> [playerName]
metropolis-debug-generatetesthomes:
description: This command is ignored on non-debug builds.
permission: metropolis.debug