fix: ICV modifying offline inventories via API, close #275

feat/data-edit-commands
William 11 months ago
parent 0f1cc2d24f
commit 7b8fb92737
No known key found for this signature in database

@ -174,6 +174,7 @@ public class HuskSyncAPI {
public void editCurrentData(@NotNull User user, @NotNull ThrowingConsumer<DataSnapshot.Unpacked> editor) { public void editCurrentData(@NotNull User user, @NotNull ThrowingConsumer<DataSnapshot.Unpacked> editor) {
getCurrentData(user).thenAccept(optional -> optional.ifPresent(data -> { getCurrentData(user).thenAccept(optional -> optional.ifPresent(data -> {
editor.accept(data); editor.accept(data);
data.setId(UUID.randomUUID());
setCurrentData(user, data); setCurrentData(user, data);
})); }));
} }
@ -264,9 +265,9 @@ public class HuskSyncAPI {
* *
* @param user The user to save the data for * @param user The user to save the data for
* @param snapshot The snapshot to save * @param snapshot The snapshot to save
* @param callback A callback to run after the data has been saved (if the DataSaveEvent was not cancelled) * @param callback A callback to run after the data has been saved (if the DataSaveEvent was not canceled)
* @apiNote This will fire the {@link net.william278.husksync.event.DataSaveEvent} event, unless * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will be fired unless the
* the save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN} * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false}
* @since 3.3.2 * @since 3.3.2
*/ */
public void addSnapshot(@NotNull User user, @NotNull DataSnapshot snapshot, 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 user The user to save the data for
* @param snapshot The snapshot to save * @param snapshot The snapshot to save
* @apiNote This will fire the {@link net.william278.husksync.event.DataSaveEvent} event, unless * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will be fired unless the
* * the save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN} * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false}
* @since 3.0 * @since 3.0
*/ */
public void addSnapshot(@NotNull User user, @NotNull DataSnapshot snapshot) { public void addSnapshot(@NotNull User user, @NotNull DataSnapshot snapshot) {

@ -161,6 +161,16 @@ public class DataSnapshot {
return id; return id;
} }
/**
* <b>Internal use only</b> 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 * Get the short display ID of the snapshot
* *
@ -826,7 +836,7 @@ public class DataSnapshot {
* *
* @since 2.0 * @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 * Indicates data was saved by editing inventory contents via the {@code /inventory} command
@ -861,25 +871,27 @@ public class DataSnapshot {
* *
* @since 2.0 * @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) * Indicates data was saved from being imported from a legacy version (v1.x -> v2.x)
* *
* @since 2.0 * @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) * Indicates data was saved from being imported from a legacy version (v2.x -> v3.x)
* *
* @since 3.0 * @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 @NotNull
private final String name; private final String name;
private final boolean fireDataSaveEvent;
/** /**
* Get or create a {@link SaveCause} from a name * Get or create a {@link SaveCause} from a name
* *
@ -888,13 +900,24 @@ public class DataSnapshot {
*/ */
@NotNull @NotNull
public static SaveCause of(@NotNull String name) { 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 @NotNull
public String getLocale(@NotNull HuskSync plugin) { public String getLocale(@NotNull HuskSync plugin) {
return plugin.getLocales() return plugin.getLocales()
.getRawLocale("save_cause_" + name().toLowerCase(Locale.ENGLISH)) .getRawLocale("save_cause_%s".formatted(name().toLowerCase(Locale.ENGLISH)))
.orElse(getDisplayName()); .orElse(getDisplayName());
} }

@ -101,16 +101,16 @@ public abstract class DataSyncer {
* @param user the user to save the data for * @param user the user to save the data for
* @param data the data to save * @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). * @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. * Note that this method can also edit the data before saving it.
* @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will <b>not</b> be fired if the * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will <b>not</b> be fired if
* save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN}. * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} (e.g., with the SERVER_SHUTDOWN cause).
* @since 3.3.2 * @since 3.3.2
*/ */
@Blocking @Blocking
public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data, public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data,
@Nullable BiConsumer<User, DataSnapshot.Packed> after) { @Nullable BiConsumer<User, DataSnapshot.Packed> after) {
if (data.getSaveCause() == DataSnapshot.SaveCause.SERVER_SHUTDOWN) { if (!data.getSaveCause().fireDataSaveEvent()) {
addSnapshotToDatabase(user, data, after); addSnapshotToDatabase(user, data, after);
return; return;
} }
@ -126,10 +126,10 @@ public abstract class DataSyncer {
* *
* @param user the user to save the data for * @param user the user to save the data for
* @param data the data to save * @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. * Note that this method can also edit the data before saving it.
* @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will <b>not</b> be fired if the * @implNote Note that the {@link net.william278.husksync.event.DataSaveEvent} will <b>not</b> be fired if
* save cause is {@link DataSnapshot.SaveCause#SERVER_SHUTDOWN}. * {@link DataSnapshot.SaveCause#fireDataSaveEvent()} is {@code false} (e.g., with the SERVER_SHUTDOWN cause).
* @since 3.3.3 * @since 3.3.3
*/ */
public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data) { public void saveData(@NotNull User user, @NotNull DataSnapshot.Packed data) {

Loading…
Cancel
Save