|
|
|
@ -19,7 +19,7 @@ import java.util.concurrent.CompletableFuture;
|
|
|
|
|
/**
|
|
|
|
|
* Manages the connection to the Redis server, handling the caching of user data
|
|
|
|
|
*/
|
|
|
|
|
public class RedisManager {
|
|
|
|
|
public class RedisManager extends JedisPubSub {
|
|
|
|
|
|
|
|
|
|
protected static final String KEY_NAMESPACE = "husksync:";
|
|
|
|
|
protected static String clusterId = "";
|
|
|
|
@ -53,21 +53,19 @@ public class RedisManager {
|
|
|
|
|
*
|
|
|
|
|
* @return a future returning void when complete
|
|
|
|
|
*/
|
|
|
|
|
public CompletableFuture<Boolean> initialize() {
|
|
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
if (redisPassword.isBlank()) {
|
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisUseSsl);
|
|
|
|
|
} else {
|
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisPassword, redisUseSsl);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
jedisPool.getResource().ping();
|
|
|
|
|
} catch (JedisException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
CompletableFuture.runAsync(this::subscribe);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
public boolean initialize() {
|
|
|
|
|
if (redisPassword.isBlank()) {
|
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisUseSsl);
|
|
|
|
|
} else {
|
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisPassword, redisUseSsl);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
jedisPool.getResource().ping();
|
|
|
|
|
} catch (JedisException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
CompletableFuture.runAsync(this::subscribe);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void subscribe() {
|
|
|
|
@ -75,38 +73,41 @@ public class RedisManager {
|
|
|
|
|
new Jedis(redisHost, redisPort, DefaultJedisClientConfig.builder()
|
|
|
|
|
.password(redisPassword).timeoutMillis(0).ssl(redisUseSsl).build())) {
|
|
|
|
|
subscriber.connect();
|
|
|
|
|
subscriber.subscribe(new JedisPubSub() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
|
RedisMessageType.getTypeFromChannel(channel).ifPresent(messageType -> {
|
|
|
|
|
if (messageType == RedisMessageType.UPDATE_USER_DATA) {
|
|
|
|
|
final RedisMessage redisMessage = RedisMessage.fromJson(message);
|
|
|
|
|
plugin.getOnlineUser(redisMessage.targetUserUuid).ifPresent(user -> {
|
|
|
|
|
final UserData userData = plugin.getDataAdapter().fromBytes(redisMessage.data);
|
|
|
|
|
user.setData(userData, plugin.getSettings(), plugin.getEventCannon(),
|
|
|
|
|
plugin.getLoggingAdapter(), plugin.getMinecraftVersion()).thenAccept(succeeded -> {
|
|
|
|
|
if (succeeded) {
|
|
|
|
|
switch (plugin.getSettings().notificationDisplaySlot) {
|
|
|
|
|
case CHAT -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(user::sendMessage);
|
|
|
|
|
case ACTION_BAR -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(user::sendActionBar);
|
|
|
|
|
case TOAST -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(locale -> user.sendToast(locale, new MineDown(""),
|
|
|
|
|
"minecraft:bell", "TASK"));
|
|
|
|
|
}
|
|
|
|
|
plugin.getEventCannon().fireSyncCompleteEvent(user);
|
|
|
|
|
} else {
|
|
|
|
|
plugin.getLocales().getLocale("data_update_failed")
|
|
|
|
|
.ifPresent(user::sendMessage);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, Arrays.stream(RedisMessageType.values()).map(RedisMessageType::getMessageChannel).toArray(String[]::new));
|
|
|
|
|
subscriber.subscribe(this, Arrays.stream(RedisMessageType.values())
|
|
|
|
|
.map(RedisMessageType::getMessageChannel)
|
|
|
|
|
.toArray(String[]::new));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
|
final RedisMessageType messageType = RedisMessageType.getTypeFromChannel(channel).orElse(null);
|
|
|
|
|
if (messageType != RedisMessageType.UPDATE_USER_DATA) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final RedisMessage redisMessage = RedisMessage.fromJson(message);
|
|
|
|
|
plugin.getOnlineUser(redisMessage.targetUserUuid).ifPresent(user -> {
|
|
|
|
|
final UserData userData = plugin.getDataAdapter().fromBytes(redisMessage.data);
|
|
|
|
|
user.setData(userData, plugin.getSettings(), plugin.getEventCannon(),
|
|
|
|
|
plugin.getLoggingAdapter(), plugin.getMinecraftVersion()).thenAccept(succeeded -> {
|
|
|
|
|
if (succeeded) {
|
|
|
|
|
switch (plugin.getSettings().notificationDisplaySlot) {
|
|
|
|
|
case CHAT -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(user::sendMessage);
|
|
|
|
|
case ACTION_BAR -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(user::sendActionBar);
|
|
|
|
|
case TOAST -> plugin.getLocales().getLocale("data_update_complete")
|
|
|
|
|
.ifPresent(locale -> user.sendToast(locale, new MineDown(""),
|
|
|
|
|
"minecraft:bell", "TASK"));
|
|
|
|
|
}
|
|
|
|
|
plugin.getEventCannon().fireSyncCompleteEvent(user);
|
|
|
|
|
} else {
|
|
|
|
|
plugin.getLocales().getLocale("data_update_failed")
|
|
|
|
|
.ifPresent(user::sendMessage);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void sendMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
|