src/main/java/com/davidcubesvk/repairItem/utils/Repairer.java
/**
     * Repairs the given item.
     *
     * @param itemStack the item to repair
     * @return the repair result
     */
Repairs the given item.
@param itemStack the item to repair
@return the repair result
Repairs the given item . @param itemStack the item to repair @return the repair result
public RepairResult repairItem(ItemStack itemStack) {
        if (itemStack == null || itemStack.getType() == null || itemStack.getType() == Material.AIR)
            return new RepairResult(RepairResult.Status.FAIL_NO_ITEM);
        if (itemStack.getType().getMaxDurability() == 0)
            return new RepairResult(RepairResult.Status.FAIL_UNREPAIRABLE);
        for (BlockedItem blocked : blockedItems)
            if (blocked.compare(itemStack))
                return new RepairResult(RepairResult.Status.FAIL_BLOCKED);
        try {
            if (getDurabilityMethod == null || setDurabilityMethod == null)
                return new RepairResult(RepairResult.Status.FAIL_OTHER);
            if (oldDurabilitySystem) {
                if (((Short) getDurabilityMethod.invoke(itemStack)) == 0)
                    return new RepairResult(RepairResult.Status.FAIL_ALREADY_REPAIRED);
                setDurabilityMethod.invoke(itemStack, (short) 0);
            } else {
                if (damageableClass == null)
                    return new RepairResult(RepairResult.Status.FAIL_OTHER);
                Object damageable = damageableClass.cast(itemStack.getItemMeta());
                if (((Integer) getDurabilityMethod.invoke(damageable)) == 0)
                    return new RepairResult(RepairResult.Status.FAIL_ALREADY_REPAIRED);
                setDurabilityMethod.invoke(damageable, 0);
                itemStack.setItemMeta((ItemMeta) damageable);
            }
        } catch (IllegalAccessException | InvocationTargetException ex) {
            plugin.getLogger().log(Level.SEVERE, "Failed to repair an item! If the problem persists, please report it.", ex);
            return new RepairResult(RepairResult.Status.FAIL_OTHER);
        }
        return new RepairResult(RepairResult.Status.SUCCESS);
    }
public RepairResult repairItem ( ItemStack itemStack ) { if ( itemStack == null || itemStack . getType ( ) == null || itemStack . getType ( ) == Material . AIR ) return new RepairResult ( RepairResult . Status . FAIL_NO_ITEM ) ; if ( itemStack . getType ( ) . getMaxDurability ( ) == 0 ) return new RepairResult ( RepairResult . Status . FAIL_UNREPAIRABLE ) ; for ( BlockedItem blocked : blockedItems ) if ( blocked . compare ( itemStack ) ) return new RepairResult ( RepairResult . Status . FAIL_BLOCKED ) ; try { if ( getDurabilityMethod == null || setDurabilityMethod == null ) return new RepairResult ( RepairResult . Status . FAIL_OTHER ) ; if ( oldDurabilitySystem ) { if ( ( ( Short ) getDurabilityMethod . invoke ( itemStack ) ) == 0 ) return new RepairResult ( RepairResult . Status . FAIL_ALREADY_REPAIRED ) ; setDurabilityMethod . invoke ( itemStack , ( short ) 0 ) ; } else { if ( damageableClass == null ) return new RepairResult ( RepairResult . Status . FAIL_OTHER ) ; Object damageable = damageableClass . cast ( itemStack . getItemMeta ( ) ) ; if ( ( ( Integer ) getDurabilityMethod . invoke ( damageable ) ) == 0 ) return new RepairResult ( RepairResult . Status . FAIL_ALREADY_REPAIRED ) ; setDurabilityMethod . invoke ( damageable , 0 ) ; itemStack . setItemMeta ( ( ItemMeta ) damageable ) ; } } catch ( IllegalAccessException | InvocationTargetException ex ) { plugin . getLogger ( ) . log ( Level . SEVERE , "Failed to repair an item! If the problem persists, please report it." , ex ) ; return new RepairResult ( RepairResult . Status . FAIL_OTHER ) ; } return new RepairResult ( RepairResult . Status . SUCCESS ) ; }
Repairs the given item.
Repairs the given item .
//If no item is present //If the item can not be repaired //Loop through all blocked items //If the item is blocked //If get/set durability method is null //If version is older than 1.13 //If already repaired //Use only the stack's method //If damageable class is null //Cast to the damageable //If already repaired //Cast the item meta to damageable and then set damage //Set the item meta //Log //Repaired