Skip to content

Commit

Permalink
Merge pull request #32 from TheNextLvl-net/dependabot/gradle/net.then…
Browse files Browse the repository at this point in the history
…extlvl.services-service-io-2.2.0

Bump net.thenextlvl.services:service-io from 2.1.0 to 2.2.0
  • Loading branch information
NonSwag authored Dec 24, 2024
2 parents 06500f3 + 2da25a4 commit a792f8c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public interface EconomyController {
*/
String getCurrencySymbol();

/**
* Loads all accounts asynchronously.
*
* @return a {@link CompletableFuture} that, when completed, will provide a {@link Set} of {@link Account} objects representing
* all the accounts available.
*/
CompletableFuture<@Unmodifiable Set<Account>> loadAccounts();

/**
* Retrieves all the accounts currently available.
*
* @return a set of accounts
*/
@Unmodifiable
Set<Account> getAccounts();

/**
* Retrieve the account for the specified player.
*
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {

dependencies {
compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT")
compileOnly("net.thenextlvl.services:service-io:2.1.0")
compileOnly("net.thenextlvl.services:service-io:2.2.0")
compileOnly("org.projectlombok:lombok:1.18.36")

implementation("net.thenextlvl.core:i18n:1.0.20")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private LiteralArgumentBuilder<CommandSourceStack> prune() {

private int prune(CommandContext<CommandSourceStack> context, @Nullable World world) {
var duration = context.getArgument("time", Duration.class);
CompletableFuture.supplyAsync(() -> plugin.dataController().getAccounts(world))
CompletableFuture.supplyAsync(() -> plugin.dataController().getAccountOwners(world))
.thenApply(accounts -> accounts.stream().map(plugin.getServer()::getOfflinePlayer))
.thenApply(players -> players.filter(player -> !player.isConnected())
.filter(player -> player.getLastSeen() < Instant.now().minus(duration).toEpochMilli()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@NullMarked
Expand Down Expand Up @@ -71,6 +77,16 @@ public String getCurrencySymbol() {
return plugin.config().currency().symbol();
}

@Override
public CompletableFuture<@Unmodifiable Set<Account>> loadAccounts() {
return CompletableFuture.supplyAsync(() -> dataController().getAccounts(null));
}

@Override
public @Unmodifiable Set<Account> getAccounts() {
return Set.copyOf(cache.values());
}

@Override
public Optional<Account> getAccount(UUID uuid) {
return Optional.ofNullable(cache.get(new Identifier(uuid, null)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@NullMarked
public interface DataController {
@Nullable Account getAccount(UUID uuid, @Nullable World world);
@Nullable
Account getAccount(UUID uuid, @Nullable World world);

Set<Account> getAccounts(@Nullable World world);

Account createAccount(UUID uuid, @Nullable World world);

BigDecimal getTotalBalance(@Nullable World world);

List<Account> getOrdered(@Nullable World world, int start, int limit);

List<UUID> getAccounts(@Nullable World world);
Set<UUID> getAccountOwners(@Nullable World world);

boolean deleteAccounts(List<UUID> accounts, @Nullable World world);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

@NullMarked
Expand Down Expand Up @@ -88,12 +90,12 @@ SELECT SUM(balance) as total_balance FROM accounts WHERE (world = ? OR (? IS NUL

@Override
@SneakyThrows
public List<UUID> getAccounts(@Nullable World world) {
public Set<UUID> getAccountOwners(@Nullable World world) {
var name = world != null ? world.key().asString() : null;
return Objects.requireNonNull(executeQuery("""
SELECT uuid FROM accounts WHERE (world = ? OR (? IS NULL AND world IS NULL))
""", resultSet -> {
var accounts = new ArrayList<UUID>();
var accounts = new HashSet<UUID>();
while (resultSet.next()) {
var owner = UUID.fromString(resultSet.getString("uuid"));
accounts.add(owner);
Expand All @@ -114,6 +116,23 @@ SELECT uuid FROM accounts WHERE (world = ? OR (? IS NULL AND world IS NULL))
}, uuid, name, name);
}

@Override
@SneakyThrows
public Set<Account> getAccounts(@Nullable World world) {
var name = world != null ? world.key().asString() : null;
return Objects.requireNonNull(executeQuery("""
SELECT uuid, balance FROM accounts WHERE (world = ? OR (? IS NULL AND world IS NULL))
""", resultSet -> {
var accounts = new HashSet<Account>();
while (resultSet.next()) {
var owner = UUID.fromString(resultSet.getString("uuid"));
var balance = resultSet.getBigDecimal("balance");
accounts.add(new EconomistAccount(balance, world, owner));
}
return accounts;
}, name, name));
}

@Override
@SneakyThrows
public boolean save(Account account) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import net.thenextlvl.service.api.economy.bank.BankController;
import org.bukkit.World;
import org.bukkit.plugin.ServicePriority;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@NullMarked
@RequiredArgsConstructor
Expand Down Expand Up @@ -54,6 +57,20 @@ public String getCurrencySymbol() {
return economyController().getCurrencySymbol();
}

@Override
public CompletableFuture<@Unmodifiable Set<Account>> loadAccounts() {
return economyController().loadAccounts().thenApply(accounts -> accounts.stream()
.map(ServiceAccount::new)
.collect(Collectors.toUnmodifiableSet()));
}

@Override
public @Unmodifiable Set<Account> getAccounts() {
return economyController().getAccounts().stream()
.map(ServiceAccount::new)
.collect(Collectors.toUnmodifiableSet());
}

@Override
public Optional<Account> getAccount(UUID uuid) {
return economyController().getAccount(uuid).map(ServiceAccount::new);
Expand Down

0 comments on commit a792f8c

Please sign in to comment.