Search in sources :

Example 36 with DataView

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

the class FireworksItemTypeObjectSerializer method serializeValues.

@Override
public void serializeValues(ItemStack itemStack, SimpleValueContainer valueContainer, DataView dataView) {
    super.serializeValues(itemStack, valueContainer, dataView);
    final DataView fireworksView = dataView.createView(FIREWORKS);
    valueContainer.remove(Keys.FIREWORK_EFFECTS).ifPresent(effects -> fireworksView.set(EXPLOSIONS, effects.stream().map(FireworkChargeItemTypeObjectSerializer::serializeExplosion).collect(Collectors.toList())));
    valueContainer.remove(Keys.FIREWORK_FLIGHT_MODIFIER).ifPresent(modifier -> fireworksView.set(FLIGHT_MODIFIER, modifier.byteValue()));
}
Also used : DataView(org.spongepowered.api.data.DataView)

Example 37 with DataView

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

the class ItemStackStore method deserialize.

@Override
public void deserialize(LanternItemStack object, DataView dataView) {
    object.setQuantity(dataView.getInt(QUANTITY).get());
    // All the extra data we will handle will be stored in the tag
    final DataView tag = dataView.getView(TAG).orElseGet(() -> DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED));
    tag.set(ItemTypeObjectSerializer.DATA_VALUE, dataView.getShort(DATA).get());
    super.deserialize(object, tag);
}
Also used : DataView(org.spongepowered.api.data.DataView)

Example 38 with DataView

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

the class ItemStackStore method deserializeValues.

@Override
public void deserializeValues(LanternItemStack object, SimpleValueContainer valueContainer, DataView dataView) {
    final ItemTypeObjectSerializer serializer = this.itemTypeSerializers.get(object.getType());
    if (serializer != null) {
        serializer.deserializeValues(object, valueContainer, dataView);
    }
    final Optional<DataView> optDisplayView = dataView.getView(DISPLAY);
    if (optDisplayView.isPresent()) {
        final DataView displayView = optDisplayView.get();
        if (!valueContainer.get(Keys.DISPLAY_NAME).isPresent()) {
            Optional<String> name = displayView.getString(NAME);
            if (name.isPresent()) {
                valueContainer.set(Keys.DISPLAY_NAME, LanternTexts.fromLegacy(name.get()));
            } else if ((name = displayView.getString(LOCALIZED_NAME)).isPresent()) {
                valueContainer.set(Keys.DISPLAY_NAME, t(name.get()));
            }
        }
        dataView.getStringList(LORE).ifPresent(lore -> {
            if (!lore.isEmpty()) {
                valueContainer.set(Keys.ITEM_LORE, lore.stream().map(LanternTexts::fromLegacy).collect(Collectors.toList()));
            }
        });
    }
    dataView.getStringList(CAN_DESTROY).ifPresent(types -> {
        if (!types.isEmpty()) {
            final Set<BlockType> blockTypes = new HashSet<>();
            types.forEach(type -> BlockRegistryModule.get().getById(type).ifPresent(blockTypes::add));
            valueContainer.set(Keys.BREAKABLE_BLOCK_TYPES, blockTypes);
        }
    });
    deserializeEnchantments(dataView, ENCHANTMENTS, Keys.ITEM_ENCHANTMENTS, valueContainer);
    deserializeEnchantments(dataView, STORED_ENCHANTMENTS, Keys.STORED_ENCHANTMENTS, valueContainer);
    super.deserializeValues(object, valueContainer, dataView);
}
Also used : DataView(org.spongepowered.api.data.DataView) BlockType(org.spongepowered.api.block.BlockType) LanternTexts(org.lanternpowered.server.text.LanternTexts) HashSet(java.util.HashSet)

Example 39 with DataView

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

the class ItemStackStore method serializeEnchantments.

private void serializeEnchantments(DataView dataView, DataQuery query, List<Enchantment> enchantments) {
    if (enchantments.isEmpty()) {
        return;
    }
    final List<DataView> dataViews = new ArrayList<>();
    for (Enchantment enchantment : enchantments) {
        final DataView enchantmentView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
        enchantmentView.set(ENCHANTMENT_ID, (short) ((LanternEnchantmentType) enchantment.getType()).getInternalId());
        enchantmentView.set(ENCHANTMENT_LEVEL, (short) enchantment.getLevel());
        dataViews.add(enchantmentView);
    }
    dataView.set(query, dataViews);
}
Also used : DataView(org.spongepowered.api.data.DataView) LanternEnchantmentType(org.lanternpowered.server.item.enchantment.LanternEnchantmentType) ArrayList(java.util.ArrayList) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Example 40 with DataView

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

the class ItemStackStore method serialize.

@Override
public void serialize(LanternItemStack object, DataView dataView) {
    dataView.set(QUANTITY, (byte) object.getQuantity());
    final DataView tag = dataView.createView(TAG);
    super.serialize(object, tag);
    dataView.set(DATA, tag.getShort(ItemTypeObjectSerializer.DATA_VALUE).orElse((short) 0));
    tag.remove(ItemTypeObjectSerializer.DATA_VALUE);
    if (tag.isEmpty()) {
        dataView.remove(TAG);
    }
}
Also used : DataView(org.spongepowered.api.data.DataView)

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