Updated API Usages (Inventories) (markdown)

Ben Woo 2021-03-15 15:28:45 +08:00
parent 9dcaec9ee4
commit 4d46d4bac5

@ -1,53 +1,74 @@
# Developer API for Multiverse-Inventories
This pages provides examples and explanation for the common API usages of Multiverse-Inventories introduced [here](Developer-API-Starter). It's impossible to cover all API methods in this guide, but rest assured we have [javadocs]() provided.
This document outlines the basics of how plugin developers can use multiverse-inventories to both add custom shares to be used by their plugins, as well as how to use multiverse-inventories to check for a default or a share setup by another plugin.
## [WorldGroupManager](https://github.com/Multiverse/Multiverse-Inventories/blob/main/src/main/java/com/onarandombox/multiverseinventories/profile/WorldGroupManager.java)
World groups (stored in `groups.yml`) have powerful features to customise how player data is share/separated between worlds on your server. The 2 main parts of a `WorldGroup` is the worlds and sharables. To learn more on detailed mechanics see [this](Sharing-Details-(Inventories)).
## I want to add my own custom shareable config entry.
1. Create a class for the static sharable definitions to go
### Get WorldGroupManager instance
```java
WorldGroupManager groupManager = inventories.getGroupManager();
```
public class MySharables {
}
### Create new [WorldGroup](https://github.com/Multiverse/Multiverse-Inventories/blob/main/src/main/java/com/onarandombox/multiverseinventories/WorldGroup.java) for SMP setup
The following is a simple example method to create a world group for a typical SMP setup. You can easily adapt this to other world setups as well.
```java
public void createSMPWorldGroup(String worldName) {
// Create new group named after the world.
// Note this does not add group to Multiverse-Inventories knowledge yet.
WorldGroup newGroup = groupManager.newEmptyGroup(worldName);
2. Decide what data the sharable will store.
// Add the 3 usual SMP world dims.
newGroup.addWorld(worldName);
newGroup.addWorld(worldName + "_nether");
newGroup.addWorld(worldName + "_the_end");
If my plugin was an economy plugin i would use the Double type so that I could store the amount for each player
// Set to shares to all, so player data is consistent in overworld, nether and end.
newGroup.getShares().addAll(Sharables.allOf());
If I have a plugin that gives backpacks, I would use an array of ItemStack as the type because thats what is stored.
// Finally we add it to Multiverse-Inventories knowledge.
// This step is important, else your WorldGroup will not work!
groupManager.updateGroup(newGroup);
}
```
If I had something like a custom set of data per player, I could also write a custom class and use that.
3. Create a Sharable definition in your class. In this case I will use an ItemStack array, as its it will be for a custom inventory.
public class MySharables {
public static final Sharable<ItemStack[]> MySharable = new Sharable.Builder<ItemStack[]>("mySharable", ItemStack[].class, new SharableHandler<ItemStack[]>() {
@Override
public void updateProfile(PlayerProfile profile, Player player) {
profile.set(MySharable, player.getInventory().getContents());
}
@Override
public boolean updatePlayer(Player player, PlayerProfile profile) {
ItemStack[] value = profile.get(MySharable);
if ( value == null ) {
return false;
}
player.getInventory().setContents(value);
player.updateInventory();
return true;
}
}).stringSerializer(new ProfileEntry(false, "mySharable")).altName("ms").build();
}
## [Sharable](https://github.com/Multiverse/Multiverse-Inventories/blob/main/src/main/java/com/onarandombox/multiverseinventories/share/Sharable.java)
Let's learn how to create your unique sharable to allow for per world/worldgroup data!
1. Create a class for the static sharable definitions to go.
```java
public class MyAmazingSharables {
The final ProfileEntry string defines the name in the config file itself, not just internally. The altName(String) function allows you to specify other values in the config file that would equate to the same internal name for the share.
}
```
2. Decide what data the sharable will store.
* If my plugin was an economy plugin I would use the Double type so that I could store the amount for each player
* If I have a plugin that gives backpacks, I would use an array of ItemStack as the type because that's what is stored.
* If I had something like a custom set of data per player, I could also write a custom class and use that.
## I want to get all the groups for a world
_Unfinished Wiki_
## I want to check a default or my own shareables presence
_Unfinished Wiki_
3. Create a Sharable definition in your class. In this case, I will use an ItemStack array, as it will be for a custom inventory.
```java
public class MyAmazingSharables {
public static final Sharable<ItemStack[]> MySharable = new Sharable.Builder<ItemStack[]>("mySharable", ItemStack[].class, new SharableHandler<ItemStack[]>() {
@Override
public void updateProfile(PlayerProfile profile, Player player) {
profile.set(MySharable, player.getInventory().getContents());
}
@Override
public boolean updatePlayer(Player player, PlayerProfile profile) {
ItemStack[] value = profile.get(MySharable);
if ( value == null ) {
return false;
}
player.getInventory().setContents(value);
player.updateInventory();
return true;
}
}).stringSerializer(new ProfileEntry(false, "mySharable")).altName("ms").build();
}
```
The final ProfileEntry string defines the name in the storage file itself, not just internally. The altName(String) function allows you to specify other values in the config file that would equate to the same internal name for the share.