fix: correct issues with same-server RTP validation

This commit is contained in:
William committed 2024-12-13 17:33:49 +00:00
1 parent eb79ee8107
commit 1eebdb2718
6 files changed
+56 -20

No files matched your search

@@ -87,8 +87,8 @@ public class RtpCommand extends Command implements UserListTabCompletable {
} }
// Validate world and server, and execute RTP // Validate world and server, and execute RTP
Optional<Map.Entry<World, String>> validatedTarget = validateRtp(teleporter, executor, worldName, targetServer); validateRtp(teleporter, executor, worldName.replace("minecraft:", ""), targetServer)
validatedTarget.ifPresent(entry -> executeRtp(teleporter, executor, entry.getKey(), entry.getValue(), args)); .ifPresent(entry -> executeRtp(teleporter, executor, entry.getKey(), entry.getValue(), args));
} }
@Nullable @Nullable
@@ -149,14 +149,10 @@ public class RtpCommand extends Command implements UserListTabCompletable {
}; };
} }
/** /**
* Validates the RTP target world and server based on arguments, ensuring the server contains the target world. * Validates that a random teleport operation is valid.
* - If no server is specified, randomly selects a server containing the world.
* - Returns both the validated world and server as a pair.
* *
* @param teleporter The player to teleport * @param teleporter The player being teleported
* @param executor The player executing the command * @param executor The player executing the command
* @param worldName The world name to teleport to * @param worldName The world name to teleport to
* @param targetServer The server name to teleport to (optional) * @param targetServer The server name to teleport to (optional)
@@ -176,9 +172,43 @@ public class RtpCommand extends Command implements UserListTabCompletable {
return Optional.empty(); return Optional.empty();
} }
Map<String, List<String>> randomTargetServers = plugin.getSettings().getRtp().getRandomTargetServers(); // Validate a cross-server RTP, if applicable
if (plugin.getSettings().getRtp().isCrossServer() && !plugin.getServerName().equals(targetServer)) {
return validateCrossServerRtp(executor, worldName, targetServer);
}
// Find the local world
final Optional<World> localWorld = plugin.getWorlds().stream().filter((world) -> world
.getName().replace("minecraft:", "")
.equalsIgnoreCase(worldName)).findFirst();
if (localWorld.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_world", worldName)
.ifPresent(executor::sendMessage);
return Optional.empty();
}
// Check the local world is not restricted
if (plugin.getSettings().getRtp().isWorldRtpRestricted(localWorld.get())) {
plugin.getLocales().getLocale("error_rtp_restricted_world")
.ifPresent(executor::sendMessage);
return Optional.empty();
}
return localWorld.map(world -> new AbstractMap.SimpleImmutableEntry<>(world, worldName));
}
/**
* Validates the RTP target world and server based on arguments, ensuring the server contains the target world.
* - If no server is specified, randomly selects a server containing the world.
* - Returns both the validated world and server as a pair.
*
* @param executor The player executing the command
* @param worldName The world name to teleport to
* @param targetServer The server name to teleport to (optional)
* @return A pair of the target world and server to use for teleportation, if valid
*/
private Optional<Map.Entry<World, String>> validateCrossServerRtp(CommandUser executor, String worldName, String targetServer) {
// Get a list of servers that have the specified world // Get a list of servers that have the specified world
Map<String, List<String>> randomTargetServers = plugin.getSettings().getRtp().getRandomTargetServers();
List<String> eligibleServers = randomTargetServers.entrySet().stream() List<String> eligibleServers = randomTargetServers.entrySet().stream()
.filter(entry -> entry.getValue().contains(worldName)) .filter(entry -> entry.getValue().contains(worldName))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
@@ -196,11 +226,12 @@ public class RtpCommand extends Command implements UserListTabCompletable {
} }
Optional<World> targetWorld = plugin.getWorlds().stream() Optional<World> targetWorld = plugin.getWorlds().stream()
.filter(world -> world.getName().equalsIgnoreCase(worldName)) .filter(world -> world.getName().replace("minecraft:", "")
.equalsIgnoreCase(worldName))
.findFirst() .findFirst()
.or(() -> Optional.of(World.from(worldName, UUID.randomUUID()))); .or(() -> Optional.of(World.from(worldName)));
return targetWorld.map(world -> new AbstractMap.SimpleEntry<>(world, selectedServer)); return targetWorld.map(world -> new AbstractMap.SimpleImmutableEntry<>(world, selectedServer));
} }
/** /**
@@ -211,7 +242,7 @@ public class RtpCommand extends Command implements UserListTabCompletable {
* @param executor The player executing the command * @param executor The player executing the command
* @param world The validated world to teleport to * @param world The validated world to teleport to
* @param targetServer The validated server to teleport to * @param targetServer The validated server to teleport to
* @param args Arguments to pass to the RTP engine * @param args Arguments to pass to the RTP engine
*/ */
private void executeRtp(@NotNull OnlineUser teleporter, @NotNull CommandUser executor, private void executeRtp(@NotNull OnlineUser teleporter, @NotNull CommandUser executor,
@NotNull World world, @NotNull String targetServer, @NotNull String[] args) { @NotNull World world, @NotNull String targetServer, @NotNull String[] args) {
@@ -220,7 +251,7 @@ public class RtpCommand extends Command implements UserListTabCompletable {
.ifPresent(teleporter::sendMessage); .ifPresent(teleporter::sendMessage);
if (plugin.getSettings().getRtp().isCrossServer() && plugin.getSettings().getCrossServer().isEnabled() if (plugin.getSettings().getRtp().isCrossServer() && plugin.getSettings().getCrossServer().isEnabled()
&& plugin.getSettings().getCrossServer().getBrokerType() == Broker.Type.REDIS) { && plugin.getSettings().getCrossServer().getBrokerType() == Broker.Type.REDIS) {
if (targetServer.equals(plugin.getServerName())) { if (targetServer.equals(plugin.getServerName())) {
performLocalRTP(teleporter, executor, world, args); performLocalRTP(teleporter, executor, world, args);
return; return;
@@ -347,7 +347,7 @@ public final class Settings {
@Comment({"List of server in which /rtp is allowed. (Only relevant when using cross server mode WITH REDIS)", @Comment({"List of server in which /rtp is allowed. (Only relevant when using cross server mode WITH REDIS)",
"If a server is not defined here the RTP logic has no way of knowing its existence."}) "If a server is not defined here the RTP logic has no way of knowing its existence."})
private Map<String, List<String>> randomTargetServers = new HashMap<>( private Map<String, List<String>> randomTargetServers = new HashMap<>(
Map.of("survival_server", List.of("world", "world_nether", "world_the_end")) Map.of("server", List.of("world", "world_nether", "world_the_end"))
); );
} }
@@ -50,6 +50,11 @@ public class World {
return new World(name, uuid, null); return new World(name, uuid, null);
} }
@NotNull
public static World from(@NotNull String name) {
return from(name, UUID.randomUUID());
}
@NotNull @NotNull
@SuppressWarnings("unused") @SuppressWarnings("unused")
public Environment getEnvironment() { public Environment getEnvironment() {
@@ -49,7 +49,7 @@ public class MessageSerializationTests {
.target("TestTarget", Message.TargetType.PLAYER) .target("TestTarget", Message.TargetType.PLAYER)
.payload(Payload.position( .payload(Payload.position(
Position.at(63.25, 127.43, -32, 180f, -94.3f, Position.at(63.25, 127.43, -32, 180f, -94.3f,
World.from("TestWorld", UUID.randomUUID()), "TestServer"))) World.from("TestWorld"), "TestServer")))
.build(), .build(),
Message.builder() Message.builder()
.type(Message.MessageType.TELEPORT_TO_NETWORKED_USER) .type(Message.MessageType.TELEPORT_TO_NETWORKED_USER)
@@ -61,7 +61,7 @@ public class MessageSerializationTests {
.target("TestTarget", Message.TargetType.PLAYER) .target("TestTarget", Message.TargetType.PLAYER)
.payload(Payload.position( .payload(Payload.position(
Position.at(63.25, 127.43, -32, 180f, -94.3f, Position.at(63.25, 127.43, -32, 180f, -94.3f,
World.from("TestWorld", UUID.randomUUID()), "TestServer"))) World.from("TestWorld"), "TestServer")))
.build() .build()
); );
@@ -81,7 +81,7 @@ public class SavedPositionTests {
@NotNull @NotNull
private static Stream<Arguments> provideWarpData() { private static Stream<Arguments> provideWarpData() {
final Position position = Position.at(63.25, 127.43, -32, 180f, -94.3f, final Position position = Position.at(63.25, 127.43, -32, 180f, -94.3f,
World.from("TestWorld", UUID.randomUUID()), "TestServer"); World.from("TestWorld"), "TestServer");
return POSITION_UNSAFE_NAMES.entrySet().stream() return POSITION_UNSAFE_NAMES.entrySet().stream()
.map(entry -> Arguments.of( .map(entry -> Arguments.of(
Warp.from(position, PositionMeta.create(entry.getKey(), "")), Warp.from(position, PositionMeta.create(entry.getKey(), "")),
@@ -93,7 +93,7 @@ public class SavedPositionTests {
@NotNull @NotNull
private static Stream<Arguments> provideHomeData() { private static Stream<Arguments> provideHomeData() {
final Position position = Position.at(63.25, 127.43, -32, 180f, -94.3f, final Position position = Position.at(63.25, 127.43, -32, 180f, -94.3f,
World.from("TestWorld", UUID.randomUUID()), "TestServer"); World.from("TestWorld"), "TestServer");
return POSITION_UNSAFE_NAMES.entrySet().stream() return POSITION_UNSAFE_NAMES.entrySet().stream()
.map(entry -> Arguments.of( .map(entry -> Arguments.of(
Home.from( Home.from(
@@ -105,7 +105,7 @@ public class NormalDistributionTests {
final List<Location> locations = new ArrayList<>(); final List<Location> locations = new ArrayList<>();
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
locations.add(NormalDistributionEngine.generateLocation( locations.add(NormalDistributionEngine.generateLocation(
Location.at(0, 0, 0, 0, 0, World.from("TestWorld", UUID.randomUUID())), Location.at(0, 0, 0, 0, 0, World.from("TestWorld")),
MEAN, STANDARD_DEVIATION, SPAWN_RADIUS, MAX_RADIUS)); MEAN, STANDARD_DEVIATION, SPAWN_RADIUS, MAX_RADIUS));
} }
return locations; return locations;