Skip to content

Commit

Permalink
Tweak FriendAdder to have searchFriends instead of resolveFriend
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 19, 2024
1 parent 826d115 commit cb97261
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ private void setFriend(FriendListFriend friend) {

private void resolveFriend(String name) {
final int currentReloadCount = ++reloadCount;
friendAdder.resolveFriend(name)
friendAdder.searchFriends(name, 1)
.thenAcceptAsync(ready -> {
if (reloadCount == currentReloadCount) {
setFriend(ready.orElse(null));
setFriend(!ready.isEmpty() ? ready.getFirst() : null);
}
}, Minecraft.getInstance())
.exceptionally(t -> {
Expand All @@ -112,7 +112,7 @@ protected void init() {
lastTyping = Util.getMillis();
setFriend(null);
addFriendButton.active = false;
if (friendAdder.rateLimit(name)) {
if (friendAdder.delayLookup(name)) {
delayedFriendUpdate = true;
} else {
delayedFriendUpdate = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import net.minecraft.network.chat.Component;

import java.util.Optional;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface FriendAdder {
Component label();

CompletableFuture<Optional<FriendListFriend>> resolveFriend(String name);
CompletableFuture<List<? extends FriendListFriend>> searchFriends(String name, int maxResults);

boolean rateLimit(String name);
boolean delayLookup(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.network.chat.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -23,32 +24,31 @@ public Component label() {
}

@Override
public CompletableFuture<Optional<FriendListFriend>> resolveFriend(String name) {
public CompletableFuture<List<? extends FriendListFriend>> searchFriends(String name, int maxResults) {
if (VALID_USERNAME.matcher(name).matches()) {
final CompletableFuture<Optional<FriendListFriend>> result = new CompletableFuture<>();
final CompletableFuture<Optional<GameProfile>> result = new CompletableFuture<>();
WorldHost.getMaybeAsync(
WorldHost.getProfileCache(), name,
profile -> result.complete(profile.map(WorldHostFriendListFriend::new))
WorldHost.getProfileCache(), name, result::complete
);
return result;
return result.thenApply(p -> p.map(WorldHostFriendListFriend::new).map(List::of).orElse(List.of()));
}
if (VALID_UUID.matcher(name).matches()) {
return CompletableFuture.completedFuture(Optional.of(
return CompletableFuture.completedFuture(List.of(
new WorldHostFriendListFriend(UUID.fromString(name))
));
}
if (name.startsWith("o:")) {
final String actualName = name.substring(2);
// TODO: Use createOfflinePlayerUUID when 1.19.2+ becomes the minimum, and createOfflineProfile in 1.20.4+
return CompletableFuture.completedFuture(Optional.of(new WorldHostFriendListFriend(new GameProfile(
return CompletableFuture.completedFuture(List.of(new WorldHostFriendListFriend(new GameProfile(
UUID.nameUUIDFromBytes(("OfflinePlayer:" + actualName).getBytes(StandardCharsets.UTF_8)), actualName
))));
}
return CompletableFuture.completedFuture(Optional.empty());
return CompletableFuture.completedFuture(List.of());
}

@Override
public boolean rateLimit(String name) {
public boolean delayLookup(String name) {
return !VALID_UUID.matcher(name).matches() && !name.startsWith("o:");
}
}

0 comments on commit cb97261

Please sign in to comment.