From 55da3e4092845eb29c76b7fe4ca3fb93e1bbbb5a Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sun, 11 Aug 2024 09:27:38 -0700 Subject: [PATCH] Return prior value from set --- .../java/net/countercraft/movecraft/craft/BaseCraft.java | 4 ++-- .../java/net/countercraft/movecraft/craft/Craft.java | 2 +- .../movecraft/craft/datatag/CraftDataTagContainer.java | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java index 3a687e8f1..9fc542f89 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/craft/BaseCraft.java @@ -551,8 +551,8 @@ public void setAudience(@NotNull Audience audience) { } @Override - public void setDataTag(final @NotNull CraftDataTagKey tagKey, final T data) { - dataTagContainer.set(tagKey, data); + public T setDataTag(final @NotNull CraftDataTagKey tagKey, final T data) { + return dataTagContainer.set(tagKey, data); } @Override diff --git a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java index c7db251dc..deaad0c72 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/Craft.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/Craft.java @@ -274,7 +274,7 @@ default void setLastDZ(int dZ){} void setAudience(Audience audience); - void setDataTag(@NotNull final CraftDataTagKey tagKey, final T data); + T setDataTag(@NotNull final CraftDataTagKey tagKey, final T data); T getDataTag(@NotNull final CraftDataTagKey tagKey); diff --git a/api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagContainer.java b/api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagContainer.java index 32f934827..4eee17a55 100644 --- a/api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagContainer.java +++ b/api/src/main/java/net/countercraft/movecraft/craft/datatag/CraftDataTagContainer.java @@ -47,12 +47,17 @@ public T get(final @NotNull Craft craft, final @NotNull CraftDataTagKey t * @param value the value to set for future lookups * @param the type of the value */ - public void set(final @NotNull CraftDataTagKey tagKey, final @NotNull T value) { + public @Nullable T set(final @NotNull CraftDataTagKey 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 compute(final @NotNull Craft craft, final @NotNull CraftDataTagKey tagKey, final @NotNull Function computation){