Get recipe name properly?

Discussion in 'Plugin Development' started by MegaNarwhal, Aug 9, 2014.

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

    MegaNarwhal

    I'm writing a plugin that adds several recipes (one being ShapelessRecipe lboots) and requires a permission to craft:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void decraft(CraftItemEvent event){
    3. Player p = (Player) event.getWhoClicked();
    4. p.sendMessage(p.getName());
    5. //problem below
    6. if(event.getRecipe() != lboots){
    7. p.sendMessage("Not lboots");
    8. return;}
    9. if(!p.hasPermission("decrafting.decraft")){
    10. event.setCancelled(true);
    11. p.sendMessage("You do not have permission to decraft.");
    12. }else{
    13. p.sendMessage("Decrafted!");
    14. }
    15. }
    16. }

    Even if I craft the recipe, it says it isn't lboots and doesn't cancel it in spite of me not having the permission. How can I get the name of the recipe, and how can I provide an array of multiple recipe names to require the permission for all of them?
     
  2. Offline

    MegaNarwhal

    DoppelRR I know. I'm relatively new to the Bukkit API so I'm still figuring out how everything works. I have defined lboots so I don't know where I'm going wrong:
    Code:java
    1. Server server = this.getServer();
    2. ShapelessRecipe lboots, llegs;
    3. @Override
    4. public void onEnable(){
    5. lboots = new ShapelessRecipe(new ItemStack(Material.LEATHER, 4));
    6. lboots.addIngredient(1, Material.LEATHER_BOOTS);
    7. server.addRecipe(lboots);
    8. //list of recipes...
    9. server.getPluginManager().registerEvents(this, this);
    10. }


    I don't think that this justifies making a new thread, but I've been adding other recipes and I can't figure out how to specify a data value for ingredients of a recipe. I have looked at the JavaDocs, but as I learn by example it hasn't been tremendously helpful. Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  3. Offline

    MegaNarwhal

  4. Offline

    mythbusterma

    MegaNarwhal

    In short, you can't. In long, you have to use NMS methods to redefine how Bukkit handles recipes.
     
  5. Offline

    Zupsub

    You can try a (more or less) hacky way to archive what you want without NMS:

    Create a 'special item stack', f.e. A special lore that safely indicates, that it is no 'normal' ItemStack. Set this ItemStack as the result for your crafting recipe.

    Listen for the PrepareCraftEvent, get the crafting result and check the lore. If it is your 'special' ItemStack check for permisson etc. and clear the result or set a 'normal' ItemStack as result.

    Needed methods: PrepareItemCraftEvent#getRecipe().getResult()

    PrepareItemCraftEvent#getInventory().setResult(ItemStack)
     
Thread Status:
Not open for further replies.

Share This Page