Search in sources :

Example 26 with ObjectMappingException

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;
}
Also used : Pattern(java.util.regex.Pattern) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) BlockType(org.spongepowered.api.block.BlockType) Matcher(java.util.regex.Matcher) ItemType(org.spongepowered.api.item.ItemType) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 27 with ObjectMappingException

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();
}
Also used : Optional(java.util.Optional) DataBuilder(org.spongepowered.api.data.persistence.DataBuilder) DataManager(org.spongepowered.api.data.DataManager) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 28 with ObjectMappingException

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()));
        }
    }
}
Also used : TypeSerializer(ninja.leaping.configurate.objectmapping.serialize.TypeSerializer) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode)

Example 29 with ObjectMappingException

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);
    }
}
Also used : GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 30 with ObjectMappingException

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;
}
Also used : Optional(java.util.Optional) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) ItemType(org.spongepowered.api.item.ItemType) BiMap(com.google.common.collect.BiMap) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) LinkedHashMap(java.util.LinkedHashMap) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)30 IOException (java.io.IOException)21 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)10 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)8 Map (java.util.Map)7 GsonConfigurationLoader (ninja.leaping.configurate.gson.GsonConfigurationLoader)6 Optional (java.util.Optional)5 Detection (com.ichorpowered.guardianapi.detection.Detection)4 Path (java.nio.file.Path)4 Set (java.util.Set)4 Text (org.spongepowered.api.text.Text)4 Maps (com.google.common.collect.Maps)3 ConfigurationAssignment (com.ichorpowered.guardian.content.assignment.ConfigurationAssignment)3 GuardianMapValue (com.ichorpowered.guardian.util.item.mutable.GuardianMapValue)3 GuardianValue (com.ichorpowered.guardian.util.item.mutable.GuardianValue)3 ContentContainer (com.ichorpowered.guardianapi.content.ContentContainer)3 ContentKey (com.ichorpowered.guardianapi.content.key.ContentKey)3 DetectionContentLoader (com.ichorpowered.guardianapi.detection.DetectionContentLoader)3 Key (com.ichorpowered.guardianapi.util.item.key.Key)3 MapValue (com.ichorpowered.guardianapi.util.item.value.mutable.MapValue)3