Search in sources :

Example 91 with DataView

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

the class UserStore method deserializePlayerInventory.

private static void deserializePlayerInventory(AbstractUserInventory<?> inventory, List<DataView> itemViews) {
    final LanternMainPlayerInventory mainInventory = inventory.getMain();
    final LanternPlayerEquipmentInventory equipmentInventory = inventory.getEquipment();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    for (DataView itemView : itemViews) {
        final int slot = itemView.getByte(SLOT).get() & 0xff;
        final LanternItemStack itemStack = ItemStackStore.INSTANCE.deserialize(itemView);
        if (slot >= 0 && slot < mainInventory.capacity()) {
            mainInventory.set(new SlotIndex(slot), itemStack);
        } else if (slot >= 100 && slot - 100 < equipmentInventory.capacity()) {
            equipmentInventory.set(new SlotIndex(slot - 100), itemStack);
        } else if (slot == 150) {
            offHandSlot.set(itemStack);
        }
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 92 with DataView

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

the class UserStore method deserializeEnderChest.

private static void deserializeEnderChest(GridInventory enderChestInventory, List<DataView> itemViews) {
    for (DataView itemView : itemViews) {
        final int slot = itemView.getByte(SLOT).get() & 0xff;
        final LanternItemStack itemStack = ItemStackStore.INSTANCE.deserialize(itemView);
        enderChestInventory.set(new SlotIndex(slot), itemStack);
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 93 with DataView

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

the class ItemStackStore method serializeValues.

@Override
public void serializeValues(LanternItemStack object, SimpleValueContainer valueContainer, DataView dataView) {
    final ItemTypeObjectSerializer serializer = this.itemTypeSerializers.get(object.getType());
    if (serializer != null) {
        serializer.serializeValues(object, valueContainer, dataView);
    }
    DataView displayView = null;
    final Optional<Text> optDisplayName = valueContainer.remove(Keys.DISPLAY_NAME);
    if (optDisplayName.isPresent()) {
        displayView = getOrCreateView(dataView, DISPLAY);
        final Text displayName = optDisplayName.get();
        if (displayName instanceof TranslatableText) {
            final TranslatableText name1 = (TranslatableText) displayName;
            // We can only do this for translatable text without any formatting
            if (name1.getArguments().isEmpty() && name1.getChildren().isEmpty() && name1.getStyle().isEmpty() && name1.getColor() == TextColors.NONE) {
                displayView.set(LOCALIZED_NAME, name1.getTranslation().getId());
            } else {
                displayView.set(NAME, LanternTexts.toLegacy(displayName));
            }
        } else {
            displayView.set(NAME, LanternTexts.toLegacy(displayName));
        }
    }
    final Optional<List<Text>> optLore = valueContainer.remove(Keys.ITEM_LORE);
    if (optLore.isPresent() && !optLore.get().isEmpty()) {
        if (displayView == null) {
            displayView = getOrCreateView(dataView, DISPLAY);
        }
        displayView.set(LORE, optLore.get().stream().map(LanternTexts::toLegacy).collect(Collectors.toList()));
    }
    if (valueContainer.remove(Keys.UNBREAKABLE).orElse(false)) {
        dataView.set(UNBREAKABLE, (byte) 1);
    }
    final Optional<Set<BlockType>> optBlockTypes = valueContainer.remove(Keys.BREAKABLE_BLOCK_TYPES);
    if (optBlockTypes.isPresent() && !optBlockTypes.get().isEmpty()) {
        dataView.set(CAN_DESTROY, optBlockTypes.get().stream().map(CatalogType::getId).collect(Collectors.toSet()));
    }
    valueContainer.remove(Keys.ITEM_ENCHANTMENTS).ifPresent(list -> serializeEnchantments(dataView, ENCHANTMENTS, list));
    valueContainer.remove(Keys.STORED_ENCHANTMENTS).ifPresent(list -> serializeEnchantments(dataView, STORED_ENCHANTMENTS, list));
    super.serializeValues(object, valueContainer, dataView);
}
Also used : TranslatableText(org.spongepowered.api.text.TranslatableText) DataView(org.spongepowered.api.data.DataView) CatalogType(org.spongepowered.api.CatalogType) Set(java.util.Set) HashSet(java.util.HashSet) LanternTexts(org.lanternpowered.server.text.LanternTexts) TranslatableText(org.spongepowered.api.text.TranslatableText) Text(org.spongepowered.api.text.Text) List(java.util.List) ArrayList(java.util.ArrayList)

Example 94 with DataView

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

the class SimpleObjectSerializer method serialize.

@Override
public DataView serialize(T object) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    this.objectStore.serialize(object, dataView);
    return dataView;
}
Also used : DataView(org.spongepowered.api.data.DataView)

Example 95 with DataView

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

the class ContainerTileEntityStore method serialize.

@Override
public void serialize(T object, DataView dataView) {
    super.serialize(object, dataView);
    final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
    final List<DataView> itemViews = new ArrayList<>();
    final Inventory inventory = object.getInventory();
    final Iterable<Slot> slots = inventory.slots();
    for (Slot slot : slots) {
        final Optional<ItemStack> optItemStack = slot.peek();
        if (!optItemStack.isPresent()) {
            continue;
        }
        final DataView itemView = itemStackSerializer.serialize((LanternItemStack) optItemStack.get());
        // noinspection ConstantConditions
        itemView.set(SLOT, (byte) inventory.getProperty(slot, SlotIndex.class, null).get().getValue().intValue());
        itemViews.add(itemView);
    }
    dataView.set(ITEMS, itemViews);
}
Also used : DataView(org.spongepowered.api.data.DataView) ArrayList(java.util.ArrayList) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory)

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