ItemCraftEvent

Discussion in 'Spigot Plugin Development' started by bmac20, Jun 23, 2016.

  1. I am trying to make a plugin that will disable the crafting of certain items on my server, and this is what I have so far that doesn't seem to be working for some reason.
    Code (Text):
    @EventHandler
        public void onPlayerCraft(CraftItemEvent e){
            if(e.getResult().equals(Material.HOPPER)){
                e.setCancelled(true);
                e.getWhoClicked().sendMessage(ChatColor.AQUA + "TheColossal " + ChatColor.DARK_GRAY + ">> " + ChatColor.RED + "You are not allowed to craft this item.");
            }
        }
     
    • Like Like x 1
    • Like Like x 1
    • Agree Agree x 1
  2. Choco

    Moderator

    The result you're getting is the Event.Result enum, and not the result of the crafting recipe. That will return either ALLOW, DENY, or DEFAULT. What you want is to get the item crafted. To do that, get the inventory (CraftItemEvent#getInventory()) which should return a CraftingInventory. Use the CraftingInventory#getResult() method to get the ItemStack created, get the Material from that (ItemStack#getType()), and compare it to what you want to cancel.
     
    • Like Like x 1
    • Agree Agree x 2
  3. Choco

    Moderator

    What purpose would that have? You plan on iterating through all available recipes registered in Minecraft (Which you cannot obtain through Spigot afaik)
     
  4. @2008Choco

    I can do EVERYTHING.

    Code (Text):
    public static void disableRecipe(ItemStack result) {

            Iterator<Recipe> it = Bukkit.getServer().recipeIterator();
            Recipe recipe;
            while (it.hasNext()) {
                recipe = it.next();
                if (recipe != null && recipe.getResult().equals(result)) {
                    it.remove();
                }
            }

        }
     
    • Winner Winner x 2
    • Like Like x 1
    • Optimistic Optimistic x 1
  5. @Omel such smart, much impressed
     
    • Like Like x 1
    • Friendly Friendly x 1
    • Like Like x 2
  6. Choco

    Moderator

    Yes, that's one way of doing it. Though that's not really the best idea. For one, you're iterating over every single recipe whenever you call that method (Can be really inefficient if you're using that method too often). Second of all, you have to take into consideration of the fact that the OP may want a permission-based crafting system. Perhaps there will be a permission node that allows you to craft a certain item whereas some players will not be allowed given that they do not have the permission.

    I find your way to be quite inefficient and not as flexible, but that's just my opinion
     
    • Agree Agree x 1
  7. Thats true you remove the Recipe for everyone. but this is the best way to disable recipes.
     
    • Like Like x 1
  8. Choco

    Moderator

    No, not the best way if you want some flexibility with your plugin ;) People may also get really confused as to why some recipes aren't showing up properly in the crafting inventory when they're putting in their favourite recipe for the hopper! Also, may mess with other plugins that are potentially listening for the CraftItemEvent, PrepareItemCraftEvent, etc. trying to detect if an item is being crafted. There's just so many flaws with removing a crafting recipe
     
  9. Good Job Omel
    Thaks for the system of NemesisPvP you are a great Developer and a Java specialist thanks for all
     
    • Friendly Friendly x 1
  10. @TwizZIm Thats not the Topic of this Thread. Haha. but Thanks.
     
    • Like Like x 1