Adds /metropolis-plot-go command to teleport to a plot.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
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 static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldguard.protection.flags.Flags;
|
||||
import com.sk89q.worldguard.protection.flags.StateFlag;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PlotReservationLookupTest {
|
||||
private static final UUID PLAYER_ID = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
|
||||
|
||||
@Test
|
||||
void findsReservationsWherePlayerIsOwnerOrMemberButExcludesHomeCityAndSpawn() {
|
||||
ProtectedCuboidRegion memberReservation = region("r_member");
|
||||
memberReservation.getMembers().addPlayer(PLAYER_ID);
|
||||
ProtectedCuboidRegion ownerReservation = region("private-yard");
|
||||
ownerReservation.getOwners().addPlayer(PLAYER_ID);
|
||||
ProtectedCuboidRegion home = region("h_1_" + PLAYER_ID);
|
||||
home.getMembers().addPlayer(PLAYER_ID);
|
||||
ProtectedCuboidRegion city = region("City");
|
||||
city.getMembers().addPlayer(PLAYER_ID);
|
||||
ProtectedCuboidRegion spawn = region("Spawn");
|
||||
spawn.getMembers().addPlayer(PLAYER_ID);
|
||||
ProtectedCuboidRegion unrelated = region("other-region", false);
|
||||
unrelated.getMembers().addPlayer(PLAYER_ID);
|
||||
|
||||
List<Plot> reservations =
|
||||
PlotReservationLookup.forPlayer(
|
||||
List.of(memberReservation, ownerReservation, home, city, spawn, unrelated), PLAYER_ID);
|
||||
|
||||
assertEquals(
|
||||
List.of("private-yard", "r_member"),
|
||||
reservations.stream().map(Plot::getRegionName).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void findsNamedReservationWithOrWithoutPrefix() {
|
||||
ProtectedCuboidRegion reservation = region("r_camp");
|
||||
reservation.getMembers().addPlayer(PLAYER_ID);
|
||||
List<Plot> reservations = PlotReservationLookup.forPlayer(List.of(reservation), PLAYER_ID);
|
||||
|
||||
assertEquals("r_camp", PlotReservationLookup.findByName(reservations, "camp").getRegionName());
|
||||
assertEquals(
|
||||
"r_camp", PlotReservationLookup.findByName(reservations, "r_camp").getRegionName());
|
||||
}
|
||||
|
||||
private static ProtectedCuboidRegion region(String id) {
|
||||
return region(id, true);
|
||||
}
|
||||
|
||||
private static ProtectedCuboidRegion region(String id, boolean reservedFlags) {
|
||||
ProtectedCuboidRegion region =
|
||||
new ProtectedCuboidRegion(id, BlockVector3.ZERO, BlockVector3.at(5, 5, 5));
|
||||
if (reservedFlags) {
|
||||
region.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY);
|
||||
region.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY);
|
||||
}
|
||||
return region;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
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.commands;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin;
|
||||
import com.majinnaibu.bukkitplugins.metropolis.Plot;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MetropolisPlotGoCommandTest {
|
||||
private static final UUID PLAYER_ID = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
|
||||
|
||||
@Test
|
||||
void teleportsPlayerToTheirOnlyReservation() {
|
||||
MetropolisPlugin plugin = mock(MetropolisPlugin.class);
|
||||
Player player = player("Member", PLAYER_ID);
|
||||
Plot reservation = plot("r_camp");
|
||||
when(plugin.getReservationsForPlayer(PLAYER_ID)).thenReturn(List.of(reservation));
|
||||
MetropolisPlotGoCommand command = new MetropolisPlotGoCommand(plugin);
|
||||
|
||||
assertTrue(command.onCommand(player, mock(Command.class), "plot-go", new String[0]));
|
||||
|
||||
verify(plugin).teleportPlayerToPlot(player, reservation);
|
||||
verify(player).sendMessage(contains("reservation r_camp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void asksForAReservationNameWhenPlayerBelongsToSeveral() {
|
||||
MetropolisPlugin plugin = mock(MetropolisPlugin.class);
|
||||
Player player = player("Member", PLAYER_ID);
|
||||
when(plugin.getReservationsForPlayer(PLAYER_ID))
|
||||
.thenReturn(List.of(plot("r_camp"), plot("r_garden")));
|
||||
MetropolisPlotGoCommand command = new MetropolisPlotGoCommand(plugin);
|
||||
|
||||
assertFalse(command.onCommand(player, mock(Command.class), "plot-go", new String[0]));
|
||||
|
||||
verify(player).sendMessage(contains("r_camp, r_garden"));
|
||||
verify(plugin, never()).teleportPlayerToPlot(player, plot("r_camp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizedSenderCanTargetAnOnlineMember() {
|
||||
MetropolisPlugin plugin = mock(MetropolisPlugin.class);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
Player target = player("Member", PLAYER_ID);
|
||||
Plot reservation = plot("r_camp");
|
||||
when(sender.hasPermission("metropolis.plot.go")).thenReturn(true);
|
||||
when(plugin.getPlayer("Member")).thenReturn(target);
|
||||
when(plugin.getReservationsForPlayer(PLAYER_ID)).thenReturn(List.of(reservation));
|
||||
MetropolisPlotGoCommand command = new MetropolisPlotGoCommand(plugin);
|
||||
|
||||
assertTrue(command.onCommand(sender, mock(Command.class), "plot-go", new String[] {"Member"}));
|
||||
|
||||
verify(plugin).teleportPlayerToPlot(target, reservation);
|
||||
verify(target).sendMessage(contains("You were teleported"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsNoMembershipAndUnsafeDestinations() {
|
||||
MetropolisPlugin plugin = mock(MetropolisPlugin.class);
|
||||
Player player = player("Member", PLAYER_ID);
|
||||
when(plugin.getReservationsForPlayer(PLAYER_ID)).thenReturn(List.of());
|
||||
MetropolisPlotGoCommand command = new MetropolisPlotGoCommand(plugin);
|
||||
|
||||
assertFalse(command.onCommand(player, mock(Command.class), "plot-go", new String[0]));
|
||||
verify(player).sendMessage(contains("not a member or owner"));
|
||||
|
||||
Plot reservation = plot("r_camp");
|
||||
when(plugin.getReservationsForPlayer(PLAYER_ID)).thenReturn(List.of(reservation));
|
||||
when(plugin.teleportPlayerToPlot(player, reservation)).thenReturn("No safe teleport location");
|
||||
assertFalse(command.onCommand(player, mock(Command.class), "plot-go", new String[0]));
|
||||
verify(player).sendMessage(contains("No safe teleport location"));
|
||||
}
|
||||
|
||||
private static Player player(String name, UUID id) {
|
||||
Player player = mock(Player.class);
|
||||
when(player.getName()).thenReturn(name);
|
||||
when(player.getUniqueId()).thenReturn(id);
|
||||
return player;
|
||||
}
|
||||
|
||||
private static Plot plot(String name) {
|
||||
return new Plot(name, BlockVector3.ZERO, BlockVector3.at(5, 5, 5));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user