Skip to content

Commit 456dade

Browse files
committed
Added ShopOngoingFeeEvent
1 parent c7b5c8e commit 456dade

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/main/java/org/maxgamer/quickshop/event/QSEvent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.maxgamer.quickshop.event;
2121

22+
import org.bukkit.event.Cancellable;
2223
import org.bukkit.event.Event;
2324
import org.bukkit.event.HandlerList;
2425
import org.jetbrains.annotations.NotNull;
@@ -50,4 +51,15 @@ public void callEvent() {
5051
QuickShop.getInstance().getServer().getPluginManager().callEvent(this);
5152
}
5253

54+
/**
55+
* Call event on Bukkit event bus and check if cancelled
56+
* @return Returns true if cancelled, and false if didn't cancel
57+
*/
58+
public boolean callCancellableEvent(){
59+
QuickShop.getInstance().getServer().getPluginManager().callEvent(this);
60+
if(this instanceof Cancellable)
61+
return ((Cancellable)this).isCancelled();
62+
return false;
63+
}
64+
5365
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.maxgamer.quickshop.event;
2+
3+
import org.bukkit.event.Cancellable;
4+
import org.maxgamer.quickshop.shop.Shop;
5+
6+
import java.util.UUID;
7+
8+
public class ShopOngoingFeeEvent extends QSEvent implements Cancellable {
9+
private final UUID player;
10+
11+
private final Shop shop;
12+
13+
private double cost;
14+
15+
public ShopOngoingFeeEvent(Shop shop, UUID player, double cost){
16+
this.shop = shop;
17+
this.player = player;
18+
this.cost = cost;
19+
}
20+
21+
/**
22+
* Sets the ongoing fee to replace old one
23+
* @param cost The ongoing fee
24+
*/
25+
public void setCost(double cost) {
26+
this.cost = cost;
27+
}
28+
29+
/**
30+
* Getting the cost in this event
31+
* @return The ongoing fee
32+
*/
33+
public double getCost() {
34+
return cost;
35+
}
36+
37+
/**
38+
* Getting related shop in this event
39+
* @return The shop triggered ongoing fee event
40+
*/
41+
public Shop getShop() {
42+
return shop;
43+
}
44+
/**
45+
* Getting related player in this event
46+
* @return The player triggered ongoing fee event
47+
*/
48+
public UUID getPlayer() {
49+
return player;
50+
}
51+
52+
private boolean cancelled;
53+
@Override
54+
public boolean isCancelled() {
55+
return cancelled;
56+
}
57+
58+
@Override
59+
public void setCancelled(boolean b) {
60+
this.cancelled = b;
61+
}
62+
}

src/main/java/org/maxgamer/quickshop/watcher/OngoingFeeWatcher.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.maxgamer.quickshop.QuickShop;
2828
import org.maxgamer.quickshop.economy.EconomyTransaction;
2929
import org.maxgamer.quickshop.economy.Trader;
30+
import org.maxgamer.quickshop.event.ShopOngoingFeeEvent;
3031
import org.maxgamer.quickshop.shop.Shop;
3132
import org.maxgamer.quickshop.util.MsgUtil;
3233
import org.maxgamer.quickshop.util.Util;
@@ -55,13 +56,14 @@ public void run() {
5556
Util.debugLog("Economy hadn't get ready.");
5657
return;
5758
}
58-
int cost = plugin.getConfig().getInt("shop.ongoing-fee.cost-per-shop");
59+
5960
boolean allowLoan = plugin.getConfig().getBoolean("shop.allow-economy-loan");
6061
boolean ignoreUnlimited = plugin.getConfig().getBoolean("shop.ongoing-fee.ignore-unlimited");
6162
for (Shop shop : plugin.getShopManager().getAllShops()) {
6263
if ((!shop.isUnlimited() || !ignoreUnlimited) && !shop.isDeleted()) {
6364
UUID shopOwner = shop.getOwner();
6465
Location location = shop.getLocation();
66+
double cost = plugin.getConfig().getDouble("shop.ongoing-fee.cost-per-shop");
6567
if (!location.isWorldLoaded()) {
6668
//ignore unloaded world
6769
continue;
@@ -74,12 +76,21 @@ public void run() {
7476
taxAccount = new Trader(shop.getTaxAccount().toString(), Bukkit.getOfflinePlayer(shop.getTaxAccount()));
7577
else
7678
taxAccount = plugin.getShopManager().getCacheTaxAccount();
79+
80+
ShopOngoingFeeEvent event = new ShopOngoingFeeEvent(shop,shopOwner,cost);
81+
if(Util.fireCancellableEvent(event))
82+
continue;
83+
84+
cost = event.getCost();
85+
double finalCost = cost;
86+
7787
Util.mainThreadRun(() -> {
7888
EconomyTransaction transaction = EconomyTransaction.builder()
7989
.allowLoan(allowLoan)
8090
.currency(shop.getCurrency())
8191
.core(plugin.getEconomy())
8292
.world(world)
93+
.amount(finalCost)
8394
.to(taxAccount == null ? null : taxAccount.getUniqueId())
8495
.from(shopOwner).build();
8596

0 commit comments

Comments
 (0)