String in file to item name not working

Discussion in 'Plugin Development' started by JHG0, Dec 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    JHG0

    So I'm writing an inventory parser

    And I can't seem to get the item name, lore, and enchantments to copy over...Durability and id copy over but not the other stuff

    Here is the code that isn't working:
    Code:
    try{
                            name = (String) fConfig.get(i + ".name");
                            if(!name.equalsIgnoreCase("%noname%")){
                                item.getItemMeta().setDisplayName((String) fConfig.get(i + ".name"));
                            }
                        } catch (NullPointerException e){
                            name = (String) fConfig.get(i + ".name");
                            if(!name.equalsIgnoreCase("%noname%")){
                                item.getItemMeta().setDisplayName((String) fConfig.get(i + ".name"));
                            }
                        }
                        if(fConfig.get(i + ".lore") instanceof List && !fConfig.get(i + ".lore").equals("%nolore%")){
                            lore = (List<String>) fConfig.get(i + ".lore");
                            item.getItemMeta().setLore(lore);
                        }
                        if(fConfig.get(i + ".enchantments") instanceof Map && !fConfig.get(i + ".enchantments").equals("%noenchants%")){
                            for(int j = 0; j < fConfig.getInt(i + ".enchantments"); j++) {
                                int    enchantId = (Integer) fConfig.get(i + ".enchantments." + j + ".id");
                                int enchantLevel = (Integer) fConfig.get(i + ".enchantments." + j + ".level");
                                enchants.put(Enchantment.getById(enchantId), enchantLevel);
                            }
                        }
     
  2. Offline

    Malikk

    Changing something in item.getItemMeta doesn't update the original itemstack. Declare an ItemMeta object, edit it, and set it back to the original itemStack with ItemStack.setItemMeta(meta).

    Assuming your parser works, lol.
     
  3. Offline

    JHG0

    Malikk
    Wow...durr **bows head in blarg mode :p**

    Well I'll try that...it will be the FIRST inventory to string parser out there I believe :)

    Malikk

    Why won't this add the enchantment?
    Code:
    if(fConfig.get(i + ".enchantments.0.id") != null) {
                            for(int j = 0; fConfig.get(i + ".enchantments." + j + ".id") != null; j++) {
                                if(fConfig.get(i + ".enchantments." + j + ".id") == null){
                                    break;
                                } else {
                                    int    enchantId = (Integer) fConfig.get(i + ".enchantments." + j + ".id");
                                    int enchantLevel = (Integer) fConfig.get(i + ".enchantments." + j + ".level");
                                    item.addUnsafeEnchantment(Enchantment.getById(enchantId), enchantLevel);
                                }
                            }
                        }
    I did System.out.print(enchantId + " " + enchantLevel); and it loops through all the enchantments and prints them out...its just not adding the enchantment to the item :/

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    MayoDwarf

    What are you trying to do?
     
  5. Offline

    JHG0

    MayoDwarf
    Text from file to Enchantments

    It reads all enchantments...but doesn't add them to the item
     
  6. Offline

    MayoDwarf

    JHG0 When adding lores and display names it doesn't work with getItemMeta().setLore() and stuff. You have to do: ItemMeta im = itemname.getItemMeta(); //Then you set the lores and display names after this. Once you are done with them you do itemname.setItemMeta(im);
    EDIT: Remember to use im.setLore and im.setDisplayName
     
  7. Offline

    JHG0

    @MayoDwarf
    That was already resolved

    My problem is with the enchants
     
  8. Offline

    MayoDwarf

    JHG0 getById(), I think there is another method that gets it by the name, unless this is the method that gets it by name.
     
  9. Offline

    JHG0

    MayoDwarf
    Okay...
    After many debugs and tests...I found that the enchantments ARE being added...however, they "disappear" outside of the for loop
    Code:
                           for(int j = 0; fConfig.get(i + ".enchantments." + j + ".name") != null; j++) {
                                if(fConfig.get(i + ".enchantments." + j + ".name") == null){
                                    break;
                                } else {
                                    enchantName = fConfig.getString(i + ".enchantments." + j + ".name");
                                    enchantLevel = fConfig.getInt(i + ".enchantments." + j + ".level");
                                    ench = Enchantment.getByName(enchantName);
                                    try{
                                        item.addEnchantment(ench, enchantLevel);
                                    } catch (IllegalArgumentException exep){
                                        safe = false;
                                    }
                                    if(safe){
                                        item.addEnchantment(ench, enchantLevel);
                                    } else {
                                        item.addUnsafeEnchantment(ench, enchantLevel);
                                    }
                                }
                            }
     
  10. Offline

    JHG0

    Bump
     
Thread Status:
Not open for further replies.

Share This Page