Search code examples
javaminecraftbukkit

Bukkit/JavaPlugin - Setting outcome for existing crafting recipe not working


I'm new to Plugin Development for Bukkit

For my private Minecraft Server I want to add/change crafting recipes. I want to craft SlimeBlocks with just 4 SlimeBalls so here is what I got:

 @Override
public void onEnable() {

    ItemStack slimeBlockStack = new ItemStack(Material.SLIME_BLOCK);
    ShapedRecipe slimeBlockRecipe = new ShapedRecipe(slimeBlockStack);

    slimeBlockRecipe.shape("###", "#oo", "#oo");
    slimeBlockRecipe.setIngredient('o', Material.SLIME_BALL);
    slimeBlockRecipe.setIngredient('#', Material.AIR);

    getServer().addRecipe(slimeBlockRecipe);

//....here comes more   
}

Now so you cannot "cheat" slimeballs by crafting blocks with 4 and then crafting it back to 9 slimeballs I want to override the outcome of the existing crafting recipe - I tried to Iterate over a List with all Recipes and then setting the amount of the result, but it is not working...

Iterator<Recipe> it = Bukkit.getServer().recipeIterator();
    while(it.hasNext()) {
        ItemStack result = it.next().getResult();
        if(result.isSimilar(new ItemStack(Material.SLIME_BALL))) {

            result.setAmount(4);

        }
    }

What am I doing wrong, I apreciate every help/hint


Solution

  • I got it working by changing some things... Shapedrecipe only gives a clone/copy of the Recipe and not the actual one. I used the PrepareItemCraftEvent to change the outcome if a player wants to craft something:

    public class MyListener implements Listener {
    
    @EventHandler
    public void craftEvent(PrepareItemCraftEvent event) {
        ItemStack[] contents = event.getInventory().getContents();
        ItemStack firstInContents = contents[0];
    
        if((firstInContents.getType()==Material.SLIME_BALL) && (firstInContents.getAmount() == 9)) {
            firstInContents.setAmount(4);
        }
    
    }
    }
    

    in the onEnable method I registered my Listener getServer().getPluginManager().registerEvents(new MyListener(), this);