diff --git a/common/src/main/java/net/william278/husksync/api/HuskSyncAPI.java b/common/src/main/java/net/william278/husksync/api/HuskSyncAPI.java index 5c7a17a4..17af5ab6 100644 --- a/common/src/main/java/net/william278/husksync/api/HuskSyncAPI.java +++ b/common/src/main/java/net/william278/husksync/api/HuskSyncAPI.java @@ -174,6 +174,7 @@ public class HuskSyncAPI { public void editCurrentData(@NotNull User user, @NotNull ThrowingConsumer editor) { getCurrentData(user).thenAccept(optional -> optional.ifPresent(data -> { editor.accept(data); + data.setId(UUID.randomUUID()); setCurrentData(user, data); })); } @@ -264,9 +265,9 @@ public class HuskSyncAPI { * * @param user The user to save the data for * @param snapshot The snapshot to save - * @param callback A callback to run after the data has been saved (if the DataSaveEvent was not cancelled) - * @apiNote This will fire the {@link net.william278.husksync.event.DataSaveEvent} event, unless - * the save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN} + * @param callback A callback to run after the data has been saved (if the DataSaveEvent was not canceled) + * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will be fired unless the + * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} * @since 3.3.2 */ public void addSnapshot(@NotNull User user, @NotNull DataSnapshot snapshot, @@ -284,8 +285,8 @@ public class HuskSyncAPI { * * @param user The user to save the data for * @param snapshot The snapshot to save - * @apiNote This will fire the {@link net.william278.husksync.event.DataSaveEvent} event, unless - * * the save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN} + * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will be fired unless the + * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} * @since 3.0 */ public void addSnapshot(@NotNull User user, @NotNull DataSnapshot snapshot) { diff --git a/common/src/main/java/net/william278/husksync/data/DataSnapshot.java b/common/src/main/java/net/william278/husksync/data/DataSnapshot.java index 97bd93ce..ba7a6591 100644 --- a/common/src/main/java/net/william278/husksync/data/DataSnapshot.java +++ b/common/src/main/java/net/william278/husksync/data/DataSnapshot.java @@ -161,6 +161,16 @@ public class DataSnapshot { return id; } + /** + * Internal use only Set the ID of the snapshot + * @param id The snapshot ID + * @since 3.0 + */ + @ApiStatus.Internal + public void setId(@NotNull UUID id) { + this.id = id; + } + /** * Get the short display ID of the snapshot * @@ -826,7 +836,7 @@ public class DataSnapshot { * * @since 2.0 */ - public static final SaveCause SERVER_SHUTDOWN = of("SERVER_SHUTDOWN"); + public static final SaveCause SERVER_SHUTDOWN = of("SERVER_SHUTDOWN", false); /** * Indicates data was saved by editing inventory contents via the {@code /inventory} command @@ -861,25 +871,27 @@ public class DataSnapshot { * * @since 2.0 */ - public static final SaveCause MPDB_MIGRATION = of("MPDB_MIGRATION"); + public static final SaveCause MPDB_MIGRATION = of("MPDB_MIGRATION", false); /** * Indicates data was saved from being imported from a legacy version (v1.x -> v2.x) * * @since 2.0 */ - public static final SaveCause LEGACY_MIGRATION = of("LEGACY_MIGRATION"); + public static final SaveCause LEGACY_MIGRATION = of("LEGACY_MIGRATION", false); /** * Indicates data was saved from being imported from a legacy version (v2.x -> v3.x) * * @since 3.0 */ - public static final SaveCause CONVERTED_FROM_V2 = of("CONVERTED_FROM_V2"); + public static final SaveCause CONVERTED_FROM_V2 = of("CONVERTED_FROM_V2", false); @NotNull private final String name; + private final boolean fireDataSaveEvent; + /** * Get or create a {@link SaveCause} from a name * @@ -888,13 +900,24 @@ public class DataSnapshot { */ @NotNull public static SaveCause of(@NotNull String name) { - return new SaveCause(name.length() > 32 ? name.substring(0, 31) : name); + return new SaveCause(name.length() > 32 ? name.substring(0, 31) : name, true); + } + + /** + * Get or create a {@link SaveCause} from a name and whether it should fire a save event + * @param name the name to be displayed + * @param firesSaveEvent whether the cause should fire a save event + * @return the cause + */ + @NotNull + public static SaveCause of(@NotNull String name, boolean firesSaveEvent) { + return new SaveCause(name.length() > 32 ? name.substring(0, 31) : name, firesSaveEvent); } @NotNull public String getLocale(@NotNull HuskSync plugin) { return plugin.getLocales() - .getRawLocale("save_cause_" + name().toLowerCase(Locale.ENGLISH)) + .getRawLocale("save_cause_%s".formatted(name().toLowerCase(Locale.ENGLISH))) .orElse(getDisplayName()); } diff --git a/common/src/main/java/net/william278/husksync/sync/DataSyncer.java b/common/src/main/java/net/william278/husksync/sync/DataSyncer.java index f7f4b1c9..7be66a6b 100644 --- a/common/src/main/java/net/william278/husksync/sync/DataSyncer.java +++ b/common/src/main/java/net/william278/husksync/sync/DataSyncer.java @@ -101,16 +101,16 @@ public abstract class DataSyncer { * @param user the user to save the data for * @param data the data to save * @param after a consumer to run after data has been saved. Will be run async (off the main thread). - * @apiNote Data will not be saved if the {@link net.william278.husksync.event.DataSaveEvent} is cancelled. + * @apiNote Data will not be saved if the {@link net.william278.husksync.event.DataSaveEvent} is canceled. * Note that this method can also edit the data before saving it. - * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will not be fired if the - * save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN}. + * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will not be fired if + * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} (e.g., with the SERVER_SHUTDOWN cause). * @since 3.3.2 */ @Blocking public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data, @Nullable BiConsumer after) { - if (data.getSaveCause() == DataSnapshot.SaveCause.SERVER_SHUTDOWN) { + if (!data.getSaveCause().fireDataSaveEvent()) { addSnapshotToDatabase(user, data, after); return; } @@ -126,10 +126,10 @@ public abstract class DataSyncer { * * @param user the user to save the data for * @param data the data to save - * @apiNote Data will not be saved if the {@link net.william278.husksync.event.DataSaveEvent} is cancelled. + * @apiNote Data will not be saved if the {@link net.william278.husksync.event.DataSaveEvent} is canceled. * Note that this method can also edit the data before saving it. - * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will not be fired if the - * save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN}. + * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will not be fired if + * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} (e.g., with the SERVER_SHUTDOWN cause). * @since 3.3.3 */ public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data) {