Minor refactoring / code improvements

feat/data-edit-commands
William 2 years ago
parent ded89ad343
commit 2017ecc20f

@ -14,6 +14,7 @@ dependencies {
compileOnly 'de.themoep:minedown-adventure:1.7.1-SNAPSHOT' compileOnly 'de.themoep:minedown-adventure:1.7.1-SNAPSHOT'
compileOnly 'dev.dejvokep:boosted-yaml:1.3' compileOnly 'dev.dejvokep:boosted-yaml:1.3'
compileOnly 'com.zaxxer:HikariCP:5.0.1' compileOnly 'com.zaxxer:HikariCP:5.0.1'
compileOnly 'redis.clients:jedis:' + jedis_version
compileOnly 'net.william278:DesertWell:1.1' compileOnly 'net.william278:DesertWell:1.1'
compileOnly 'net.william278:Annotaml:2.0' compileOnly 'net.william278:Annotaml:2.0'
compileOnly 'net.william278:AdvancementAPI:97a9583413' compileOnly 'net.william278:AdvancementAPI:97a9583413'

@ -134,7 +134,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
// Prepare redis connection // Prepare redis connection
this.redisManager = new RedisManager(this); this.redisManager = new RedisManager(this);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server..."); getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server...");
initialized.set(this.redisManager.initialize().join()); initialized.set(this.redisManager.initialize());
if (initialized.get()) { if (initialized.get()) {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server"); getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server");
} else { } else {

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

Loading…
Cancel
Save