mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 06:28:53 +00:00
Style changes & UUID cache
This commit is contained in:
1 parent
be2ee01f9f
commit
10fc409266
8 files changed
+80
-38
No files matched your search
@@ -18,6 +18,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface CarbonChat {
|
public interface CarbonChat {
|
||||||
|
|
||||||
@@ -31,6 +32,10 @@ public interface CarbonChat {
|
|||||||
|
|
||||||
void reloadConfig();
|
void reloadConfig();
|
||||||
|
|
||||||
|
@NonNull UUID resolveUUID(@NonNull String name);
|
||||||
|
|
||||||
|
@NonNull String resolveName(@NonNull UUID uuid);
|
||||||
|
|
||||||
@NonNull Logger logger();
|
@NonNull Logger logger();
|
||||||
|
|
||||||
@NonNull Path dataFolder();
|
@NonNull Path dataFolder();
|
||||||
|
|||||||
@@ -7,9 +7,6 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public interface UserService<T extends PlayerUser> {
|
public interface UserService<T extends PlayerUser> {
|
||||||
|
|
||||||
@Nullable
|
|
||||||
UUID resolve(String name);
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
T wrap(UUID uuid);
|
T wrap(UUID uuid);
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ import java.nio.file.Path;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@SuppressWarnings("initialization.fields.uninitialized")
|
@SuppressWarnings("initialization.fields.uninitialized")
|
||||||
@@ -158,21 +157,19 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
return users;
|
return users;
|
||||||
};
|
};
|
||||||
|
|
||||||
final Function<String, UUID> nameResolver = name -> Bukkit.getOfflinePlayer(name).getUniqueId();
|
|
||||||
|
|
||||||
if (storageType == StorageType.MYSQL) {
|
if (storageType == StorageType.MYSQL) {
|
||||||
this.logger().info("Enabling MySQL storage!");
|
this.logger().info("Enabling MySQL storage!");
|
||||||
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
|
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
|
||||||
this.userService = new MySQLUserService<>(this, credentials, supplier, BukkitPlayerUser::new,
|
this.userService = new MySQLUserService<>(this, credentials, supplier,
|
||||||
nameResolver, BukkitConsoleUser::new);
|
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||||
} else if (storageType == StorageType.JSON) {
|
} else if (storageType == StorageType.JSON) {
|
||||||
this.logger().info("Enabling JSON storage!");
|
this.logger().info("Enabling JSON storage!");
|
||||||
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, BukkitPlayerUser::new,
|
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
|
||||||
nameResolver, BukkitConsoleUser::new);
|
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||||
} else {
|
} else {
|
||||||
this.logger().error("Invalid storage type selected! Falling back to JSON.");
|
this.logger().error("Invalid storage type selected! Falling back to JSON.");
|
||||||
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, BukkitPlayerUser::new,
|
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
|
||||||
nameResolver, BukkitConsoleUser::new);
|
BukkitPlayerUser::new, BukkitConsoleUser::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup listeners
|
// Setup listeners
|
||||||
@@ -357,10 +354,60 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
new PAPIContext();
|
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() {
|
public @NonNull MessageManager messageManager() {
|
||||||
return this.messageManager;
|
return this.messageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public @NonNull UserService<BukkitPlayerUser> userService() {
|
public @NonNull UserService<BukkitPlayerUser> userService() {
|
||||||
return this.userService;
|
return this.userService;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-9
@@ -37,7 +37,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
|
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 Map<@NonNull String, @NonNull SimpleUserChannelSettings> channelSettings = new HashMap<>();
|
||||||
private final @NonNull List<@NonNull UUID> ignoredUsers = new ArrayList<>();
|
private final @NonNull List<@NonNull UUID> ignoredUsers = new ArrayList<>();
|
||||||
private @NonNull UUID uuid;
|
private @NonNull UUID uuid;
|
||||||
@@ -179,14 +179,7 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience.Single {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public @NonNull String name() {
|
public @NonNull String name() {
|
||||||
final OfflinePlayer player = Bukkit.getOfflinePlayer(this.uuid());
|
return this.carbonChat.resolveName(this.uuid);
|
||||||
final String name = player.getName();
|
|
||||||
|
|
||||||
if (name != null) {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.uuid.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,7 @@ public final class PlayerUserArgument<C> extends CommandArgument<C, PlayerUser>
|
|||||||
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
|
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
|
||||||
}
|
}
|
||||||
|
|
||||||
final UUID uuid = CarbonChatProvider.carbonChat().userService().resolve(input);
|
final UUID uuid = CarbonChatProvider.carbonChat().resolveUUID(input);
|
||||||
|
|
||||||
if (uuid == null) {
|
if (uuid == null) {
|
||||||
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
|
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
|
||||||
|
|||||||
+16
@@ -5,6 +5,8 @@ import com.google.common.io.ByteArrayDataOutput;
|
|||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import io.lettuce.core.RedisClient;
|
import io.lettuce.core.RedisClient;
|
||||||
import io.lettuce.core.RedisURI;
|
import io.lettuce.core.RedisURI;
|
||||||
|
import io.lettuce.core.api.StatefulRedisConnection;
|
||||||
|
import io.lettuce.core.api.sync.RedisStringCommands;
|
||||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
@@ -12,6 +14,7 @@ import net.draycia.carbon.api.messaging.MessageService;
|
|||||||
import net.draycia.carbon.api.config.RedisCredentials;
|
import net.draycia.carbon.api.config.RedisCredentials;
|
||||||
import net.draycia.carbon.api.users.PlayerUser;
|
import net.draycia.carbon.api.users.PlayerUser;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -30,6 +33,8 @@ public class RedisMessageService implements MessageService {
|
|||||||
|
|
||||||
private final @NonNull RedisPubSubCommands<@NonNull String, @NonNull String> publishSync;
|
private final @NonNull RedisPubSubCommands<@NonNull String, @NonNull String> publishSync;
|
||||||
|
|
||||||
|
private final @NonNull RedisStringCommands<@NonNull String, @NonNull String> getSetSync;
|
||||||
|
|
||||||
private final @NonNull UUID serverUUID = UUID.randomUUID();
|
private final @NonNull UUID serverUUID = UUID.randomUUID();
|
||||||
|
|
||||||
private final @NonNull CarbonChat carbonChat;
|
private final @NonNull CarbonChat carbonChat;
|
||||||
@@ -55,6 +60,9 @@ public class RedisMessageService implements MessageService {
|
|||||||
final StatefulRedisPubSubConnection<String, String> publishConnection = client.connectPubSub();
|
final StatefulRedisPubSubConnection<String, String> publishConnection = client.connectPubSub();
|
||||||
this.publishSync = publishConnection.sync();
|
this.publishSync = publishConnection.sync();
|
||||||
|
|
||||||
|
final StatefulRedisConnection<String, String> getSetConnection = client.connect();
|
||||||
|
this.getSetSync = getSetConnection.sync();
|
||||||
|
|
||||||
subscribeConnection.addListener((RedisListener) (channel, message) -> {
|
subscribeConnection.addListener((RedisListener) (channel, message) -> {
|
||||||
final ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(message));
|
final ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(message));
|
||||||
|
|
||||||
@@ -108,6 +116,14 @@ public class RedisMessageService implements MessageService {
|
|||||||
this.subscribeSync.unsubscribe(key);
|
this.subscribeSync.unsubscribe(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @Nullable String get(final @NonNull String key) {
|
||||||
|
return this.getSetSync.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(final @NonNull String key, final @NonNull String value) {
|
||||||
|
this.getSetSync.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(final @NonNull String key, final @NonNull UUID uuid, final @NonNull Consumer<@NonNull ByteArrayDataOutput> consumer) {
|
public void sendMessage(final @NonNull String key, final @NonNull UUID uuid, final @NonNull Consumer<@NonNull ByteArrayDataOutput> consumer) {
|
||||||
final ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
final ByteArrayDataOutput msg = ByteStreams.newDataOutput();
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
|
|||||||
private final @NonNull Type userType;
|
private final @NonNull Type userType;
|
||||||
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
|
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
|
||||||
private final @NonNull Function<UUID, T> userFactory;
|
private final @NonNull Function<UUID, T> userFactory;
|
||||||
private final @NonNull Function<String, UUID> nameResolver;
|
|
||||||
private final @NonNull Supplier<C> consoleFactory;
|
private final @NonNull Supplier<C> consoleFactory;
|
||||||
|
|
||||||
@SuppressWarnings("methodref.receiver.bound.invalid")
|
@SuppressWarnings("methodref.receiver.bound.invalid")
|
||||||
@@ -47,13 +46,11 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
|
|||||||
final @NonNull CarbonChat carbonChat,
|
final @NonNull CarbonChat carbonChat,
|
||||||
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
|
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
|
||||||
final @NonNull Function<UUID, T> userFactory,
|
final @NonNull Function<UUID, T> userFactory,
|
||||||
final @NonNull Function<String, UUID> nameResolver,
|
|
||||||
final @NonNull Supplier<C> consoleFactory) {
|
final @NonNull Supplier<C> consoleFactory) {
|
||||||
this.userType = userType;
|
this.userType = userType;
|
||||||
this.carbonChat = carbonChat;
|
this.carbonChat = carbonChat;
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
this.userFactory = userFactory;
|
this.userFactory = userFactory;
|
||||||
this.nameResolver = nameResolver;
|
|
||||||
this.consoleFactory = consoleFactory;
|
this.consoleFactory = consoleFactory;
|
||||||
|
|
||||||
final TimerTask timerTask = new TimerTask() {
|
final TimerTask timerTask = new TimerTask() {
|
||||||
@@ -66,11 +63,6 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
|
|||||||
new Timer().schedule(timerTask, 300000L, 300000L);
|
new Timer().schedule(timerTask, 300000L, 300000L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID resolve(final @NonNull String name) {
|
|
||||||
return this.nameResolver.apply(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
this.userCache.invalidateAll();
|
this.userCache.invalidateAll();
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
|
|||||||
private final @NonNull Database database;
|
private final @NonNull Database database;
|
||||||
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
|
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
|
||||||
private final @NonNull Function<UUID, T> userFactory;
|
private final @NonNull Function<UUID, T> userFactory;
|
||||||
private final @NonNull Function<String, UUID> nameResolver;
|
|
||||||
private final @NonNull Supplier<C> consoleFactory;
|
private final @NonNull Supplier<C> consoleFactory;
|
||||||
|
|
||||||
@SuppressWarnings("methodref.receiver.bound.invalid")
|
@SuppressWarnings("methodref.receiver.bound.invalid")
|
||||||
@@ -49,12 +48,10 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
|
|||||||
public MySQLUserService(final @NonNull CarbonChat carbonChat, final @NonNull SQLCredentials credentials,
|
public MySQLUserService(final @NonNull CarbonChat carbonChat, final @NonNull SQLCredentials credentials,
|
||||||
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
|
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
|
||||||
final @NonNull Function<UUID, T> userFactory,
|
final @NonNull Function<UUID, T> userFactory,
|
||||||
final @NonNull Function<String, UUID> nameResolver,
|
|
||||||
final @NonNull Supplier<C> consoleFactory) {
|
final @NonNull Supplier<C> consoleFactory) {
|
||||||
this.carbonChat = carbonChat;
|
this.carbonChat = carbonChat;
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
this.userFactory = userFactory;
|
this.userFactory = userFactory;
|
||||||
this.nameResolver = nameResolver;
|
|
||||||
this.consoleFactory = consoleFactory;
|
this.consoleFactory = consoleFactory;
|
||||||
|
|
||||||
final String username = credentials.username();
|
final String username = credentials.username();
|
||||||
@@ -99,11 +96,6 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
|
|||||||
new Timer().schedule(timerTask, 0L, 300000L);
|
new Timer().schedule(timerTask, 0L, 300000L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID resolve(final @NonNull String name) {
|
|
||||||
return this.nameResolver.apply(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
this.userCache.invalidateAll();
|
this.userCache.invalidateAll();
|
||||||
|
|||||||
Reference in new issue
Block a user