Search in sources :

Example 1 with NucleusItemStackSnapshot

use of io.github.nucleuspowered.nucleus.configurate.wrappers.NucleusItemStackSnapshot in project Nucleus by NucleusPowered.

the class NucleusItemStackSnapshotSerialiser method deserialize.

@Override
public NucleusItemStackSnapshot deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    // Process enchantments, temporary fix before Sponge gets a more general fix in.
    boolean emptyEnchant = false;
    ConfigurationNode ench = value.getNode("UnsafeData", "ench");
    if (!ench.isVirtual()) {
        List<? extends ConfigurationNode> enchantments = ench.getChildrenList();
        if (enchantments.isEmpty()) {
            // Remove empty enchantment list.
            value.getNode("UnsafeData").removeChild("ench");
        } else {
            enchantments.forEach(x -> {
                try {
                    short id = Short.parseShort(x.getNode("id").getString());
                    short lvl = Short.parseShort(x.getNode("lvl").getString());
                    x.getNode("id").setValue(id);
                    x.getNode("lvl").setValue(lvl);
                } catch (NumberFormatException e) {
                    x.setValue(null);
                }
            });
        }
    }
    ConfigurationNode data = value.getNode("Data");
    if (!data.isVirtual() && data.hasListChildren()) {
        List<? extends ConfigurationNode> n = data.getChildrenList().stream().filter(x -> !x.getNode("DataClass").getString("").endsWith("SpongeEnchantmentData") || (!x.getNode("ManipulatorData", "ItemEnchantments").isVirtual() && x.getNode("ManipulatorData", "ItemEnchantments").hasListChildren())).collect(Collectors.toList());
        emptyEnchant = n.size() != data.getChildrenList().size();
        if (emptyEnchant) {
            if (n.isEmpty()) {
                value.removeChild("Data");
            } else {
                value.getNode("Data").setValue(n);
            }
        }
    }
    DataContainer dataContainer = DataTranslators.CONFIGURATION_NODE.translate(value);
    Set<DataQuery> ldq = dataContainer.getKeys(true);
    for (DataQuery dataQuery : ldq) {
        String el = dataQuery.asString(".");
        if (el.contains("$Array$")) {
            try {
                Tuple<DataQuery, Object> r = TypeHelper.getArray(dataQuery, dataContainer);
                dataContainer.set(r.getFirst(), r.getSecond());
            } catch (Exception e) {
                e.printStackTrace();
            }
            dataContainer.remove(dataQuery);
        }
    }
    ItemStack snapshot;
    try {
        snapshot = ItemStack.builder().fromContainer(dataContainer).build();
    } catch (Exception e) {
        return NucleusItemStackSnapshot.NONE;
    }
    if (emptyEnchant) {
        snapshot.offer(Keys.ITEM_ENCHANTMENTS, Lists.newArrayList());
        return new NucleusItemStackSnapshot(snapshot.createSnapshot());
    }
    if (snapshot.get(Keys.ITEM_ENCHANTMENTS).isPresent()) {
        // Reset the data.
        snapshot.offer(Keys.ITEM_ENCHANTMENTS, snapshot.get(Keys.ITEM_ENCHANTMENTS).get());
        return new NucleusItemStackSnapshot(snapshot.createSnapshot());
    }
    return new NucleusItemStackSnapshot(snapshot.createSnapshot());
}
Also used : TypeHelper(io.github.nucleuspowered.nucleus.util.TypeHelper) Keys(org.spongepowered.api.data.key.Keys) DataContainer(org.spongepowered.api.data.DataContainer) Set(java.util.Set) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) TypeToken(com.google.common.reflect.TypeToken) NucleusItemStackSnapshot(io.github.nucleuspowered.nucleus.configurate.wrappers.NucleusItemStackSnapshot) Tuple(org.spongepowered.api.util.Tuple) TypeSerializer(ninja.leaping.configurate.objectmapping.serialize.TypeSerializer) DataQuery(org.spongepowered.api.data.DataQuery) Collectors(java.util.stream.Collectors) ItemStack(org.spongepowered.api.item.inventory.ItemStack) List(java.util.List) Lists(com.google.common.collect.Lists) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) DataTranslators(org.spongepowered.api.data.persistence.DataTranslators) Map(java.util.Map) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) NucleusItemStackSnapshot(io.github.nucleuspowered.nucleus.configurate.wrappers.NucleusItemStackSnapshot) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) DataContainer(org.spongepowered.api.data.DataContainer) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) DataQuery(org.spongepowered.api.data.DataQuery) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

Lists (com.google.common.collect.Lists)1 TypeToken (com.google.common.reflect.TypeToken)1 NucleusItemStackSnapshot (io.github.nucleuspowered.nucleus.configurate.wrappers.NucleusItemStackSnapshot)1 TypeHelper (io.github.nucleuspowered.nucleus.util.TypeHelper)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 TypeSerializer (ninja.leaping.configurate.objectmapping.serialize.TypeSerializer)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataQuery (org.spongepowered.api.data.DataQuery)1 Keys (org.spongepowered.api.data.key.Keys)1 DataTranslators (org.spongepowered.api.data.persistence.DataTranslators)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 Tuple (org.spongepowered.api.util.Tuple)1