I'm making a system where you can repair items infinitely in an anvil with a cap 30 lvl at max. I looked into the resource https://github.com/Geeveloper/MaxRepairCost. this kind of does what I want but uses methods that cause warnings in the console which I try to avoid at all costs. Warning Code (Text): >WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by me.abandoncaptian.pluginName.Events.Anvil(file plugins/pluginName.jar) to field java.lang.reflect.Field.modifiers WARNING: Please consider reporting this to the maintainers of me.abandoncaptian.pluginName.Events.Anvil WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release I just want the anvil to check if the repair cost is over 30 and if so change it to 30.
https://papermc.io/javadocs/paper/1.14/org/bukkit/inventory/AnvilInventory.html#setRepairCost-int- somethign simple along the lines of this should work - inventory click event - get anvil view - override repair cost
Actually I got it working, Code (Java): @EventHandler(priority = EventPriority.HIGHEST) public void anvilCost(PrepareAnvilEvent e){ if(Main.repairCostNerf) { Player p = (Player) e.getViewers().get(0); AnvilInventory inv = e.getInventory(); if (inv.getRepairCost() > Main.maxRepairCost){ Bukkit.getScheduler().runTask(pl, ()->inv.setRepairCost(Main.maxRepairCost)); } if(Main.maxRepairCost > 40) { if (inv.getMaximumRepairCost() != Integer.MAX_VALUE) { try { CraftInventoryAnvil anvil = CraftInventoryAnvil.class.cast(inv); Field container = CraftInventoryAnvil.class.getDeclaredField("container"); container.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(container, container.getModifiers() & ~Modifier.FINAL); Object containerAnvil = container.get(anvil); Field maximumRepairCost = ContainerAnvil.class.getField("maximumRepairCost"); maximumRepairCost.set(containerAnvil, Integer.MAX_VALUE); } catch (NoSuchFieldException | IllegalAccessException ex) { ex.printStackTrace(); } } } if (inv.getItem(0) != null && inv.getItem(1) != null && inv.getItem(2) == null) p.updateInventory(); } }
awesome! another thought: maybe the code could potentially condensed by setting the repaircost on the itemstack directly (which is also possible) rather, than dealing with the whole anvil GUI