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