use of org.spongepowered.api.data.value.immutable.ImmutableValue in project SpongeCommon by SpongePowered.
the class MixinWorld_Data method offer.
@Override
public DataTransactionResult offer(int x, int y, int z, DataManipulator<?, ?> manipulator, MergeFunction function) {
final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
final ImmutableDataManipulator<?, ?> immutableDataManipulator = manipulator.asImmutable();
if (blockState.supports((Class) immutableDataManipulator.getClass())) {
final List<ImmutableValue<?>> old = new ArrayList<>(blockState.getValues());
final BlockState newState = blockState.with(immutableDataManipulator).get();
old.removeAll(newState.getValues());
setBlock(x, y, z, newState);
return DataTransactionResult.successReplaceResult(old, manipulator.getValues());
}
return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(manipulator, function)).orElseGet(() -> DataTransactionResult.failResult(manipulator.getValues()));
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue in project SpongeCommon by SpongePowered.
the class MixinWorld_Data method getValues.
@Override
public Set<ImmutableValue<?>> getValues(int x, int y, int z) {
final ImmutableSet.Builder<ImmutableValue<?>> builder = ImmutableSet.builder();
final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
builder.addAll(blockState.getValues());
final Optional<TileEntity> tileEntity = getTileEntity(x, y, z);
if (tileEntity.isPresent()) {
builder.addAll(tileEntity.get().getValues());
}
return builder.build();
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue 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.immutable.ImmutableValue in project LanternServer by LanternPowered.
the class CompositeValueStoreHelper method processDataTransactionResult.
protected static DataTransactionResult processDataTransactionResult(ICompositeValueStore store, DataTransactionResult result, BooleanSupplier hasListeners) {
if (!(store instanceof DataHolder) || !result.isSuccessful() || !hasListeners.getAsBoolean()) {
return result;
}
final Cause cause = CauseStack.currentOrEmpty().getCurrentCause();
final ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(cause, result, (DataHolder) store);
Lantern.getGame().getEventManager().post(event);
// Nothing is allowed to change, revert everything fast
if (event.isCancelled()) {
store.undoFastNoEvents(result);
return DataTransactionResult.failNoData();
}
final DataTransactionResult original = result;
result = event.getEndResult();
// Check if something actually changed
if (result != original) {
final Map<Key<?>, ImmutableValue<?>> success = new HashMap<>();
for (ImmutableValue<?> value : original.getSuccessfulData()) {
success.put(value.getKey(), value);
}
for (ImmutableValue<?> value : result.getSuccessfulData()) {
final ImmutableValue<?> value1 = success.remove(value.getKey());
if (value1 == null || value1.get() != value.get()) {
store.offerNoEvents(value);
}
}
// A previously successful offering got removed, revert this
if (!success.isEmpty()) {
for (ImmutableValue<?> value : original.getReplacedData()) {
if (success.containsKey(value.getKey())) {
store.offerNoEvents(value);
}
}
}
}
return event.getEndResult();
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue in project LanternServer by LanternPowered.
the class ICompositeValueStore method offerFastNoEvents.
default boolean offerFastNoEvents(H valueContainer, MergeFunction function) {
if (valueContainer instanceof IDataManipulatorBase) {
// Offer all the default key values as long if they are supported
final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getByMutable(((IDataManipulatorBase) valueContainer).getMutableType());
if (optRegistration.isPresent()) {
if (function == MergeFunction.FORCE_NOTHING) {
return true;
} else if (function != MergeFunction.IGNORE_ALL) {
ValueContainer old = DataHelper.create(this, optRegistration.get());
if (valueContainer instanceof IImmutableDataManipulator && old != null) {
old = ((DataManipulator) old).asImmutable();
}
valueContainer = (H) function.merge(old, valueContainer);
}
boolean success = false;
for (ImmutableValue value : valueContainer.getValues()) {
if (offerFast(value)) {
success = true;
break;
}
}
return success;
}
}
if (this instanceof AdditionalContainerHolder) {
final AdditionalContainerCollection<H> containers = ((AdditionalContainerHolder<H>) this).getAdditionalContainers();
final Class key = valueContainer.getClass();
final H old = (H) containers.get(key).orElse(null);
final H merged = function.merge(old, valueContainer);
containers.offer(merged);
return true;
}
return false;
}
Aggregations