#PrepareAnvilEvent setResult bug? item in slot 1 is replaced directly

1 messages · Page 1 of 1 (latest)

limber barn
#

Hey everyone, so i'm trying to do a little custom enchants plugin for my server and everything was working fine until i tried to apply the enchant to an item, here the code for context : java public void onAnvilChange(PrepareAnvilEvent e){ Inventory inv = e.getInventory(); // 1 = star add / 2 = tools/armor add if(anutils.anvilcheck(inv) == 1 && anutils.enchantcheck(inv, 1) != null){ Logger log = Bukkit.getLogger(); log.warning("Type 1 event"); e.setResult(anutils.enchantcheck(inv, 1)); plugin.getServer().getScheduler().runTask(plugin, () ->{ e.getInventory().setRepairCost(5);}); } else if(anutils.anvilcheck(inv) == 2 && anutils.enchantcheck(inv, 2) != null){ ItemStack item = anutils.enchantcheck(inv, 2); Logger log = Bukkit.getLogger(); log.warning("Type 2 event"); e.setResult(item); e.getInventory().setRepairCost(8); } }

#

so first IF statement is working as intended, but the second not so much

#

so the item is not null or anything, it's just that it doesn't set it in the result slot, instead it directly update the slot 1 item

#

bypassing the level cost and not consuming the slot 2 item

#

i tried with the scheduler but it's the same thing, since it update the slot 1 the repaircost is never updated

bold lily
#

?paste

#

;/

#

Code is hard to read on mobile

limber barn
#

i'll upload it on pastebin one sec

#

i'm starting to have an idea of the error but still no sense as to why it's working on the first if statement :x

limber barn
#

i think the problem is that the event is called 5 times every time someone change something in an anvil
the first 2 times the item is not null cause it's done too fast (1tick?) but after that since it updated the slot 1 my enchantcheck function is no longer returning an item but null since conditions have changed

#

why does it change the slot 1 though no idea

#

could it be because the item is exactly the same except the enchant?

#

hey @real parrot, i noticed you helped in other theads so i hope i'm not disturbing you :x

limber barn
#

found the problem

#

i was using the item in slot 1 as a parameter for a function and it was using the SAME slot even if i was not returning it