use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project AdamantineShield by Karanum.
the class LookupLine method getHoverText.
public Text getHoverText() {
Text result = Text.of(TextColors.DARK_AQUA, "Location: ", TextColors.AQUA, pos.toString());
if (data == null)
return result;
ConfigurationNode workingNode = null;
ConfigurationNode node = null;
try {
node = DataUtils.configNodeFromString(data);
} catch (IOException e) {
e.printStackTrace();
return result;
}
// Common tags
workingNode = node.getNode("UnsafeDamage");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Damage: ", TextColors.AQUA, workingNode.getInt());
}
// Item exclusive tags
if (target instanceof ItemType) {
workingNode = node.getNode("UnsafeData", "SkullOwner");
if (!workingNode.isVirtual()) {
String name = workingNode.getString();
if (!workingNode.getNode("Name").isVirtual())
name = workingNode.getNode("Name").getString();
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Player: ", TextColors.AQUA, name);
}
workingNode = node.getNode("UnsafeData", "display");
if (!workingNode.isVirtual()) {
ConfigurationNode innerNode = workingNode.getNode("Name");
if (!innerNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Name: ", TextColors.AQUA, innerNode.getString());
}
innerNode = workingNode.getNode("color");
if (!innerNode.isVirtual()) {
int color = innerNode.getInt();
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Color: ", TextColors.AQUA, "(", TextColors.RED, color >> 16, TextColors.AQUA, ", ", TextColors.GREEN, (color >> 8) % 256, TextColors.AQUA, ", ", TextColors.BLUE, color % 256, TextColors.AQUA, ")");
}
innerNode = workingNode.getNode("Lore");
if (!innerNode.isVirtual()) {
try {
Text sub = Text.of(TextColors.DARK_AQUA, "Lore: ");
for (String line : innerNode.getList(TypeToken.of(String.class))) {
sub = Text.of(sub, Text.NEW_LINE, TextColors.DARK_AQUA, " - ", TextColors.AQUA, line);
}
result = Text.of(result, Text.NEW_LINE, sub);
} catch (ObjectMappingException e) {
e.printStackTrace();
}
}
}
workingNode = node.getNode("UnsafeData", "title");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Title: ", TextColors.AQUA, workingNode.getString());
}
workingNode = node.getNode("UnsafeData", "author");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Author: ", TextColors.AQUA, workingNode.getString());
}
workingNode = node.getNode("UnsafeData", "Unbreakable");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.AQUA, "Is unbreakable");
}
}
// Block exclusive tags
if (target instanceof BlockType) {
workingNode = node.getNode("UnsafeData", "Owner", "Name");
if (!workingNode.isVirtual() && !workingNode.getString().isEmpty()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Player: ", TextColors.AQUA, workingNode.getString());
}
workingNode = node.getNode("UnsafeData", "CustomName");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Name: ", TextColors.AQUA, workingNode.getString());
}
workingNode = node.getNode("UnsafeData", "Text1");
if (!workingNode.isVirtual()) {
ConfigurationNode dataNode = node.getNode("UnsafeData");
if (!(dataNode.getNode("Text2").isVirtual() || dataNode.getNode("Text3").isVirtual() || dataNode.getNode("Text4").isVirtual())) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Text:");
Pattern p = DataUtils.SIGN_TEXT_REGEX;
for (int i = 1; i <= 4; ++i) {
Matcher m = p.matcher(dataNode.getNode("Text" + i).getString());
if (m.matches()) {
result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, " - ", TextColors.AQUA, m.group(1));
}
}
} else {
result = Text.of(result, Text.NEW_LINE, TextColors.RED, "Contains incomplete sign data");
}
}
workingNode = node.getNode("UnsafeData", "Lock");
if (!workingNode.isVirtual()) {
result = Text.of(result, Text.NEW_LINE, TextColors.AQUA, "Is locked");
}
}
return result;
}
use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project LanternServer by LanternPowered.
the class DataSerializableTypeSerializer method deserialize.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DataSerializable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
final DataManager dataManager = Sponge.getDataManager();
final Optional<DataBuilder<?>> builderOpt = (Optional) dataManager.getBuilder(type.getRawType().asSubclass(DataSerializable.class));
if (!builderOpt.isPresent()) {
throw new ObjectMappingException("No data builder is registered for " + type);
}
final Optional<? extends DataSerializable> built = builderOpt.get().build(ConfigurateTranslator.instance().translate(value));
if (!built.isPresent()) {
throw new ObjectMappingException("Unable to build instance of " + type);
}
return built.get();
}
use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project LanternServer by LanternPowered.
the class MultimapTypeSerializer method serialize.
@Override
public void serialize(TypeToken<?> type, Multimap obj, ConfigurationNode node) throws ObjectMappingException {
TypeToken<?> key = type.resolveType(Multimap.class.getTypeParameters()[0]);
TypeToken<?> value = type.resolveType(Multimap.class.getTypeParameters()[1]);
TypeSerializer keySerial = node.getOptions().getSerializers().get(key);
TypeSerializer valueSerial = node.getOptions().getSerializers().get(value);
if (keySerial == null) {
throw new ObjectMappingException("No type serializer available for type " + key);
}
if (valueSerial == null) {
throw new ObjectMappingException("No type serializer available for type " + value);
}
node.setValue(ImmutableMap.of());
for (Object k : obj.keySet()) {
for (Object v : obj.get(k)) {
SimpleConfigurationNode keyNode = SimpleConfigurationNode.root();
keySerial.serialize(key, k, keyNode);
valueSerial.serialize(value, v, node.getNode(keyNode.getValue()));
}
}
}
use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project LanternServer by LanternPowered.
the class TextTypeSerializer method serialize.
@Override
public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException {
final String json = TextSerializers.JSON.serialize(obj);
final GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder().setSource(() -> new BufferedReader(new StringReader(json))).build();
try {
value.setValue(gsonLoader.load());
} catch (IOException e) {
throw new ObjectMappingException(e);
}
}
use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project HuskyCrates-Sponge by codeHusky.
the class CrateConfigParser method itemFromNode.
private static ItemStack itemFromNode(ConfigurationNode itemRoot) {
try {
if (itemRoot.getNode("id").isVirtual()) {
HuskyCrates.instance.logger.error("NO ITEM ID: " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
ItemType type;
Integer dmg = null;
try {
type = itemRoot.getNode("id").getValue(TypeToken.of(ItemType.class));
} catch (Exception e) {
String id = itemRoot.getNode("id").getString();
String[] parts = id.split(":");
try {
if (parts.length == 3) {
id = parts[0] + ":" + parts[1];
dmg = Integer.parseInt(parts[2]);
} else if (parts.length == 2) {
id = parts[0];
dmg = Integer.parseInt(parts[1]);
}
} catch (Exception ee) {
HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
Optional<ItemType> optType = Sponge.getRegistry().getType(ItemType.class, id);
if (optType.isPresent()) {
type = optType.get();
} else {
HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
}
ItemStack item = ItemStack.builder().itemType(type).quantity(itemRoot.getNode("count").getInt(1)).build();
if (!itemRoot.getNode("name").isVirtual()) {
item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
}
if (!itemRoot.getNode("lore").isVirtual()) {
ArrayList<Text> lore = new ArrayList<>();
for (String ll : itemRoot.getNode("lore").getList(TypeToken.of(String.class))) {
lore.add(TextSerializers.FORMATTING_CODE.deserialize(ll));
}
item.offer(Keys.ITEM_LORE, lore);
}
if (!itemRoot.getNode("name").isVirtual()) {
item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
}
if (!itemRoot.getNode("enchants").isVirtual()) {
ArrayList<Enchantment> enchantments = new ArrayList<>();
for (Object key : itemRoot.getNode("enchants").getChildrenMap().keySet()) {
int level = itemRoot.getNode("enchants").getChildrenMap().get(key).getInt();
String enchantID = (String) key;
Optional<EnchantmentType> pEnchantType = Sponge.getRegistry().getType(EnchantmentType.class, enchantID);
if (!pEnchantType.isPresent()) {
HuskyCrates.instance.logger.error("INVALID ENCHANT ID: \"" + key + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
return ItemStack.of(ItemTypes.NONE, 1);
}
Enchantment pEnchant = Enchantment.of(pEnchantType.get(), level);
enchantments.add(pEnchant);
}
item.offer(Keys.ITEM_ENCHANTMENTS, enchantments);
}
if (!itemRoot.getNode("damage").isVirtual()) {
// HuskyCrates.instance.logger.info("damage override called");
item = ItemStack.builder().fromContainer(// OVERRIDE DAMAGE VAL! :)
item.toContainer().set(DataQuery.of("UnsafeDamage"), itemRoot.getNode("damage").getInt(0))).build();
} else if (dmg != null) {
item = ItemStack.builder().fromContainer(// OVERRIDE DAMAGE VAL! :)
item.toContainer().set(DataQuery.of("UnsafeDamage"), dmg)).build();
}
if (!itemRoot.getNode("nbt").isVirtual()) {
// nbt overrrides
LinkedHashMap items = (LinkedHashMap) itemRoot.getNode("nbt").getValue();
if (item.toContainer().get(DataQuery.of("UnsafeData")).isPresent()) {
BiMap real = ((BiMap) item.toContainer().getMap(DataQuery.of("UnsafeData")).get());
items.putAll(real);
}
// System.out.println(item.toContainer().get(DataQuery.of("UnsafeData")).get().getClass());
item = ItemStack.builder().fromContainer(item.toContainer().set(DataQuery.of("UnsafeData"), items)).build();
}
// item.offer(Keys.PICKUP_DELAY,itemRoot.getNode("pickupdelay").getInt())
return item;
} catch (ObjectMappingException e) {
e.printStackTrace();
}
return null;
}
Aggregations