ChestSort/HOW_TO_USE_API.md

126 lines
4.3 KiB
Markdown
Raw Normal View History

2018-11-07 23:59:42 +01:00
# API Usage
2018-11-08 00:33:30 +01:00
2021-05-29 18:27:06 +02:00
### Note: The API documentation is currently outdated, I will update them in the upcoming days - please look at the source code instead, it's really easy to understand. Sorry for the trouble. If you need assistance on using the API, feel free to contact me on Discord at https://discord.jeff-media.de
2020-07-12 04:03:27 +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
- 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
2021-02-20 17:43:34 +01:00
2021-02-20 17:43:20 +01:00
and more.
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-07-12 02:51:51 +02:00
```xml
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>
2020-07-12 02:51:51 +02:00
<artifactId>ChestSortAPI</artifactId>
<version>11.0.0-SNAPSHOT</version>
2020-07-12 02:51:51 +02:00
<scope>compile</scope>
2020-06-10 18:21:41 +02:00
</dependency>
</dependencies>
```
## Accessing the API
2020-07-12 02:51:51 +02:00
Then you can access the API via the plugin manager:
2018-11-07 23:59:42 +01:00
2020-07-12 03:03:31 +02:00
```java
2020-07-12 03:02:07 +02:00
ChestSort chestSort = (ChestSort) getServer().getPluginManager().getPlugin("ChestSort");
if(chestSort==null) {
2020-06-10 18:21:41 +02:00
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-07-12 02:51:51 +02:00
```java
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-07-12 02:51:51 +02:00
```java
2020-06-10 18:21:41 +02:00
chestSortAPI.sortInventory(Inventory inventory, int startSlot, int endSlot);
2019-01-12 15:51:21 +01:00
```
You can also check if a player has automatic sorting enabled or disabled:
2020-07-12 02:51:51 +02:00
```java
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.
2020-07-12 02:51:51 +02:00
```java
2020-06-10 18:21:41 +02:00
@EventHandler
public void onChestSortEvent(ChestSortEvent event) {
if(event.getInventory() == whatever) {
event.setCancelled(true);
}
}
```
2021-01-02 21:43:33 +01:00
You can also exempt certain slots / ItemStacks from being sorted using the following methods:
2021-01-02 21:42:34 +01:00
```java
public void setUnmovable(int slot)
public void setUnmovable(ItemStack itemStack)
public void removeUnmovable(int slot)
public void removeUnmovable(ItemStack itemStack)
public boolean isUnmovable(int slot)
public boolean isUnmovable(ItemStack itemStack)
```
For example, to avoid the first item in the player's hotbar from being sorted:
```java
@EventHandler
public void onChestSortEvent(ChestSortEvent event) {
event.setUnmovable(0);
}
```
2020-07-12 02:51:51 +02:00
### 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
2020-07-12 03:02:07 +02:00
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)
2020-07-12 02:51:51 +02:00
```
2021-02-20 17:49:44 +01:00
You can also instead use your own custom inventory holder that either `implements ISortable` or `extends Sortable`.
2020-06-10 18:21:41 +02:00
## Example Plugin
2021-05-29 18:27:06 +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) (OUTDATED: The example plugin still uses API version 2.0.0)
2020-07-12 03:02:07 +02:00
2020-07-12 15:56:49 +02:00
## Javadocs & Source code
2021-05-29 18:29:04 +02:00
- [ChestSortAPI Javadocs](https://repo.jeff-media.de/javadocs/ChestSortAPI). (OUTDATED: The JavaDocs are still on version 2.0.0, I will update it in the next days, sorry for the trouble - contact me on Discord at https://discord.jeff-media.de if you need assistance)
2021-02-20 17:43:20 +01:00
- [ChestSortAPI source code](https://github.com/JEFF-Media-GbR/Spigot-ChestSortAPI).