PrepareItemCraftEvent - want to craft item, with named and lored ingredients

Discussion in 'Plugin Development' started by karer, Jun 30, 2013.

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

    karer

    Hi!

    First, I wil show my code:
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    2. public void onPrepareItemCraft(PrepareItemCraftEvent event)
    3. {
    4. ItemStack[] inv = event.getInventory().getMatrix();
    5.  
    6. for(ItemStack ingr : inv){
    7. if(ingr.getType() == Material.SPONGE){
    8. //ingr to itemstack, mozemy na nim operowac
    9. ItemMeta ingr_meta = ingr.getItemMeta();
    10. String ingr_name = ingr_meta.getDisplayName();
    11. //Player plr = (Player) event.getView().getPlayer();
    12. //List<String> lore = ingr_meta.getLore();
    13. if(ingr_name != "Swiety kamien"){
    14. event.getInventory().setResult(new ItemStack(Material.AIR));
    15. return;
    16. }
    17.  
    18. }
    19. }
    20. }


    So... I have new craft shape (4x sponge -> blaze rod with name), but i want to make blaze rod with name ONLY with named and lored 4x sponge (it drops from mobs). Code works that: if in crafting table is sponge, but with no-name, result will be stone. But it doesn't work ;c

    No errors, but when I insert 4x sponge with name "Swiety kamien", in result is air :(
    Sorry for my bad english, but i think, that you can read this ;D

    Regards,
    karer
     
  2. Offline

    lulzsun

    I had the same problem as you did, and I managed to get it to work, here you go :)

    Code:java
    1.  
    2. @EventHandler(priority=EventPriority.HIGH)
    3. public void customCrafting(PrepareItemCraftEvent e){
    4. if( e.getInventory().getResult().getType() == Material.ANVIL){
    5. for( int i = 0 ; i < e.getInventory().getSize() ; i++ ){
    6. final ItemStack item = e.getInventory().getItem(i);
    7. if(e.getInventory().getItem(i) != null){
    8. if(item.getType() == Material.GHAST_TEAR){
    9. if(item.getItemMeta().getDisplayName().contains("Puppies")){
    10. e.getInventory().setResult(new ItemStack(Material.LEATHER, 1));
    11. }
    12. }
    13. }
    14. }
    15. }
    16. }
    17.  


    This is what it does: If the result of the crafting recipe is an anvil, it will check the recipe for a "GHAST_TEAR" (which is part of the recipe) and if it has the name "Puppies" then it will change the recipe result into LEATHER.

    I hope you understand and make use out of this :D
     
Thread Status:
Not open for further replies.

Share This Page