use of org.spongepowered.api.data.value.BaseValue in project SpongeCommon by SpongePowered.
the class MixinStateImplementation method cycleValue.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public BlockState cycleValue(Key<? extends BaseValue<? extends Cycleable<?>>> key) {
if (supports(key)) {
final Cycleable value = (Cycleable) get((Key) key).get();
final Cycleable next = value.cycleNext();
return with((Key<? extends BaseValue<Object>>) (Key<?>) key, next).get();
}
throw new IllegalArgumentException("Used an invalid cyclable key! Check with supports in the future!");
}
use of org.spongepowered.api.data.value.BaseValue in project SpongeCommon by SpongePowered.
the class MixinDataHolder method offer.
@Override
public <E> DataTransactionResult offer(Key<? extends BaseValue<E>> key, E value) {
TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
SpongeTimings.dataOfferKey.startTimingIfSync();
final Optional<ValueProcessor<E, ? extends BaseValue<E>>> optional = DataUtil.getBaseValueProcessor(key);
if (optional.isPresent()) {
final DataTransactionResult result = optional.get().offerToStore(this, value);
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return result;
} else if (this instanceof IMixinCustomDataHolder) {
final DataTransactionResult result = ((IMixinCustomDataHolder) this).offerCustom(key, value);
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return result;
}
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return DataTransactionResult.failNoData();
}
use of org.spongepowered.api.data.value.BaseValue in project SpongeCommon by SpongePowered.
the class MixinWorld_Data method offer.
@Override
public <E> DataTransactionResult offer(int x, int y, int z, Key<? extends BaseValue<E>> key, E value) {
final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
if (blockState.supports(key)) {
ImmutableValue<E> old = ((Value<E>) getValue(x, y, z, (Key) key).get()).asImmutable();
setBlock(x, y, z, blockState.with(key, value).get());
ImmutableValue<E> newVal = ((Value<E>) getValue(x, y, z, (Key) key).get()).asImmutable();
return DataTransactionResult.successReplaceResult(newVal, old);
}
return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(key, value)).orElseGet(DataTransactionResult::failNoData);
}
use of org.spongepowered.api.data.value.BaseValue in project LanternServer by LanternPowered.
the class ICompositeValueStore method offerNoEvents.
default <E> DataTransactionResult offerNoEvents(Key<? extends BaseValue<E>> key, E element) {
// Check the local key registration
final KeyRegistration<?, ?> localKeyRegistration = (KeyRegistration<?, ?>) getValueCollection().get((Key) key).orElse(null);
if (localKeyRegistration != null) {
return ((Processor<BaseValue<E>, E>) localKeyRegistration).offerTo(this, element);
}
// Check for a global registration
final Optional<ValueProcessorKeyRegistration> globalRegistration = LanternValueFactory.get().getKeyRegistration((Key) key);
if (globalRegistration.isPresent()) {
return ((Processor<BaseValue<E>, E>) globalRegistration.get()).offerTo(this, element);
}
// Check if custom data is supported by this container
if (this instanceof AdditionalContainerHolder) {
// Check for the custom value containers
final AdditionalContainerCollection<H> containers = ((AdditionalContainerHolder<H>) this).getAdditionalContainers();
for (H valueContainer : containers.getAll()) {
if (valueContainer.supports(key)) {
if (valueContainer instanceof ICompositeValueStore) {
return ((ICompositeValueStore) valueContainer).offerNoEvents(key, element);
} else if (valueContainer instanceof CompositeValueStore) {
return ((CompositeValueStore) valueContainer).offer(key, element);
} else if (valueContainer instanceof DataManipulator) {
final ImmutableValue oldImmutableValue = (ImmutableValue) valueContainer.getValue((Key) key).map(value -> ValueHelper.toImmutable((BaseValue) value)).orElse(null);
((DataManipulator) valueContainer).set(key, element);
final ImmutableValue immutableValue = (ImmutableValue) valueContainer.getValue((Key) key).map(value -> ValueHelper.toImmutable((BaseValue) value)).orElse(null);
if (oldImmutableValue == null && immutableValue == null) {
return DataTransactionResult.successNoData();
} else if (oldImmutableValue == null) {
return DataTransactionResult.successResult(immutableValue);
} else if (immutableValue == null) {
return DataTransactionResult.successRemove(oldImmutableValue);
} else {
return DataTransactionResult.successReplaceResult(immutableValue, oldImmutableValue);
}
} else {
// TODO: Support immutable manipulators?
return DataTransactionResult.failNoData();
}
}
}
}
return DataTransactionResult.failNoData();
}
use of org.spongepowered.api.data.value.BaseValue in project LanternServer by LanternPowered.
the class IImmutableValueHolder method getImmutableValueFor.
/**
* Attempts to get a {@link ImmutableValue} for the given
* {@link Key}. {@link Optional#empty()} will be returned
* when it fails.
*
* @param key The key
* @return The immutable value, if success
*/
default <E, R extends ImmutableValue<E>> Optional<R> getImmutableValueFor(Key<? extends BaseValue<E>> key) {
checkNotNull(key, "key");
final ImmutableContainerCache cache = getContainerCache();
if (cache != null) {
Object value = cache.values.get(key);
if (value != null) {
return value == ImmutableContainerCache.NONE ? Optional.empty() : Optional.of((R) value);
}
}
Optional optValue = getRawValueFor((Key) key);
if (optValue.isPresent() && !(optValue.get() instanceof ImmutableValue)) {
optValue = Optional.of(((Value) optValue.get()).asImmutable());
}
if (cache != null) {
cache.values.put(key, optValue.orElse(ImmutableContainerCache.NONE));
}
return Optional.empty();
}
Aggregations