1 Developer API
Eric B edited this page 2020-03-01 20:40:32 +01:00

Add ShopChest to project

Maven

In your pom.xml file, add the following repository and dependency:

<repositories>
    <repository>
        <id>codemc-repo</id>
        <url>https://repo.codemc.io/repository/maven-public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>de.epiceric</groupId>
        <artifactId>ShopChest</artifactId>
        <version>1.13-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Gradle

In your build.gradle file, add the following repository and dependency:

repositories {
    maven {
        url "https://repo.codemc.io/repository/maven-public/"
    }
}

dependencies {
    compileOnly 'de.epiceric:ShopChest:1.13-SNAPSHOT'
}

First steps

  • Add ShopChest to your plugin's depend or softdepend in the plugin.yml
  • Check if ShopChest is enabled via Bukkit.getServer().getPluginManager().isPluginEnabled("ShopChest")
  • Use one of the following ways to get an instance of ShopChest's main class
    • ShopChest shopChest = (ShopChest) Bukkit.getServer().getPluginManager().getPlugin("ShopChest");
    • ShopChest shopChest = ShopChest.getInstance();

Example

private ShopChest shopChest;

@Override
public void onEnable() {
    // Only register ShopChest event listener if the plugin is enabled
    if (getServer().getPluginManager().isPluginEnabled("ShopChest")) {
        this.shopChest = ShopChest.getInstance();
        getServer().getPluginManager().registerEvents(new ShopChestListener(this), this);
    }
}

Events

Event Description Cancellable
ShopBuySellEvent Called when a player buys from or sells to a shop true
ShopPreCreateEvent Called when a player enters the command to create a shop true
ShopPreRemoveEvent Called when a player enters the command to remove a shop true
ShopPreOpenEvent Called when a player enters the command to open a shop true
ShopPreInfoEvent Called when a player enters the command to retrieve information about a shop true
ShopCreateEvent Called when a player creates a shop true
ShopExtendEvent Called when a player extends a shop to a double chest true
ShopRemoveEvent Called when a player removes a shop true
ShopRemoveAllEvent Called when a player removes all shops of a player true
ShopInfoEvent Called when a player retrieves information about a shop true
ShopOpenEvent Called when a player opens a shop true
ShopReloadEvent Called when shops are reloaded via command true
ShopsLoadedEvent Called when shops have been loaded and added to the server. Since shops are loaded dynamically based on chunk loading, this event is called several times until all shops are loaded. false

The javadoc for all event classes can be found here.

Examples

@EventHandler(ignoreCancelled = true)
public void onShopPreCreate(ShopPreCreateEvent event) {
    ItemStack item = event.getShop().getProduct().getItemStack();
    if (item.getType() == Material.GOLD_INGOT) {
        event.setCancelled(true);
        event.getPlayer().sendMessage("You are not allowed to create a shop selling gold ingots.");
    }
}

@EventHandler(ignoreCancelled = true)
public void onShopBuySell(ShopBuySellEvent event) {
    Shop shop = event.getShop();
    long worldTime = shop.getLocation().getWorld().getTime();
    if (worldTime > 20000 || worldTime < 7000) {
        event.setCancelled(true);
        event.getPlayer().sendMessage("You can only use a shop between 7am and 8pm.");
    }
}

Managing shops

Creating a shop

ShopProduct product = new ShopProduct(itemStack, amount);
Shop shop = new Shop(shopChest, vendor, product, location, buyPrice, sellPrice, shopType);
boolean success = shop.create(true); // Creates the shop's hologram and item. Passing true so errors are logged in the console.

if (success) {
    // Adds the shop to the server's shop list
    // Passing true so the shop is added to the database
    // The callback is optional
    shopChest.getShopUtils().addShop(shop, true, new Callback<Void>(shopChest) {
        @Override
        public void onResult(Void result) {
            // Optionally handle the success
        }

        @Override
        public void onError(Throwable throwable) {
            // Optionally handle the error here
        }
    });
}

Removing a shop

Shop shop = ...

// Removes the shop from the server's shop list
// Passing true so the shop is removed from the database
// The callback is optional
shopChest.getShopUtils().removeShop(shop, true, new Callback<Void>(shopChest) {
    @Override
    public void onResult(Void result) {
        // Optionally handle the success
    }

    @Override
    public void onError(Throwable throwable) {
        // Optionally handle the error here
    }
});

Getting shops

// Get all loaded shops
Collection<Shop> shops = shopChest.getShopUtils().getShops();

// Get shop at location (may return null)
// The given location is automatically converted into a block location
Shop shop = shopChest.getShopUtils().getShop(location);

// same as shopChest.getShopUtils().getShop(location) != null
boolean isShop = shopChest.getShopUtils().isShop(location);

Shop limits

int shopAmount = shopChest.getShopUtils().getShopAmount(player);
int shopLimit = shopChest.getShopUtils().getShopLimit(player);