Solved Blocking and making custom recipe

Discussion in 'Spigot Plugin Development' started by Strum, May 25, 2018.

Thread Status:
Not open for further replies.
  1. Hello i'm trying to make a custom recipe for the diamond axe while blocking the original one here is the code for it:
    Code (Text):
    package com.customrecipe.main;

    import java.util.Iterator;

    import org.bukkit.Material;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{

        public void onEnable() {
        loadRecipe3();
         
            System.out.println("Custom recipes enabled");
         
         
         

         
         
            while(it.hasNext())
            {
            recipe = it.next();
            if (recipe != null && recipe.getResult().getType() == Material.DIAMOND_AXE)
            {
            it.remove();
            }  
         
     
    }
        }


         



      private void loadRecipe3() {
         
            ItemStack item = new ItemStack(Material.DIAMOND_AXE);
         
            ShapedRecipe recipe = new ShapedRecipe(item);
         
         

        recipe.shape("%% ","%& "," & ");
     
     
         
            recipe.setIngredient('%', Material.DIAMOND_BLOCK);
            recipe.setIngredient('&', Material.STICK);
         
         
        this.getServer().addRecipe(recipe);  
         
     
     
        }








    }
    The custom recipe for the diamond axe works but the original one isn't getting blocked, i'm getting no errors.
    And as a bonus do any of you know how I could set the default damage of the diamond axe to 7 damage?

    Thanks for the help.

    Strum.
     
  2. NathanWolf

    Supporter

    Are you sure you aren't getting any errors? it.remove throws an exception in recent Spigot versions.
     
  3. No, no errors at all. This is the full plugin maybe something is wrong with other stuffed which I removed in the OP because it was irrelevant to what I needed but there it is (and it's for spigot 1.8.8):

    Code (Java):

    package com.customrecipe.main;

    import java.util.Iterator;

    import org.bukkit.Material;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{

        public void onEnable() {
         
            loadRecipe();
            loadRecipe2();
            loadRecipe3();
            System.out.println("Custom recipes enabled");
           
           
            Iterator<Recipe> it = getServer().recipeIterator();
            Recipe recipe;
            while(it.hasNext())
            {
            recipe = it.next();
            if (recipe != null && recipe.getResult().getType() == Material.DIAMOND_AXE
                    )
            {
            it.remove();
            }  
           
       
    }

           
           
            while(it.hasNext())
            {
            recipe = it.next();
            if (recipe != null && recipe.getResult().getType() == Material.DIAMOND_AXE)
            {
            it.remove();
            }  
           
       
    }
        }


            private void loadRecipe() {
               
                ItemStack item = new ItemStack(Material.SMOOTH_BRICK);
               
                ShapedRecipe recipe = new ShapedRecipe(item);
               
                recipe.shape("%% ","%% ","   ");
                recipe.shape("%% ","%% ","   ");
               
               
                recipe.setIngredient('%', Material.STONE);
               
            this.getServer().addRecipe(recipe);  
               
           
           
            }

      private void loadRecipe2() {
               
                ItemStack item = new ItemStack(Material.SMOOTH_BRICK);
               
                ShapedRecipe recipe = new ShapedRecipe(item);
               
               

            recipe.shape("   "," %%"," %%");
            recipe.shape("   "," %%"," %%");

               
                recipe.setIngredient('%', Material.STONE);
               
            this.getServer().addRecipe(recipe);  
               
           
           
            }



      private void loadRecipe3() {
           
            ItemStack item = new ItemStack(Material.DIAMOND_AXE);
           
            ShapedRecipe recipe = new ShapedRecipe(item);
           
           

        recipe.shape("%% ","%& "," & ");
       
       
           
            recipe.setIngredient('%', Material.DIAMOND_BLOCK);
            recipe.setIngredient('&', Material.STICK);
           
           
        this.getServer().addRecipe(recipe);  
           
       
       
        }














    }
     
     
  4. Alright I got it to block it but I think it also blocks the custom recipe to make the diamond axe, any ideas to fix that?
     
  5. NathanWolf

    Supporter

    Oh, 1.8, yeah still works there.

    Add your recipes after removing the default ones, just switch the order of those two blocks of code. Your removal code is probably catching the recipes you added.

    And note this won't work if you ever update to some non-ancient version of Spigot.
     
  6. I tried puting the disabling of the recipe after the custom recipe but it still disables both.
     
  7. anyone got an idea?
     
  8. NathanWolf

    Supporter

    Was this a typo? Did you mean you moved adding the custom recipes after removing the default ones?
     
  9. No, I removed the default one to then add the custom recipe, here it the code:


    Code (Text):
    package com.txrecipes.plugin;

    import java.util.Iterator;

    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{




            public void onEnable() {
               
                //Custom recipes
               
               
               
               
                   

                    ItemStack Axe = new ItemStack(Material.DIAMOND_AXE);
           
                    ShapedRecipe Recipee = new ShapedRecipe(Axe);
                   
                    //1
               
                    Recipee.shape("%% ","%& "," & ");
               
               
                   
                    Recipee.setIngredient('%', Material.DIAMOND_BLOCK);
                    Recipee.setIngredient('&', Material.STICK);
                   
                   
                    this.getServer().addRecipe(Recipee);  
               
                    //2
               
                    Recipee.shape(" %%"," &%"," & ");
               
               
                   
                    Recipee.setIngredient('%', Material.DIAMOND_BLOCK);
                    Recipee.setIngredient('&', Material.STICK);
                   
                   
                    this.getServer().addRecipe(Recipee);  
               
               
               
               
               
               
               
               
                    //disable recipes
                   
                   
                   
                   
                    Iterator<Recipe> it = getServer().recipeIterator();
                    Recipe recipe1;
                    while(it.hasNext())
                    {
                    recipe1 = it.next();
                    if (recipe1 != null && recipe1.getResult().getType() == Material.DIAMOND_AXE
                            )
                    {
                    it.remove();
                    }  
                   
               
            }
               
               
               
               
               
               
               
               
               
               
            }
































    }
     
    This disables both recipes.
     
Thread Status:
Not open for further replies.