use of org.spongepowered.api.data.value.immutable.ImmutableValue 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.immutable.ImmutableValue in project LanternServer by LanternPowered.
the class ICompositeValueStore method offerNoEvents.
default DataTransactionResult offerNoEvents(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.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);
}
final DataTransactionResult.Builder builder = DataTransactionResult.builder();
boolean success = false;
for (ImmutableValue value : valueContainer.getValues()) {
final DataTransactionResult result = offerNoEvents(value);
if (result.isSuccessful()) {
builder.success(value);
builder.replace(result.getReplacedData());
success = true;
} else {
builder.reject(value);
}
}
if (success) {
builder.result(DataTransactionResult.Type.SUCCESS);
} else {
builder.result(DataTransactionResult.Type.FAILURE);
}
return builder.build();
}
}
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);
final DataTransactionResult.Builder builder = DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS);
builder.success(merged.getValues());
if (old != null) {
builder.replace(old.getValues());
}
return builder.build();
}
return DataTransactionResult.failNoData();
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue 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();
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue 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();
}
use of org.spongepowered.api.data.value.immutable.ImmutableValue in project LanternServer by LanternPowered.
the class TestInventoryPlugin method onInit.
@Listener
public void onInit(GameInitializationEvent event) {
this.myArchetype = InventoryArchetype.builder().with(InventoryArchetypes.MENU_ROW).title(Text.of("My Fancy Title")).property(InventoryDimension.of(9, 1)).property(new GuiIdProperty(GuiIds.CHEST)).build("inventory_test:test", "Test Inventory");
Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
if (!(src instanceof Player)) {
throw new CommandException(t("Only players may use this command."));
}
final Inventory inventory = Inventory.builder().of(this.myArchetype).withCarrier((Carrier) src).build(this);
System.out.println(inventory.getClass().getName());
final ItemStack itemStack = ItemStack.of(ItemTypes.LOG, 64);
itemStack.offer(Keys.TREE_TYPE, TreeTypes.JUNGLE);
inventory.offer(itemStack);
((Player) src).openInventory(inventory);
return CommandResult.success();
}).build(), "test-a-inv");
Keys.COAL_TYPE.registerEvent(ItemStack.class, event1 -> {
final DataTransactionResult result = event1.getEndResult();
final List<ImmutableValue<?>> newSuccessfulData = new ArrayList<>(result.getSuccessfulData());
Iterator<ImmutableValue<?>> it = newSuccessfulData.iterator();
while (it.hasNext()) {
final ImmutableValue<?> value = it.next();
if (value.getKey() == Keys.COAL_TYPE) {
System.out.println("Changed coal type to: " + ((CoalType) value.get()).getId() + ", but this not allowed");
it.remove();
break;
}
}
final List<ImmutableValue<?>> newReplacedData = new ArrayList<>(result.getReplacedData());
it = newSuccessfulData.iterator();
while (it.hasNext()) {
final ImmutableValue<?> value = it.next();
if (value.getKey() == Keys.COAL_TYPE) {
it.remove();
break;
}
}
event1.proposeChanges(DataTransactionResult.builder().result(result.getType()).reject(result.getRejectedData()).replace(newReplacedData).success(newSuccessfulData).build());
});
}
Aggregations