From 5c2f3022b52aa679f62f79ad0386004ff1a90f6a Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 17 Aug 2019 14:07:10 +0200 Subject: [PATCH] Use consumers as callback functions --- .../epiceric/shopchest/api/ShopManager.java | 57 ++++++++++--------- .../de/epiceric/shopchest/api/shop/Shop.java | 7 ++- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/api/src/main/java/de/epiceric/shopchest/api/ShopManager.java b/api/src/main/java/de/epiceric/shopchest/api/ShopManager.java index 8e89ee5..fdf9b11 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/ShopManager.java +++ b/api/src/main/java/de/epiceric/shopchest/api/ShopManager.java @@ -2,14 +2,13 @@ package de.epiceric.shopchest.api; import java.util.Collection; import java.util.Optional; +import java.util.function.Consumer; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.World; import de.epiceric.shopchest.api.event.ShopReloadEvent; -import de.epiceric.shopchest.api.exceptions.ChestNotFoundException; -import de.epiceric.shopchest.api.exceptions.NoSpaceAboveChestException; import de.epiceric.shopchest.api.shop.Shop; import de.epiceric.shopchest.api.shop.ShopProduct; @@ -70,20 +69,18 @@ public interface ShopManager { * When {@code sellPrice} is zero, a player cannot sell to the shop. * You cannot have {@code buyPrice} and {@code sellPrice} be zero. * - * @param vendor the shop's vendor - * @param product the shop's product - * @param location the shop's chest location. - * Can be either chest if it's on a double chest - * @param buyPrice the price a player can buy the product for. - * @param sellPrice the price a player can sell the product for. - * @throws ChestNotFoundException when there is no chest at the given location - * @throws NoSpaceAboveChestException when there is no empty space above the chest - * @return the shop if created successfully or an empty optional if not + * @param vendor the shop's vendor + * @param product the shop's product + * @param location the shop's chest location. + * Can be either chest if it's on a double chest + * @param buyPrice the price a player can buy the product for. + * @param sellPrice the price a player can sell the product for. + * @param callback the callback returning the created shop on success + * @param errorCallback the callback returning the error if one occurred * @since 1.13 - * @see ShopManager#addAdminShop(ShopProduct, Location, double, double) + * @see ShopManager#addAdminShop(ShopProduct, Location, double, double, Consumer, Consumer) */ - Shop addShop(OfflinePlayer vendor, ShopProduct product, Location location, double buyPrice, double sellPrice) - throws ChestNotFoundException, NoSpaceAboveChestException; + void addShop(OfflinePlayer vendor, ShopProduct product, Location location, double buyPrice, double sellPrice, Consumer callback, Consumer errorCallback); /** * Creates an admin shop and adds it to the database @@ -92,43 +89,47 @@ public interface ShopManager { * When {@code sellPrice} is zero, a player cannot sell to the shop. * You cannot have {@code buyPrice} and {@code sellPrice} be zero. * - * @param product the shop's product - * @param location the shop's chest location. - * Can be either chest if it's on a double chest - * @param buyPrice the price a player can buy the product for. - * @param sellPrice the price a player can sell the product for. - * @throws ChestNotFoundException when there is no chest at the given location - * @throws NoSpaceAboveChestException when there is no empty space above the chest - * @return the created admin shop + * @param product the shop's product + * @param location the shop's chest location. + * Can be either chest if it's on a double chest + * @param buyPrice the price a player can buy the product for. + * @param sellPrice the price a player can sell the product for. + * @param callback the callback returning the created shop on success + * @param errorCallback the callback returning the error if one occurred * @since 1.13 - * @see ShopManager#addShop(OfflinePlayer, ShopProduct, Location, double, double) + * @see ShopManager#addShop(OfflinePlayer, ShopProduct, Location, double, double, Consumer, Consumer) */ - Shop addAdminShop(ShopProduct product, Location location, double buyPrice, double sellPrice) - throws ChestNotFoundException, NoSpaceAboveChestException; + void addAdminShop(ShopProduct product, Location location, double buyPrice, double sellPrice, Consumer callback, Consumer errorCallback); /** * Removes a shop from the database * * @param shop the shop to remove + * @param callback the callback returning nothing on success + * @param errorCallback the callback returning the error if one occurred * @since 1.13 */ - void removeShop(Shop shop); + void removeShop(Shop shop, Consumer callback, Consumer errorCallback); /** * Removes a shop from the database by its ID * * @param shopId the id of the shop to remove + * @param callback the callback returning nothing on success + * @param errorCallback the callback returning the error if one occurred * @since 1.13 */ - void removeShop(int shopId); + void removeShop(int shopId, Consumer callback, Consumer errorCallback); /** * Asynchronously reloads all shops from the database *

* This does not trigger the {@link ShopReloadEvent}. * + * @param callback the callback returning the amount of shops on success + * @param errorCallback the callback returning the error if one occurred * @since 1.13 */ - void reloadShops(); + void reloadShops(Consumer callback, Consumer errorCallback); } \ No newline at end of file diff --git a/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java b/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java index 9e40e6e..138dee9 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java +++ b/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java @@ -1,5 +1,7 @@ package de.epiceric.shopchest.api.shop; +import java.util.function.Consumer; + import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.World; @@ -12,8 +14,8 @@ import de.epiceric.shopchest.api.exceptions.ChestNotFoundException; * Represents a shop * * @since 1.13 - * @see ShopManager#addShop(OfflinePlayer, ShopProduct, Location, double, double) - * @see ShopManager#addAdminShop(ShopProduct, Location, double, double) + * @see ShopManager#addShop(OfflinePlayer, ShopProduct, Location, double, double, Consumer, Consumer) + * @see ShopManager#addAdminShop(ShopProduct, Location, double, double, Consumer, Consumer) */ public interface Shop { @@ -21,6 +23,7 @@ public interface Shop { * Gets this shop's ID * * @return the ID + * @throws IllegalStateException if the shop has not been added to the database yet * @since 1.13 */ int getId();