Solved How to remove recipes?

Discussion in 'Plugin Development' started by Spectre93, Jan 13, 2013.

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

    Spectre93

    How does one remove a recipe?
    getServer().resetRecipes() and getServer().clearRecipes() remove the recipes of all plugins if I'm correct.

    Somewhere I found that I should the recipeIterator(), but I think I'm not using it correctly as there was no good example to be found.

    Code:
    public void removeRecipe(Recipe recipe){   
            while(getServer().recipeIterator().hasNext()){
                Recipe recp = getServer().recipeIterator().next();
               
                if(recp.equals(recipe)){
                    getServer().recipeIterator().remove();
                }
               
            }
    Now that I think of it... I try to use this to remove a ShapedRecipe, maybe that's the problem.

    The thing is, the loop runs forever for some reason. Can anyone tell me how to remove (Shaped) recipes properly?

    Shameless self-bump :3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    Ugleh

  3. Offline

    DJSanderrr

    Maybe this works;
    If you know how to add a recipe,
    Add one in the shape you want to disable,
    RESULT = New ItemStack(Material.AIR);
     
  4. Offline

    Cjreek

    In my experience the first recipe always overrides the second one.
     
  5. Offline

    desht

    Spectre93 there isn't a method to directly remove a recipe, but as you noted, you can use recipeIterator() from Server to iterate over all known recipes. Where you're going wrong is creating a new Iterator on every pass of your while loop, so hasNext() will always be returning true.

    PHP:
    Iterator<Recipeiter getServer().recipeIterator();
    while (
    iter.hasNext()) {
      
    Recipe r iter.next();
      
    // May not be safe to depend on == here for recipe comparison
      // Probably safer to compare the recipe result (an ItemStack)
      
    if (== unwantedRecipe) {
        
    iter.remove();
      }
    }
     
  6. Offline

    Spectre93

    How stupid of me! Thank you very much!

    For everyone interested in the solution:

    PHP:
    public void removeRecipe(ShapedRecipe inputRecipe){
            
    Iterator<Recipeit getServer().recipeIterator();
            while(
    it.hasNext()){
                
    Recipe itRecipe it.next();
                if(
    itRecipe instanceof ShapedRecipe){
                    
    ShapedRecipe itShaped = (ShapedRecipeitRecipe;
                 
                    
    Map<CharacterItemStackitShaped.getIngredientMap();
                    
    Map<CharacterItemStackinputRecipe.getIngredientMap();
                 
                    if(
    m.values().containsAll(n.values())){
                        
    String[] list = itShaped.getShape();
                        
    String listString = list[0] + list[1] + list[2];
                     
                        
    String[] list2 inputRecipe.getShape();
                        
    String listString2 list2[0] + list2[1] + list2[2];
                     
                        for(
    int i 0listString.length(); i++){
                            if(!
    m.get(listString.charAt(i)).equals(n.get(listString2.charAt(i)))){
                                
    getLogger().fine("Recipe not found.");
                                return;
                            }
                        } 
                        
    it.remove();
                        
    getLogger().fine("Recipe removed!");
                    }
                }
            }
        }
    This code can be easily adjusted to fit non-ShapedRecipes as well.
    You may use this piece of code on one condition: if you have any improvements, let me know.
     
  7. Offline

    wezzyFTW

    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. Iterator<Recipe> it = getServer().recipeIterator();
    5. Recipe recipe;
    6. while(it.hasNext())
    7. {
    8. recipe = it.next();
    9. if (recipe != null && recipe.getResult().getType() == Material.BREAD)
    10. {
    11. it.remove();
    12. }
    13. }
    14. }

    I think its much easier to just test if the outcome of the recipe is the same material as what you want to block like did up there^.
    Bread has three different shaped recipes(I think) and this saves time and code. Just like desht said.
     
Thread Status:
Not open for further replies.

Share This Page