Update API for better item selection handling

This commit is contained in:
Eric 2019-08-17 21:35:39 +02:00
parent 57b685ecb9
commit 50b69f6445
3 changed files with 98 additions and 21 deletions

View File

@ -1,11 +1,10 @@
package de.epiceric.shopchest.api.event; package de.epiceric.shopchest.api.event;
import de.epiceric.shopchest.api.shop.ShopProduct;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
/** /**
* Called when a player enters the command to create a shop * 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 static final HandlerList handlers = new HandlerList();
private Player player; private Player player;
private ShopProduct product; private ItemStack itemStack;
private int amount;
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private boolean admin; private boolean admin;
private boolean cancelled; 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.player = player;
this.product = product; this.itemStack = itemStack;
this.amount = amount;
this.buyPrice = buyPrice; this.buyPrice = buyPrice;
this.sellPrice = sellPrice; this.sellPrice = sellPrice;
this.admin = admin; 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 * @since 1.13
*/ */
public ShopProduct getProduct() { public ItemStack getItemStack() {
return product; 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 * @return whether the item has been selected
* @since 1.13 * @since 1.13
*/ */
public boolean hasProduct() { public boolean isItemSelected() {
return getProduct() != null; return getItemStack() != null;
} }
/** /**

View File

@ -4,8 +4,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import de.epiceric.shopchest.api.shop.ShopProduct;
/** /**
* Called when a player has selected an item * 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 static final HandlerList handlers = new HandlerList();
private Player player; private Player player;
private ShopProduct product; private ItemStack item;
private int amount;
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private boolean admin; private boolean admin;
private boolean cancelled; 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.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 * @return the item
* @since 1.13 * @since 1.13
*/ */
public ShopProduct getProduct() { public ItemStack getItem() {
return product; return item;
} }
/** /**
* Sets the item * Sets the item
* <p>
* If {@code item} is {@code null}, the event will be cancelled.
* *
* @param product the item * @param item the item
* @since 1.13 * @since 1.13
*/ */
public void setProduct(ShopProduct product) { public void setItem(ItemStack item) {
this.product = product; 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;
} }
/** /**

View File

@ -8,12 +8,56 @@ import de.epiceric.shopchest.api.player.ShopPlayer;
* Represents the flag a player has after entering the create command * Represents the flag a player has after entering the create command
*/ */
public class SelectFlag implements Flag { public class SelectFlag implements Flag {
private int amount;
private double buyPrice;
private double sellPrice;
private boolean admin;
private GameMode gameMode; 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; 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 * Gets the game mode the player had before entering the command
* <p> * <p>