Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly support multi-world vault implementations #429

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to Total Economy will be documented in this file.

## [0.13.2] - 2024-09-14

### Added

- Implementations for multi world Vault API functions (Bukkit only)
- Since Total Economy does not support multi-world, these implementations will act the same as the global Vault API functions.

## [0.13.1] - 2024-09-08

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.13.1
version=0.13.2
group=com.ericgrandt.totaleconomy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.OfflinePlayer;

public class EconomyImpl implements Economy {
Expand Down Expand Up @@ -70,7 +69,7 @@ public boolean hasAccount(OfflinePlayer player) {

@Override
public boolean hasAccount(OfflinePlayer player, String worldName) {
throw new NotImplementedException("World specific accounts are not yet supported");
return hasAccount(player);
}

@Override
Expand All @@ -80,7 +79,7 @@ public boolean createPlayerAccount(OfflinePlayer player) {

@Override
public boolean createPlayerAccount(OfflinePlayer player, String worldName) {
throw new NotImplementedException("World specific accounts are not yet supported");
return createPlayerAccount(player);
}

@Override
Expand All @@ -90,7 +89,7 @@ public double getBalance(OfflinePlayer player) {

@Override
public double getBalance(OfflinePlayer player, String world) {
throw new NotImplementedException("World specific accounts are not yet supported");
return getBalance(player);
}

@Override
Expand All @@ -100,7 +99,7 @@ public boolean has(OfflinePlayer player, double amount) {

@Override
public boolean has(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return has(player, amount);
}

@Override
Expand All @@ -125,7 +124,7 @@ public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {

@Override
public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return withdrawPlayer(player, amount);
}

@Override
Expand All @@ -150,7 +149,7 @@ public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {

@Override
public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
throw new NotImplementedException("World specific accounts are not yet supported");
return depositPlayer(player, amount);
}

@Override
Expand Down Expand Up @@ -272,8 +271,4 @@ public boolean createPlayerAccount(String playerName) {
public boolean createPlayerAccount(String playerName, String worldName) {
throw new UnsupportedOperationException();
}

public CurrencyDto getDefaultCurrency() {
return defaultCurrency;
}
}
2 changes: 1 addition & 1 deletion totaleconomy-bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: TotalEconomy
version: 0.13.1
version: 0.13.2
main: com.ericgrandt.totaleconomy.TotalEconomy
description: All in one economy plugin for Minecraft
author: Eric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ public void hasAccount_WithPlayerHavingAnAccount_ShouldReturnTrue() {
assertTrue(actual);
}

@Test
@Tag("Unit")
public void hasAccount_World_WithPlayerHavingAnAccount_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.hasAccount(playerUUID)).thenReturn(true);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.hasAccount(playerMock, "randomWorld");

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void createPlayerAccount_WithSuccessfulCallToCreateAccount_ShouldReturnTrue() throws SQLException {
Expand All @@ -184,6 +202,24 @@ public void createPlayerAccount_WithSuccessfulCallToCreateAccount_ShouldReturnTr
assertTrue(actual);
}

@Test
@Tag("Unit")
public void createPlayerAccount_World_WithSuccessfulCallToCreateAccount_ShouldReturnTrue() throws SQLException {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.createAccount(playerUUID)).thenReturn(true);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.createPlayerAccount(playerMock, "randomWorld");

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void getBalance_WithBalanceFound_ShouldReturnBalance() throws SQLException {
Expand All @@ -203,6 +239,25 @@ public void getBalance_WithBalanceFound_ShouldReturnBalance() throws SQLExceptio
assertEquals(expected, actual);
}

@Test
@Tag("Unit")
public void getBalance_World_WithBalanceFound_ShouldReturnBalance() throws SQLException {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
double actual = sut.getBalance(playerMock, "randomWorld");
double expected = 10;

// Assert
assertEquals(expected, actual);
}

@Test
@Tag("Unit")
public void has_WithAmountLessThanBalance_ShouldReturnTrue() {
Expand Down Expand Up @@ -257,6 +312,60 @@ public void has_WithAmountLessThanBalance_ShouldReturnFalse() {
assertFalse(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountLessThanBalance_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.valueOf(100));

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 10);

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountEqualToBalance_ShouldReturnTrue() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 10);

// Assert
assertTrue(actual);
}

@Test
@Tag("Unit")
public void has_World_WithAmountLessThanBalance_ShouldReturnFalse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.getBalance(playerUUID, 1)).thenReturn(BigDecimal.TEN);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
boolean actual = sut.has(playerMock, "randomWorld", 11);

// Assert
assertFalse(actual);
}

@Test
@Tag("Unit")
public void withdrawPlayer_WithSuccessfulWithdraw_ShouldReturnCorrectEconomyResponse() {
Expand Down Expand Up @@ -315,6 +424,64 @@ public void withdrawPlayer_WithFailedWithdraw_ShouldReturnCorrectEconomyResponse
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void withdrawPlayer_World_WithSuccessfulWithdraw_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.withdraw(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.SUCCESS, "")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.withdrawPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.SUCCESS,
""
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void withdrawPlayer_World_WithFailedWithdraw_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.withdraw(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.FAILURE, "Failed")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.withdrawPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.FAILURE,
"Failed"
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_WithSuccessfulDeposit_ShouldReturnCorrectEconomyResponse() {
Expand Down Expand Up @@ -372,4 +539,62 @@ public void depositPlayer_WithFailedDeposit_ShouldReturnCorrectEconomyResponse()
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_World_WithSuccessfulDeposit_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.deposit(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.SUCCESS, "")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.depositPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.SUCCESS,
""
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}

@Test
@Tag("Unit")
public void depositPlayer_World_WithFailedDeposit_ShouldReturnCorrectEconomyResponse() {
// Arrange
UUID playerUUID = UUID.randomUUID();
OfflinePlayer playerMock = mock(OfflinePlayer.class);
when(playerMock.getUniqueId()).thenReturn(playerUUID);
when(economyMock.deposit(playerUUID, 1, BigDecimal.valueOf(10D), true)).thenReturn(
new TransactionResult(TransactionResult.ResultType.FAILURE, "Failed")
);

EconomyImpl sut = new EconomyImpl(true, defaultCurrency, economyMock);

// Act
EconomyResponse actual = sut.depositPlayer(playerMock, "randomWorld", 10);
EconomyResponse expected = new EconomyResponse(
10,
0,
EconomyResponse.ResponseType.FAILURE,
"Failed"
);

// Assert
assertEquals(expected.amount, actual.amount);
assertEquals(expected.balance, actual.balance);
assertEquals(expected.type, actual.type);
assertEquals(expected.errorMessage, actual.errorMessage);
}
}