Skip to content

Commit

Permalink
Return prior value from set
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-noey committed Aug 11, 2024
1 parent 202e5df commit 55da3e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ public void setAudience(@NotNull Audience audience) {
}

@Override
public <T> void setDataTag(final @NotNull CraftDataTagKey<T> tagKey, final T data) {
dataTagContainer.set(tagKey, data);
public <T> T setDataTag(final @NotNull CraftDataTagKey<T> tagKey, final T data) {
return dataTagContainer.set(tagKey, data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ default void setLastDZ(int dZ){}

void setAudience(Audience audience);

<T> void setDataTag(@NotNull final CraftDataTagKey<T> tagKey, final T data);
<T> T setDataTag(@NotNull final CraftDataTagKey<T> tagKey, final T data);

<T> T getDataTag(@NotNull final CraftDataTagKey<T> tagKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ public <T> T get(final @NotNull Craft craft, final @NotNull CraftDataTagKey<T> t
* @param value the value to set for future lookups
* @param <T> the type of the value
*/
public <T> void set(final @NotNull CraftDataTagKey<T> tagKey, final @NotNull T value) {
public <T> @Nullable T set(final @NotNull CraftDataTagKey<T> tagKey, final @NotNull T value) {
if (!CraftDataTagRegistry.INSTANCE.isRegistered(tagKey.key)) {
throw new IllegalArgumentException(String.format("The provided key %s was not registered.", tagKey));
}
try {
//noinspection unchecked
return (T) backing.put(tagKey, value);
} catch (ClassCastException cce) {
throw new IllegalStateException(String.format("The provided key %s has an invalid value type.", tagKey), cce);
}

backing.put(tagKey, value);
}

public <T> T compute(final @NotNull Craft craft, final @NotNull CraftDataTagKey<T> tagKey, final @NotNull Function<? super T, ? extends T> computation){
Expand Down

0 comments on commit 55da3e4

Please sign in to comment.