Search in sources :

Example 16 with LanternItemStack

use of org.lanternpowered.server.inventory.LanternItemStack 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 17 with LanternItemStack

use of org.lanternpowered.server.inventory.LanternItemStack 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 18 with LanternItemStack

use of org.lanternpowered.server.inventory.LanternItemStack in project LanternServer by LanternPowered.

the class ItemStackStore method deserialize.

@Override
public LanternItemStack deserialize(DataView dataView) throws InvalidDataException {
    final String identifier = dataView.getString(IDENTIFIER).get();
    final ItemType itemType = ItemRegistryModule.get().getById(identifier).orElseThrow(() -> new InvalidDataException("There is no item type with the id: " + identifier));
    final LanternItemStack itemStack = new LanternItemStack(itemType);
    deserialize(itemStack, dataView);
    return itemStack;
}
Also used : ItemType(org.spongepowered.api.item.ItemType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 19 with LanternItemStack

use of org.lanternpowered.server.inventory.LanternItemStack 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)

Example 20 with LanternItemStack

use of org.lanternpowered.server.inventory.LanternItemStack in project LanternServer by LanternPowered.

the class HandlerPlayInSignBook method handle.

@Override
public void handle(NetworkContext context, MessagePlayInSignBook message) {
    final LanternPlayer player = context.getSession().getPlayer();
    final AbstractSlot slot = player.getInventory().getHotbar().getSelectedSlot();
    ItemStack itemStack = slot.peek().orElse(null);
    if (itemStack != null && itemStack.getType() == ItemTypes.WRITABLE_BOOK) {
        final ItemStack itemStack1 = new LanternItemStack(ItemTypes.WRITTEN_BOOK);
        itemStack.getValues().forEach(itemStack1::offer);
        itemStack1.offer(Keys.BOOK_PAGES, message.getPages().stream().map(Text::of).collect(Collectors.toList()));
        itemStack1.offer(Keys.BOOK_AUTHOR, Text.of(message.getAuthor()));
        itemStack1.offer(Keys.DISPLAY_NAME, Text.of(message.getTitle()));
        slot.set(itemStack1);
    }
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)23 DataView (org.spongepowered.api.data.DataView)9 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)7 ArrayList (java.util.ArrayList)5 Optional (java.util.Optional)5 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)5 SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)5 Sponge (org.spongepowered.api.Sponge)4 ItemType (org.spongepowered.api.item.ItemType)4 Slot (org.spongepowered.api.item.inventory.Slot)4 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 Nullable (javax.annotation.Nullable)3 Lantern (org.lanternpowered.server.game.Lantern)3 Vector3d (com.flowpowered.math.vector.Vector3d)2 Streams (com.google.common.collect.Streams)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 List (java.util.List)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2