Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public CompletableFuture<User> findOrCreate(UUID uniqueId, String name) {
User cached = this.usersByUniqueId.get(uniqueId);

if (cached != null) {
this.updateNameIfChanged(cached, name);
return CompletableFuture.completedFuture(this.updateLastSeen(cached));
User updatedUser = this.updateNameIfChanged(cached, name);
return CompletableFuture.completedFuture(this.updateLastSeen(updatedUser));
}

return this.userRepository.getUser(uniqueId)
.thenApply(optionalUser -> {
User user = optionalUser
.map(existing -> {
this.updateNameIfChanged(existing, name);
return this.updateLastSeen(existing);
User updatedUser = this.updateNameIfChanged(existing, name);
return this.updateLastSeen(updatedUser);
})
.orElseGet(() -> this.createNewUser(uniqueId, name));

Expand All @@ -59,12 +59,16 @@ private User createNewUser(UUID uniqueId, String name) {
return user;
}

private void updateNameIfChanged(User user, String name) {
if (!user.getName().equals(name)) {
User updated = new User(user.getUniqueId(), name, user.getLastSeen(), user.getAccountCreated());
this.add(updated);
this.userRepository.saveUser(updated);
private User updateNameIfChanged(User user, String name) {
if (user.getName().equals(name)) {
return user;
}

User updated = new User(user.getUniqueId(), name, user.getLastSeen(), user.getAccountCreated());
this.removeNameMapping(user);
this.add(updated);
this.userRepository.saveUser(updated);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if completable future failes?

return updated;
}
Comment thread
vLuckyyy marked this conversation as resolved.

private User updateLastSeen(User user) {
Expand All @@ -74,6 +78,10 @@ private User updateLastSeen(User user) {
return updated;
}

private void removeNameMapping(User user) {
this.usersByName.remove(user.getName(), user);
}

private void add(User user) {
this.usersByUniqueId.put(user.getUniqueId(), user);
this.usersByName.put(user.getName(), user);
Expand Down