Skip to content

Commit

Permalink
feat: Add action cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Nov 1, 2023
1 parent 941191b commit 15925bb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/main/java/com/blank038/servermarket/gui/AbstractGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ public abstract class AbstractGui {

static {
Bukkit.getScheduler().runTaskTimerAsynchronously(ServerMarket.getInstance(), () -> {
COOLDOWN.entrySet().removeIf((entry) -> System.currentTimeMillis() > entry.getValue());
synchronized (COOLDOWN) {
COOLDOWN.entrySet().removeIf((entry) -> System.currentTimeMillis() > entry.getValue());
}
}, 1200L, 1200L);
}

public boolean isCooldown(UUID uuid) {
if (System.currentTimeMillis() <= COOLDOWN.getOrDefault(uuid, 0L)) {
return true;
}
COOLDOWN.put(uuid, System.currentTimeMillis() + ServerMarket.getInstance().getConfig().getInt("cooldown.action"));
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ public void openGui(Player player) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return;
}
Player clicker = (Player) e.getWhoClicked();
if (this.isCooldown(clicker.getUniqueId())) {
clicker.sendMessage(I18n.getStrAndHeader("cooldown"));
return;
}
NBTItem nbtItem = new NBTItem(itemStack);
String key = nbtItem.getString("SaleUUID"), action = nbtItem.getString("MarketAction");
// 强转玩家
Player clicker = (Player) e.getWhoClicked();
if (key != null && !key.isEmpty()) {
// 购买商品
DataContainer.MARKET_DATA.get(this.sourceMarketKey).tryBuySale(clicker, key, e.isShiftClick(), lastPage, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public void open(int currentPage) {
if (itemStack == null) {
return;
}
if (this.isCooldown(clicker.getUniqueId())) {
clicker.sendMessage(I18n.getStrAndHeader("cooldown"));
return;
}
NBTItem nbtItem = new NBTItem(itemStack);
String storeId = nbtItem.getString("StoreID"), action = nbtItem.getString("action");
if ("market".equalsIgnoreCase(action)) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ data-option:
# 如果未填写市场名的话, 默认操作市场
default-market: "example"
# 是否启用命令短写帮助
command-help: true
command-help: true
# 冷却设定, 单位: 秒
cooldown:
action: 3
1 change: 1 addition & 0 deletions src/main/resources/language/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ no-permission: "You do not have permission to do that."
lack-money: "You do not have enough %economy% to purchase this item."
shout-tax: "You do not have enough %economy% to pay the tax fee."
changeSaleType: "Switched category. Current category: &b%type%"
cooldown: "You cannot do this, it is in cooldown."
show:
- "&6Market usage"
- " "
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/language/zh_CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ no-permission: "你没有权限这么做."
lack-money: "你没有足够的%economy%来购买这个商品."
shout-tax: "你没有足够的%economy%来上缴税费."
changeSaleType: "已切换分类, 当前分类: &b%type%"
cooldown: "你不能这么做, 正在冷却中."
show:
- "&6市场用法"
- " "
Expand Down

0 comments on commit 15925bb

Please sign in to comment.