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