Saving and Loading Crafting Recipes

Discussion in 'Plugin Development' started by cdnyassuo34, Sep 8, 2019.

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

    cdnyassuo34

    Hi, I am creating a custom crafts plugin and I would like to know if it's possible to save and load Custom crafts that has been created.
    If it is how cand I make it ?
    thanks for any answers that will be given ^^
     
  2. What do you mean with 'save and load' ?
     
  3. Offline

    cdnyassuo34

    So, I am creating a custom craft plugin from a Menu. And i am using ShapedReciped method to create new crafts and everything work but when the server reload, all crafts disappear. So I am trying to make save and load new crafts. And i don't know if there is a way to create permanent crafts if yes how if no how to save crafts from a Menu?

    thanks for any new answers that will be given ^-^
     
  4. You can't save them permanently, you'll have to save the result and the ingredients in a config for example and load them when the server restarts
     
  5. Offline

    cdnyassuo34

    Do you have an idea of how I can save the ingredients and result ?
     
  6. Depends on what you want to save. You could save a StringList in the config which contains all the information about the result (such as ID/subId or Namespace, amount etc.) and each Material for the 9 craftingSlots.
    You can also save things like DisplayName, Lore, Enchantments.. you just have to find a good system which can be converted back to an ItemStack and some Materials. Just think about what you need to save, write an Example down and then create the methods for writing and reading these Strings
     
  7. Offline

    cdnyassuo34

    Hi, I tryied this to make a save-load but I have an error when loading the Crafts (made it myself so this might be the reason of the error)

    here is the Code and the error I had:
    Show Spoiler

    Code:
    //mat1^mat2^mat3^mat4^mat5^mat6^mat7^mat8^mat9 &  result_type^result_amout  &  ench^ench1°lvl^ench2°lvl^ench3°lvl^ench4°lvl  &  lore1^lore2^lore3^lore4^lore5^lore6^lore7^lore8^lore9
    
        public void SaveItem(Material mat1,Material mat2,Material mat3,Material mat4,Material mat5,Material mat6,Material mat7,Material mat8,Material mat9,ItemStack result)
        {
            String newString = "";String m1 = mat1 + "";String m2 = mat2 + "";String m3 = mat3 + "";String m4 = mat4 + "";String m5 = mat5 + "";String m6 = mat6 + "";String m7 = mat7 + "";String m8 = mat8 + "";String m9 = mat9 + "";
        
            String result_mat_amount = result.getType() + "^" + result.getAmount() + "^" + result.getItemMeta().getDisplayName();
            String enchants = "ench";String lores = "lore";
            for(Enchantment e : result.getItemMeta().getEnchants().keySet())
            {
                enchants = enchants + "^" + e.getName() + "°" + e.getKey();
            }
            for(String l : result.getItemMeta().getLore())
            {
                lores = lores + "^" + l;
            }
            newString = m1 + "^" + m2 + "^" + m3 + "^" + m4 + "^" + m5 + "^" + m6 + "^" + m7 + "^" + m8 + "^" + m9 + "&" + result_mat_amount + "&" + enchants + "&" + lores;
            crafts.add(newString);
            getConfig().set("crafts" , crafts);
            saveConfig();
        }
        public void loadCrafts(String s)
        {
        
            //0 = recette || 1 = result_mat_amout_displayName || 2 = enchants || 3 = lores
            String[] MainList = s.split("&");
        
            //recette
            String[] m = MainList[0].split("^");
            Material mat1 = Material.getMaterial(m[0]);
            Material mat2 = Material.getMaterial(m[1]);
            Material mat3 = Material.getMaterial(m[2]);
            Material mat4 = Material.getMaterial(m[3]);
            Material mat5 = Material.getMaterial(m[4]);
            Material mat6 = Material.getMaterial(m[5]);
            Material mat7 = Material.getMaterial(m[6]);
            Material mat8 = Material.getMaterial(m[7]);
            Material mat9 = Material.getMaterial(m[8]);
        
            List<String> lroes = new ArrayList<>();
            String[] enchan = MainList[2].split("^");
            String[] loress = MainList[2].split("^");
            for(int i = 1; i < loress.length; i++)
            {
                String[] lo = MainList[3].split("^");
                lroes.add(lo[i]);
            }
            String[] mat_amount = MainList[1].split("^");
            Material result_mat = Material.getMaterial(mat_amount[0]);
            int result_amount = Integer.parseInt(mat_amount[1]);
            ItemStack result = new ItemStack(result_mat , result_amount);
            ItemMeta result_meta = result.getItemMeta();
            result_meta.setDisplayName(mat_amount[2]);
            result_meta.setLore(lroes);
            for(int i = 1; i < enchan.length; i++)
            {
                String[] en = enchan[i].split("°");
            
                Enchantment e = Enchantment.getByName(en[0]);
                int lvll = Integer.parseInt(en[1]);
                result_meta.addEnchant(e, lvll, true);
            }
            result.setItemMeta(result_meta);
            ShapedRecipe recette = new ShapedRecipe(result);
            recette.shape(new String[] {"ABC","DEF","GHI"});
        
            recette.setIngredient('A', mat1);
            recette.setIngredient('B', mat2);
            recette.setIngredient('C', mat3);
        
            recette.setIngredient('D', mat4);
            recette.setIngredient('E', mat5);
            recette.setIngredient('F', mat6);
        
            recette.setIngredient('G', mat7);
            recette.setIngredient('H', mat8);
            recette.setIngredient('I', mat9);
        
            Bukkit.getServer().addRecipe(recette);
        }
    
    and here is the Error:
    Code:
    [18:29:04 INFO]: [SnowClaim] Enabling SnowClaim v1.0
    [18:29:04 ERROR]: Error occurred while enabling SnowClaim v1.0 (Is it up to date?)
    java.lang.ArrayIndexOutOfBoundsException: 4
            at fr.snowdingerr.snowclaim.mainclass.LoadData(mainclass.java:341) ~[?:?]
            at fr.snowdingerr.snowclaim.mainclass.onEnable(mainclass.java:107) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:265) ~[craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:339) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:409) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin(CraftServer.java:423) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins(CraftServer.java:349) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.reload(CraftServer.java:786) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.Bukkit.reload(Bukkit.java:613) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:28) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:684) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchServerCommand(CraftServer.java:670) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.handleCommandQueue(DedicatedServer.java:423) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:383) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:792) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:695) [craftbukkit-1.13.2.jar:git-Bukkit-84f3da3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    
     
    Last edited: Sep 10, 2019
  8. Should be:
    Code:Java
    1. List<String> lroes = new ArrayList<>(Arrays.asList(MainList[3].split("^"));
    2. //Your Lores have Index 3 not 2, no need for the for loop which was causing the error
    3. String[] enchan = MainList[2].split("^");
    4. /*String[] loress = MainList[2].split("^");
    5. for(int i = 1; i < loress.length; i++)
    6. {
    7. String[] lo = MainList[3].split("^");
    8. lroes.add(lo);
    9. }*/
    The loop for the enchantments should start at 0 not 1

    And just one more hint: if you post a stack trace also tell us which line it points to since we can't see your line numbers
     
Thread Status:
Not open for further replies.

Share This Page