Style changes & UUID cache

This commit is contained in:
Draycia committed 2020-10-25 03:42:21 -07:00
1 parent be2ee01f9f
commit 10fc409266
8 files changed
+80 -38

No files matched your search

@@ -81,7 +81,6 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
@SuppressWarnings("initialization.fields.uninitialized")
@@ -158,21 +157,19 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
return users;
};
final Function<String, UUID> nameResolver = name -> Bukkit.getOfflinePlayer(name).getUniqueId();
if (storageType == StorageType.MYSQL) {
this.logger().info("Enabling MySQL storage!");
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
this.userService = new MySQLUserService<>(this, credentials, supplier, BukkitPlayerUser::new,
nameResolver, BukkitConsoleUser::new);
this.userService = new MySQLUserService<>(this, credentials, supplier,
BukkitPlayerUser::new, BukkitConsoleUser::new);
} else if (storageType == StorageType.JSON) {
this.logger().info("Enabling JSON storage!");
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, BukkitPlayerUser::new,
nameResolver, BukkitConsoleUser::new);
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
BukkitPlayerUser::new, BukkitConsoleUser::new);
} else {
this.logger().error("Invalid storage type selected! Falling back to JSON.");
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, BukkitPlayerUser::new,
nameResolver, BukkitConsoleUser::new);
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
BukkitPlayerUser::new, BukkitConsoleUser::new);
}
// Setup listeners
@@ -357,10 +354,60 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
new PAPIContext();
}
@Override
public @NonNull String resolveName(final @NonNull UUID uuid) {
if (this.messageService() instanceof RedisMessageService) {
final RedisMessageService redis = (RedisMessageService) this.messageService();
final String value = redis.get("UUID:" + uuid.toString());
if (value != null) {
return value;
} else {
final String name = Bukkit.getOfflinePlayer(uuid).getName();
if (name != null) {
redis.set("UUID:" + uuid.toString(), name);
return name;
}
}
}
final String name = Bukkit.getOfflinePlayer(uuid).getName();
if (name != null) {
return name;
}
return uuid.toString();
}
@Override
public @NonNull UUID resolveUUID(final @NonNull String name) {
if (this.messageService() instanceof RedisMessageService) {
final RedisMessageService redis = (RedisMessageService) this.messageService();
final String value = redis.get("Name:" + name);
if (value != null) {
return UUID.fromString(value);
} else {
final UUID uuid = Bukkit.getOfflinePlayer(name).getUniqueId();
redis.set("UUID:" + uuid.toString(), name);
return uuid;
}
}
return Bukkit.getOfflinePlayer(name).getUniqueId();
}
public @NonNull MessageManager messageManager() {
return this.messageManager;
}
@Override
public @NonNull UserService<BukkitPlayerUser> userService() {
return this.userService;
}
@@ -37,7 +37,7 @@ import java.util.UUID;
public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
private final @NonNull transient CarbonChatBukkit carbonChat;
private final transient @NonNull CarbonChatBukkit carbonChat;
private final @NonNull Map<@NonNull String, @NonNull SimpleUserChannelSettings> channelSettings = new HashMap<>();
private final @NonNull List<@NonNull UUID> ignoredUsers = new ArrayList<>();
private @NonNull UUID uuid;
@@ -179,14 +179,7 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
}
public @NonNull String name() {
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
final String name = player.getName();
if (name != null) {
return name;
}
return this.uuid.toString();
return this.carbonChat.resolveName(this.uuid);
}
@Override