Skip to content

Commit

Permalink
optimize the CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tining123 committed Jul 18, 2022
1 parent f1310ca commit b4e6114
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Set diamond block with $440.
+ may-pay: Max amount of money can be transfer in a transaction.Set -1 to disable limitation, set 0 to disable pay command.
+ pay-unit: Transfer unit. All transfer amount will be split as pay-unit to transfer. The tax will be calculated times.
+ payer-tax: whether the payer pays the transfer tax (the tax payed by the receiver in default)
+ auto-refresh: Enable to auto refresh the price in gui
+ auto-refresh-gap: Refresh interval (second per time)
## Mathematical Theory
+ TAX=(1 - TaxRate)

Expand Down
2 changes: 2 additions & 0 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
+ may-pay: 单次转账最大金额限制,默认设置为-1不限制,设置为0则等于关闭交易功能。
+ pay-unit: 转账单位。大额转账将会被拆分成转账单位级的小额转账自动多次计算。
+ payer-tax: 是否由付款人支付转账税款(默认由接收人所受金额扣除)
+ auto-refresh: 是否开启自动刷新结算价格
+ auto-refresh-gap: 自动刷间隔(秒)
## 理论支持
以下为计算价格的实际公式。 其中
+ price=物品设定价格
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.Tining</groupId>
<artifactId>DemonMarket</artifactId>
<version>1.3.6</version>
<version>1.3.7</version>
<build>
<plugins>
<plugin>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/tining/demonmarket/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public void onDisable() {
public void onEnable() {
instance = this;

//释放配置文件
// 释放配置文件
saveDefaultConfig();
ConfigReader.initRelease();
ConfigReader.reloadConfig();
//TODO: 如果有设置强制预言,加载强制语言
// 如果有设置强制预言,加载强制语言
if (!Objects.isNull(ConfigReader.getLanguage()) && !StringUtils.isEmpty(ConfigReader.getLanguage())) {
LangReader.setLanguage(ConfigReader.getLanguage());
}
Expand All @@ -80,7 +80,14 @@ public void onEnable() {
ConfigReader.getDisablePayList();

//注册
Bukkit.getScheduler().runTaskTimerAsynchronously(this, new ChestDrawTask(), 0L, 1L);
if(ConfigReader.getEnableAutoRefresh()){
long interval = (long) ConfigReader.getAutoRefreshInterval();
if(interval < 20){
interval = 20L;
}
Bukkit.getScheduler().runTaskTimerAsynchronously(this, new ChestDrawTask(), 0L, interval);
}
// Bukkit.getScheduler().runTaskTimerAsynchronously(this, new ChestDrawTask(), 0L, 1L);

}

Expand Down
49 changes: 46 additions & 3 deletions src/main/java/com/tining/demonmarket/common/util/WorthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.tining.demonmarket.Main;
import com.tining.demonmarket.storage.ConfigReader;
import com.tining.demonmarket.storage.ConfigFileNameEnum;
import org.apache.commons.collections.CollectionUtils;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -16,6 +18,10 @@
*/
public class WorthUtil {

private static Map<String, Double> nbtWorth;

private static Map<String, Double> worth;

/**
* 获取物品价值
*
Expand Down Expand Up @@ -130,11 +136,30 @@ public static boolean isWorthNBTContain(ItemStack is) {
return false;
}

/**
* 获取价值表
* @return
*/
public static Map<String, Double> getWorth() {
if(Objects.isNull(worth)){
worth = loadWorth();
}
return worth;
}

/**
* 重载价值表
*/
public static void reloadWorth(){
worth = loadWorth();
}


/**
* 获取物品总价值表
* @return 物品总价值表
*/
public static Map<String, Double> getWorth() {
public static Map<String, Double> loadWorth() {
FileConfiguration config = getWorthConfig();
Map<String, Double> value = new HashMap<>();
Map<String, Object> data = config.getConfigurationSection("worth").getValues(false);
Expand All @@ -144,17 +169,35 @@ public static Map<String, Double> getWorth() {
return value;
}

/**
* 获取NBT价值表
* @return
*/
public static Map<String, Double> getNBTWorth() {
if(Objects.isNull(nbtWorth)){
nbtWorth = loadNBTWorth();
}
return nbtWorth;
}

/**
* 重载NBT价值表
*/
public static void reloadNBTWorth(){
nbtWorth = loadNBTWorth();
}

/**
* 获取NBT物品总价值表
* @return NBT物品总价值表
*/
public static Map<String, Double> getNBTWorth() {
public static Map<String, Double> loadNBTWorth() {
FileConfiguration config = getNBTWorthConfig();
Map<String, Double> value = new HashMap<>();
if (Objects.isNull(config.getConfigurationSection("nbtworth"))) {
config.addDefault("nbtworth", value);
config.set("nbtworth", value);
ConfigReader.reloadConfig();
ConfigReader.saveConfig(ConfigFileNameEnum.NBT_WORTH_FILE_NAME.getName(),config);
}
if (!Objects.isNull(config.getConfigurationSection("nbtworth"))) {
Map<String, Object> data = config.getConfigurationSection("nbtworth").getValues(false);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/tining/demonmarket/event/ChestGuiEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Objects;
import java.util.UUID;

/**
* @author tinga
Expand Down Expand Up @@ -74,4 +76,19 @@ public void disableMove(InventoryClickEvent e) {
}
}

@EventHandler
public void refresh(InventoryClickEvent e){
if (e.getWhoClicked() instanceof Player && e.getClickedInventory() != null) {
Player player = (Player) e.getWhoClicked();
if (ChestGui.isChestGui(player)) {
new BukkitRunnable() {
@Override
public void run() {
ChestGui.drawPage(player);
}
}.runTask(Main.getInstance());
}
}
}

}
13 changes: 12 additions & 1 deletion src/main/java/com/tining/demonmarket/storage/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tining.demonmarket.storage;

import com.tining.demonmarket.Main;
import com.tining.demonmarket.common.util.WorthUtil;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

Expand Down Expand Up @@ -58,8 +59,10 @@ public static void reloadConfig() {
String configName = w.getName();
FileConfiguration configuration = YamlConfiguration.loadConfiguration(new File(ROOT_FOLDER, configName));
configMap.put(configName, configuration);

}
// 重载价格
WorthUtil.reloadWorth();
WorthUtil.reloadNBTWorth();
}

/**
Expand Down Expand Up @@ -238,4 +241,12 @@ public static double getPayUnit(){
* @return
*/
public static boolean getPayerTax(){return ConfigReader.config.getBoolean("payer-tax");}

/**
* 获取是否自动刷新
* @return
*/
public static boolean getEnableAutoRefresh(){return ConfigReader.config.getBoolean("auto-refresh");}

public static int getAutoRefreshInterval(){return ConfigReader.config.getInt("auto-refresh-gap") * 20;}
}
6 changes: 5 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ disable-sell-list:
- "/eco sell"

# Payer pay for the tax
payer-tax: false
payer-tax: false

# enable to auto refresh the price in gui(second per time)
auto-refresh: true
auto-refresh-gap: 1
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: DemonMarket
main: com.tining.demonmarket.Main
version: 1.3.6
version: 1.3.7
api-version: 1.13
# YAML 中的注释,一行有效
# 以上是插件基本信息,以下是命令注册
Expand Down

0 comments on commit b4e6114

Please sign in to comment.