Skip to content

Commit 5aa7d00

Browse files
committed
Fix bug on initialisation that economy section could be reset in some environments?
1 parent 8665528 commit 5aa7d00

6 files changed

Lines changed: 42 additions & 18 deletions

File tree

src/main/java/com/rwconnected/serverkit/api/economy/ICurrency.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface ICurrency {
77

88
public Identifier getId();
99
public IAccount getDefaultAccount(IPlayer<?> player);
10+
String formatValue(long value);
1011
}

src/main/java/com/rwconnected/serverkit/api/economy/Patbox/PbCurrency.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public IAccount getDefaultAccount(IPlayer<?> player) {
2525
ServerPlayerEntity mcPlayer = ((Player) player).getSource();
2626
return new PbAccount(currency.provider().getDefaultAccount(mcPlayer, currency));
2727
}
28+
29+
@Override
30+
public String formatValue(long value) {
31+
return currency.formatValue(value, false);
32+
}
2833
}

src/main/java/com/rwconnected/serverkit/config/Config.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,27 @@ public HttpServerConfig() {
193193
}
194194

195195
public record EconomyConfig(
196-
Identifier currencyId,
197-
Identifier accountId
196+
String currencyId,
197+
String accountId
198198
) {
199-
public EconomyConfig(Identifier currencyId, Identifier accountId) {
199+
public EconomyConfig(String currencyId, String accountId) {
200200
this.currencyId = currencyId;
201201
this.accountId = accountId;
202202
}
203203
public EconomyConfig() {
204204
this(
205-
Identifier.of("guishop", "credit"),
206-
Identifier.of("guishop", "account")
205+
"guishop:credit",
206+
"guishop:account"
207207
);
208208
}
209+
210+
public Identifier CurrencyIdentifier() {
211+
return Identifier.of(currencyId);
212+
}
213+
214+
public Identifier AccountIdentifier() {
215+
return Identifier.of(accountId);
216+
}
209217
}
210218

211219
}

