Skip to content

Commit c4045d6

Browse files
committed
fix: resolve multiple bugs in providers, commands, config and data migration
1 parent ccc1a92 commit c4045d6

9 files changed

Lines changed: 99 additions & 62 deletions

File tree

src/main/java/me/onebone/economyapi/AsyncOperator.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ public void onRun() {
181181
Server.getInstance().getPluginManager().callEvent(event);
182182

183183
if (!event.isCancelled() || force) {
184+
double finalAmount = event.getAmount();
184185
double money = EconomyAPI.getInstance().provider.getMoney(currencyName, lowerId);
185186
if (money != -1) {
186-
if (money + amount > EconomyAPI.getInstance().getMaxMoney(currencyName)) {
187+
if (money + finalAmount > EconomyAPI.getInstance().getMaxMoney(currencyName)) {
187188
this.setResult(EconomyAPI.RET_INVALID);
188189
} else {
189-
EconomyAPI.getInstance().provider.addMoney(currencyName, lowerId, amount);
190+
EconomyAPI.getInstance().provider.addMoney(currencyName, lowerId, finalAmount);
190191
this.setResult(EconomyAPI.RET_SUCCESS);
191192
}
192193
} else {
@@ -523,15 +524,18 @@ private Optional<UUID> checkAndConvertLegacy(String id) {
523524

524525
private void checkAndConvertLegacy(UUID uuid, String name) {
525526
name = name.toLowerCase();
526-
if (!EconomyAPI.getInstance().provider.accountExists(name)) {
527-
return;
528-
}
529-
if (EconomyAPI.getInstance().provider.accountExists(uuid.toString())) {
530-
EconomyAPI.getInstance().provider.removeAccount(name);
531-
return;
527+
String uuidStr = uuid.toString().toLowerCase();
528+
for (String currencyName : EconomyAPI.MAIN_CONFIG.getCurrencyList()) {
529+
if (!EconomyAPI.getInstance().provider.accountExists(currencyName, name)) {
530+
continue;
531+
}
532+
if (EconomyAPI.getInstance().provider.accountExists(currencyName, uuidStr)) {
533+
EconomyAPI.getInstance().provider.removeAccount(currencyName, name);
534+
continue;
535+
}
536+
double money = EconomyAPI.getInstance().provider.getMoney(currencyName, name);
537+
EconomyAPI.getInstance().provider.createAccount(currencyName, uuidStr, money);
538+
EconomyAPI.getInstance().provider.removeAccount(currencyName, name);
532539
}
533-
double money = EconomyAPI.getInstance().provider.getMoney(name);
534-
EconomyAPI.getInstance().provider.createAccount(uuid.toString(), money);
535-
EconomyAPI.getInstance().provider.removeAccount(name);
536540
}
537541
}

src/main/java/me/onebone/economyapi/EconomyAPI.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ private boolean createAccountInternal(String id, double defaultMoney, boolean fo
125125
CreateAccountEvent event = new CreateAccountEvent(id, defaultMoney);
126126
this.getServer().getPluginManager().callEvent(event);
127127
if (!event.isCancelled() || force) {
128-
defaultMoney = event.getDefaultMoney() == -1D ? this.getDefaultMoney() : event.getDefaultMoney();
129128
boolean failed = false;
130129
for (String currencyName : MAIN_CONFIG.getCurrencyList()) {
131-
failed = failed || !this.provider.createAccount(currencyName, id, defaultMoney);
130+
double money = event.getDefaultMoney() == -1D ? getDefaultMoney(currencyName) : event.getDefaultMoney();
131+
failed = failed || !this.provider.createAccount(currencyName, id, money);
132132
}
133-
return !failed;// usually return true.
133+
return !failed;
134134
}
135135
return false;
136136
}
@@ -270,6 +270,7 @@ private int addMoneyInternal(String id, double amount, boolean force) {
270270
AddMoneyEvent event = new AddMoneyEvent(id, amount);
271271
this.getServer().getPluginManager().callEvent(event);
272272
if (!event.isCancelled() || force) {
273+
amount = event.getAmount();
273274
double money;
274275
if ((money = this.provider.getMoney(id)) != -1) {
275276
if (money + amount > this.getMaxMoney()) {
@@ -498,6 +499,7 @@ private int addMoneyInternal(String id, double amount, String currencyName, bool
498499
AddMoneyEvent event = new AddMoneyEvent(id, amount, currencyName);
499500
this.getServer().getPluginManager().callEvent(event);
500501
if (!event.isCancelled() || force) {
502+
amount = event.getAmount();
501503
double money = this.provider.getMoney(currencyName, id);
502504
if (money != -1) {
503505
if (money + amount > getMaxMoney(currencyName)) {
@@ -665,6 +667,8 @@ public void onEnable() {
665667
if (tryUpgradeSQLiteData()) {
666668
EconomyAPI.getInstance().getLogger().info("SQLite data upgrade complete.");
667669
}
670+
} else {
671+
return;
668672
}
669673
} else {
670674
EconomyAPI.getInstance().saveDefaultConfig();
@@ -687,8 +691,8 @@ public void onJoin(PlayerJoinEvent event) {
687691
@Override
688692
public void onDisable() {
689693
this.saveAll();
690-
if (MAIN_CONFIG.getProvider().equals("mysql")) {
691-
provider.close();
694+
if (this.provider != null) {
695+
this.provider.close();
692696
}
693697
}
694698

@@ -764,18 +768,19 @@ private Optional<UUID> checkAndConvertLegacy(String id) {
764768

765769
private void checkAndConvertLegacy(UUID uuid, String name) {
766770
name = name.toLowerCase();
767-
if (!provider.accountExists(name)) {
768-
return;
769-
}
770-
771-
if (provider.accountExists(uuid.toString())) {
772-
provider.removeAccount(name);
773-
return;
771+
String uuidStr = uuid.toString().toLowerCase();
772+
for (String currencyName : MAIN_CONFIG.getCurrencyList()) {
773+
if (!provider.accountExists(currencyName, name)) {
774+
continue;
775+
}
776+
if (provider.accountExists(currencyName, uuidStr)) {
777+
provider.removeAccount(currencyName, name);
778+
continue;
779+
}
780+
double money = provider.getMoney(currencyName, name);
781+
provider.createAccount(currencyName, uuidStr, money);
782+
provider.removeAccount(currencyName, name);
774783
}
775-
776-
double money = provider.getMoney(name);
777-
provider.createAccount(uuid.toString(), money);
778-
provider.removeAccount(name);
779784
}
780785

781786
private void initServerLangCode() {

src/main/java/me/onebone/economyapi/command/GiveMoneyCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
5656
if (!this.plugin.isEnabled()) return false;
5757
LangCode langCode = sender instanceof Player ? ((Player) sender).getLanguageCode() : serverLangCode;
5858
if (!sender.hasPermission("economyapi.command.givemoney")) {
59-
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%cofalsemmands.generic.permission"));
59+
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.permission"));
6060
return false;
6161
}
6262

src/main/java/me/onebone/economyapi/command/PayCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
118118

119119
sender.sendMessage(EconomyAPI.getI18n().tr(langCode, "pay-success", EconomyAPI.MONEY_FORMAT.format(amount), plugin.getMonetaryUnit(currencyName), player));
120120
if (p != null) {
121-
p.sendMessage(EconomyAPI.getI18n().tr(langCode, "money-paid", sender.getName(), EconomyAPI.MONEY_FORMAT.format(amount), plugin.getMonetaryUnit(currencyName)));
121+
p.sendMessage(EconomyAPI.getI18n().tr(p.getLanguageCode(), "money-paid", sender.getName(), EconomyAPI.MONEY_FORMAT.format(amount), plugin.getMonetaryUnit(currencyName)));
122122
}
123123
break;
124124
}

src/main/java/me/onebone/economyapi/command/TopMoneyCommand.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,17 @@ public boolean execute(final CommandSender sender, String label, final String[]
7979
return false;
8080
}
8181

82-
int arg = args.length > 0 ? Integer.parseInt(args[0]) : 1;
82+
int arg;
83+
if (args.length > 0) {
84+
try {
85+
arg = Integer.parseInt(args[0]);
86+
} catch (NumberFormatException e) {
87+
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.getUsage()));
88+
return false;
89+
}
90+
} else {
91+
arg = 1;
92+
}
8393
final String currencyName = args.length >= 2 ? args[1] : MAIN_CONFIG.getDefaultCurrency().getName();
8494

8595
sender.getServer().getScheduler().scheduleTask(EconomyAPI.getInstance(), () -> {

src/main/java/me/onebone/economyapi/config/EconomyAPIConfig.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,30 @@ public Config getConfig() {
5050

5151
// 获取默认货币
5252
public Currency getDefaultCurrency() {
53-
return currencies.get(defaultCurrency);
53+
Currency currency = currencies.get(defaultCurrency);
54+
if (currency == null) {
55+
throw new IllegalStateException("Default currency '" + defaultCurrency + "' not found in config. Check 'data.default-currency' and 'currencies' in config.yml.");
56+
}
57+
return currency;
5458
}
5559

5660
// 获取特定货币的信息
5761
public Currency getCurrency(String name) {
58-
return currencies.get(name);
62+
Currency currency = currencies.get(name);
63+
if (currency == null) {
64+
throw new IllegalArgumentException("Currency '" + name + "' not found in config. Available currencies: " + currencies.keySet());
65+
}
66+
return currency;
5967
}
6068

6169
// 获取货币种类列表
6270
public List<String> getCurrencyList() {
6371
return currencies.keySet().stream().toList();
6472
}
6573

66-
// 获取auto-save-interval
74+
// 获取auto-save-interval(单位:分钟)
6775
public int getAutoSaveInterval() {
68-
return autoSaveInterval * 1200;
76+
return autoSaveInterval;
6977
}
7078

7179
// 获取provider

src/main/java/me/onebone/economyapi/config/UpgradeConfig.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public static boolean updateDoubleConfirmation() {
5151
if (new Scanner(System.in).nextLine().toLowerCase().startsWith("y")) {
5252
System.out.println("User confirmed the upgrade. Starting the upgrade process...");
5353
} else {
54-
System.out.println("The plugin version is too high and does not support this configuration file. Please use version 2.0.6 of EconomyAPI!");
55-
System.exit(1);
54+
EconomyAPI.getInstance().getLogger().warning("The plugin version is too high and does not support this configuration file. Please use version 2.0.6 of EconomyAPI!");
55+
EconomyAPI.getInstance().getServer().getPluginManager().disablePlugin(EconomyAPI.getInstance());
56+
return false;
5657
}
5758

5859
EconomyAPI.getInstance().saveDefaultConfig();
@@ -104,46 +105,52 @@ public static boolean tryUpgradeSQLiteData() {
104105
EconomyAPI.getInstance().getLogger().info("SQLite is not enabled, no upgrade is required.");
105106
return false;
106107
}
107-
Path sourceFile = Paths.get(EconomyAPI.getInstance().getDataFolder().toString(), "MoneyV3.db");
108-
Path targetFile = Paths.get(EconomyAPI.getInstance().getDataFolder().toString(), "Money.db.old");
109-
try {
110-
Files.move(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
111-
} catch (IOException ignored) {
112-
}
108+
String dataFolder = EconomyAPI.getInstance().getDataFolder().getAbsolutePath();
109+
Path sourceFile = Paths.get(dataFolder, "MoneyV3.db");
110+
Path backupFile = Paths.get(dataFolder, "MoneyV3.db.old");
111+
113112
if (!Files.exists(sourceFile)) {
114113
EconomyAPI.getInstance().getLogger().warning("MoneyV3.db file not found.");
115114
return false;
116115
}
116+
117+
try {
118+
Files.move(sourceFile, backupFile, StandardCopyOption.REPLACE_EXISTING);
119+
} catch (IOException e) {
120+
EconomyAPI.getInstance().getLogger().warning("Failed to backup MoneyV3.db.", e);
121+
return false;
122+
}
123+
117124
String TABLE_NAME = "Money";
118125
try {
119-
SQLiteHelper oldSQLiteHelper = new SQLiteHelper(EconomyAPI.getInstance().getDataFolder().getAbsolutePath() + File.separator + "Money.old.db");
126+
SQLiteHelper oldSQLiteHelper = new SQLiteHelper(backupFile.toAbsolutePath().toString());
120127
if (!oldSQLiteHelper.exists(TABLE_NAME)) {
121-
System.out.println("Old data table 'Money' not found in Money.old.db, data migration skipped.");
128+
EconomyAPI.getInstance().getLogger().warning("Old data table 'Money' not found in MoneyV3.db.old, data migration skipped.");
122129
return false;
123130
}
124131
List<OldMoneyData> oldMoneyDataList = oldSQLiteHelper.getAll(TABLE_NAME, OldMoneyData.class);
125132
if (oldMoneyDataList == null) {
126-
System.out.println("Failed to read data from 'Money' table in Money.old.db, data migration aborted.");
133+
EconomyAPI.getInstance().getLogger().warning("Failed to read data from 'Money' table, data migration aborted.");
127134
return false;
128135
}
129-
SQLiteHelper sqLiteHelper = new SQLiteHelper(EconomyAPI.getInstance().getDataFolder().getAbsolutePath() + File.separator + "MoneyV3.db");
136+
SQLiteHelper sqLiteHelper = new SQLiteHelper(dataFolder + File.separator + "MoneyV3.db");
130137
if (!sqLiteHelper.exists(TABLE_NAME)) {
131138
sqLiteHelper.addTable(TABLE_NAME, SQLiteHelper.DBTable.asDbTable(SQLiteProvider.MoneyData.class));
132139
}
133140
for (OldMoneyData data : oldMoneyDataList) {
134-
// 将 OldMoneyData 转换为 MoneyData
135141
SQLiteProvider.MoneyData moneyData = new SQLiteProvider.MoneyData(data.getPlayer(), data.getMoney());
136142
moneyData.setCurrency(EconomyAPI.MAIN_CONFIG.getDefaultCurrency().getName());
137-
138-
sqLiteHelper.add(TABLE_NAME, moneyData); // 插入新的 MoneyData 对象
143+
sqLiteHelper.add(TABLE_NAME, moneyData);
139144
}
140-
System.out.println("SQLite data migration complete!"); // 添加迁移完成的提示
145+
oldSQLiteHelper.close();
146+
sqLiteHelper.close();
147+
EconomyAPI.getInstance().getLogger().info("SQLite data migration complete!");
141148
return true;
142149
} catch (ClassNotFoundException e) {
143150
EconomyAPI.getInstance().getLogger().warning("Failed to upgrade MoneyV3.db data, runtime error.", e);
144151
return false;
145152
} catch (SQLException e) {
146-
EconomyAPI.getInstance().getLogger().warning("Failed to upgrade Money.yml data, sql exception.", e);
153+
EconomyAPI.getInstance().getLogger().warning("Failed to upgrade MoneyV3.db data, sql exception.", e);
147154
return false;
148155
}
149156
}

src/main/java/me/onebone/economyapi/provider/MySQLProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public boolean removeAccount(String id) {
117117
public boolean createAccount(String currencyName, String id, double defaultMoney) {
118118
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return false;
119119
// convert money to bigint
120-
long money = (long) defaultMoney * 100;
120+
long money = (long) (defaultMoney * 100);
121121
if (!accountExists(currencyName, id)) {
122122
SqlData sqlData = new SqlData("player", id).put("money", money);
123123
return MySQLProvider.manager.insertData(TABLE_NAME_PREFIX + currencyName, sqlData);
@@ -133,7 +133,7 @@ public boolean createAccount(String id, double defaultMoney) {
133133
@Override
134134
public boolean setMoney(String currencyName, String id, double amount) {
135135
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return false;
136-
long money = (long) amount * 100;
136+
long money = (long) (amount * 100);
137137
return MySQLProvider.manager.setData(TABLE_NAME_PREFIX + currencyName, new SqlData("money", money), new SqlData("player", id));
138138
}
139139

@@ -145,7 +145,7 @@ public boolean setMoney(String id, double amount) {
145145
@Override
146146
public boolean addMoney(String currencyName, String id, double amount) {
147147
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return false;
148-
long money = (long) (getMoney(currencyName, id) + amount) * 100;
148+
long money = (long) ((getMoney(currencyName, id) + amount) * 100);
149149
return MySQLProvider.manager.setData(TABLE_NAME_PREFIX + currencyName, new SqlData("money", money), new SqlData("player", id));
150150
}
151151

@@ -157,7 +157,7 @@ public boolean addMoney(String id, double amount) {
157157
@Override
158158
public boolean reduceMoney(String currencyName, String id, double amount) {
159159
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return false;
160-
long money = (long) (getMoney(currencyName, id) - amount) * 100;
160+
long money = (long) ((getMoney(currencyName, id) - amount) * 100);
161161
return MySQLProvider.manager.setData(TABLE_NAME_PREFIX + currencyName, new SqlData("money", money), new SqlData("player", id));
162162
}
163163

@@ -168,9 +168,9 @@ public boolean reduceMoney(String id, double amount) {
168168

169169
@Override
170170
public double getMoney(String currencyName, String id) {
171-
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return 0;
171+
if (!MAIN_CONFIG.getCurrencyList().contains(currencyName)) return -1;
172172
SqlDataList<SqlData> sqlDataList = MySQLProvider.manager.getData(TABLE_NAME_PREFIX + currencyName, "money", new SqlData("player", id));
173-
if (sqlDataList.isEmpty()) return 0;
173+
if (sqlDataList.isEmpty()) return -1;
174174
return sqlDataList.get(0).getLong("money") / 100.0;
175175
}
176176

@@ -186,7 +186,7 @@ public LinkedHashMap<String, Double> getAll(String currencyName) {
186186
SqlData emptyData = new SqlData();
187187
SqlDataList<SqlData> sqlDataList = MySQLProvider.manager.getData(TABLE_NAME_PREFIX + currencyName, "*", emptyData);
188188
if (sqlDataList == null) {
189-
return null;
189+
return map;
190190
}
191191
for (SqlData sqlData : sqlDataList) {
192192
LinkedHashMap<String, Object> data = sqlData.getData();

src/main/java/me/onebone/economyapi/provider/SQLiteProvider.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ public boolean removeAccount(String currencyName, String id) {
7474
if (!accountExists(currencyName, id)) {
7575
return false;
7676
}
77-
this.sqLiteHelper.remove(TABLE_NAME, COLUMN_PLAYER, id); // 移除账户时使用 player 作为 key,currency 通过 MoneyData 对象内部 currency 字段区分
78-
this.cache.remove(getCacheKey(currencyName, id)); // 从缓存中移除
77+
MoneyData moneyData = this.getMoneyData(currencyName, id);
78+
if (moneyData != null) {
79+
this.sqLiteHelper.remove(TABLE_NAME, (int) moneyData.getId());
80+
}
81+
this.cache.remove(getCacheKey(currencyName, id));
7982
return true;
8083
}
8184

@@ -108,8 +111,8 @@ public boolean setMoney(String currencyName, String id, double amount) {
108111
}
109112
MoneyData moneyData = this.getMoneyData(currencyName, id);
110113
moneyData.setMoney(amount);
111-
this.sqLiteHelper.set(TABLE_NAME, COLUMN_PLAYER, id, moneyData); // 使用 player 作为 key 更新,MoneyData 对象内部包含 currency 信息
112-
this.cache.put(getCacheKey(currencyName, id), moneyData); // 更新缓存
114+
this.sqLiteHelper.set(TABLE_NAME, COLUMN_ID, String.valueOf(moneyData.getId()), moneyData);
115+
this.cache.put(getCacheKey(currencyName, id), moneyData);
113116
return true;
114117
}
115118

0 commit comments

Comments
 (0)