mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 06:28:49 +00:00
Fix local username closest-match resolution overriding global exact matches (#392)
This commit is contained in:
1 parent
b1bd44ac64
commit
daa72182f8
11 files changed
+85
-23
No files matched your search
@@ -202,7 +202,6 @@ public class BukkitPluginTests {
|
|||||||
Assertions.assertFalse(plugin.getValidator().isValidName(name));
|
Assertions.assertFalse(plugin.getValidator().isValidName(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
// test descriptions
|
|
||||||
@DisplayName("Test Validator Accepts Valid Descriptions")
|
@DisplayName("Test Validator Accepts Valid Descriptions")
|
||||||
@ParameterizedTest(name = "Valid Description: \"{0}\"")
|
@ParameterizedTest(name = "Valid Description: \"{0}\"")
|
||||||
@ValueSource(strings = {
|
@ValueSource(strings = {
|
||||||
@@ -366,7 +365,6 @@ public class BukkitPluginTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// home tests, like warps but with a user parameter (owner) as well as name and position and a test for changing home privacy
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("Home Tests")
|
@DisplayName("Home Tests")
|
||||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
|||||||
@@ -78,6 +78,31 @@ public interface HuskHomes extends TaskRunner, EventDispatcher, SafetyResolver {
|
|||||||
@NotNull
|
@NotNull
|
||||||
List<OnlineUser> getOnlineUsers();
|
List<OnlineUser> getOnlineUsers();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a local {@link OnlineUser} by their name. Auto-completes partially typed names for the closest match
|
||||||
|
*
|
||||||
|
* @param playerName the name of the player to find
|
||||||
|
* @return an {@link Optional} containing the {@link OnlineUser} if found, or an empty {@link Optional} if not found
|
||||||
|
*/
|
||||||
|
default Optional<OnlineUser> getOnlineUser(@NotNull String playerName) {
|
||||||
|
return getOnlineUserExact(playerName)
|
||||||
|
.or(() -> getOnlineUsers().stream()
|
||||||
|
.filter(user -> user.getUsername().toLowerCase().startsWith(playerName.toLowerCase()))
|
||||||
|
.findFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a local {@link OnlineUser} by their name.
|
||||||
|
*
|
||||||
|
* @param playerName the name of the player to find
|
||||||
|
* @return an {@link Optional} containing the {@link OnlineUser} if found, or an empty {@link Optional} if not found
|
||||||
|
*/
|
||||||
|
default Optional<OnlineUser> getOnlineUserExact(@NotNull String playerName) {
|
||||||
|
return getOnlineUsers().stream()
|
||||||
|
.filter(user -> user.getUsername().equalsIgnoreCase(playerName))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
Set<SavedUser> getSavedUsers();
|
Set<SavedUser> getSavedUsers();
|
||||||
|
|
||||||
@@ -111,22 +136,6 @@ public interface HuskHomes extends TaskRunner, EventDispatcher, SafetyResolver {
|
|||||||
log(Level.INFO, "Successfully initialized " + name);
|
log(Level.INFO, "Successfully initialized " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds a local {@link OnlineUser} by their name. Auto-completes partially typed names for the closest match
|
|
||||||
*
|
|
||||||
* @param playerName the name of the player to find
|
|
||||||
* @return an {@link Optional} containing the {@link OnlineUser} if found, or an empty {@link Optional} if not found
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
default Optional<OnlineUser> findOnlinePlayer(@NotNull String playerName) {
|
|
||||||
return getOnlineUsers().stream()
|
|
||||||
.filter(user -> user.getUsername().equalsIgnoreCase(playerName))
|
|
||||||
.findFirst()
|
|
||||||
.or(() -> getOnlineUsers().stream()
|
|
||||||
.filter(user -> user.getUsername().toLowerCase().startsWith(playerName.toLowerCase()))
|
|
||||||
.findFirst());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plugin {@link Settings} loaded from file
|
* The plugin {@link Settings} loaded from file
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ public class RtpCommand extends Command implements UserListTabProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||||
final Optional<OnlineUser> optionalTeleporter = args.length >= 1 ? plugin.findOnlinePlayer(args[0])
|
final Optional<OnlineUser> optionalTeleporter = args.length >= 1 ? plugin.getOnlineUser(args[0])
|
||||||
: executor instanceof OnlineUser ? Optional.of((OnlineUser) executor) : Optional.empty();
|
: executor instanceof OnlineUser ? Optional.of((OnlineUser) executor) : Optional.empty();
|
||||||
if (optionalTeleporter.isEmpty()) {
|
if (optionalTeleporter.isEmpty()) {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ public class RequestsManager {
|
|||||||
@NotNull TeleportRequest.Type type) throws IllegalArgumentException {
|
@NotNull TeleportRequest.Type type) throws IllegalArgumentException {
|
||||||
final long expiry = Instant.now().getEpochSecond() + plugin.getSettings().getTeleportRequestExpiryTime();
|
final long expiry = Instant.now().getEpochSecond() + plugin.getSettings().getTeleportRequestExpiryTime();
|
||||||
final TeleportRequest request = new TeleportRequest(requester, type, expiry);
|
final TeleportRequest request = new TeleportRequest(requester, type, expiry);
|
||||||
final Optional<OnlineUser> localTarget = plugin.findOnlinePlayer(targetUser);
|
final Optional<OnlineUser> localTarget = plugin.getOnlineUser(targetUser);
|
||||||
if (localTarget.isPresent()) {
|
if (localTarget.isPresent()) {
|
||||||
if (localTarget.get().equals(requester)) {
|
if (localTarget.get().equals(requester)) {
|
||||||
throw new IllegalArgumentException("Cannot send a teleport request to yourself");
|
throw new IllegalArgumentException("Cannot send a teleport request to yourself");
|
||||||
@@ -282,7 +282,7 @@ public class RequestsManager {
|
|||||||
.ifPresent(recipient::sendMessage);
|
.ifPresent(recipient::sendMessage);
|
||||||
|
|
||||||
// Find the requester and inform them of the response
|
// Find the requester and inform them of the response
|
||||||
final Optional<OnlineUser> localRequester = plugin.findOnlinePlayer(request.getRequesterName());
|
final Optional<OnlineUser> localRequester = plugin.getOnlineUser(request.getRequesterName());
|
||||||
if (localRequester.isPresent()) {
|
if (localRequester.isPresent()) {
|
||||||
handleLocalRequestResponse(localRequester.get(), request);
|
handleLocalRequestResponse(localRequester.get(), request);
|
||||||
} else if (plugin.getSettings().doCrossServer()) {
|
} else if (plugin.getSettings().doCrossServer()) {
|
||||||
|
|||||||
@@ -21,8 +21,17 @@ package net.william278.huskhomes.teleport;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a target; a player name or a location that will be teleported <i>to</i>.
|
||||||
|
*/
|
||||||
public interface Target {
|
public interface Target {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@link Target} from a player name
|
||||||
|
*
|
||||||
|
* @param target the player name
|
||||||
|
* @return the target
|
||||||
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
static Target username(@NotNull String target) {
|
static Target username(@NotNull String target) {
|
||||||
return new Username(target);
|
return new Username(target);
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the process of a {@link Teleportable} being teleported to a {@link Target}.
|
||||||
|
*
|
||||||
|
* @see Teleport#builder(HuskHomes)
|
||||||
|
*/
|
||||||
public class Teleport {
|
public class Teleport {
|
||||||
|
|
||||||
protected final HuskHomes plugin;
|
protected final HuskHomes plugin;
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A builder for {@link Teleport} and {@link TimedTeleport} objects.
|
||||||
|
*/
|
||||||
public class TeleportBuilder {
|
public class TeleportBuilder {
|
||||||
private final HuskHomes plugin;
|
private final HuskHomes plugin;
|
||||||
private OnlineUser executor;
|
private OnlineUser executor;
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ public class TeleportRequest {
|
|||||||
/**
|
/**
|
||||||
* The user making the request
|
* The user making the request
|
||||||
*/
|
*/
|
||||||
|
@NotNull
|
||||||
public String getRequesterName() {
|
public String getRequesterName() {
|
||||||
return requesterName;
|
return requesterName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,22 @@
|
|||||||
package net.william278.huskhomes.teleport;
|
package net.william278.huskhomes.teleport;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a teleporter; the person performing the teleport.
|
||||||
|
* <p>
|
||||||
|
* Can be represented as an {@link OnlineUser} locally or as a {@link Username username} reference
|
||||||
|
* that needs to be resolved first.
|
||||||
|
*/
|
||||||
public interface Teleportable {
|
public interface Teleportable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@link Teleportable} from a player name
|
||||||
|
*
|
||||||
|
* @param teleporter the player name
|
||||||
|
* @return the teleportable
|
||||||
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
static Teleportable username(@NotNull String teleporter) {
|
static Teleportable username(@NotNull String teleporter) {
|
||||||
return new Username(teleporter);
|
return new Username(teleporter);
|
||||||
|
|||||||
@@ -30,6 +30,11 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a {@link Teleport} that has an associated warmup time; the teleport will not be performed until the
|
||||||
|
* warmup time has elapsed, during which the user must not move or take damage.
|
||||||
|
* @see Teleport#builder(HuskHomes)
|
||||||
|
*/
|
||||||
public class TimedTeleport extends Teleport {
|
public class TimedTeleport extends Teleport {
|
||||||
|
|
||||||
public static final String BYPASS_PERMISSION = "huskhomes.bypass_teleport_warmup";
|
public static final String BYPASS_PERMISSION = "huskhomes.bypass_teleport_warmup";
|
||||||
|
|||||||
@@ -25,11 +25,30 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the username of a player who may or may not be online, either locally as an {@link OnlineUser},
|
||||||
|
* or on a different server.
|
||||||
|
*
|
||||||
|
* @param name The name of the player
|
||||||
|
*/
|
||||||
public record Username(@NotNull String name) implements Teleportable, Target {
|
public record Username(@NotNull String name) implements Teleportable, Target {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a local {@link OnlineUser} by their name.
|
||||||
|
*
|
||||||
|
* @param plugin The instance of {@link HuskHomes}
|
||||||
|
* @return An {@link Optional} containing the {@link OnlineUser} if found
|
||||||
|
* @throws TeleportationException If the user is not found
|
||||||
|
* @implNote If a user by the name provided is on the {@link HuskHomes#getPlayerList() player list}, then this
|
||||||
|
* method will search for the user by exact name.
|
||||||
|
* <p>
|
||||||
|
* Otherwise, the lookup will first attempt to find the user by exact name, and if that fails, it will search for
|
||||||
|
* the closest name match.
|
||||||
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public Optional<OnlineUser> findLocally(@NotNull HuskHomes plugin) throws TeleportationException {
|
public Optional<OnlineUser> findLocally(@NotNull HuskHomes plugin) {
|
||||||
return plugin.findOnlinePlayer(name);
|
return plugin.getPlayerList(true).stream().anyMatch(listedName -> listedName.equalsIgnoreCase(name))
|
||||||
|
? plugin.getOnlineUserExact(name) : plugin.getOnlineUser(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user