Search in sources :

Example 41 with DataContainer

use of org.spongepowered.api.data.DataContainer in project TotalEconomy by Erigitic.

the class ShopManager method removeShopItemData.

/**
 * Removes ShopItemData from an ItemStack.
 *
 * @param itemStack The ItemStack to remove ShopItemData from
 * @return ItemStack An ItemStack with ShopItemData removed
 */
private ItemStack removeShopItemData(ItemStack itemStack) {
    itemStack.remove(Keys.ITEM_LORE);
    itemStack.remove(ShopKeys.SHOP_ITEM);
    // Remove the DataQuery for lore, otherwise an empty NBT tag will be attached to the item
    DataContainer dataContainer = itemStack.toContainer().remove(DataQuery.of("DefaultReplacement", "UnsafeData", "display", "Lore")).copy();
    itemStack = ItemStack.builder().fromContainer(dataContainer).itemType(itemStack.getItem()).quantity(1).build();
    return itemStack;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 42 with DataContainer

use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.

the class IImmutableDataHolder method toContainer.

@Override
default DataContainer toContainer() {
    final DataContainer dataContainer = DataContainer.createNew();
    DataHelper.serializeRawData(dataContainer, this);
    return dataContainer;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 43 with DataContainer

use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.

the class DataSerializableTypeSerializer method serialize.

@Override
public void serialize(TypeToken<?> type, DataSerializable obj, ConfigurationNode value) throws ObjectMappingException {
    final DataContainer container = obj.toContainer();
    value.setValue(ConfigurateTranslator.instance().translate(container));
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 44 with DataContainer

use of org.spongepowered.api.data.DataContainer 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 45 with DataContainer

use of org.spongepowered.api.data.DataContainer in project LanternServer by LanternPowered.

the class MemoryDataView method copy.

@Override
public DataContainer copy(SafetyMode safety) {
    final DataContainer container = new MemoryDataContainer(safety);
    getKeys(false).forEach(query -> get(query).ifPresent(obj -> container.set(query, obj)));
    return container;
}
Also used : Arrays(java.util.Arrays) ArrayUtils(org.apache.commons.lang3.ArrayUtils) TypeToken(com.google.common.reflect.TypeToken) DataQuery(org.spongepowered.api.data.DataQuery) Supplier(java.util.function.Supplier) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) DataSerializable(org.spongepowered.api.data.DataSerializable) LinkedHashMap(java.util.LinkedHashMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects(com.google.common.base.Objects) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) DataQuery.of(org.spongepowered.api.data.DataQuery.of) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) BaseValue(org.spongepowered.api.data.value.BaseValue) MoreObjects(com.google.common.base.MoreObjects) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Stream(java.util.stream.Stream) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) Coerce(org.spongepowered.api.util.Coerce) Optional(java.util.Optional) DataContainer(org.spongepowered.api.data.DataContainer)

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)110 DataView (org.spongepowered.api.data.DataView)30 DataQuery (org.spongepowered.api.data.DataQuery)28 Test (org.junit.Test)24 List (java.util.List)15 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 Nullable (javax.annotation.Nullable)11 Collection (java.util.Collection)10 ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableMap (com.google.common.collect.ImmutableMap)9 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Optional (java.util.Optional)9 CatalogType (org.spongepowered.api.CatalogType)9 Sponge (org.spongepowered.api.Sponge)8 Set (java.util.Set)7 Collectors (java.util.stream.Collectors)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 DataSerializable (org.spongepowered.api.data.DataSerializable)7