use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SpongeLockableBuilder method buildContent.
@Override
protected Optional<T> buildContent(DataView container) throws InvalidDataException {
return super.buildContent(container).flatMap(lockable -> {
if (!container.contains(DataQueries.BLOCK_ENTITY_ITEM_CONTENTS)) {
((TileEntity) lockable).invalidate();
return Optional.empty();
}
List<DataView> contents = container.getViewList(DataQueries.BLOCK_ENTITY_ITEM_CONTENTS).get();
for (DataView content : contents) {
net.minecraft.item.ItemStack stack = (net.minecraft.item.ItemStack) content.getSerializable(DataQueries.BLOCK_ENTITY_SLOT_ITEM, ItemStack.class).get();
((IInventory) lockable).setInventorySlotContents(content.getInt(DataQueries.BLOCK_ENTITY_SLOT).get(), stack);
}
if (container.contains(Keys.LOCK_TOKEN.getQuery())) {
lockable.offer(Keys.LOCK_TOKEN, container.getString(Keys.LOCK_TOKEN.getQuery()).get());
}
return Optional.of(lockable);
});
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class DataUpdaterDelegate method update.
@Override
public DataView update(DataView content) {
// backup
final DataView copied = content.copy();
DataView updated = copied;
for (DataContentUpdater updater : this.updaters) {
try {
updated = updater.update(updated);
} catch (Exception e) {
Exception exception = new RuntimeException("There was error attempting to update some data for the content updater:" + updater.getClass().getName() + "\nThe original data is being returned, possibly causing" + "issues later on, \nbut the original data should not be lost. Please notify the developer" + "of this exception with the stacktrace.", e);
exception.printStackTrace();
return copied;
}
}
return updated;
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class LanternTextHelper method raw.
public static RawAction raw(HoverAction<?> hoverAction) {
if (hoverAction instanceof HoverAction.ShowText) {
return new RawAction("show_text", ((HoverAction.ShowText) hoverAction).getResult());
} else if (hoverAction instanceof HoverAction.ShowEntity) {
final HoverAction.ShowEntity.Ref ref = ((HoverAction.ShowEntity) hoverAction).getResult();
final DataContainer dataContainer = DataContainer.createNew().set(SHOW_ENTITY_ID, ref.getUniqueId().toString()).set(SHOW_ENTITY_NAME, ref.getName());
ref.getType().ifPresent(type -> dataContainer.set(SHOW_ENTITY_TYPE, type.getId()));
try {
return new RawAction("show_entity", JsonDataFormat.writeAsString(dataContainer));
} catch (IOException e) {
throw new IllegalStateException(e);
}
} else if (hoverAction instanceof HoverAction.ShowItem) {
final ItemStackSnapshot itemStackSnapshot = ((HoverAction.ShowItem) hoverAction).getResult();
final LanternItemStack itemStack = (LanternItemStack) itemStackSnapshot.createStack();
final DataView dataView = ItemStackStore.INSTANCE.serialize(itemStack);
try {
return new RawAction("show_item", JsonDataFormat.writeAsString(dataView));
} catch (IOException e) {
throw new IllegalStateException(e);
}
} else {
throw new IllegalArgumentException("Unknown hover action type: " + hoverAction.getClass().getName());
}
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class MemoryDataView method get.
@Override
public Optional<Object> get(DataQuery path) {
checkNotNull(path, "path");
final List<String> queryParts = path.getParts();
int sz = queryParts.size();
if (sz == 0) {
return Optional.of(this);
}
final String key = queryParts.get(0);
if (sz == 1) {
final Object object = this.map.get(key);
if (object == null) {
return Optional.empty();
}
if (this.safety == SafetyMode.ALL_DATA_CLONED) {
if (object.getClass().isArray()) {
if (object instanceof byte[]) {
return Optional.of(ArrayUtils.clone((byte[]) object));
} else if (object instanceof short[]) {
return Optional.of(ArrayUtils.clone((short[]) object));
} else if (object instanceof int[]) {
return Optional.of(ArrayUtils.clone((int[]) object));
} else if (object instanceof long[]) {
return Optional.of(ArrayUtils.clone((long[]) object));
} else if (object instanceof float[]) {
return Optional.of(ArrayUtils.clone((float[]) object));
} else if (object instanceof double[]) {
return Optional.of(ArrayUtils.clone((double[]) object));
} else if (object instanceof boolean[]) {
return Optional.of(ArrayUtils.clone((boolean[]) object));
} else {
return Optional.of(ArrayUtils.clone((Object[]) object));
}
}
}
return Optional.of(object);
}
final Optional<DataView> subViewOptional = getUnsafeView(key);
if (!subViewOptional.isPresent()) {
return Optional.empty();
}
final DataView subView = subViewOptional.get();
return subView.get(path.popFirst());
}
use of org.spongepowered.api.data.DataView in project LanternServer by LanternPowered.
the class MemoryDataView method setCollection.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
final ImmutableList.Builder<Object> builder = ImmutableList.builder();
final LanternDataManager manager = getDataManager();
for (Object object : value) {
if (object instanceof DataSerializable) {
builder.add(((DataSerializable) object).toContainer());
} else if (object instanceof DataView) {
if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
final MemoryDataView view = new MemoryDataContainer(this.safety);
final DataView internalView = (DataView) object;
for (Map.Entry<DataQuery, Object> entry : internalView.getValues(false).entrySet()) {
view.set(entry.getKey(), entry.getValue());
}
builder.add(view);
} else {
builder.add(object);
}
} else if (object instanceof CatalogType) {
builder.add(((CatalogType) object).getId());
} else if (object instanceof Map) {
builder.add(ensureSerialization((Map) object));
} else if (object instanceof Collection) {
builder.add(ensureSerialization((Collection) object));
} else {
final TypeToken<?> typeToken = TypeToken.of(object.getClass());
final DataTypeSerializer serializer = manager == null ? null : manager.getTypeSerializer(typeToken).orElse(null);
if (serializer != null) {
final Object result = serializer.serialize(typeToken, manager.getTypeSerializerContext(), object);
checkArgument(!result.equals(this), "Cannot insert self-referencing Objects!");
builder.add(result);
} else {
builder.add(object);
}
}
}
this.map.put(key, builder.build());
}
Aggregations