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);
}
}
}
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);
}
}
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);
}
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;
}
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);
}
Aggregations