From d09d8cda2ca44e93b8c9452da9483a07841ede89 Mon Sep 17 00:00:00 2001 From: Steven Downer Date: Sun, 26 Jul 2020 15:20:41 -0500 Subject: [PATCH] Resolve issue with registration references Signed-off-by: Steven Downer --- .../provider/DataProviderRegistrator.java | 52 ++++++++----------- .../data/provider/inventory/SlotData.java | 2 +- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/spongepowered/common/data/provider/DataProviderRegistrator.java b/src/main/java/org/spongepowered/common/data/provider/DataProviderRegistrator.java index 76cc19efeca..6d7b136346d 100644 --- a/src/main/java/org/spongepowered/common/data/provider/DataProviderRegistrator.java +++ b/src/main/java/org/spongepowered/common/data/provider/DataProviderRegistrator.java @@ -43,7 +43,7 @@ public class DataProviderRegistrator { protected final DataProviderRegistratorBuilder builder; - public DataProviderRegistrator(DataProviderRegistratorBuilder builder) { + public DataProviderRegistrator(final DataProviderRegistratorBuilder builder) { this.builder = builder; } @@ -63,26 +63,6 @@ public ImmutableRegistrator asImmutable(final Class target) { return new ImmutableRegistrator<>(this.builder, target); } - @SuppressWarnings({"unchecked", "UnstableApiUsage"}) - protected static Function> toOptionalFunc(final Key> key, final Function getter) { - // Optimize boolean optionals - if (key.getElementToken().getRawType() == Boolean.class) { - return h -> (Optional) OptBool.of((Boolean) getter.apply(h)); - } else { - return h -> Optional.ofNullable(getter.apply(h)); - } - } - - @SuppressWarnings({"unchecked", "UnstableApiUsage"}) - protected static BiFunction> toOptionalBiFunc(final Key> key, final BiFunction biFunc) { - // Optimize boolean optionals - if (key.getElementToken().getRawType() == Boolean.class) { - return (h, v) -> (Optional) OptBool.of((Boolean) biFunc.apply(h, v)); - } else { - return (h, v) -> Optional.ofNullable(biFunc.apply(h, v)); - } - } - public static final class MutableRegistrator extends DataProviderRegistrator { private final Class target; @@ -114,10 +94,11 @@ public MutableRegistration create(final Key> key) { return registration; } + @SuppressWarnings({"unchecked", "UnstableApiUsage"}) protected MutableRegistrator register(final MutableRegistration registration) { - final Function> optionalGetter = toOptionalFunc(registration.key, registration.get); this.builder.register( new GenericMutableDataProvider(registration.key, this.target) { + final boolean isBooleanKey = registration.key.getElementToken().getRawType() == Boolean.class; @Override protected Value constructValue(final T dataHolder, final K element) { @@ -129,7 +110,10 @@ protected Value constructValue(final T dataHolder, final K element) { @Override protected Optional getFrom(final T dataHolder) { - return optionalGetter.apply(dataHolder); + if (this.isBooleanKey) { + return (Optional) OptBool.of((Boolean) registration.get.apply(dataHolder)); + } + return Optional.ofNullable(registration.get.apply(dataHolder)); } @Override @@ -213,11 +197,11 @@ public ImmutableRegistration create(final Key> key) return registration; } + @SuppressWarnings({"unchecked", "UnstableApiUsage"}) protected ImmutableRegistrator register(final ImmutableRegistration registration) { - final Function> optionalGetter = toOptionalFunc(registration.key, registration.get); - final BiFunction> optionalSetter = toOptionalBiFunc(registration.key, registration.set); this.builder.register( new GenericImmutableDataProvider(registration.key) { + final boolean isBooleanKey = registration.key.getElementToken().getRawType() == Boolean.class; @Override protected Value constructValue(final T dataHolder, final K element) { @@ -229,12 +213,18 @@ protected Value constructValue(final T dataHolder, final K element) { @Override protected Optional getFrom(final T dataHolder) { - return optionalGetter.apply(dataHolder); + if (this.isBooleanKey) { + return (Optional) OptBool.of((Boolean) registration.get.apply(dataHolder)); + } + return Optional.ofNullable(registration.get.apply(dataHolder)); } @Override protected Optional set(final T dataHolder, final K value) { - return optionalSetter.apply(dataHolder, value); + if (this.isBooleanKey) { + return (Optional) OptBool.of((Boolean) registration.set.apply(dataHolder, value)); + } + return Optional.ofNullable(registration.set.apply(dataHolder, value)); } @Override @@ -330,7 +320,9 @@ public MutableRegistration create(final Supplier MutableRegistration create(final Key> key) { - return new MutableRegistration<>(this.registrator, key); + final MutableRegistration registration = new MutableRegistration<>(this.registrator, key); + this.registrator.register(registration); + return registration; } /** @@ -400,7 +392,9 @@ public ImmutableRegistration create(final Supplier ImmutableRegistration create(final Key> key) { - return new ImmutableRegistration<>(this.registrator, key); + final ImmutableRegistration registration = new ImmutableRegistration<>(this.registrator, key); + this.registrator.register(registration); + return registration; } /** diff --git a/src/main/java/org/spongepowered/common/data/provider/inventory/SlotData.java b/src/main/java/org/spongepowered/common/data/provider/inventory/SlotData.java index 65867ccb627..a8fa07a91bf 100644 --- a/src/main/java/org/spongepowered/common/data/provider/inventory/SlotData.java +++ b/src/main/java/org/spongepowered/common/data/provider/inventory/SlotData.java @@ -64,6 +64,6 @@ private static D getter(final Slot holder, final Supplier>> sup final Lens parentLens = ((InventoryBridge) holder.parent()).bridge$getAdapter().inventoryAdapter$getRootLens(); final Lens childLens = ((InventoryBridge) holder).bridge$getAdapter().inventoryAdapter$getRootLens(); final Map, Object> dataMap = parentLens.getDataFor(childLens); - return (D) dataMap.get(suppliedKey.get().getKey()); + return (D) dataMap.get(suppliedKey.get()); } }