Add more events

This commit is contained in:
Len 2022-08-22 15:29:28 +02:00
parent f3030afa33
commit cd850eeee3
5 changed files with 202 additions and 12 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,7 +1,7 @@
package com.alttd.playershops.shop; package com.alttd.playershops.shop;
import com.alttd.playershops.PlayerShops; 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.EconomyUtils;
import com.alttd.playershops.utils.InventoryUtils; import com.alttd.playershops.utils.InventoryUtils;
import com.alttd.playershops.utils.ShopUtil; import com.alttd.playershops.utils.ShopUtil;
@ -30,7 +30,7 @@ public class PlayerShop {
private String ownerName; private String ownerName;
@Getter @Getter
private UUID ownerUUID; private UUID ownerUUID;
@Getter @Setter @Getter
private ShopType type = ShopType.NONE; private ShopType type = ShopType.NONE;
@Getter @Getter
private final Location signLocation; private final Location signLocation;
@ -38,19 +38,20 @@ public class PlayerShop {
private final Location shopLocation; private final Location shopLocation;
@Getter @Getter
private String server; private String server;
@Getter @Setter @Getter
private double price; private double price;
@Getter @Setter @Getter
private int amount; private int amount;
@Getter @Setter @Getter
private double balance; private double balance;
@Getter @Setter @Getter
private ItemStack itemStack; private ItemStack itemStack;
@Getter @Setter @Getter @Setter
private long lastTransaction; private long lastTransaction;
@Getter @Setter @Getter @Setter
private boolean notifiedOwner = false; private boolean notifiedOwner = false;
@Getter @Setter
private boolean dirty;
public PlayerShop(Location shopLocation, Location signLocation, Player player) { public PlayerShop(Location shopLocation, Location signLocation, Player player) {
this(shopLocation, signLocation, player.getUniqueId(), player.getName()); this(shopLocation, signLocation, player.getUniqueId(), player.getName());
@ -65,7 +66,7 @@ public class PlayerShop {
updateSign(); 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, Location shopLocation, Location signLocation, double price, int amount,
double balance, ItemStack item, long lastTransaction) { double balance, ItemStack item, long lastTransaction) {
PlayerShop playerShop = new PlayerShop(shopLocation, signLocation, ownerUUID, ownerName); PlayerShop playerShop = new PlayerShop(shopLocation, signLocation, ownerUUID, ownerName);
@ -162,12 +163,26 @@ public class PlayerShop {
return this.getPrice() / this.getAmount(); 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; 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; this.balance += amount;
return true;
} }
@ -209,9 +224,11 @@ public class PlayerShop {
return TransactionError.CANCELLED; return TransactionError.CANCELLED;
} }
if (!addBalance(price))
return TransactionError.CANCELLED;
InventoryUtils.removeItem(getInventory(), itemStack); InventoryUtils.removeItem(getInventory(), itemStack);
EconomyUtils.removeFunds(player, price); EconomyUtils.removeFunds(player, price);
addBalance(price);
InventoryUtils.addItem(player.getInventory(), itemStack); InventoryUtils.addItem(player.getInventory(), itemStack);
player.updateInventory(); player.updateInventory();
@ -247,8 +264,10 @@ public class PlayerShop {
return TransactionError.CANCELLED; return TransactionError.CANCELLED;
} }
if (!removeBalance(price))
return TransactionError.CANCELLED;
InventoryUtils.removeItem(player.getInventory(), itemStack); InventoryUtils.removeItem(player.getInventory(), itemStack);
removeBalance(price);
EconomyUtils.addFunds(player, price); EconomyUtils.addFunds(player, price);
InventoryUtils.addItem(getInventory(), itemStack); InventoryUtils.addItem(getInventory(), itemStack);
player.updateInventory(); player.updateInventory();
@ -260,4 +279,51 @@ public class PlayerShop {
return TransactionError.NONE; 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() {
}
} }