Skip to content

Commit

Permalink
feat: Add taxes account
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Mar 24, 2024
1 parent 1bf02da commit b022f57
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +11,17 @@
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;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;

/**
* @author Blank038
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public class MarketData {
* 商品列表
*/
private final Map<String, String> 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<String> 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
Expand Down Expand Up @@ -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));
Expand All @@ -113,15 +113,6 @@ public MarketData(File file) {
Bukkit.getPluginManager().callEvent(event);
}

/**
* 获取货币类型
*
* @return 货币类型枚举
*/
public PayType getPayType() {
return this.paytype;
}

/**
* 获取玩家在权限节点上的值
*
Expand Down Expand Up @@ -172,26 +163,29 @@ 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;
}
Optional<SaleCache> 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@ public OfflineTransactionTask() {

@Override
public synchronized void run() {

Bukkit.getOnlinePlayers().forEach(this::checkResult);
}

private void checkResult(Player player) {
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ settings:
# 冷却设定, 单位: 秒
cooldown:
action: 3
# 税收流向账户
tax-account: "Blank038"
# 税收流向账户, 参数类型: UUID
tax-account: "UUID"

0 comments on commit b022f57

Please sign in to comment.