Plugin somehow saves data to config

Discussion in 'Plugin Development' started by NIK220V, Dec 20, 2015.

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

    NIK220V

    Title is strange because i don't really know how to explain this.
    But i'll try my best.

    What i'm trying to do: get ItemStack from config, then edit something in it, and return edited ItemStack for my things.

    Code:
    private ItemStack Itemc(int amt){
             int oneprice = 1;
             ItemStack Item = plugin.getItem(path);
             Item.setAmount(amt);
             List<String> AMLore;
             if (Item.getItemMeta().getLore() != null) {AMLore = Item.getItemMeta().getLore();AMLore.add(this.plugin.c("Shop.Itemlore").replace("%price", Integer.toString(amt * oneprice)).replace("%amount", Integer.toString(amt)));} else {AMLore = Arrays.asList(new String[] { this.plugin.c("Shop.Itemlore").replace("%price", Integer.toString(amt * oneprice)).replace("%amount", Integer.toString(amt)) });}                 
             ItemMeta Meta = Item.getItemMeta();
             Meta.setLore(AMLore);
             Item.setItemMeta(Meta);   
             return Item;
            }
    Variable path is initialized in my class, don't worry about it. What's in the lore isn't necesarry, it will duplicate it anyway.
    This code actually works fine... Really, i think this is fine, but i'm not sure.

    So, how i get the ItemStack: (Logger for debugging)
    Code:
    public ItemStack getItem(String path){
           getLogger().info("Returned that: "+this.signs.get(path+".Item").toString());
           return (ItemStack)this.signs.get(path+".Item");
       }
    path.Item is path to the stored ItemStack. signs is the YamlConfiguration. Sure this is fine.
    It returns things. Yeah. Works. First time it returns exactly what i need. Something like ItemStack{SIGN x 1}. If i added sign to config, of course.
    But next time.. next time it returns same item, but with lore. With lore from Itemc method. Note, Itemc just returns item, it didn't save anything to my config..
    I have only one void method that edits the config. And it wasn't activated.
    When i check my config, it really has lore in the itemstack meta. I can't realize how this happens, because THE ONLY ONE method that edits signs file wasn't activated.

    So, total:
    For example, i had Sign. Just sign in the config. Method returned me my sign, with lore, that method added.
    Every next time, i get sign + x(depends on how much times i called Itemc, 1 per each) lore from itemc + lore from current Itemc. AND. section "x(depends on how much times i called Itemc, 1 per each) lore from itemc" IS IN THE CONFIG with my sign. But THE ONLY ONE event, that can possible edit file with my item wasn't activated.
     
  2. Offline

    Zombie_Striker

    @NIK220V
    It looks like you want a way to save an Itemstack to a config, and a way to get it out.
    Code:
    String itemstackString = Itemstack.deserialize;
    
    Itemstack itemstack = Itemstack.serialize(string);
     
  3. Offline

    NIK220V

    @Zombie_Striker
    I actually saved it, and actually got it out.
    This sections is fine.
    Did you read the problem?
     
  4. Offline

    mythbusterma

    @NIK220V

    He's suggesting a way to improve that code so that it is actually readable.

    Also, make your variables lower case. It's nearly impossible to read that code.
     
  5. Offline

    Tirco

    So, if i understood this correctly, if you start by storing an item with lore, and everytime you call for it, the lore duplicates?

    I can't see any way for this to happen with the current code provided. (then again, I'm an amateur coder)
    What event are you using to store the lore in the first place? There's no way for it to retrigger at all?
     
Thread Status:
Not open for further replies.

Share This Page