Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Dec 7, 2023
1 parent d6b6988 commit 19ef538
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.common.data.key.SpongeKey;
import org.spongepowered.common.data.provider.GenericMutableDataProvider;
import org.spongepowered.common.util.DataUtil;

import java.util.Collection;
Expand Down Expand Up @@ -89,6 +90,9 @@ default <E> DataTransactionResult offerSingle(Key<? extends CollectionValue<E, ?
final SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>> key0 =
(SpongeKey<? extends CollectionValue<E, Collection<E>>, Collection<E>>) key;
return this.impl$applyTransaction(key0, (p, m) -> {
if (p instanceof GenericMutableDataProvider) {
return ((GenericMutableDataProvider)p).offerSingle(m, element);
}
final Collection<E> collection = p.get(m)
.map(DataUtil::ensureMutable)
.orElseGet(key0.getDefaultValueSupplier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.spongepowered.api.data.persistence.DataContentUpdater;
import org.spongepowered.api.data.persistence.DataStore;
import org.spongepowered.api.data.persistence.DataView;
import org.spongepowered.api.data.value.CollectionValue;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.registry.DefaultedRegistryReference;
import org.spongepowered.api.util.OptBool;
Expand All @@ -54,6 +55,7 @@

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -205,6 +207,13 @@ protected <K, V extends Value<K>> MutableRegistrator<T> register(final MutableRe
this.registrationBuilder.dataKey(provider.key()).provider(provider);
return this;
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
protected <K, V extends Value<K>, L> MutableRegistrator<T> register(final MutableCollectionRegistration<T, K, L> registration) {
final DataProvider<?, ?> provider = registration.build(this.target);
this.registrationBuilder.dataKey(provider.key()).provider(provider);
return this;
}
}

public static final class ImmutableRegistrator<T> extends DataProviderRegistrator {
Expand Down Expand Up @@ -400,6 +409,15 @@ protected boolean supports(final H dataHolder) {
}
return super.supports(dataHolder);
}

@Override
public <TE> DataTransactionResult offerSingle(final DataHolder.Mutable dataHolder, final TE element) {
if (registration instanceof MutableCollectionRegistration<?, ?, ?> haha && haha.single != null) {
((BiConsumer<H, TE>)haha.single).accept((H) dataHolder, element);
return DataTransactionResult.successNoData();
}
return DataTransactionResult.failNoData();
}
};


Expand All @@ -426,6 +444,12 @@ public <NE> MutableRegistration<H, NE> create(final Key<? extends Value<NE>> key
return registration;
}

public <NE extends CollectionValue<?, V>, V extends Collection<VE>, VE> MutableCollectionRegistration<H, V, VE> createCollection(final Key<? extends NE> key) {
final MutableCollectionRegistration<H, V, VE> registration = new MutableCollectionRegistration<H, V, VE>(this.registrator, key);
this.registrator.register(registration);
return registration;
}

/**
* Creates a new {@link MutableRegistrator}
* @return The registrator
Expand All @@ -443,6 +467,33 @@ public <NT> ImmutableRegistrator<NT> asImmutable(final Class<NT> target) {
}
}

public static final class MutableCollectionRegistration<H, E, V> extends MutableRegistrationBase<H, E, MutableCollectionRegistration<H, E, V>> {

private final MutableRegistrator<H> registrator;

@Nullable BiConsumer<H, V> single;

private MutableCollectionRegistration(final MutableRegistrator<H> registrator, final Key<? extends Value<E>> key) {
super(key);
this.registrator = registrator;
}

public <NE> MutableRegistration<H, NE> create(final DefaultedRegistryReference<? extends Key<? extends Value<NE>>> suppliedKey) {
return this.create(suppliedKey.get());
}

public <NE> MutableRegistration<H, NE> create(final Key<? extends Value<NE>> key) {
final MutableRegistration<H, NE> registration = new MutableRegistration<>(this.registrator, key);
this.registrator.register(registration);
return registration;
}

public MutableCollectionRegistration<H, E, V> single(BiConsumer<H, V> single) {
this.single = single;
return this;
}
}

@SuppressWarnings("unchecked")
private static class ImmutableRegistrationBase<H, E, R extends ImmutableRegistrationBase<H, E, R>> {
private final Key<? extends Value<E>> key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,8 @@ public final DataTransactionResult remove(final DataHolder.Mutable dataHolder) {
}
return this.deleteAndGetResult((H) dataHolder);
}

public <TE> DataTransactionResult offerSingle(final DataHolder.Mutable dataHolder, final TE element) {
return DataTransactionResult.failNoData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static void register(final DataProviderRegistrator registrator) {
.create(Keys.MAX_HEALTH)
.get(h -> (double) h.getMaxHealth())
.set((h, v) -> h.getAttribute(Attributes.MAX_HEALTH).setBaseValue(v))
.create(Keys.POTION_EFFECTS)
.createCollection(Keys.POTION_EFFECTS)
.get(h -> {
final Collection<MobEffectInstance> effects = h.getActiveEffects();
return PotionEffectUtil.copyAsPotionEffects(effects);
Expand All @@ -167,6 +167,9 @@ public static void register(final DataProviderRegistrator registrator) {
h.addEffect(PotionEffectUtil.copyAsEffectInstance(effect));
}
})
.single((h, v) -> {
h.addEffect(PotionEffectUtil.copyAsEffectInstance(v));
})
.create(Keys.SCALE)
.get(h -> (double) h.getScale())
.create(Keys.STUCK_ARROWS)
Expand Down

0 comments on commit 19ef538

Please sign in to comment.