PrepareItemCraftEvent

Discussion in 'Plugin Development' started by DJCowGaming, Oct 23, 2019.

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

    DJCowGaming

    I don't usually use the forums for help, but I really feel like I have been lately. I'm trying to have custom recipes where the items have to have custom names.
    I have a recipe here:
    Code:
            ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(pl, "ARCANE_SWORD_1"), ArcaneSwords.sword1());
            recipe.shape(" i ", " i ", " b ");
            recipe.setIngredient('i', Material.PRISMARINE_SHARD);
            recipe.setIngredient('b', Material.BLAZE_ROD);
            pl.getServer().addRecipe(recipe);
    which does exactly what it needs to. I want to use the PrepareItemCraftEvent to check if the items being used to craft have the names that I want them to.
    The event is here:
    Code:
        @EventHandler
        public void onPrepareItemCraft(PrepareItemCraftEvent event) {
            CraftingInventory inv = event.getInventory();
            ItemStack[] items = inv.getMatrix();
            if (equal(inv.getResult(), ArcaneSwords.sword1())) {
                if (!(items[0] == null && items[1] != null && equal(items[1], ArcaneMaterials.shards(1)) && items[2] == null
                        && items[3] == null && items[4] != null && equal(items[4], ArcaneMaterials.shards(1))
                        && items[5] == null && items[6] == null && items[7] != null
                        && items[7].getType() == Material.BLAZE_ROD && items[8] == null)) {
                    inv.setResult(null);
                }
            }
        }
    
        private boolean equal(ItemStack item1, ItemStack item2) {
            String name1 = item1.getItemMeta().getDisplayName();
            String name2 = item2.getItemMeta().getDisplayName();
            return name1.equals(name2);
        }
    This also does what I want it to do, though when I craft the item, it throws a NPE at line 30, which is String name2. I don't know what is wrong with it, or why it would be throwing a null pointer exception. Do I need to add a check for both the ItemStacks in the equal method to see if they have ItemMeta? Any help is appreciated.
     
  2. Offline

    robertlit

    Does the error occur every time you use equal()? Or is it just one specific time?
     
  3. Offline

    DJCowGaming

    The error only fires at equal(items[1], ArcaneMaterials.shards(1)), and the error points to line 19 (this line) and line 30, which is the String name2.
     
  4. Offline

    CraftCreeper6

    @DJCowGaming
    name2 must not have a display name, or the ItemStack, item2 is null.

    Also, what version are you using.
     
  5. Offline

    DJCowGaming

    Though I know for a fact that the ItemStack has ItemMeta and a display name, since ArcaneMaterials.shards(1) has ItemMeta and a custom display name.

    I fixed what was throwing the npe. I added a null check for the result in the event and a null check for item1 and item2 in the equal method.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 24, 2019
Thread Status:
Not open for further replies.

Share This Page