diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index 674f529..3d73e93 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { implementation(project(":lighteco-common")) implementation("dev.jorel:commandapi-bukkit-shade:9.1.0") + implementation("net.kyori:adventure-platform-bukkit:4.2.0") compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT") diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java new file mode 100644 index 0000000..c2a5ad1 --- /dev/null +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java @@ -0,0 +1,39 @@ +package dev.xhyrom.lighteco.bukkit.chat; + +import dev.xhyrom.lighteco.common.model.chat.AbstractCommandSender; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.Component; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.UUID; + +public class BukkitCommandSender extends AbstractCommandSender { + private final Audience audience; + + public BukkitCommandSender(CommandSender sender, BukkitAudiences audienceFactory) { + super(sender); + + this.audience = audienceFactory.sender(sender); + } + + @Override + public String getUsername() { + return this.delegate.getName(); + } + + @Override + public UUID getUniqueId() { + if (super.delegate instanceof Player) { + return ((Player) super.delegate).getUniqueId(); + } + + return null; + } + + @Override + public void sendMessage(Component message) { + this.audience.sendMessage(message); + } +} \ No newline at end of file diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java index 0722f65..45c6513 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java @@ -1,26 +1,165 @@ package dev.xhyrom.lighteco.bukkit.manager; +import dev.jorel.commandapi.CommandAPI; import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.DoubleArgument; +import dev.jorel.commandapi.arguments.IntegerArgument; +import dev.jorel.commandapi.arguments.OfflinePlayerArgument; +import dev.jorel.commandapi.executors.CommandArguments; +import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.common.manager.command.AbstractCommandManager; import dev.xhyrom.lighteco.common.model.currency.Currency; -import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.java.JavaPlugin; import org.checkerframework.checker.nullness.qual.NonNull; +import java.math.BigDecimal; +import java.util.List; + public class BukkitCommandManager extends AbstractCommandManager { + private final BukkitAudiences audienceFactory; + public BukkitCommandManager(LightEcoPlugin plugin) { super(plugin); + + this.audienceFactory = BukkitAudiences.create((JavaPlugin) this.plugin.getBootstrap().getLoader()); + } + + private List getBalanceCommands(String name, String permissionBase, Currency currency) { + return List.of( + new CommandAPICommand(name) + .withPermission(permissionBase + "balance.others") + .withArguments(new OfflinePlayerArgument("target")) + .executes((sender, args) -> { + this.handleBalance(sender, args, currency); + }), + new CommandAPICommand(name) + .withPermission(permissionBase + "balance") + .executesPlayer((sender, args) -> { + this.handleBalance(sender, args, currency); + }) + ); } @Override public void registerCurrencyCommand(@NonNull Currency currency) { - new CommandAPICommand(currency.getIdentifier()) - .withPermission("lighteco.command.balance." + currency.getIdentifier()) - .executesPlayer((sender, args) -> { - User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; - this.onBalance(user, currency); - }) + // Balance + getBalanceCommands(currency.getIdentifier(), permissionBase, currency) + .forEach(CommandAPICommand::register); + + List balanceCommands = getBalanceCommands("balance", permissionBase, currency); + + new CommandAPICommand(currency.getIdentifier()) + .withSubcommand(new CommandAPICommand("set") + .withPermission(permissionBase + "set") + .withArguments( + new OfflinePlayerArgument("target"), + currency.getProxy().getDecimalPlaces() > 0 + ? new DoubleArgument("amount", 0) + : new IntegerArgument("amount", 0) + ) + .executes((sender, args) -> { + this.handleSet(sender, args, currency); + }) + ) + .withSubcommand(new CommandAPICommand("give") + .withPermission(permissionBase + "give") + .withArguments( + new OfflinePlayerArgument("target"), + currency.getProxy().getDecimalPlaces() > 0 + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) + ) + .executes((sender, args) -> { + this.handleGive(sender, args, currency); + }) + ) + .withSubcommand(new CommandAPICommand("take") + .withPermission(permissionBase + "take") + .withArguments( + new OfflinePlayerArgument("target"), + currency.getProxy().getDecimalPlaces() > 0 + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) + ) + .executes((sender, args) -> { + this.handleTake(sender, args, currency); + }) + ) + .withSubcommand(balanceCommands.get(0)) + .withSubcommands(balanceCommands.get(1)) .register(); } + + private void handleBalance(CommandSender originalSender, CommandArguments args, Currency currency) { + BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); + OfflinePlayer target = (OfflinePlayer) args.get("target"); + + if (target == null) { + this.onBalance(sender, currency); + + return; + } + + this.plugin.getUserManager().loadUser(target.getUniqueId()) + .thenAcceptAsync(result -> { + String name = target.getName() != null ? target.getName() : args.getRaw("target"); + result.setUsername(name); + + this.onBalance(sender, currency, result); + }); + } + + private void handleSet(CommandSender originalSender, CommandArguments args, Currency currency) { + BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); + OfflinePlayer target = (OfflinePlayer) args.get("target"); + BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); + + if (!this.canUse(sender)) return; + + this.plugin.getUserManager().loadUser(target.getUniqueId()) + .thenAcceptAsync(result -> { + String name = target.getName() != null ? target.getName() : args.getRaw("target"); + result.setUsername(name); + + this.onSet(sender, currency, result, amount); + }); + } + + private void handleGive(CommandSender originalSender, CommandArguments args, Currency currency) { + BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); + OfflinePlayer target = (OfflinePlayer) args.get("target"); + BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); + + if (!this.canUse(sender)) return; + + this.plugin.getUserManager().loadUser(target.getUniqueId()) + .thenAcceptAsync(result -> { + String name = target.getName() != null ? target.getName() : args.getRaw("target"); + result.setUsername(name); + + this.onGive(sender, currency, result, amount); + }); + } + + public void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) { + BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); + OfflinePlayer target = (OfflinePlayer) args.get("target"); + BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); + + if (!this.canUse(sender)) return; + + this.plugin.getUserManager().loadUser(target.getUniqueId()) + .thenAcceptAsync(result -> { + String name = target.getName() != null ? target.getName() : args.getRaw("target"); + result.setUsername(name); + + this.onTake(sender, currency, result, amount); + }); + } } diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java index ae81d5f..756f3f7 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java +++ b/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java @@ -15,11 +15,9 @@ import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; -public class TestPlugin extends JavaPlugin implements Listener { +public class TestPlugin extends JavaPlugin { @Override public void onEnable() { - getServer().getPluginManager().registerEvents(this, this); - getLogger().info("TestPlugin loaded!"); LightEco provider = LightEcoProvider.get(); @@ -36,40 +34,4 @@ public class TestPlugin extends JavaPlugin implements Listener { provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test")); } - - @EventHandler - public void onWalk(AsyncPlayerChatEvent event) throws ParseException { - Player player = event.getPlayer(); - - LightEco provider = LightEcoProvider.get(); - CurrencyManager currencyManager = provider.getCurrencyManager(); - - User user = provider.getPlayerAdapter(Player.class).getUser(player); - - String message = event.getMessage(); - String command = message.split(" ")[0]; - String[] args = message.substring(command.length()).trim().split(" "); - - Currency currency = currencyManager.getCurrency(args[0]); - - switch (command) { - case "balance" -> player.sendMessage(user.getBalance(currency).toString()); - case "add" -> { - if (currency.getDecimalPlaces() > 0) { - DecimalFormat decimalFormat = new DecimalFormat("#0.00"); - user.setBalance(currency, BigDecimal.valueOf( - Double.parseDouble( - decimalFormat.format( - Double.parseDouble(args[1]) - ) - ) - )); - } else { - user.setBalance(currency, BigDecimal.valueOf(Integer.parseInt(args[1]))); - } - - provider.getUserManager().saveUser(user).thenAccept(aVoid -> player.sendMessage("Saved!")); - } - } - } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 6983b61..5220994 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -4,6 +4,12 @@ plugins { dependencies { api(project(":lighteco-api")) + api("net.kyori:adventure-api:4.12.0") { + exclude(module = "adventure-bom") + exclude(module = "checker-qual") + exclude(module = "annotations") + } + api("net.kyori:adventure-text-minimessage:4.14.0") implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:5.0.0-beta.5") implementation("eu.okaeri:okaeri-configs-validator-okaeri:5.0.0-beta.5") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java index d5c932a..b65e905 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUser.java @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.common.api.impl; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.user.User; import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import java.math.BigDecimal; import java.util.UUID; @@ -20,12 +21,12 @@ public class ApiUser implements User { } @Override - public @NonNull String getUsername() { - return null; + public @Nullable String getUsername() { + return this.handler.getUsername(); } @Override - public BigDecimal getBalance(@NonNull Currency currency) { + public @NonNull BigDecimal getBalance(@NonNull Currency currency) { dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java index 6ae4500..b5f474c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiUserManager.java @@ -20,7 +20,8 @@ public class ApiUserManager extends ApiAbstractManager loadUser(@NonNull UUID uniqueId) { - return this.plugin.getStorage().loadUser(uniqueId); + return this.plugin.getStorage().loadUser(uniqueId) + .thenApply(ApiUserManager::wrap); } @Override diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java b/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java deleted file mode 100644 index 50fe332..0000000 --- a/common/src/main/java/dev/xhyrom/lighteco/common/cache/TypedMap.java +++ /dev/null @@ -1,24 +0,0 @@ -package dev.xhyrom.lighteco.common.cache; - -import java.util.HashMap; -import java.util.Map; - -public class TypedMap { - private final Map map = new HashMap<>(); - - public V get(T key) { - return (V) map.get(key); - } - - public V getOrDefault(T key, V defaultValue) { - return (V) map.getOrDefault(key, defaultValue); - } - - public void put(T key, V value) { - map.put(key, value); - } - - public void clear() { - map.clear(); - } -} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java index aff0b83..8cfc483 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java @@ -1,23 +1,113 @@ package dev.xhyrom.lighteco.common.manager.command; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import net.kyori.adventure.text.minimessage.MiniMessage; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; public abstract class AbstractCommandManager implements CommandManager { protected final LightEcoPlugin plugin; + private final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final ArrayList mustWait = new ArrayList<>(); public AbstractCommandManager(LightEcoPlugin plugin) { this.plugin = plugin; } @Override - public void onBalance(User sender, Currency currency) { - BigDecimal balance = sender.getBalance(currency); + public boolean canUse(CommandSender sender) { + // Console doesn't need to wait + if (sender.getUniqueId() == null) return true; - System.out.println("Your balance: " + balance.toPlainString() + " " + currency.getIdentifier()); - //sender.sendMessage("&aYour balance: &e" + balance.toPlainString() + " &a" + currency.getIdentifier() + "&r"); + if (mustWait.contains(sender.getUniqueId())) { + sender.sendMessage( + miniMessage.deserialize("Please wait a moment before using this command again.") + ); + + return false; + } + + return true; + } + + private void addToMustWait(UUID ...uuids) { + for (UUID uuid : uuids) { + if (uuid != null) + mustWait.add(uuid); + } + } + + private void removeFromMustWait(UUID ...uuids) { + for (UUID uuid : uuids) { + if (uuid != null) + mustWait.remove(uuid); + } + } + + @Override + public void onBalance(CommandSender sender, Currency currency) { + User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + BigDecimal balance = user.getBalance(currency); + + sender.sendMessage( + miniMessage.deserialize("Your balance: " + balance.toPlainString() + " " + currency.getIdentifier()) + ); + } + + @Override + public void onBalance(CommandSender sender, Currency currency, User target) { + BigDecimal balance = target.getBalance(currency); + + sender.sendMessage( + miniMessage.deserialize("" + target.getUsername() + "'s balance: " + balance.toPlainString() + " " + currency.getIdentifier()) + ); + } + + @Override + public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) { + addToMustWait(sender.getUniqueId(), target.getUniqueId()); + + target.setBalance(currency, amount); + + sender.sendMessage( + miniMessage.deserialize("Set " + target.getUsername() + "'s balance to " + amount.toPlainString() + " " + currency.getIdentifier()) + ); + + this.plugin.getUserManager().saveUser(target) + .thenAccept(v -> removeFromMustWait(target.getUniqueId(), sender.getUniqueId())); + } + + @Override + public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) { + addToMustWait(sender.getUniqueId(), target.getUniqueId()); + + target.setBalance(currency, target.getBalance(currency).add(amount)); + + sender.sendMessage( + miniMessage.deserialize("Gave " + target.getUsername() + " " + amount.toPlainString() + " " + currency.getIdentifier()) + ); + + this.plugin.getUserManager().saveUser(target) + .thenAccept(v -> removeFromMustWait(target.getUniqueId(), sender.getUniqueId())); + } + + @Override + public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) { + addToMustWait(sender.getUniqueId(), target.getUniqueId()); + + target.setBalance(currency, target.getBalance(currency).subtract(amount)); + + sender.sendMessage( + miniMessage.deserialize("Took " + amount.toPlainString() + " " + currency.getIdentifier() + " from " + target.getUsername()) + ); + + this.plugin.getUserManager().saveUser(target) + .thenAccept(v -> removeFromMustWait(target.getUniqueId(), sender.getUniqueId())); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java index 042313a..efa48e3 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java @@ -1,11 +1,21 @@ package dev.xhyrom.lighteco.common.manager.command; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import org.checkerframework.checker.nullness.qual.NonNull; +import java.math.BigDecimal; + public interface CommandManager { void registerCurrencyCommand(@NonNull Currency currency); - void onBalance(User sender, Currency currency); + boolean canUse(CommandSender sender); + + void onBalance(CommandSender sender, Currency currency); + void onBalance(CommandSender sender, Currency currency, User target); + + void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount); + void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount); + void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/StandardUserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/StandardUserManager.java index 2132188..29e88ef 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/StandardUserManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/StandardUserManager.java @@ -21,6 +21,11 @@ public class StandardUserManager extends AbstractManager implements return new User(this.plugin, uniqueId); } + @Override + public CompletableFuture loadUser(UUID uniqueId) { + return this.plugin.getStorage().loadUser(uniqueId); + } + @Override public CompletableFuture load() { Set uniqueIds = new HashSet<>(keys()); @@ -31,6 +36,11 @@ public class StandardUserManager extends AbstractManager implements .collect(CompletableFuture::allOf, (future, userFuture) -> future.join(), (future, userFuture) -> future.join()); } + @Override + public CompletableFuture saveUser(User user) { + return this.plugin.getStorage().saveUser(user.getProxy()); + } + @Override public void invalidateCaches() { values().forEach(User::invalidateCaches); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserManager.java index 64014f0..3a4a44d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserManager.java @@ -7,7 +7,10 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; public interface UserManager extends Manager { + CompletableFuture saveUser(User user); + CompletableFuture load(); + CompletableFuture loadUser(UUID uniqueId); void invalidateCaches(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/AbstractCommandSender.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/AbstractCommandSender.java new file mode 100644 index 0000000..23a3be7 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/AbstractCommandSender.java @@ -0,0 +1,9 @@ +package dev.xhyrom.lighteco.common.model.chat; + +public abstract class AbstractCommandSender implements CommandSender { + protected final T delegate; + + public AbstractCommandSender(T delegate) { + this.delegate = delegate; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/CommandSender.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/CommandSender.java new file mode 100644 index 0000000..9827b2b --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/chat/CommandSender.java @@ -0,0 +1,12 @@ +package dev.xhyrom.lighteco.common.model.chat; + +import net.kyori.adventure.text.Component; + +import java.util.UUID; + +public interface CommandSender { + String getUsername(); + UUID getUniqueId(); + + void sendMessage(Component message); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java index 9a47c55..a3a6665 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/user/User.java @@ -1,12 +1,14 @@ package dev.xhyrom.lighteco.common.model.user; import dev.xhyrom.lighteco.common.api.impl.ApiUser; -import dev.xhyrom.lighteco.common.cache.TypedMap; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; +import lombok.Setter; import org.checkerframework.checker.nullness.qual.NonNull; +import java.math.BigDecimal; +import java.util.HashMap; import java.util.UUID; @Getter @@ -17,18 +19,26 @@ public class User { @Getter private final UUID uniqueId; - private final TypedMap balances = new TypedMap<>(); + @Getter + @Setter + private String username; + private final HashMap balances = new HashMap<>(); public User(LightEcoPlugin plugin, UUID uniqueId) { + this(plugin, uniqueId, null); + } + + public User(LightEcoPlugin plugin, UUID uniqueId, String username) { this.plugin = plugin; this.uniqueId = uniqueId; + this.username = username; } - public T getBalance(@NonNull Currency currency) { - return balances.getOrDefault(currency, (T) currency.getDefaultBalance()); + public BigDecimal getBalance(@NonNull Currency currency) { + return balances.getOrDefault(currency, currency.getDefaultBalance()); } - public void setBalance(@NonNull Currency currency, @NonNull T balance) { + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) { balances.put(currency, balance); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java index b7aab2e..2a6574c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java @@ -1,6 +1,6 @@ package dev.xhyrom.lighteco.common.storage; -import dev.xhyrom.lighteco.api.model.user.User; +import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.api.storage.StorageProvider; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; @@ -37,10 +37,16 @@ public class Storage { } public CompletableFuture loadUser(UUID uniqueId) { - return future(() -> this.provider.loadUser(uniqueId)); + User user = this.plugin.getUserManager().getIfLoaded(uniqueId); + if (user != null) { + return CompletableFuture.completedFuture(user); + } + + return future(() -> this.provider.loadUser(uniqueId)) + .thenApply(apiUser -> this.plugin.getUserManager().getIfLoaded(apiUser.getUniqueId())); } - public CompletableFuture saveUser(User user) { + public CompletableFuture saveUser(dev.xhyrom.lighteco.api.model.user.User user) { return future(() -> this.provider.saveUser(user)); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java index 057e1cf..05a9c71 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/memory/MemoryStorageProvider.java @@ -43,7 +43,7 @@ public class MemoryStorageProvider implements StorageProvider { private void simulateSlowDatabaseQuery() { try { - Thread.sleep(500); + Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }