From 50b69f6445dcad4580572f6b723815151a581cc3 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 17 Aug 2019 21:35:39 +0200 Subject: [PATCH] Update API for better item selection handling --- .../api/event/ShopPreCreateEvent.java | 33 ++++++++----- .../api/event/ShopSelectItemEvent.java | 40 ++++++++++++---- .../shopchest/api/flag/SelectFlag.java | 46 ++++++++++++++++++- 3 files changed, 98 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/de/epiceric/shopchest/api/event/ShopPreCreateEvent.java b/api/src/main/java/de/epiceric/shopchest/api/event/ShopPreCreateEvent.java index 7da8743..d0d8fe7 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/event/ShopPreCreateEvent.java +++ b/api/src/main/java/de/epiceric/shopchest/api/event/ShopPreCreateEvent.java @@ -1,11 +1,10 @@ package de.epiceric.shopchest.api.event; -import de.epiceric.shopchest.api.shop.ShopProduct; - import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; /** * Called when a player enters the command to create a shop @@ -20,15 +19,17 @@ public class ShopPreCreateEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private Player player; - private ShopProduct product; + private ItemStack itemStack; + private int amount; private double buyPrice; private double sellPrice; private boolean admin; private boolean cancelled; - public ShopPreCreateEvent(Player player, ShopProduct product, double buyPrice, double sellPrice, boolean admin) { + public ShopPreCreateEvent(Player player, ItemStack itemStack, int amount, double buyPrice, double sellPrice, boolean admin) { this.player = player; - this.product = product; + this.itemStack = itemStack; + this.amount = amount; this.buyPrice = buyPrice; this.sellPrice = sellPrice; this.admin = admin; @@ -45,13 +46,23 @@ public class ShopPreCreateEvent extends Event implements Cancellable { } /** - * Gets the product the shop will sell or buy + * Gets the item stack the shop will sell or buy * - * @return the product + * @return the product or {@code null} if it has not been selected * @since 1.13 */ - public ShopProduct getProduct() { - return product; + public ItemStack getItemStack() { + return itemStack == null ? null : itemStack.clone(); + } + + /** + * Gets the amount of items the shop will sell or buy + * + * @return the amount + * @since 1.13 + */ + public int getAmount() { + return amount; } /** @@ -60,8 +71,8 @@ public class ShopPreCreateEvent extends Event implements Cancellable { * @return whether the item has been selected * @since 1.13 */ - public boolean hasProduct() { - return getProduct() != null; + public boolean isItemSelected() { + return getItemStack() != null; } /** diff --git a/api/src/main/java/de/epiceric/shopchest/api/event/ShopSelectItemEvent.java b/api/src/main/java/de/epiceric/shopchest/api/event/ShopSelectItemEvent.java index c7a4691..f6612be 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/event/ShopSelectItemEvent.java +++ b/api/src/main/java/de/epiceric/shopchest/api/event/ShopSelectItemEvent.java @@ -4,8 +4,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; - -import de.epiceric.shopchest.api.shop.ShopProduct; +import org.bukkit.inventory.ItemStack; /** * Called when a player has selected an item @@ -16,14 +15,20 @@ public class ShopSelectItemEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private Player player; - private ShopProduct product; + private ItemStack item; + private int amount; private double buyPrice; private double sellPrice; private boolean admin; private boolean cancelled; - public ShopSelectItemEvent(Player player) { + public ShopSelectItemEvent(Player player, ItemStack item, int amount, double buyPrice, double sellPrice, boolean admin) { this.player = player; + this.item = item == null ? null : item.clone(); + this.amount = amount; + this.buyPrice = buyPrice; + this.sellPrice = sellPrice; + this.admin = admin; } /** @@ -42,18 +47,35 @@ public class ShopSelectItemEvent extends Event implements Cancellable { * @return the item * @since 1.13 */ - public ShopProduct getProduct() { - return product; + public ItemStack getItem() { + return item; } /** * Sets the item + *

+ * If {@code item} is {@code null}, the event will be cancelled. * - * @param product the item + * @param item the item * @since 1.13 */ - public void setProduct(ShopProduct product) { - this.product = product; + public void setItem(ItemStack item) { + if (item == null) { + setCancelled(true); + this.item = null; + } else { + this.item = item.clone(); + } + } + + /** + * Gets the amount of items the player wants to sell or buy at the shop + * + * @return the amount + * @since 1.13 + */ + public int getAmount() { + return amount; } /** diff --git a/api/src/main/java/de/epiceric/shopchest/api/flag/SelectFlag.java b/api/src/main/java/de/epiceric/shopchest/api/flag/SelectFlag.java index 27eea3d..9669de1 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/flag/SelectFlag.java +++ b/api/src/main/java/de/epiceric/shopchest/api/flag/SelectFlag.java @@ -8,12 +8,56 @@ import de.epiceric.shopchest.api.player.ShopPlayer; * Represents the flag a player has after entering the create command */ public class SelectFlag implements Flag { + private int amount; + private double buyPrice; + private double sellPrice; + private boolean admin; private GameMode gameMode; - public SelectFlag(GameMode gameMode) { + public SelectFlag(int amount, double buyPrice, double sellPrice, boolean admin, GameMode gameMode) { + this.amount = amount; + this.buyPrice = buyPrice; + this.sellPrice = sellPrice; + this.admin = admin; this.gameMode = gameMode; } + /** + * Gets the amount of items the player wants their shop to sell or buy + * + * @return the amount + */ + public int getAmount() { + return amount; + } + + /** + * Gets the price the player wants others to buy the product for + * + * @return the buy price + */ + public double getBuyPrice() { + return buyPrice; + } + + /** + * Gets whether the player wants to create an admin shop + * + * @return whether the players wants to create an admin shop + */ + public boolean isAdminShop() { + return admin; + } + + /** + * Gets the price the player wants others to sell the product for + * + * @return the sell price + */ + public double getSellPrice() { + return sellPrice; + } + /** * Gets the game mode the player had before entering the command *