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

Improve error handling on data sync

This commit is contained in:
William
2023-09-22 22:07:31 +01:00
parent b63e1bd283
commit 55e443cd49
3 changed files with 37 additions and 19 deletions

View File

@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
* A holder of data in the form of {@link Data}s, which can be synced
@@ -83,22 +84,40 @@ public interface UserDataHolder extends DataHolder {
* The {@code runAfter} callback function will be run after the snapshot has been applied.
*
* @param snapshot the snapshot to apply
* @param runAfter the function to run asynchronously after the snapshot has been applied
* @param runAfter a consumer accepting a boolean value, indicating if the data was successfully applied,
* which will be run after the snapshot has been applied
* @since 3.0
*/
default void applySnapshot(@NotNull DataSnapshot.Packed snapshot, @NotNull ThrowingConsumer<UserDataHolder> runAfter) {
default void applySnapshot(@NotNull DataSnapshot.Packed snapshot, @NotNull ThrowingConsumer<Boolean> runAfter) {
final HuskSync plugin = getPlugin();
final DataSnapshot.Unpacked unpacked = snapshot.unpack(plugin);
// Unpack the snapshot
final DataSnapshot.Unpacked unpacked;
try {
unpacked = snapshot.unpack(plugin);
} catch (Throwable e) {
plugin.log(Level.SEVERE, String.format("Failed to unpack data snapshot for %s", getUsername()), e);
return;
}
// Synchronously attempt to apply the snapshot
plugin.runSync(() -> {
unpacked.getData().forEach((type, data) -> {
if (plugin.getSettings().isSyncFeatureEnabled(type)) {
if (type.isCustom()) {
getCustomDataStore().put(type, data);
try {
for (Map.Entry<Identifier, Data> entry : unpacked.getData().entrySet()) {
final Identifier identifier = entry.getKey();
if (plugin.getSettings().isSyncFeatureEnabled(identifier)) {
if (identifier.isCustom()) {
getCustomDataStore().put(identifier, entry.getValue());
}
entry.getValue().apply(this, plugin);
}
data.apply(this, plugin);
}
});
plugin.runAsync(() -> runAfter.accept(this));
} catch (Throwable e) {
plugin.log(Level.SEVERE, String.format("Failed to apply data snapshot to %s", getUsername()), e);
plugin.runAsync(() -> runAfter.accept(false));
return;
}
plugin.runAsync(() -> runAfter.accept(true));
});
}
@@ -157,6 +176,9 @@ public interface UserDataHolder extends DataHolder {
this.setData(Identifier.PERSISTENT_DATA, persistentData);
}
@NotNull
String getUsername();
@NotNull
Map<Identifier, Data> getCustomDataStore();

View File

@@ -125,12 +125,14 @@ public abstract class OnlineUser extends User implements CommandUser, UserDataHo
* Set a player's status from a {@link DataSnapshot}
*
* @param snapshot The {@link DataSnapshot} to set the player's status from
* @param cause The {@link DataSnapshot.UpdateCause} of the snapshot
* @since 3.0
*/
public void applySnapshot(@NotNull DataSnapshot.Packed snapshot, @NotNull DataSnapshot.UpdateCause cause) {
getPlugin().fireEvent(getPlugin().getPreSyncEvent(this, snapshot), (event) -> {
if (!isOffline()) {
UserDataHolder.super.applySnapshot(
event.getData(), (owner) -> completeSync(true, cause, getPlugin())
event.getData(), (succeeded) -> completeSync(succeeded, cause, getPlugin())
);
}
});