Skip to content

Commit

Permalink
feat: Add option to set sale maximum for personal
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Nov 1, 2023
1 parent ecbf321 commit 3c9c3b2
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MarketData {
private final List<String> loreBlackList, typeBlackList, saleTypes;
private final int min, max, effectiveTime;
private final PayType paytype;
private final ConfigurationSection taxSection, shoutTaxSection;
private final ConfigurationSection taxSection, shoutTaxSection, limitCountSection;
private MarketStatus marketStatus;
private boolean showSaleInfo, saleBroadcast;
private String dateFormat;
Expand All @@ -70,6 +70,7 @@ public MarketData(File file) {
this.saleTypes = options.getStringList("types");
this.taxSection = options.getConfigurationSection("tax");
this.shoutTaxSection = options.getConfigurationSection("shout-tax");
this.limitCountSection = options.getConfigurationSection("limit-count");
this.showSaleInfo = options.getBoolean("show-sale-info");
this.saleBroadcast = options.getBoolean("sale-broadcast");
this.dateFormat = options.getString("simple-date-format");
Expand Down Expand Up @@ -117,11 +118,14 @@ public PayType getPayType() {

/**
* 获取扣税后的价格
* <p>
* TODO: 此方法将在未来版本移除, 将改用为 MarketData#getPermsValueForPlayer 方法
*
* @param player 目标玩家
* @param money 初始金币
* @return 扣税后金币
*/
@Deprecated
public double getLastMoney(ConfigurationSection section, Player player, double money) {
String header = section.getString("header");
double tax = section.getDouble("node.default");
Expand All @@ -134,7 +138,13 @@ public double getLastMoney(ConfigurationSection section, Player player, double m
return money - money * tax;
}

public double getTax(ConfigurationSection section, Player player) {
/**
* 获取玩家在权限节点上的值
*
* @param player 目标玩家
* @return 最终值
*/
private double getPermsValueForPlayer(ConfigurationSection section, Player player) {
String header = section.getString("header");
double tax = section.getDouble("node.default");
for (String key : section.getConfigurationSection("node").getKeys(false)) {
Expand Down Expand Up @@ -304,15 +314,24 @@ public boolean performSellCommand(Player player, String message) {
.replace("%max%", extraPrice.split("-")[1]));
return true;
}
double tax = this.getTax(this.getShoutTaxSection(), player);
// 判断玩家上架物品是否上限
int currentCount = ServerMarket.getStorageHandler().getSaleCountByPlayer(player.getUniqueId(), this.marketKey);
if (currentCount >= this.getPermsValueForPlayer(this.limitCountSection, player)) {
player.sendMessage(I18n.getStrAndHeader("maximum-sale"));
return true;
}
// 判断余额是否足够交上架税
double tax = this.getPermsValueForPlayer(this.getShoutTaxSection(), player);
if (BaseEconomy.getEconomyBridge(this.paytype).balance(player, this.ecoType) < tax) {
player.sendMessage(I18n.getStrAndHeader("shout-tax")
.replace("%economy%", this.economyName));
return true;
}
// 扣除费率
if (tax > 0) {
BaseEconomy.getEconomyBridge(this.paytype).take(player, this.ecoType, tax);
if (tax > 0 && !BaseEconomy.getEconomyBridge(this.paytype).take(player, this.ecoType, tax)) {
player.sendMessage(I18n.getStrAndHeader("shout-tax")
.replace("%economy%", this.economyName));
return true;
}
// 设置玩家手中物品为空
player.getInventory().setItemInMainHand(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* It's only used in YAML storage mode.
Expand Down Expand Up @@ -50,6 +52,12 @@ public Map<String, SaleCache> getSales() {
return this.saleMap;
}

public Map<String, SaleCache> getSaleByPlayer(UUID uuid) {
return this.saleMap.entrySet().stream()
.filter((entry) -> entry.getValue().getOwnerUUID().equals(uuid.toString()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public String getMarketSourceId() {
return this.market;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public interface IStorageHandler {
*/
boolean hasSale(String market, String saleId);

/**
* 获取玩家在目标市场上架的商品数量
*
* @param uuid 目标玩家 UUID
* @param market 目标市场
* @return 已上架商品数量
*/
int getSaleCountByPlayer(UUID uuid, String market);

/**
* 获取商品数据
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

Expand Down Expand Up @@ -80,6 +81,23 @@ public boolean hasSale(String market, String saleId) {
return atomicBoolean.get();
}

@Override
public int getSaleCountByPlayer(UUID uuid, String market) {
AtomicInteger count = new AtomicInteger(0);
storageHandler.connect((preparedStatement) -> {
try (ResultSet resultSet = preparedStatement.executeQuery()){
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, market);
if (resultSet.next()) {
count.set(resultSet.getInt(1));
}
} catch (SQLException e) {
ServerMarket.getInstance().getLogger().log(Level.WARNING, e, () -> "Failed to get sale count.");
}
}, "SELECT count(*) FROM " + this.salesTable + " WHERE owner_uuid = ? AND market = ?;");
return count.get();
}

@Override
public Optional<SaleCache> getSaleItem(String market, String saleId) {
AtomicReference<SaleCache> reference = new AtomicReference<>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public boolean hasSale(String market, String saleId) {
return false;
}

@Override
public int getSaleCountByPlayer(UUID uuid, String market) {
if (MARKET_STORAGE_DATA_MAP.containsKey(market)) {
return MARKET_STORAGE_DATA_MAP.get(market).getSaleByPlayer(uuid).size();
}
return 0;
}

@Override
public Optional<SaleCache> getSaleItem(String market, String saleId) {
if (MARKET_STORAGE_DATA_MAP.containsKey(market)) {
Expand Down
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 @@ -46,6 +46,7 @@ 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."
maximum-sale: "You have reached the maximum number of listings."
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 @@ -46,6 +46,7 @@ lack-money: "你没有足够的%economy%来购买这个商品."
shout-tax: "你没有足够的%economy%来上缴税费."
changeSaleType: "已切换分类, 当前分类: &b%type%"
cooldown: "你不能这么做, 正在冷却中."
maximum-sale: "你已经达到上架数量上限."
show:
- "&6市场用法"
- " "
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/market/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ shout-tax:
node:
default: 0.1
vip: 0.05
# 玩家上架物品数量限制(当前市场)
# 权限: header.node(例如: servermarket.limit.count.default)
# 默认玩家拥有 default 节点权限
limit-count:
header: "servermarket.limit.count"
node:
default: 3
vip: 10
# 日期格式
simple-date-format: "yyyy/MM/dd HH:mm:ss"
# 商品超时时间, 单位: 秒
Expand Down

0 comments on commit 3c9c3b2

Please sign in to comment.