Custom enchantments with anvil recipies.

Discussion in 'Plugin Development' started by Tacodude, Jun 20, 2020.

  1. Offline

    Tacodude

    I have made a custom enchantment plugins, and I want my enchantments to work in an anvil. This includes combining books (IE Drain I + Drain I = Drain II) and booking books to weapons (IE Diamond Sword + Drain I = Diamond Sword with Drain I). I have been trying to achieve this with InventoryClickEvents, to no luck. Is there a way I can get the server to treat my enchantments how it would treat vanilla enchantments? If not, how do I use InventoryClickEvents to create custom anvil crafting recipes?
     
  2. Offline

    Machine Maker

    I do not know if there is a way to automatically make them work like vanilla enchants, but I would try to use PrepareAnvilEvent instead of InventoryClickEvent.
     
  3. Offline

    Tacodude

    That event would be perfect, but it seems that in the game when I call .setResult() on that event, I can see the result, I just can't take the result out of the anvil.
    1) How can I make it so players can take the result out of the anvil?
    2) Is there a way I can use that event to set the XP cost?
    3) That event doesn't trigger when I shift click things into the anvil, how do I fix that?

    Edit: After doing some research, it appears XP cost is calculated on the client side so #2 isn't possible. :(
     
    Last edited: Jun 21, 2020
  4. Offline

    Machine Maker

    Try setting the result in the next game tick. Use a BukkitRunnable and just call runTask(plugin) on it.
     
  5. Offline

    Tacodude

    If I do it like this, there is no result in the slot to take.
    @EventHandler
    public void onAnvilEvent(PrepareAnvilEvent e) {
    new BukkitRunnable() {
    @Override
    public void run() {
    e.setResult(new ItemStack(Material.OAK_WOOD));
    }
    }.runTask(BeefyPlugin.getInstance());
    }
     
  6. Offline

    Machine Maker

    Try getting the AnvilInventory and setting the slot to the output instead of using the setResult() method on the event. Its likely that the setResult() method won't work on the next tick, while a reference to the AnvilInventory still might.
     
  7. Offline

    Tacodude

    I can see the item again but still can't take it out of the results square. Starting to think I'm just going to have to code a custom anvil GUI...
    Interestingly, if I type a custom name into the anvil I can take the result out... maybe this is a client side problem :(
     
  8. Offline

    Legendary_zotar

    Maybe you need to update the anvil inventory?


    Can you show us your current code?
     
  9. Offline

    Tacodude

    It's pretty much what you would expect. I'm just trying to make everything in the anvil craft wood as a test.
    Code:
    @EventHandler
       public void onAnvilEvent(PrepareAnvilEvent e) {
         new BukkitRunnable() {
           @Override
           public void run() {
             e.getInventory().setItem(2, (new ItemStack(Material.OAK_WOOD)));
           }
         }.runTask(BeefyPlugin.getInstance());
       }
     
    Last edited by a moderator: Jun 25, 2020

Share This Page