Solved Anvil crafting

Discussion in 'Spigot Plugin Development' started by FiXed, Jun 7, 2016.

Thread Status:
Not open for further replies.
  1. So I'm trying to make my own "recipes" with an anvil and it's not working out to well. This is my current code:
    Code (Java):
        @EventHandler
        public void onClickTest(InventoryClickEvent event) {
            final Material[] mats = { Material.WOOD_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE,
                    Material.GOLD_PICKAXE, Material.DIAMOND_PICKAXE };
            ItemStack item = event.getInventory().getItem(0);
            ItemStack item2 = event.getInventory().getItem(1);
            if(item == null || item2 == null)
                return;
            if (event.getView().getType() == InventoryType.ANVIL && event.getRawSlot() == 1
                    && Arrays.asList(mats).contains(item.getType()) && item2.getType() == Material.NETHER_STAR) {
                event.getInventory().setItem(2, new ItemStack(Material.APPLE));
            }
        }
    and when I input the nether star it should put an apple in the third slot and I'm wondering why it doesn't, is there another way to accomplish this task or what should I do here?
    [​IMG]
    By the way yes, I did click it in and not shift click it in.
     
  2. MiniDigger

    Supporter

    did you made sure that the code gets executed? throw some breakpoints or debug messages in there and see how far it gets executed.
     
  3. I did, I threw a message right in between and it executed.
     
  4. MiniDigger

    Supporter

    mmh, maybe the inventory doesn't get properly updated. what happens if you click into the result slot?
     
  5. ahhh, it needed a delay, but putting the item in there isn't working so I guess just check click event and check for rawslot 2*
     
  6. I got it to work flawlessly (imo), this is my code:
    Code (Text):
        @EventHandler
        public void onClickTest(InventoryClickEvent event) {
            ItemStack item = event.getInventory().getItem(0);
            ItemStack item2 = event.getInventory().getItem(1);
         
            if (item == null)
                return;
         
            final Material[] mats = { Material.WOOD_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE,
                    Material.GOLD_PICKAXE, Material.DIAMOND_PICKAXE };
         
            if (event.getRawSlot() == 2
                    && Arrays.asList(mats).contains(item.getType())
                    && item2 != null
                    && item2.getType() == Material.NETHER_STAR) {
             
                event.getWhoClicked().setItemOnCursor(new ItemStack(Material.APPLE));
                event.getInventory().clear();
                return;
             
            }
            new BukkitRunnable() {
                @Override
                public void run() {
                 
                    ItemStack item2 = event.getInventory().getItem(1);
                    if (item2 == null)
                        return;
                    if (event.getView().getType() == InventoryType.ANVIL
                            && Arrays.asList(mats).contains(item.getType())
                            && item2.getType() == Material.NETHER_STAR)
                        event.getInventory().setItem(2, new ItemStack(Material.APPLE));
                 
                }
            }.runTaskLater(this, 10L);
        }
    http://i.imgur.com/muTwRBD.gifv
     
Thread Status:
Not open for further replies.