src/main/java/com/rwconnected/serverkit/service/LoginStreakService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import com.rwconnected.serverkit.api.economy.ITransaction;
66
import com.rwconnected.serverkit.api.minecraft.player.IPlayer;
77
import com.rwconnected.serverkit.api.minecraft.storage.virtual.IStorage;
8+
import com.rwconnected.serverkit.api.util.time.ITimeProvider;
89
import com.rwconnected.serverkit.config.Config;
910
import com.rwconnected.serverkit.module.Log;
10-
import com.rwconnected.serverkit.api.util.time.ITimeProvider;
1111
import com.rwconnected.serverkit.util.ModUtils;
1212
import net.minecraft.util.Identifier;
1313
import org.jetbrains.annotations.NotNull;
@@ -74,7 +74,7 @@ record = 0;
7474
public int reward(IPlayer<?> player, int amount) {
7575
amount = Math.max(0, amount);
7676

77-
Identifier currencyId = Config.instance().economy.currencyId();
77+
Identifier currencyId = Config.instance().economy.CurrencyIdentifier();
7878
try {
7979
IAccount account = economyProvider.getDefaultAccount(player, currencyId);
8080
ITransaction transaction = account.increaseBalance(amount);

src/test/java/com/rwconnected/api/economy/mock/MockCurrency.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public class MockCurrency implements ICurrency {
99
private final Identifier id;
1010
private final MockAccount defaultAccount;
1111

12+
public String prefix = "";
13+
public String suffix = " RP";
14+
public int decimalPlaces = 2;
15+
1216
public MockCurrency(Identifier id, MockAccount defaultAccount) {
1317
this.id = id;
1418
this.defaultAccount = defaultAccount;
@@ -29,4 +33,11 @@ public Identifier getId() {
2933
public IAccount getDefaultAccount(IPlayer<?> player) {
3034
return defaultAccount;
3135
}
36+
37+
@Override
38+
public String formatValue(long value) {
39+
int fraction = (int) (value % (long) Math.pow(10, decimalPlaces));
40+
long whole = value / (long) Math.pow(10, decimalPlaces);
41+
return String.format("%s%d.%0" + decimalPlaces + "d%s", prefix, whole, fraction, suffix);
42+
}
3243
}

src/test/java/com/rwconnected/serverkit/service/LoginStreakServiceTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.rwconnected.serverkit.api.economy.IAccount;
66
import com.rwconnected.serverkit.api.util.time.MockTimeProvider;
77
import com.rwconnected.serverkit.config.Config;
8-
import net.minecraft.util.Identifier;
98
import org.junit.jupiter.api.Test;
109

1110
import java.util.*;
@@ -57,8 +56,8 @@ private void initConfig(
5756
null,
5857
null,
5958
economyConfig != null ? economyConfig : new Config.EconomyConfig(
60-
Identifier.of("mock", "currency"),
61-
Identifier.of("mock", "account")
59+
"mock:currency",
60+
"mock:account"
6261
)
6362
));
6463
}
@@ -221,7 +220,7 @@ public void testPlayerGetsRewardedForStreak() {
221220
loginStreakService.process(player);
222221

223222
IAccount account = this.economyProvider.getDefaultAccount(
224-
this.player, Config.instance().economy.currencyId()
223+
this.player, Config.instance().economy.CurrencyIdentifier()
225224
);
226225

227226
assertEquals(DAILY_REWARD, account.getBalance());
@@ -238,7 +237,7 @@ public void testPlayerGetsNoRewardAfterMissingDay() {
238237
loginStreakService.process(player);
239238

240239
IAccount account = this.economyProvider.getDefaultAccount(
241-
this.player, Config.instance().economy.currencyId()
240+
this.player, Config.instance().economy.CurrencyIdentifier()
242241
);
243242

244243
assertEquals(0, account.getBalance());
@@ -255,7 +254,7 @@ public void testPlayerGetsRewardForWeeklyMilestone() {
255254
loginStreakService.process(player);
256255

257256
IAccount account = this.economyProvider.getDefaultAccount(
258-
this.player, Config.instance().economy.currencyId()
257+
this.player, Config.instance().economy.CurrencyIdentifier()
259258
);
260259

261260
assertEquals(DAILY_REWARD + WEEKLY_REWARD, account.getBalance());
@@ -272,7 +271,7 @@ public void testPlayerGetsRewardForMultipleMilestones() {
272271
loginStreakService.process(player);
273272

274273
IAccount account = this.economyProvider.getDefaultAccount(
275-
this.player, Config.instance().economy.currencyId()
274+
this.player, Config.instance().economy.CurrencyIdentifier()
276275
);
277276

278277
assertEquals(DAILY_REWARD + WEEKLY_REWARD + MONTHLY_REWARD, account.getBalance());
@@ -289,7 +288,7 @@ public void testPlayerGetsRewardForRegularMilestone() {
289288
loginStreakService.process(player);
290289

291290
IAccount account = this.economyProvider.getDefaultAccount(
292-
this.player, Config.instance().economy.currencyId()
291+
this.player, Config.instance().economy.CurrencyIdentifier()
293292
);
294293

295294
assertEquals(DAILY_REWARD + DAY_100_REWARD, account.getBalance());
@@ -307,7 +306,7 @@ public void testPlayerGetsNoRewardForAlreadyReceivedRegularMilestone() {
307306
loginStreakService.process(player);
308307

309308
IAccount account = this.economyProvider.getDefaultAccount(
310-
this.player, Config.instance().economy.currencyId()
309+
this.player, Config.instance().economy.CurrencyIdentifier()
311310
);
312311

313312
assertEquals(DAILY_REWARD, account.getBalance());
@@ -316,8 +315,8 @@ public void testPlayerGetsNoRewardForAlreadyReceivedRegularMilestone() {
316315
@Test
317316
public void testLoginStreakServiceStillWorksWithMisconfiguredEconomyConfig() {
318317
initConfig(new Config.EconomyConfig(
319-
Identifier.of("invalid", "id1"),
320-
Identifier.of("invalid", "id2")
318+
"invalid:id1",
319+
"invalid:id2"
321320
));
322321
setupService();
323322
initPlayer();

0 commit comments

Comments
 (0)