Search in sources :

Example 21 with DataView

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);
    });
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory) DataView(org.spongepowered.api.data.DataView) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 22 with DataView

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;
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContentUpdater(org.spongepowered.api.data.persistence.DataContentUpdater)

Example 23 with DataView

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());
    }
}
Also used : JsonParseException(com.google.gson.JsonParseException) ClickAction(org.spongepowered.api.text.action.ClickAction) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStackStore(org.lanternpowered.server.data.io.store.item.ItemStackStore) DataQuery(org.spongepowered.api.data.DataQuery) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) Gson(com.google.gson.Gson) HoverAction(org.spongepowered.api.text.action.HoverAction) URI(java.net.URI) LanternClickActionCallbacks(org.lanternpowered.server.text.action.LanternClickActionCallbacks) Nullable(javax.annotation.Nullable) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) MalformedURLException(java.net.MalformedURLException) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) IOException(java.io.IOException) UUID(java.util.UUID) JsonDataFormat(org.lanternpowered.server.data.persistence.json.JsonDataFormat) File(java.io.File) Consumer(java.util.function.Consumer) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) DataView(org.spongepowered.api.data.DataView) Coerce(org.spongepowered.api.util.Coerce) EntityType(org.spongepowered.api.entity.EntityType) Optional(java.util.Optional) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) IOException(java.io.IOException) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) HoverAction(org.spongepowered.api.text.action.HoverAction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Example 24 with DataView

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());
}
Also used : DataView(org.spongepowered.api.data.DataView)

Example 25 with DataView

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());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DataSerializable(org.spongepowered.api.data.DataSerializable) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) Collection(java.util.Collection) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

DataView (org.spongepowered.api.data.DataView)100 DataContainer (org.spongepowered.api.data.DataContainer)30 DataQuery (org.spongepowered.api.data.DataQuery)24 ArrayList (java.util.ArrayList)21 Map (java.util.Map)17 List (java.util.List)13 Vector3i (com.flowpowered.math.vector.Vector3i)11 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 UUID (java.util.UUID)10 Nullable (javax.annotation.Nullable)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)10 ImmutableList (com.google.common.collect.ImmutableList)8 IOException (java.io.IOException)8 Test (org.junit.Test)8 CatalogType (org.spongepowered.api.CatalogType)8 Path (java.nio.file.Path)7 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)7 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)7 ImmutableMap (com.google.common.collect.ImmutableMap)6