Solved Anvil Recipes

Discussion in 'Plugin Development' started by 97WaterPolo, Oct 11, 2014.

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

    97WaterPolo

    Is it possibly to create an anvil recipe? IE I put a sword in, and a blaze powder, the result is a sword with fire aspect. Any help would be appreciated!
     
  2. Offline

    GeorgeeeHD

  3. Offline

    97WaterPolo

    GeorgeeeHD
    I was under the impression that Shaped only work in crafting tables?
     
  4. Offline

    GeorgeeeHD

  5. Offline

    Code0

    GeorgeeeHD 97WaterPolo

    After reading the docks, I can assure you that they (at least the shapedrecipes George was talking about) only work for workbenches
     
  6. Offline

    97WaterPolo

    Code0
    That is what I thought, is it possible to listen to inventory click event, check to see what the two items are in the first two slots then set the output? Or is that not possible?
     
    Code0 likes this.
  7. Offline

    SeniorCluckers

    97WaterPolo
    Possible give me a sec to get the code

    97WaterPolo
    Code:java
    1. package me.cruz2000;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.enchantments.Enchantment;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.inventory.InventoryClickEvent;
    9. import org.bukkit.event.inventory.InventoryType;
    10. import org.bukkit.inventory.AnvilInventory;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class Blaze implements Listener{
    14.  
    15. @EventHandler
    16. public void onInventoryClick(InventoryClickEvent e)
    17. {
    18. if(e.getWhoClicked() instanceof Player)
    19. {
    20. if(e.getView().getType() == InventoryType.ANVIL)
    21. {
    22. AnvilInventory anvilInv = (AnvilInventory) e.getInventory();
    23. int slot = e.getRawSlot();
    24.  
    25. if(slot == 2)
    26. {
    27. ItemStack[] itemsInAnvil = anvilInv.getContents();
    28. Material[] swords = {Material.WOOD_SWORD, Material.STONE_SWORD, Material.GOLD_SWORD, Material.IRON_SWORD, Material.DIAMOND_SWORD, Material.BLAZE_POWDER};
    29.  
    30. for(Material m : swords)
    31. {
    32. if(itemsInAnvil[0].getType() == m && itemsInAnvil[1].getType() == m)
    33. {
    34. ItemStack slot1 = itemsInAnvil[0];
    35. ItemStack slot2 = itemsInAnvil[1];
    36.  
    37. if(slot1.getEnchantmentLevel(Enchantment.FIRE_ASPECT) == 1 && slot2.getEnchantmentLevel(Enchantment.FIRE_ASPECT) == 1)
    38. {
    39. ItemStack sword = new ItemStack(m, 1);
    40. sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 2);
    41. e.setCurrentItem(sword);
    42. }
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }
    49. }


    I tested and its not gonna work because swords can only be combined with swords but you can edit the code and make it work I'm sure.

    Main class :

    Code:java
    1. package me.cruz2000;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin implements Listener{
    8. public void onEnable(){
    9. Bukkit.getPluginManager().registerEvents(new Blaze(), this);
    10. }
    11.  
    12. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
    97WaterPolo likes this.
  8. Offline

    97WaterPolo

    SeniorCluckers

    Thanks! What you had gave me an excellent idea on how to do it! I basically checked to see if the two conditions were met, then used anvilInv.setItem(e.getView().convertSlot(2), swordIS); which set the output slot. Unfortunately it is a ghost item so I have to check to see if the items work then add the item to the player's inventory. To bad the setCursor method for bother the inventory and inventory view are glitchy, would make this 10x more realistic instead of having me add the item to the inventory.

    Thanks again for the help!
     
  9. SeniorCluckers Please don't spoon-feed. And please don't triple post.:)
     
  10. Offline

    97WaterPolo

    AdamQpzm
    Don't fault him, I just needed to know if it was possible to use a shaped recipe or if I needed to result to using InventoryClickEvent. If you look at his code that he posted, I am guessing not, you should realize it isn't really what I said in the post, mainly it helps as a guideline.

    In the end I used boo leans to determine whether or not the two items met the condition and set the third slot. When they clicked on the third slot it added the item to their inventory. That is nothing like the code he "spoon fed". He even mentioned in a footnote below that's it wasn't what I specified, it was a mere guideline.
     
Thread Status:
Not open for further replies.

Share This Page