diff --git a/src/main/java/com/alttd/playershops/events/ShopItemAmountChangeEvent.java b/src/main/java/com/alttd/playershops/events/ShopItemAmountChangeEvent.java new file mode 100644 index 0000000..18bb5aa --- /dev/null +++ b/src/main/java/com/alttd/playershops/events/ShopItemAmountChangeEvent.java @@ -0,0 +1,31 @@ +package com.alttd.playershops.events; + +import com.alttd.playershops.shop.PlayerShop; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class ShopItemAmountChangeEvent extends ShopEvent { + private static final HandlerList handlers = new HandlerList(); + private final int newAmount; + + public ShopItemAmountChangeEvent(PlayerShop shop, int newAmount) { + super(shop); + this.newAmount = newAmount; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + public int getNewAmount() { + return newAmount; + } +} diff --git a/src/main/java/com/alttd/playershops/events/ShopItemChangeEvent.java b/src/main/java/com/alttd/playershops/events/ShopItemChangeEvent.java new file mode 100644 index 0000000..269554f --- /dev/null +++ b/src/main/java/com/alttd/playershops/events/ShopItemChangeEvent.java @@ -0,0 +1,31 @@ +package com.alttd.playershops.events; + +import com.alttd.playershops.shop.PlayerShop; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class ShopItemChangeEvent extends ShopEvent { + private static final HandlerList handlers = new HandlerList(); + private final ItemStack newItem; + + public ShopItemChangeEvent(PlayerShop shop, ItemStack newItem) { + super(shop); + this.newItem = newItem; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + public ItemStack getNewItem() { + return newItem; + } +} diff --git a/src/main/java/com/alttd/playershops/events/ShopPriceChangeEvent.java b/src/main/java/com/alttd/playershops/events/ShopPriceChangeEvent.java new file mode 100644 index 0000000..f4e32ae --- /dev/null +++ b/src/main/java/com/alttd/playershops/events/ShopPriceChangeEvent.java @@ -0,0 +1,31 @@ +package com.alttd.playershops.events; + +import com.alttd.playershops.shop.PlayerShop; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class ShopPriceChangeEvent extends ShopEvent { + private static final HandlerList handlers = new HandlerList(); + private final double newPrice; + + public ShopPriceChangeEvent(PlayerShop shop, double newPrice) { + super(shop); + this.newPrice = newPrice; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + public double getNewPrice() { + return newPrice; + } +} diff --git a/src/main/java/com/alttd/playershops/events/ShopTypeChangeEvent.java b/src/main/java/com/alttd/playershops/events/ShopTypeChangeEvent.java new file mode 100644 index 0000000..2776cf9 --- /dev/null +++ b/src/main/java/com/alttd/playershops/events/ShopTypeChangeEvent.java @@ -0,0 +1,31 @@ +package com.alttd.playershops.events; + +import com.alttd.playershops.shop.PlayerShop; +import com.alttd.playershops.shop.ShopType; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class ShopTypeChangeEvent extends ShopEvent { + private static final HandlerList handlers = new HandlerList(); + private final ShopType shopType; + + public ShopTypeChangeEvent(PlayerShop shop, ShopType shopType) { + super(shop); + this.shopType = shopType; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + public ShopType getShopType() { + return shopType; + } +} diff --git a/src/main/java/com/alttd/playershops/shop/PlayerShop.java b/src/main/java/com/alttd/playershops/shop/PlayerShop.java index 8228f89..c1b9c98 100644 --- a/src/main/java/com/alttd/playershops/shop/PlayerShop.java +++ b/src/main/java/com/alttd/playershops/shop/PlayerShop.java @@ -1,7 +1,7 @@ package com.alttd.playershops.shop; import com.alttd.playershops.PlayerShops; -import com.alttd.playershops.events.PlayerExchangeShopEvent; +import com.alttd.playershops.events.*; import com.alttd.playershops.utils.EconomyUtils; import com.alttd.playershops.utils.InventoryUtils; import com.alttd.playershops.utils.ShopUtil; @@ -30,7 +30,7 @@ public class PlayerShop { private String ownerName; @Getter private UUID ownerUUID; - @Getter @Setter + @Getter private ShopType type = ShopType.NONE; @Getter private final Location signLocation; @@ -38,19 +38,20 @@ public class PlayerShop { private final Location shopLocation; @Getter private String server; - @Getter @Setter + @Getter private double price; - @Getter @Setter + @Getter private int amount; - @Getter @Setter + @Getter private double balance; - @Getter @Setter + @Getter private ItemStack itemStack; @Getter @Setter private long lastTransaction; @Getter @Setter private boolean notifiedOwner = false; - + @Getter @Setter + private boolean dirty; public PlayerShop(Location shopLocation, Location signLocation, Player player) { this(shopLocation, signLocation, player.getUniqueId(), player.getName()); @@ -65,7 +66,7 @@ public class PlayerShop { updateSign(); } - public static PlayerShop create(UUID shopID, String ownerName, UUID ownerUUID, ShopType shopType, String server, + public static PlayerShop load(UUID shopID, String ownerName, UUID ownerUUID, ShopType shopType, String server, Location shopLocation, Location signLocation, double price, int amount, double balance, ItemStack item, long lastTransaction) { PlayerShop playerShop = new PlayerShop(shopLocation, signLocation, ownerUUID, ownerName); @@ -162,12 +163,26 @@ public class PlayerShop { return this.getPrice() / this.getAmount(); } - public void removeBalance(double amount) { + public boolean removeBalance(double amount) { + ShopBalanceChangeEvent shopBalanceChangeEvent = new ShopBalanceChangeEvent(this, ShopBalanceChangeEvent.ChangeReason.WIDRAW); + if (Util.callCancellableEvent(shopBalanceChangeEvent)) + return false; // cancelled by another plugin, does this need logging? + + setDirty(true); + update(); this.balance -= amount; + return true; } - public void addBalance(double amount) { + public boolean addBalance(double amount) { + ShopBalanceChangeEvent shopBalanceChangeEvent = new ShopBalanceChangeEvent(this, ShopBalanceChangeEvent.ChangeReason.DEPOSIT); + if (Util.callCancellableEvent(shopBalanceChangeEvent)) + return false; // cancelled by another plugin, does this need logging? + + setDirty(true); + update(); this.balance += amount; + return true; } @@ -209,9 +224,11 @@ public class PlayerShop { return TransactionError.CANCELLED; } + if (!addBalance(price)) + return TransactionError.CANCELLED; + InventoryUtils.removeItem(getInventory(), itemStack); EconomyUtils.removeFunds(player, price); - addBalance(price); InventoryUtils.addItem(player.getInventory(), itemStack); player.updateInventory(); @@ -247,8 +264,10 @@ public class PlayerShop { return TransactionError.CANCELLED; } + if (!removeBalance(price)) + return TransactionError.CANCELLED; + InventoryUtils.removeItem(player.getInventory(), itemStack); - removeBalance(price); EconomyUtils.addFunds(player, price); InventoryUtils.addItem(getInventory(), itemStack); player.updateInventory(); @@ -260,4 +279,51 @@ public class PlayerShop { return TransactionError.NONE; } + public void setItemStack(ItemStack itemStack) { + ShopItemChangeEvent shopItemChangeEvent = new ShopItemChangeEvent(this, itemStack); + if (Util.callCancellableEvent(shopItemChangeEvent)) + return; // cancelled by another plugin, does this need logging? + + this.itemStack = itemStack; + setDirty(true); + update(); + } + + public void setPrice(double price) { + ShopPriceChangeEvent shopPriceChangeEvent = new ShopPriceChangeEvent(this, price); + if (Util.callCancellableEvent(shopPriceChangeEvent)) + return; // cancelled by another plugin, does this need logging? + + this.price = price; + setDirty(true); + update(); + } + + public void setAmount(int amount) { + ShopItemAmountChangeEvent ShopItemAmountChangeEvent = new ShopItemAmountChangeEvent(this, amount); + if (Util.callCancellableEvent(ShopItemAmountChangeEvent)) + return; // cancelled by another plugin, does this need logging? + + this.amount = amount; + setDirty(true); + update(); + } + + public void setShopType(ShopType shopType) { + ShopTypeChangeEvent shopTypeChangeEvent = new ShopTypeChangeEvent(this, shopType); + if (Util.callCancellableEvent(shopTypeChangeEvent)) + return; // cancelled by another plugin, does this need logging? + + this.type = shopType; + setDirty(true); + update(); + } + + /** + * Updates and saves the PlayerShop in the database + */ + private void update() { + + } + }