Solved Custom Anvil Repair Cost

Discussion in 'Spigot Plugin Development' started by abandoncaptian, Jan 27, 2020.

  1. 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.
     
  2. 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();
            }
        }
     
    • Like Like x 1
  3. drop the resource tag. youre not releasing a resource to developers.
     
  4. This is not a resource, remove the resource tag
     
  5. when u mad, u mad
     
    • Funny Funny x 2
  6. 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
     
  7. I tried that originally but it didn't seem to work, so I took this route.
     
    • Like Like x 1