use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class MemoryDataView method setCollection.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(final String key, final Collection<?> value) {
final ImmutableList.Builder<Object> builder = ImmutableList.builder();
@Nullable DataManager manager;
try {
manager = Sponge.dataManager();
} catch (final Exception e) {
manager = null;
}
for (final Object object : value) {
if (object instanceof DataSerializable) {
builder.add(((DataSerializable) object).toContainer());
} else if (object instanceof DataView) {
if (this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.ALL_DATA_CLONED || this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.CLONED_ON_SET) {
final MemoryDataView view = new MemoryDataContainer(this.safety);
final DataView internalView = (DataView) object;
for (final Map.Entry<DataQuery, Object> entry : internalView.values(false).entrySet()) {
view.set(entry.getKey(), entry.getValue());
}
builder.add(view);
} else {
builder.add(object);
}
} else if (object instanceof ResourceKey) {
builder.add(object.toString());
} else if (object instanceof Map) {
builder.add(this.ensureSerialization((Map) object));
} else if (object instanceof Collection) {
builder.add(this.ensureSerialization((Collection) object));
} else {
if (manager != null) {
final Optional<? extends DataTranslator<?>> translatorOptional = manager.translator(object.getClass());
if (translatorOptional.isPresent()) {
final DataTranslator translator = translatorOptional.get();
final DataContainer container = translator.translate(object);
checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
builder.add(container);
} else {
builder.add(object);
}
} else {
builder.add(object);
}
}
}
this.map.put(key, builder.build());
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class MemoryDataView method getDataKeyList.
@Override
public Optional<List<Key<? extends Value<?>>>> getDataKeyList(final DataQuery path) {
Objects.requireNonNull(path, "path");
final Optional<List<ResourceKey>> resourceKeys = this.getResourceKeyList(path);
if (!resourceKeys.isPresent()) {
return Optional.empty();
}
final List<Key<? extends Value<?>>> keys = new ArrayList<>();
for (final ResourceKey resourceKey : resourceKeys.get()) {
KeyProvider.INSTANCE.get(resourceKey).ifPresent(keys::add);
}
if (keys.isEmpty()) {
return Optional.empty();
}
return Optional.of(keys);
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeDataManager method registerLegacyManipulatorIds.
@Override
public void registerLegacyManipulatorIds(final String legacyId, final ResourceKey dataStoreKey) {
Objects.requireNonNull(legacyId);
Objects.requireNonNull(dataStoreKey);
final ResourceKey previous = this.legacyRegistrations.putIfAbsent(legacyId, dataStoreKey);
if (previous != null) {
throw new IllegalStateException("Legacy registration id already registered: id" + legacyId + " for registration: " + dataStoreKey);
}
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class CreatorTrackedData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(CreatorTrackedBridge.class).create(Keys.NOTIFIER).get(m -> m.tracker$getNotifierUUID().orElse(null)).set((b, n) -> b.tracker$setTrackedUUID(PlayerTracker.Type.NOTIFIER, n)).create(Keys.CREATOR).get(m -> m.tracker$getCreatorUUID().orElse(null)).set((b, n) -> b.tracker$setTrackedUUID(PlayerTracker.Type.CREATOR, n));
final ResourceKey dataStoreKey = ResourceKey.sponge("creator_tracked");
registrator.spongeDataStore(dataStoreKey, CreatorTrackedBridge.class, Keys.NOTIFIER, Keys.CREATOR);
SpongeDataManager.INSTANCE.registerLegacySpongeData(PlayerTracker.Type.NOTIFIER.compoundKey, dataStoreKey, Keys.NOTIFIER);
SpongeDataManager.INSTANCE.registerLegacySpongeData(PlayerTracker.Type.CREATOR.compoundKey, dataStoreKey, Keys.CREATOR);
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class ItemData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemEntity.class).create(Keys.ITEM_STACK_SNAPSHOT).get(h -> ItemStackUtil.snapshotOf(h.getItem())).set((h, v) -> h.setItem(ItemStackUtil.fromSnapshotToNative(v))).asMutable(ItemEntityBridge.class).create(Keys.DESPAWN_DELAY).get(h -> new SpongeTicks(h.bridge$getDespawnDelay())).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0) {
return false;
}
h.bridge$setDespawnDelay(ticks, false);
return true;
}).create(Keys.INFINITE_DESPAWN_DELAY).get(ItemEntityBridge::bridge$infiniteDespawnDelay).set((h, v) -> h.bridge$setDespawnDelay(h.bridge$getDespawnDelay(), v)).create(Keys.INFINITE_PICKUP_DELAY).get(ItemEntityBridge::bridge$infinitePickupDelay).set((h, v) -> h.bridge$setPickupDelay(h.bridge$getPickupDelay(), v)).create(Keys.PICKUP_DELAY).get(h -> new SpongeTicks(h.bridge$getPickupDelay())).set((h, v) -> h.bridge$setPickupDelay((int) v.ticks(), false)).create(ItemData.PREVIOUS_PICKUP_DELAY).get(v -> -1).set(ItemEntityBridge::bridge$setPrevPickupDelay).create(ItemData.PREVIOUS_DESPAWN_DELAY).get(v -> -1).set(ItemEntityBridge::bridge$setPrevDespawnDelay);
final ResourceKey item = ResourceKey.sponge("item");
registrator.spongeDataStore(item, 2, new DataContentUpdater[] { ItemData.INFINITE_DELAYS_UPDATER_BYTE_TO_BOOL_FIX }, ItemEntityBridge.class, Keys.INFINITE_PICKUP_DELAY, ItemData.PREVIOUS_PICKUP_DELAY, Keys.INFINITE_DESPAWN_DELAY, ItemData.PREVIOUS_DESPAWN_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.INFINITE_PICKUP_DELAY, item, Keys.INFINITE_PICKUP_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.PREVIOUS_PICKUP_DELAY, item, ItemData.PREVIOUS_PICKUP_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.INFINITE_DESPAWN_DELAY, item, Keys.INFINITE_DESPAWN_DELAY);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.Item.PREVIOUS_DESPAWN_DELAY, item, ItemData.PREVIOUS_DESPAWN_DELAY);
}
Aggregations