# API Usage
If you want to use ChestSort's advanced sorting features for your own plugin, or if ChestSort causes trouble with your own plugin, you can use the ChestSort API. It provides
- the methods `sortInventory(Inventory inv)` and `sortInventory(Inventory inv, int startSlot, int endSlot)`to sort any given inventory, following the rules you have specified in your ChestSort's plugin.yml and the corresponding category files
- a cancellable event called `ChestSortEvent` that is fired whenever ChestSort is about to sort an inventory
- a custom InventoryHolder called `Sortable` that you can use when creating inventories to tell ChestSort that this inventory should be sortable
## Maven repository
You can use maven to add ChestSort as a dependency to your Spigot-/Bukkit-Plugin:
```xml
jeff-media-repo
https://repo.jeff-media.de/maven2
de.jeff_media
ChestSortAPI
1.0.0
compile
```
If you use the `Sortable`class or the `ISortable` interface, you must also shade the ChestSortAPI into your plugin:
```xml
org.apache.maven.plugins
maven-shade-plugin
3.1.0
package
shade
```
## Accessing the API
Then you can access the API via the plugin manager:
```java
ChestSort chestSort = (ChestSort) getServer().getPluginManager().getPlugin("ChestSort");
if(chestSort==null) {
getLogger().severe("Error: ChestSort is not installed.");
return;
}
ChestSortAPI chestSortAPI = chestSort.getAPI();
```
### Sorting inventories
Now, you can sort any Inventory! Just like this:
```java
chestSortAPI.sortInventory(Inventory inventory);
```
To sort only specific slots, you can pass slot numbers where to start and end sorting. ChestSort will not modify the inventory outside the given slot range.
```java
chestSortAPI.sortInventory(Inventory inventory, int startSlot, int endSlot);
```
You can also check if a player has automatic sorting enabled or disabled:
```java
boolean sortingEnabled = chestSortAPI.sortingEnabled(Player player);
```
### Custom ChestSort event
If you want to prevent ChestSort from sorting a certain inventory, you can listen to the ChestSortEvent event.
```java
@EventHandler
public void onChestSortEvent(ChestSortEvent event) {
if(event.getInventory() == whatever) {
event.setCancelled(true);
}
}
```
### Making custom inventories sortable
If you create a new Inventory inside your plugin, you can use the `Sortable` class to tell ChestSort that your custom inventory should be sortable.
```java
Inventory inv = Bukkit.createInventory(new Sortable(), 9, "Example");
```
You can also store another InventoryHolder in the Inventory if needed:
```java
Sortable holder = new Sortable(player)
```
## Example Plugin
Here is a complete example plugin that shows to add and use the ChestSort API: [LINK](https://github.com/JEFF-Media-GbR/ChestSortAPIExample)
## Javadocs & Source code
- [ChestSortAPI Javadocs](https://repo.jeff-media.de/javadocs/ChestSortAPI).
- [ChestSortAPI source code](https://github.com/JEFF-Media-GbR/Spigot-ChestSortAPI).