From b022f57b616d8dd5b7678a02c18e8542b4d6419a Mon Sep 17 00:00:00 2001 From: Blank038 Date: Mon, 25 Mar 2024 05:03:46 +0800 Subject: [PATCH] feat: Add taxes account --- .../servermarket/api/ServerMarketApi.java | 24 +++++++++++++ .../servermarket/api/entity/MarketData.java | 36 ++++++++----------- .../command/virtual/VirtualMarketCommand.java | 10 +++--- .../internal/task/OfflineTransactionTask.java | 11 ++++-- bukkit/src/main/resources/config.yml | 4 +-- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/bukkit/src/main/java/com/blank038/servermarket/api/ServerMarketApi.java b/bukkit/src/main/java/com/blank038/servermarket/api/ServerMarketApi.java index 29be216..512ff35 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/api/ServerMarketApi.java +++ b/bukkit/src/main/java/com/blank038/servermarket/api/ServerMarketApi.java @@ -1,6 +1,7 @@ package com.blank038.servermarket.api; import com.blank038.servermarket.api.platform.IPlatformApi; +import com.blank038.servermarket.internal.economy.BaseEconomy; import com.blank038.servermarket.internal.plugin.ServerMarket; import com.blank038.servermarket.internal.data.DataContainer; import com.blank038.servermarket.api.entity.MarketData; @@ -10,6 +11,8 @@ import com.blank038.servermarket.internal.gui.impl.MarketGui; import lombok.Getter; import lombok.Setter; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -17,6 +20,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.UUID; +import java.util.logging.Level; /** * @author Blank038 @@ -72,4 +77,23 @@ public static void addOfflineTransaction(String uuid, PayType payType, String ec OfflineTransactionData resultData = new OfflineTransactionData(section); ServerMarket.getStorageHandler().addOfflineTransaction(resultData); } + + public static void sendTaxes(PayType payType, String subData, double tax) { + if (tax <= 0) { + return; + } + String taxAccount = ServerMarket.getInstance().getConfig().getString("tax-account", ""); + if (taxAccount.isEmpty() || taxAccount.equalsIgnoreCase("UUID")) { + return; + } + try { + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(taxAccount)); + if (offlinePlayer == null) { + return; + } + BaseEconomy.getEconomyBridge(payType).give(offlinePlayer, subData, tax); + } catch (Exception e) { + ServerMarket.getInstance().getLogger().log(Level.WARNING, "Fail to send the tax payment to " + taxAccount); + } + } } diff --git a/bukkit/src/main/java/com/blank038/servermarket/api/entity/MarketData.java b/bukkit/src/main/java/com/blank038/servermarket/api/entity/MarketData.java index 661bebc..c41e248 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/api/entity/MarketData.java +++ b/bukkit/src/main/java/com/blank038/servermarket/api/entity/MarketData.java @@ -40,11 +40,11 @@ public class MarketData { * 商品列表 */ private final Map extraMap = new HashMap<>(); - private final String sourceId, marketKey, permission, shortCommand, ecoType, displayName, economyName; + private final String sourceId, marketKey, permission, shortCommand, economyType, displayName, economyName; private final IFilter deniedFilter; private final List saleTypes; private final int min, max, effectiveTime; - private final PayType paytype; + private final PayType paymentType; private final ConfigurationSection taxSection, shoutTaxSection, limitCountSection; private MarketStatus marketStatus; @Setter @@ -80,18 +80,18 @@ public MarketData(File file) { this.saleBroadcast = options.getBoolean("sale-broadcast"); this.dateFormat = options.getString("simple-date-format"); this.priceFormat = options.getString("price-format", "%.1f"); - switch ((this.ecoType = options.getString("vault-type").toLowerCase())) { + switch ((this.economyType = options.getString("vault-type").toLowerCase())) { case "vault": - this.paytype = PayType.VAULT; + this.paymentType = PayType.VAULT; break; case "playerpoints": - this.paytype = PayType.PLAYER_POINTS; + this.paymentType = PayType.PLAYER_POINTS; break; default: - this.paytype = PayType.NY_ECONOMY; + this.paymentType = PayType.NY_ECONOMY; break; } - if (!BaseEconomy.PAY_TYPES.containsKey(this.paytype)) { + if (!BaseEconomy.PAY_TYPES.containsKey(this.paymentType)) { this.marketStatus = MarketStatus.ERROR; ServerMarket.getInstance().getConsoleLogger().log(false, I18n.getProperties().getProperty("load-market-eco-not-exists").replace("%s", this.displayName)); @@ -113,15 +113,6 @@ public MarketData(File file) { Bukkit.getPluginManager().callEvent(event); } - /** - * 获取货币类型 - * - * @return 货币类型枚举 - */ - public PayType getPayType() { - return this.paytype; - } - /** * 获取玩家在权限节点上的值 * @@ -172,7 +163,7 @@ public void tryBuySale(Player buyer, String uuid, boolean shift, int page, Filte buyer.sendMessage(I18n.getStrAndHeader("error-sale")); return; } - if (BaseEconomy.getEconomyBridge(this.paytype).balance(buyer, this.ecoType) < saleItem.getPrice()) { + if (BaseEconomy.getEconomyBridge(this.paymentType).balance(buyer, this.economyType) < saleItem.getPrice()) { buyer.sendMessage(I18n.getStrAndHeader("lack-money") .replace("%economy%", this.economyName)); return; @@ -180,18 +171,21 @@ public void tryBuySale(Player buyer, String uuid, boolean shift, int page, Filte Optional optional = ServerMarket.getStorageHandler().removeSaleItem(this.sourceId, uuid); if (optional.isPresent()) { saleItem = optional.get(); - BaseEconomy.getEconomyBridge(this.paytype).take(buyer, this.ecoType, saleItem.getPrice()); + BaseEconomy.getEconomyBridge(this.paymentType).take(buyer, this.economyType, saleItem.getPrice()); Player seller = Bukkit.getPlayer(UUID.fromString(saleItem.getOwnerUUID())); if (seller != null && seller.isOnline()) { - double last = saleItem.getPrice() - saleItem.getPrice() * this.getPermsValueForPlayer(this.getTaxSection(), seller, false); + double tax = saleItem.getPrice() * this.getPermsValueForPlayer(this.getTaxSection(), seller, false); + double last = saleItem.getPrice() - tax; DecimalFormat df = new DecimalFormat("#0.00"); - BaseEconomy.getEconomyBridge(this.paytype).give(seller, this.ecoType, last); + BaseEconomy.getEconomyBridge(this.paymentType).give(seller, this.economyType, last); seller.sendMessage(I18n.getStrAndHeader("sale-sell") .replace("%economy%", this.economyName) .replace("%money%", df.format(saleItem.getPrice())) .replace("%last%", df.format(last))); + // send taxes + ServerMarketApi.sendTaxes(paymentType, this.economyType, tax); } else { - ServerMarketApi.addOfflineTransaction(saleItem.getOwnerUUID(), this.paytype, this.ecoType, saleItem.getPrice(), this.marketKey); + ServerMarketApi.addOfflineTransaction(saleItem.getOwnerUUID(), this.paymentType, this.economyType, saleItem.getPrice(), this.marketKey); } // 给予购买者物品 ServerMarket.getStorageHandler().addItemToStore(buyer.getUniqueId(), saleItem, "buy"); diff --git a/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java b/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java index e1184fa..cba0594 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java +++ b/bukkit/src/main/java/com/blank038/servermarket/internal/command/virtual/VirtualMarketCommand.java @@ -1,5 +1,6 @@ package com.blank038.servermarket.internal.command.virtual; +import com.blank038.servermarket.api.ServerMarketApi; import com.blank038.servermarket.internal.plugin.ServerMarket; import com.blank038.servermarket.api.event.PlayerSaleEvent; import com.blank038.servermarket.api.entity.MarketData; @@ -103,22 +104,23 @@ private void performSellCommand(Player player, String[] args) { } // 判断余额是否足够交上架税 double tax = this.marketData.getPermsValueForPlayer(this.marketData.getShoutTaxSection(), player, false); - if (BaseEconomy.getEconomyBridge(this.marketData.getPayType()).balance(player, this.marketData.getEcoType()) < tax) { + if (BaseEconomy.getEconomyBridge(this.marketData.getPaymentType()).balance(player, this.marketData.getEconomyType()) < tax) { player.sendMessage(I18n.getStrAndHeader("shout-tax") .replace("%economy%", this.marketData.getEconomyName())); return; } - // 扣除费率 - if (tax > 0 && !BaseEconomy.getEconomyBridge(this.marketData.getPayType()).take(player, this.marketData.getEcoType(), tax)) { + if (tax > 0 && !BaseEconomy.getEconomyBridge(this.marketData.getPaymentType()).take(player, this.marketData.getEconomyType(), tax)) { player.sendMessage(I18n.getStrAndHeader("shout-tax") .replace("%economy%", this.marketData.getEconomyName())); return; } + // send taxes + ServerMarketApi.sendTaxes(this.marketData.getPaymentType(), this.marketData.getEconomyType(), tax); // 设置玩家手中物品为空 player.getInventory().setItemInMainHand(null); // 上架物品 SaleCache saleItem = new SaleCache(UUID.randomUUID().toString(), this.marketData.getMarketKey(), player.getUniqueId().toString(), - player.getName(), itemStack, PayType.VAULT, this.marketData.getEcoType(), price, System.currentTimeMillis()); + player.getName(), itemStack, PayType.VAULT, this.marketData.getEconomyType(), price, System.currentTimeMillis()); // add sale to storage handler ServerMarket.getStorageHandler().addSale(this.marketData.getMarketKey(), saleItem); // call PlayerSaleEvent.Sell diff --git a/bukkit/src/main/java/com/blank038/servermarket/internal/task/OfflineTransactionTask.java b/bukkit/src/main/java/com/blank038/servermarket/internal/task/OfflineTransactionTask.java index 50b3a41..f71821a 100644 --- a/bukkit/src/main/java/com/blank038/servermarket/internal/task/OfflineTransactionTask.java +++ b/bukkit/src/main/java/com/blank038/servermarket/internal/task/OfflineTransactionTask.java @@ -7,6 +7,7 @@ import com.blank038.servermarket.internal.economy.BaseEconomy; import com.blank038.servermarket.internal.i18n.I18n; import com.blank038.servermarket.internal.plugin.ServerMarket; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.text.DecimalFormat; @@ -25,7 +26,7 @@ public OfflineTransactionTask() { @Override public synchronized void run() { - + Bukkit.getOnlinePlayers().forEach(this::checkResult); } private void checkResult(Player player) { @@ -34,7 +35,13 @@ private void checkResult(Player player) { // 获取市场数据 MarketData marketData = DataContainer.MARKET_DATA.getOrDefault(v.getSourceMarket(), null); // 获取可获得货币 - double price = v.getAmount(), last = marketData == null ? price : (price - price * marketData.getPermsValueForPlayer(marketData.getTaxSection(), player, false)); + double price = v.getAmount(), tax = 0; + if (marketData != null) { + tax = price * marketData.getPermsValueForPlayer(marketData.getTaxSection(), player, false); + } + double last = price - tax; + // send taxes + ServerMarketApi.sendTaxes(v.getPayType(), v.getEconomyType(), tax); // 判断货币桥是否存在 if (BaseEconomy.PAY_TYPES.containsKey(v.getPayType())) { DecimalFormat df = new DecimalFormat("#0.00"); diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index 3dcc345..50f7692 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -24,5 +24,5 @@ settings: # 冷却设定, 单位: 秒 cooldown: action: 3 -# 税收流向账户 -tax-account: "Blank038" \ No newline at end of file +# 税收流向账户, 参数类型: UUID +tax-account: "UUID" \ No newline at end of file