9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-28 11:09:11 +00:00

Implement compression via DataAdapter, add option to disable compression, better exception handling

This commit is contained in:
William
2022-07-04 23:44:45 +01:00
parent f2d4bec138
commit 1829526aa7
23 changed files with 227 additions and 83 deletions

View File

@@ -12,6 +12,9 @@ import net.william278.husksync.command.HuskSyncCommand;
import net.william278.husksync.command.Permission;
import net.william278.husksync.config.Locales;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.CompressedDataAdapter;
import net.william278.husksync.data.DataAdapter;
import net.william278.husksync.data.JsonDataAdapter;
import net.william278.husksync.database.Database;
import net.william278.husksync.database.MySqlDatabase;
import net.william278.husksync.listener.BukkitEventListener;
@@ -46,6 +49,8 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
private EventListener eventListener;
private DataAdapter dataAdapter;
private Settings settings;
private Locales locales;
@@ -91,9 +96,18 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
}
return loadedSettings;
}).join();
}).thenApply(succeeded -> {
if (succeeded) {
if (settings.getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_COMPRESS_DATA)) {
dataAdapter = new CompressedDataAdapter();
} else {
dataAdapter = new JsonDataAdapter();
}
}
return succeeded;
}).thenApply(succeeded -> {
// Establish connection to the database
this.database = new MySqlDatabase(settings, resourceReader, logger);
this.database = new MySqlDatabase(settings, resourceReader, logger, dataAdapter);
if (succeeded) {
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the database...");
final CompletableFuture<Boolean> databaseConnectFuture = new CompletableFuture<>();
@@ -101,7 +115,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
final boolean initialized = this.database.initialize();
if (!initialized) {
getLoggingAdapter().log(Level.SEVERE, "Failed to establish a connection to the database. "
+ "Please check the supplied database credentials in the config file");
+ "Please check the supplied database credentials in the config file");
databaseConnectFuture.completeAsync(() -> false);
return;
}
@@ -113,13 +127,13 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return false;
}).thenApply(succeeded -> {
// Establish connection to the Redis server
this.redisManager = new RedisManager(settings);
this.redisManager = new RedisManager(settings, dataAdapter);
if (succeeded) {
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server...");
return this.redisManager.initialize().thenApply(initialized -> {
if (!initialized) {
getLoggingAdapter().log(Level.SEVERE, "Failed to establish a connection to the Redis server. "
+ "Please check the supplied Redis credentials in the config file");
+ "Please check the supplied Redis credentials in the config file");
return false;
}
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server");
@@ -167,7 +181,7 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
// Handle failed initialization
if (!succeeded) {
getLoggingAdapter().log(Level.SEVERE, "Failed to initialize HuskSync. " +
"The plugin will now be disabled");
"The plugin will now be disabled");
getServer().getPluginManager().disablePlugin(this);
} else {
getLoggingAdapter().log(Level.INFO, "Successfully enabled HuskSync v" + getVersion());
@@ -207,6 +221,11 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
return redisManager;
}
@Override
public @NotNull DataAdapter getDataAdapter() {
return dataAdapter;
}
@Override
public @NotNull Settings getSettings() {
return settings;

View File

@@ -43,7 +43,7 @@ public class BukkitSerializer {
// Return encoded data, using the encoder from SnakeYaml to get a ByteArray conversion
return Base64Coder.encodeLines(byteOutputStream.toByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to serialize item stack data");
throw new DataDeserializationException("Failed to serialize item stack data", e);
}
});
}
@@ -78,7 +78,7 @@ public class BukkitSerializer {
return inventoryContents;
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Failed to deserialize item stack data");
throw new DataDeserializationException("Failed to deserialize item stack data", e);
}
});
}
@@ -132,7 +132,7 @@ public class BukkitSerializer {
// Return encoded data, using the encoder from SnakeYaml to get a ByteArray conversion
return Base64Coder.encodeLines(byteOutputStream.toByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to serialize potion effect data");
throw new DataDeserializationException("Failed to serialize potion effect data", e);
}
});
}
@@ -167,7 +167,7 @@ public class BukkitSerializer {
return potionEffects;
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Failed to deserialize potion effects", e);
throw new DataDeserializationException("Failed to deserialize potion effects", e);
}
});
}