Developer API - moomaxie/BetterShops GitHub Wiki
Developer API
Updated for 1.6.0 release
How to add BetterShops as a dependency to your plugin
In whatever IDE you use make sure to add the "BetterShops.jar" to you libraries!
Modifying the plugin.yml
name: My Plugin
version: 1.0
description: This is my cool plugin!
depend: [BetterShops]
Shops
Getting Shops
Since BetterShops was coded as one big API for ease of use, it is very easy to manipulate shops.
First we have to get our shop.
There are 3 ways of retrieving shops, all of which are very easy.
Iterate through all loaded shops:
for (Shop shop : ShopManager.getAllShops()){
}
This gets all shops in the form of an ArrayList.
Get a shop by it's name:
Shop shop = ShopManager.fromString("My Shop");
This is a quick and easy way to retrieve a shop.
Get a shop by it's location:
Location location = new Location(world,x,y,z);
Shop shop = ShopManager.fromLocation(location);
Another quick way to get a shop.
Creating a shop through code
Creating shops is very easy to do in game but also just as easy through code.
Location location = new Location(world,x,y,z);
String name = "My Shop";
Player owner = Bukkit.getPlayer("moomaxie");
Shop shop = ShopCreate.createShopExternally(location,name,owner);
Just like that the shop is created in game at that location.
Deleting a shop through code.
Shop shop = ShopManager.fromString("My Shop");
ShopDelete.deleteShopExternally(shop);
Just like that the shop is deleted and the items are laid on the ground at that location.
Modifying a shop through code
The Shop class is very unique and allows for easy customization for any shop.
For example: To change a description:
shop.setDescription("My New Cool Description");
To add a manager:
OfflinePlayer player = Bukkit.getOfflinePlayer("Notch");
shop.addManager(player);
The best way to learn everything is to try it.
Shop Items
Get a Shop Item
The ShopItem class is what is used to keep track of all items inside of a shop.
There are numerous ways to retrieve a shop item.
for (ShopItem item : shop.getShopItems()){
}
You can also use this method with a boolean to determine if you want buying or selling items.
To get all items in a buying shop use "false"
for (ShopItem item : shop.getShopItems(false)){
}
To get all items in a selling shop use "true"
for (ShopItem item : shop.getShopItems(true)){
}
To get an item individually use:
Shop shop = ShopManager.fromString("My Shop");
ItemStack it = new ItemStack(Material.CHEST);
boolean selling = false;
ShopItem item = ShopItem.fromItemStack(shop,it,selling);
This will return the shop item in the buying part of the shop "My Shop" if it exists. It will return null if not.
Each Shop Item has it's own Id that the plugin uses to identify it. No two items in the same shop have the same Id.
To get an item by Id use:
Shop shop = ShopManager.fromString("My Shop");
int id = 1;
boolean selling = false;
ShopItem item = ShopItem.fromId(shop,id,selling);
This will return the item if it exists and null if it does not.
Modifing an item
The ShopItem class (like the Shop class) is very durable and easy to use.
The most common methods used are:
ShopItem item = ShopItem.fromId(shop,id,selling);
item.setPrice(100.50);
item.setAmount(64);
item.setStock(500);
These will set the price, amount, and stock of the item and will have it automatically update in game.
Again, like the Shop class, it's best to look at the class by yourself for what it has to offer.
Events
There are 6 events (as of 1.6.0) that can be listened for.
These events work like all other events.
The events are:
- ShopCreateEvent
- ShopDeleteEvent
- NPCShopCreateEvent
- ShopBuyItemEvent
- ShopSellItemEvent
2.1 introduces:
- StockChangeEvent
- PriceChangeEvent
- AmountChangeEvent
An example usage:
@EventHandler
public void onBuyItem(ShopBuyItemEvent event){
Shop shop = event.getShop();
ShopItem item = event.getItem();
OfflinePlayer buyer = event.getCustomer();
}
That is basically all you need to know. Enjoy.