2018-11-07 23:59:42 +01:00
# API Usage
2018-11-08 00:33:30 +01:00
2020-06-10 18:43:12 +02:00
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 methods to sort any given inventory, following the rules you have specified in your ChestSort's plugin.yml and the corresponding category files, and a cancellable ChestSortEvent event that is fired whenever ChestSort is about to sort an inventory.
2018-11-08 00:33:30 +01:00
2020-06-10 18:21:41 +02:00
## Maven repository
You can use maven to add ChestSort as a dependency to your Spigot-/Bukkit-Plugin:
2018-11-07 23:59:42 +01:00
2020-06-10 18:21:41 +02:00
```
< repositories >
< repository >
< id > jeff-media-repo< / id >
< url > https://repo.jeff-media.de/maven2< / url >
< / repository >
< / repositories >
< dependencies >
< dependency >
< groupId > de.jeff_media< / groupId >
< artifactId > ChestSort< / artifactId >
< version > 8.9< / version > <!-- Check www.chestsort.de for latest version -->
< / dependency >
< / dependencies >
```
## Accessing the API
2018-11-07 23:59:42 +01:00
Then you can access it via the plugin manager:
```
2020-06-10 18:21:41 +02:00
ChestSortPlugin chestSort = (ChestSortPlugin) getServer().getPluginManager().getPlugin("ChestSort");
if(chestSort==null || !(chestSort instanceof ChestSortPlugin)) {
getLogger().severe("Error: ChestSort is not installed.");
return;
2018-11-08 00:00:24 +01:00
}
2020-06-10 18:21:41 +02:00
ChestSortAPI chestSortAPI = chestSort.getAPI();
2018-11-07 23:59:42 +01:00
```
2020-06-10 18:21:41 +02:00
### Sorting inventories
2018-11-07 23:59:42 +01:00
Now, you can sort any Inventory! Just like this:
```
2020-06-10 18:21:41 +02:00
chestSortAPI.sortInventory(Inventory inventory);
2018-11-08 00:00:24 +01:00
```
2018-11-08 16:41:02 +01:00
2019-01-12 15:51:21 +01:00
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.
2018-11-08 16:41:02 +01:00
```
2020-06-10 18:21:41 +02:00
chestSortAPI.sortInventory(Inventory inventory, int startSlot, int endSlot);
2019-01-12 15:51:21 +01:00
```
2019-04-26 12:25:44 +02:00
You can also check if a player has automatic sorting enabled or disabled:
```
2020-06-10 18:21:41 +02:00
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.
```
@EventHandler
public void onChestSortEvent(ChestSortEvent event) {
if(event.getInventory() == whatever) {
event.setCancelled(true);
}
}
```
## Example Plugin
2020-06-10 18:43:12 +02:00
Here is a complete example plugin that shows to add and use the ChestSort API: [LINK ](https://github.com/JEFF-Media-GbR/ChestSortAPIExample )