use of org.spongepowered.api.data.key.Key in project SpongeCommon by SpongePowered.
the class TileEntityCommandDataProcessor method getValues.
@Override
protected Map<Key<?>, ?> getValues(TileEntityCommandBlock entity) {
CommandBlockBaseLogic logic = entity.getCommandBlockLogic();
Map<Key<?>, Object> values = Maps.newHashMapWithExpectedSize(4);
Optional<Text> lastCommandOutput = logic.getLastOutput() != null ? Optional.of(SpongeTexts.toText(logic.getLastOutput())) : Optional.empty();
values.put(Keys.LAST_COMMAND_OUTPUT, lastCommandOutput);
values.put(Keys.COMMAND, logic.commandStored);
values.put(Keys.SUCCESS_COUNT, logic.successCount);
values.put(Keys.TRACKS_OUTPUT, logic.shouldTrackOutput());
return values;
}
use of org.spongepowered.api.data.key.Key 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.key.Key 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.key.Key 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.key.Key in project LanternServer by LanternPowered.
the class IValueContainer method getValues.
@SuppressWarnings("unchecked")
@Override
default Set<ImmutableValue<?>> getValues() {
final ImmutableSet.Builder<ImmutableValue<?>> values = ImmutableSet.builder();
// Check local registrations
for (KeyRegistration<?, ?> entry : getValueCollection().getAll()) {
final Key key = entry.getKey();
final Optional<BaseValue> optValue = getValue(key);
optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
}
// Check for global registrations
for (ValueProcessorKeyRegistration<?, ?> registration : LanternValueFactory.get().getKeyRegistrations()) {
final Optional<BaseValue> optValue = ((Processor) registration).getValueFrom(this);
optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
}
// Check if custom data is supported by this container
if (this instanceof AdditionalContainerHolder) {
final AdditionalContainerCollection<?> containers = ((AdditionalContainerHolder<?>) this).getAdditionalContainers();
containers.getAll().forEach(manipulator -> values.addAll(manipulator.getValues()));
}
return values.build();
}
Aggregations