Search in sources :

Example 6 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project HuskyCrates-Sponge by codeHusky.

the class CrateRewardHolderParser method itemFromNode.

private static ItemStack itemFromNode(ConfigurationNode itemRoot) {
    try {
        ItemStack item = ItemStack.builder().itemType(itemRoot.getNode("id").getValue(TypeToken.of(ItemType.class))).quantity(itemRoot.getNode("count").getInt(1)).add(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString())).build();
        if (!itemRoot.getNode("variant").isVirtual()) {
        //if(Sponge.getRegistry().getType(TreeType.class,itemRoot.getNode("variant").getString()).isPresent()) {
        //System.out.println(item.offer(Keys.TREE_TYPE,getTreeType(itemRoot.getNode("variant").getString("oak"))));
        //System.out.println(itemRoot.getNode("variant").getValue());
        //}
        }
        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<ItemEnchantment> enchantments = new ArrayList<>();
            for (Object key : itemRoot.getNode("enchants").getChildrenMap().keySet()) {
                int level = itemRoot.getNode("enchants").getChildrenMap().get(key).getInt();
                String enchantID = (String) key;
                // STRINGS ONLY!
                Enchantment enc = getEnchantment(enchantID);
                ItemEnchantment itemEnchantment = new ItemEnchantment(enc, level);
                enchantments.add(itemEnchantment);
            }
            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();
        }
        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 : ItemType(org.spongepowered.api.item.ItemType) BiMap(com.google.common.collect.BiMap) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) LinkedHashMap(java.util.LinkedHashMap) ItemEnchantment(org.spongepowered.api.data.meta.ItemEnchantment) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemEnchantment(org.spongepowered.api.data.meta.ItemEnchantment) Enchantment(org.spongepowered.api.item.Enchantment) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)6 IOException (java.io.IOException)5 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)4 Location (org.spongepowered.api.world.Location)3 World (org.spongepowered.api.world.World)3 JSONObject (org.json.JSONObject)2 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 Entity (org.spongepowered.api.entity.Entity)2 ArmorStand (org.spongepowered.api.entity.living.ArmorStand)2 Listener (org.spongepowered.api.event.Listener)2 PhysicalCrate (pw.codehusky.huskycrates.crate.PhysicalCrate)2 BiMap (com.google.common.collect.BiMap)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Consumer (java.util.function.Consumer)1 ItemEnchantment (org.spongepowered.api.data.meta.ItemEnchantment)1 Enchantment (org.spongepowered.api.item.Enchantment)1 ItemType (org.spongepowered.api.item.ItemType)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1