Skip to content

Commit

Permalink
Get the max name length from the FriendAdders
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Sep 4, 2024
1 parent 529ea60 commit 5672f9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void init() {
GameProfileCache.setUsesAuthentication(true); // This makes non-existent users return an empty value instead of an offline mode fallback.

nameField = addRenderableWidget(new EditBox(font, width / 2 - 100, 66, 200, 20, nameField, FRIEND_USERNAME_TEXT));
nameField.setMaxLength(36);
friendAdders.stream().mapToInt(FriendAdder::maxValidNameLength).max().ifPresent(nameField::setMaxLength);
//#if MC >= 1.19.4
setInitialFocus(nameField);
//#else
Expand All @@ -95,6 +95,7 @@ protected void init() {
userList.clearUsers();
final List<FriendAdder> delayedAdders = new ArrayList<>();
for (final FriendAdder adder : friendAdders) {
if (name.length() > adder.maxValidNameLength()) continue;
if (adder.delayLookup(name)) {
delayedAdders.add(adder);
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/gaming32/worldhost/plugin/FriendAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ public interface FriendAdder {
*/
void searchFriends(String name, int maxResults, Consumer<FriendListFriend> friendConsumer);

/**
* Determine whether searching name is expensive to look up and should be delayed until the user has quit typing.
* @param name The name to check for delay requirements.
* @return Whether to delay the search until the user has quit typing.
*/
boolean delayLookup(String name);

/**
* The maximum length for a valid username. Names longer than this will not be given to this {@link FriendAdder}.
* @return The maximum valid username length;
*/
int maxValidNameLength();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public void searchFriends(String name, int maxResults, Consumer<FriendListFriend
public boolean delayLookup(String name) {
return !VALID_UUID.matcher(name).matches() && !name.startsWith("o:");
}

@Override
public int maxValidNameLength() {
return 36;
}
}

0 comments on commit 5672f9f

Please sign in to comment.