From a0fb26bcee9ab2aa98f39416faf530854617cce9 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Fri, 5 Apr 2024 17:51:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=92=8C=E4=BA=8B=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 +- .../mc/plugin/currency/CurrencyPlugin.java | 20 +- .../mc/plugin/currency/api/CurrencyAPI.java | 127 +++++-------- .../currency/async/CompletableTask.java | 172 ++++++++++++++++++ .../mc/plugin/currency/async/RunTask.java | 5 + .../mc/plugin/currency/async/SupplyTask.java | 5 + .../currency/CurrencyCreateCommand.java | 27 +-- .../command/currency/CurrencyGiveCommand.java | 2 +- .../command/currency/CurrencyPayCommand.java | 61 ++++--- .../currency/CurrencyResetCommand.java | 23 +-- .../command/currency/CurrencySeeCommand.java | 39 ++-- .../command/currency/CurrencySetCommand.java | 5 +- .../command/currency/CurrencyTakeCommand.java | 4 +- ...anager.java => CurrencyConfigManager.java} | 6 +- .../currency/core/CurrencyDataManager.java | 55 +++--- .../plugin/currency/core/CurrencyMessage.java | 6 +- .../mc/plugin/currency/data/CurrencyType.java | 4 - .../plugin/currency/event/CurrencyEvent.java | 36 ++++ .../currency/event/CurrencyGiveEvent.java | 12 ++ .../currency/event/CurrencySetEvent.java | 12 ++ .../currency/event/CurrencyTakeEvent.java | 12 ++ .../plugin/currency/hook/PlaceholderHook.java | 4 +- .../currency/hook/VaultEconomyHook.java | 110 +++++------ .../currency/listener/BallListener.java | 2 +- src/main/resources/config.yml | 12 +- 25 files changed, 471 insertions(+), 294 deletions(-) create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/async/CompletableTask.java create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/async/RunTask.java create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/async/SupplyTask.java rename src/main/java/cn/hamster3/mc/plugin/currency/core/{ConfigManager.java => CurrencyConfigManager.java} (82%) create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyEvent.java create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyGiveEvent.java create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencySetEvent.java create mode 100644 src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyTakeEvent.java diff --git a/build.gradle.kts b/build.gradle.kts index 3046748..d9cb297 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,8 +17,8 @@ dependencies { annotationProcessor("org.projectlombok:lombok:1.18.30") compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT") - compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.2.1") - compileOnly("cn.hamster3.mc.plugin:ball-bukkit:1.5.0") + compileOnly("cn.hamster3.mc.plugin:core-bukkit:+") + compileOnly("cn.hamster3.mc.plugin:ball-bukkit:+") compileOnly("net.milkbowl.vault:VaultAPI:1.7") compileOnly("me.clip:placeholderapi:+") } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/CurrencyPlugin.java b/src/main/java/cn/hamster3/mc/plugin/currency/CurrencyPlugin.java index ada2086..7684d87 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/CurrencyPlugin.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/CurrencyPlugin.java @@ -2,7 +2,7 @@ package cn.hamster3.mc.plugin.currency; import cn.hamster3.mc.plugin.ball.common.api.BallAPI; import cn.hamster3.mc.plugin.currency.command.currency.ParentCurrencyCommand; -import cn.hamster3.mc.plugin.currency.core.ConfigManager; +import cn.hamster3.mc.plugin.currency.core.CurrencyConfigManager; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.core.CurrencyMessage; import cn.hamster3.mc.plugin.currency.data.CurrencyType; @@ -42,7 +42,7 @@ public class CurrencyPlugin extends JavaPlugin { Logger logger = getLogger(); long start = System.currentTimeMillis(); logger.info("仓鼠经济正在初始化"); - ConfigManager.init(); + CurrencyConfigManager.init(); logger.info("已读取配置文件"); CurrencyMessage.init(this); logger.info("已初始化语言文本"); @@ -55,15 +55,15 @@ public class CurrencyPlugin extends JavaPlugin { if (Bukkit.getPluginManager().getPlugin("Vault") != null) { logger.info("检测到 Vault 已安装"); - CurrencyType vaultType = CurrencyDataManager.getVaultCurrencyType(); - if (vaultType == null) { - logger.info("未设置任何经济类型挂载至 Vault"); + Bukkit.getServicesManager().register(Economy.class, VaultEconomyHook.INSTANCE, this, ServicePriority.Normal); + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type != null) { + logger.info(String.format("已挂载货币类型 %s(%s) 至 Vault 系统", type.getId(), type.getName())); } else { - Bukkit.getServicesManager().register(Economy.class, new VaultEconomyHook(vaultType), this, ServicePriority.Normal); - logger.info(String.format("已挂载货币类型 %s(%s) 至 Vault 系统", vaultType.getId(), vaultType.getName())); - if (ConfigManager.isEnableVaultCommands()) { - logger.info("本插件暂不支持 vault 指令"); - } + logger.warning("已挂接 Vault 系统,但未找到 Vault 货币类型,请检查配置文件或创建货币类型: " + CurrencyConfigManager.getVaultCurrencyID()); + } + if (CurrencyConfigManager.isEnableVaultCommands()) { + logger.info("本插件暂不支持 vault 指令"); } } else { logger.info("未找到 Vault 插件! 取消注册 Vault 经济系统"); diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/api/CurrencyAPI.java b/src/main/java/cn/hamster3/mc/plugin/currency/api/CurrencyAPI.java index 82708fe..44909c7 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/api/CurrencyAPI.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/api/CurrencyAPI.java @@ -1,114 +1,78 @@ package cn.hamster3.mc.plugin.currency.api; -import cn.hamster3.mc.plugin.core.bukkit.hook.PointAPI; -import cn.hamster3.mc.plugin.currency.CurrencyPlugin; +import cn.hamster3.mc.plugin.currency.async.CompletableTask; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.data.CurrencyType; +import cn.hamster3.mc.plugin.currency.event.CurrencyGiveEvent; +import cn.hamster3.mc.plugin.currency.event.CurrencySetEvent; +import cn.hamster3.mc.plugin.currency.event.CurrencyTakeEvent; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -/** - * 当 currencyID 为 PlayerPoints 且服务器安装了点券插件时,会自动更改为 PlayerPoints 接口 - */ @SuppressWarnings({"unused", "UnusedReturnValue"}) public final class CurrencyAPI { private CurrencyAPI() { } - public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID) { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - return PointAPI.getPoint(uuid); - } - return CurrencyDataManager.getPlayerCurrency(uuid, currencyID); - } - - public static boolean hasPlayerCurrency(UUID uuid, @NotNull String currencyID, double amount) { - return getPlayerCurrency(uuid, currencyID) >= amount; - } - - public static void addPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) throws SQLException { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.givePoint(uuid, (int) amount); - return; - } - CurrencyDataManager.addPlayerCurrency(uuid, currencyID, amount, true); + @NotNull + public static CompletableTask givePlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) { + return CompletableTask.runAsync(() -> { + if (amount < 0) { + throw new IllegalArgumentException("amount must be greater than 0"); + } + CurrencyType type = getCurrencyType(currencyID); + if (type == null) { + throw new IllegalArgumentException("can not find currency type with id: " + currencyID); + } + CurrencyGiveEvent event = new CurrencyGiveEvent(uuid, type, amount); + Bukkit.getPluginManager().callEvent(event); + CurrencyDataManager.addPlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true); + }); } @NotNull - public static CompletableFuture addPlayerCurrencyAsync(@NotNull UUID uuid, @NotNull String currencyID, double amount) { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.givePoint(uuid, (int) amount); - return CompletableFuture.completedFuture(null); - } - CompletableFuture future = new CompletableFuture<>(); - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.addPlayerCurrency(uuid, currencyID, amount, true); - future.complete(null); - } catch (SQLException e) { - future.completeExceptionally(e); + public static CompletableTask takePlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) { + return CompletableTask.runAsync(() -> { + if (amount < 0) { + throw new IllegalArgumentException("amount must be greater than 0"); } + CurrencyType type = getCurrencyType(currencyID); + if (type == null) { + throw new IllegalArgumentException("can not find currency type with id: " + currencyID); + } + CurrencyTakeEvent event = new CurrencyTakeEvent(uuid, type, amount); + Bukkit.getPluginManager().callEvent(event); + CurrencyDataManager.takePlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true); }); - return future; - } - - public static void takePlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) throws SQLException { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.takePoint(uuid, (int) amount); - return; - } - CurrencyDataManager.takePlayerCurrency(uuid, currencyID, amount, true); } @NotNull - public static CompletableFuture takePlayerCurrencyAsync(@NotNull UUID uuid, @NotNull String currencyID, double amount) { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.takePoint(uuid, (int) amount); - return CompletableFuture.completedFuture(null); - } - CompletableFuture future = new CompletableFuture<>(); - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.takePlayerCurrency(uuid, currencyID, amount, true); - future.complete(null); - } catch (SQLException e) { - future.completeExceptionally(e); + public static CompletableTask setPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) { + return CompletableTask.runAsync(() -> { + CurrencyType type = getCurrencyType(currencyID); + if (type == null) { + throw new IllegalArgumentException("can not find currency type with id: " + currencyID); } + CurrencySetEvent event = new CurrencySetEvent(uuid, type, amount); + Bukkit.getPluginManager().callEvent(event); + CurrencyDataManager.setPlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true); }); - return future; - } - - public static void setPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID, double amount) throws SQLException { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.setPoint(uuid, (int) amount); - return; - } - CurrencyDataManager.setPlayerCurrency(uuid, currencyID, amount, true); } @NotNull - public static CompletableFuture setPlayerCurrencyAsync(@NotNull UUID uuid, @NotNull String currencyID, double amount) { - if (currencyID.equals("PlayerPoints") && Bukkit.getPluginManager().isPluginEnabled("PlayerPoints")) { - PointAPI.setPoint(uuid, (int) amount); - return CompletableFuture.completedFuture(null); - } - CompletableFuture future = new CompletableFuture<>(); - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.setPlayerCurrency(uuid, currencyID, amount, true); - future.complete(null); - } catch (SQLException e) { - future.completeExceptionally(e); - } - }); - return future; + public static CompletableTask> getPlayerCurrency(@NotNull UUID uuid) { + return CompletableTask.supplyAsync(() -> CurrencyDataManager.getPlayerCurrency(uuid)); + } + + @NotNull + public static CompletableTask getPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID) { + return CompletableTask.supplyAsync(() -> CurrencyDataManager.getPlayerCurrency(uuid, currencyID)); } @Nullable @@ -116,6 +80,7 @@ public final class CurrencyAPI { return CurrencyDataManager.getCurrencyType(id); } + @Nullable public static CurrencyType getVaultCurrencyType() { return CurrencyDataManager.getVaultCurrencyType(); } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/async/CompletableTask.java b/src/main/java/cn/hamster3/mc/plugin/currency/async/CompletableTask.java new file mode 100644 index 0000000..87496d1 --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/async/CompletableTask.java @@ -0,0 +1,172 @@ +package cn.hamster3.mc.plugin.currency.async; + +import cn.hamster3.mc.plugin.core.common.api.CoreAPI; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +@SuppressWarnings("CallToPrintStackTrace") +public class CompletableTask { + @NotNull + private final List> onSuccess; + @NotNull + private final List> onFailed; + @NotNull + private volatile State state; + private T value; + private Throwable throwable; + + public CompletableTask() { + state = State.WAITING; + onSuccess = new ArrayList<>(); + onFailed = new ArrayList<>(); + } + + @NotNull + public static CompletableTask runAsync(@NotNull RunTask runTask) { + CompletableTask task = new CompletableTask<>(); + CoreAPI.getInstance().getExecutorService().submit(() -> { + try { + runTask.run(); + task.success(null); + } catch (Exception e) { + task.failed(e); + } + }); + return task; + } + + @NotNull + public static CompletableTask supplyAsync(@NotNull SupplyTask supplyTask) { + CompletableTask task = new CompletableTask<>(); + CoreAPI.getInstance().getExecutorService().submit(() -> { + try { + T call = supplyTask.call(); + task.success(call); + } catch (Exception e) { + task.failed(e); + } + }); + return task; + } + + public void success(@Nullable T value) { + if (state != State.WAITING) { + throw new IllegalStateException(); + } + this.value = value; + state = State.SUCCESS; + synchronized (this) { + notifyAll(); + } + for (Consumer success : onSuccess) { + try { + success.accept(value); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + + public void failed(@Nullable Throwable throwable) { + if (state != State.WAITING) { + throw new IllegalStateException(); + } + this.value = null; + state = State.FAILED; + this.throwable = throwable; + synchronized (this) { + notifyAll(); + } + for (Consumer consumer : onFailed) { + try { + consumer.accept(throwable); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + + @NotNull + public CompletableTask onSuccess(@NotNull Consumer consumer) { + if (state == State.SUCCESS) { + try { + consumer.accept(value); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + onSuccess.add(consumer); + } + return this; + } + + @NotNull + public CompletableTask onFailed(@NotNull Consumer consumer) { + if (state == State.FAILED) { + try { + consumer.accept(throwable); + } catch (Throwable e) { + e.printStackTrace(); + } + } else { + onFailed.add(consumer); + } + return this; + } + + public void join() throws InterruptedException { + if (state == State.WAITING) { + synchronized (this) { + wait(); + } + } + } + + /** + * 阻塞当前线程,直到该任务完成为止 + */ + public void joinSilent() { + try { + join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + public T get() throws Throwable { + join(); + switch (state) { + case SUCCESS: + return value; + case FAILED: + throw throwable; + } + return null; + } + + public T getSilent() { + try { + return get(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + public T getOrDefault(T defaultValue) { + T value = getSilent(); + if (value == null) { + return defaultValue; + } + return value; + } + + public enum State { + WAITING, + SUCCESS, + FAILED + } +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/async/RunTask.java b/src/main/java/cn/hamster3/mc/plugin/currency/async/RunTask.java new file mode 100644 index 0000000..479c404 --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/async/RunTask.java @@ -0,0 +1,5 @@ +package cn.hamster3.mc.plugin.currency.async; + +public interface RunTask { + void run() throws Exception; +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/async/SupplyTask.java b/src/main/java/cn/hamster3/mc/plugin/currency/async/SupplyTask.java new file mode 100644 index 0000000..11b965b --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/async/SupplyTask.java @@ -0,0 +1,5 @@ +package cn.hamster3.mc.plugin.currency.async; + +public interface SupplyTask { + T call() throws Exception; +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyCreateCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyCreateCommand.java index de0d598..8dfa193 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyCreateCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyCreateCommand.java @@ -2,6 +2,7 @@ package cn.hamster3.mc.plugin.currency.command.currency; import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand; import cn.hamster3.mc.plugin.currency.CurrencyPlugin; +import cn.hamster3.mc.plugin.currency.api.CurrencyAPI; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.core.CurrencyMessage; import cn.hamster3.mc.plugin.currency.data.CurrencyType; @@ -28,7 +29,7 @@ public class CurrencyCreateCommand extends ChildCommand { @Override public @NotNull String getUsage() { - return "create <货币ID> <货币名称> <支持转账(t|f)>"; + return "create <货币ID> <货币名称> <支持转账(t|f)>"; } @Override @@ -47,7 +48,7 @@ public class CurrencyCreateCommand extends ChildCommand { ParentCurrencyCommand.INSTANCE.sendHelp(sender); return true; } - if (CurrencyDataManager.getCurrencyType(args[0]) != null) { + if (CurrencyAPI.getCurrencyType(args[0]) != null) { CurrencyMessage.CURRENCY_TYPE_EXISTED.show(sender); return true; } @@ -61,25 +62,8 @@ public class CurrencyCreateCommand extends ChildCommand { CurrencyMessage.CURRENCY_TYPE_NAME_TOO_LONG.show(sender); return true; } - boolean vault; - switch (args[2]) { - case "t": - case "true": { - vault = true; - break; - } - case "f": - case "false": { - vault = false; - break; - } - default: { - CurrencyMessage.CURRENCY_TYPE_VAULT_INPUT_ERROR.show(sender); - return true; - } - } boolean transferable; - switch (args[3]) { + switch (args[2]) { case "t": case "true": { transferable = true; @@ -97,7 +81,8 @@ public class CurrencyCreateCommand extends ChildCommand { } CurrencyPlugin.async(() -> { try { - CurrencyDataManager.updateCurrencyType(new CurrencyType(id, name, vault, transferable), true); + CurrencyType type = new CurrencyType(id, name, transferable); + CurrencyDataManager.updateCurrencyType(type, true); CurrencyMessage.CURRENCY_CREATE_SUCCESS.show(sender); } catch (SQLException e) { CurrencyMessage.CURRENCY_CREATE_ERROR.show(sender); diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyGiveCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyGiveCommand.java index c541631..a42a864 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyGiveCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyGiveCommand.java @@ -59,7 +59,7 @@ public class CurrencyGiveCommand extends ChildCommand { return true; } double amount = Double.parseDouble(args[2]); - CurrencyAPI.addPlayerCurrencyAsync(playerInfo.getUuid(), type.getId(), amount); + CurrencyAPI.givePlayerCurrency(playerInfo.getUuid(), type.getId(), amount); CurrencyMessage.CURRENCY_GIVE_SUCCESS.show(sender); return true; } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyPayCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyPayCommand.java index 6c3af9e..b30a9d3 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyPayCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyPayCommand.java @@ -5,8 +5,8 @@ import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo; import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand; import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; -import cn.hamster3.mc.plugin.currency.CurrencyPlugin; import cn.hamster3.mc.plugin.currency.api.CurrencyAPI; +import cn.hamster3.mc.plugin.currency.async.CompletableTask; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.core.CurrencyMessage; import cn.hamster3.mc.plugin.currency.data.CurrencyType; @@ -16,7 +16,6 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.sql.SQLException; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -84,34 +83,38 @@ public class CurrencyPayCommand extends ChildCommand { CurrencyMessage.PAY_CURRENCY_NOT_TRANSFERABLE.show(sender); return true; } - double hasAmount = CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type.getId()); - if (hasAmount < payAmount) { - CurrencyMessage.PAT_BALANCE_NOT_ENOUGH.show(sender, - "%has%", String.format("%.2f", hasAmount), - "%need%", String.format("%.2f", payAmount) - ); - return true; - } - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.takePlayerCurrency(player.getUniqueId(), type.getId(), payAmount, true); - CurrencyDataManager.addPlayerCurrency(targetInfo.getUuid(), type.getId(), payAmount, true); - CurrencyMessage.PAY_SUCCESS.show(sender, - "%type_id%", type.getId(), - "%type_name%", type.getName(), - "%from_player_name%", player.getName(), - "%to_player_name%", targetInfo.getName(), - "%amount%", String.valueOf(payAmount)); - DisplayMessage message = CurrencyMessage.PAY_RECEIVED.getMessage().copy() - .replace("%type_id%", type.getId()) - .replace("%type_name%", type.getName()) - .replace("%from_player_name%", player.getName()) - .replace("%to_player_name%", targetInfo.getName()) - .replace("%amount%", String.valueOf(payAmount)); - BallAPI.getInstance().sendMessageToPlayer(targetInfo.getUuid(), message, true); - } catch (SQLException e) { - e.printStackTrace(); + CompletableTask.supplyAsync(() -> { + double hasAmount = CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type.getId()).getOrDefault(0D); + if (hasAmount < payAmount) { + CurrencyMessage.PAT_BALANCE_NOT_ENOUGH.show(sender, + "%has%", String.format("%.2f", hasAmount), + "%need%", String.format("%.2f", payAmount) + ); + return false; } + CurrencyDataManager.takePlayerCurrency(player.getUniqueId(), type.getId(), payAmount, true); + CurrencyDataManager.addPlayerCurrency(targetInfo.getUuid(), type.getId(), payAmount, true); + return true; + }).onSuccess(success -> { + if (!success) { + return; + } + CurrencyMessage.PAY_SUCCESS.show(sender, + "%type_id%", type.getId(), + "%type_name%", type.getName(), + "%from_player_name%", player.getName(), + "%to_player_name%", targetInfo.getName(), + "%amount%", String.valueOf(payAmount)); + DisplayMessage message = CurrencyMessage.PAY_RECEIVED.getMessage().copy() + .replace("%type_id%", type.getId()) + .replace("%type_name%", type.getName()) + .replace("%from_player_name%", player.getName()) + .replace("%to_player_name%", targetInfo.getName()) + .replace("%amount%", String.valueOf(payAmount)); + BallAPI.getInstance().sendMessageToPlayer(targetInfo.getUuid(), message, true); + }).onFailed(throwable -> { + CurrencyMessage.ERROR_EXCEPTION.show(player); + throwable.printStackTrace(); }); return true; } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyResetCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyResetCommand.java index 0f387e2..1e2974a 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyResetCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyResetCommand.java @@ -29,7 +29,7 @@ public class CurrencyResetCommand extends ChildCommand { @Override public @NotNull String getUsage() { - return "reset <货币ID> <货币名称> <支持转账(t|f)>"; + return "reset <货币ID> <货币名称> <支持转账(t|f)>"; } @Override @@ -62,25 +62,8 @@ public class CurrencyResetCommand extends ChildCommand { CurrencyMessage.CURRENCY_TYPE_NAME_TOO_LONG.show(sender); return true; } - boolean vault; - switch (args[2]) { - case "t": - case "true": { - vault = true; - break; - } - case "f": - case "false": { - vault = false; - break; - } - default: { - CurrencyMessage.CURRENCY_TYPE_VAULT_INPUT_ERROR.show(sender); - return true; - } - } boolean transferable; - switch (args[3]) { + switch (args[2]) { case "t": case "true": { transferable = true; @@ -98,7 +81,7 @@ public class CurrencyResetCommand extends ChildCommand { } CurrencyPlugin.async(() -> { try { - CurrencyDataManager.updateCurrencyType(new CurrencyType(id, name, vault, transferable), true); + CurrencyDataManager.updateCurrencyType(new CurrencyType(id, name, transferable), true); CurrencyMessage.CURRENCY_RESET_SUCCESS.show(sender); } catch (SQLException e) { CurrencyMessage.CURRENCY_RESET_ERROR.show(sender); diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySeeCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySeeCommand.java index 1dd925c..c2c03e0 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySeeCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySeeCommand.java @@ -3,6 +3,7 @@ package cn.hamster3.mc.plugin.currency.command.currency; import cn.hamster3.mc.plugin.ball.common.api.BallAPI; import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo; import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand; +import cn.hamster3.mc.plugin.currency.api.CurrencyAPI; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.core.CurrencyMessage; import cn.hamster3.mc.plugin.currency.data.CurrencyType; @@ -11,9 +12,12 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +@SuppressWarnings("CallToPrintStackTrace") public class CurrencySeeCommand extends ChildCommand { public static final CurrencySeeCommand INSTANCE = new CurrencySeeCommand(); @@ -51,30 +55,31 @@ public class CurrencySeeCommand extends ChildCommand { CurrencyMessage.PLAYER_NOT_FOUND.show(sender); return true; } + Collection types; if (args.length >= 2) { CurrencyType type = CurrencyDataManager.getCurrencyType(args[1]); if (type == null) { CurrencyMessage.CURRENCY_TYPE_NOT_EXIST.show(sender); return true; } - double amount = CurrencyDataManager.getPlayerCurrency(playerInfo.getUuid(), type.getId()); - CurrencyMessage.CURRENCY_SEE_RESULT.show(sender, - "%type_id%", type.getId(), - "%type_name%", type.getName(), - "%amount%", String.valueOf(amount), - "%player_name%", playerInfo.getName() - ); - return true; - } - for (CurrencyType type : CurrencyDataManager.getCurrencyTypes()) { - double amount = CurrencyDataManager.getPlayerCurrency(playerInfo.getUuid(), type.getId()); - CurrencyMessage.CURRENCY_SEE_RESULT.show(sender, - "%type_id%", type.getId(), - "%type_name%", type.getName(), - "%amount%", String.valueOf(amount), - "%player_name%", playerInfo.getName() - ); + types = Collections.singleton(type); + } else { + types = CurrencyDataManager.getCurrencyTypes(); } + CurrencyAPI.getPlayerCurrency(playerInfo.getUuid()) + .onSuccess(map -> { + for (CurrencyType type : types) { + CurrencyMessage.CURRENCY_SEE_RESULT.show(sender, + "%type_id%", type.getId(), + "%type_name%", type.getName(), + "%amount%", String.valueOf(map.getOrDefault(type.getId(), 0D)), + "%player_name%", playerInfo.getName() + ); + } + }).onFailed(throwable -> { + CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender); + throwable.printStackTrace(); + }); return true; } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySetCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySetCommand.java index 3160f85..18cd5f0 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySetCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencySetCommand.java @@ -59,8 +59,9 @@ public class CurrencySetCommand extends ChildCommand { return true; } double amount = Double.parseDouble(args[2]); - CurrencyAPI.setPlayerCurrencyAsync(playerInfo.getUuid(), type.getId(), amount); - CurrencyMessage.CURRENCY_SET_SUCCESS.show(sender); + CurrencyAPI.setPlayerCurrency(playerInfo.getUuid(), type.getId(), amount) + .onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender)) + .onSuccess(unused -> CurrencyMessage.CURRENCY_SET_SUCCESS.show(sender)); return true; } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyTakeCommand.java b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyTakeCommand.java index 694c77b..1b7a2eb 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyTakeCommand.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/command/currency/CurrencyTakeCommand.java @@ -53,13 +53,13 @@ public class CurrencyTakeCommand extends ChildCommand { CurrencyMessage.PLAYER_NOT_FOUND.show(sender); return true; } - CurrencyType type = CurrencyDataManager.getCurrencyType(args[1]); + CurrencyType type = CurrencyAPI .getCurrencyType(args[1]); if (type == null) { CurrencyMessage.CURRENCY_TYPE_NOT_EXIST.show(sender); return true; } double amount = Double.parseDouble(args[2]); - CurrencyAPI.takePlayerCurrencyAsync(playerInfo.getUuid(), type.getId(), amount); + CurrencyAPI.takePlayerCurrency(playerInfo.getUuid(), type.getId(), amount); CurrencyMessage.CURRENCY_TAKE_SUCCESS.show(sender); return true; } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/core/ConfigManager.java b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyConfigManager.java similarity index 82% rename from src/main/java/cn/hamster3/mc/plugin/currency/core/ConfigManager.java rename to src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyConfigManager.java index 66bfcee..13a9960 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/core/ConfigManager.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyConfigManager.java @@ -4,10 +4,12 @@ import cn.hamster3.mc.plugin.currency.CurrencyPlugin; import lombok.Getter; import org.bukkit.configuration.file.FileConfiguration; -public class ConfigManager { +public class CurrencyConfigManager { @Getter private static boolean enableVaultCommands; @Getter + private static String vaultCurrencyID; + @Getter private static String vaultNamePlural; @Getter private static String vaultNameSingular; @@ -20,8 +22,8 @@ public class ConfigManager { FileConfiguration pluginConfig = plugin.getConfig(); enableVaultCommands = pluginConfig.getBoolean("enable-vault-commands", true); + vaultCurrencyID = pluginConfig.getString("vault-settings.currency-id", "money"); vaultNamePlural = pluginConfig.getString("vault-settings.currency-name-plural", "金币"); vaultNameSingular = pluginConfig.getString("vault-settings.currency-name-singular", "金币"); } - } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java index bcbbc34..c7527df 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyDataManager.java @@ -13,11 +13,10 @@ import java.util.Collection; import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; public final class CurrencyDataManager { - private static final ConcurrentMap CURRENCY_TYPES = new ConcurrentHashMap<>(); - private static final ConcurrentMap> PLAYER_DATA = new ConcurrentHashMap<>(); + private static final Map CURRENCY_TYPES = new ConcurrentHashMap<>(); + private static final Map> PLAYER_DATA = new ConcurrentHashMap<>(); private CurrencyDataManager() { } @@ -28,62 +27,59 @@ public final class CurrencyDataManager { statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_type(" + "`id` VARCHAR(36) PRIMARY KEY," + "`name` VARCHAR(36) NOT NULL," + - "`vault` BOOLEAN NOT NULL," + "`transferable` BOOLEAN NOT NULL" + ") CHARSET utf8mb4;"); statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_data(" + "`uuid` CHAR(36) NOT NULL," + "`currency` VARCHAR(36) NOT NULL," + "`amount` DOUBLE NOT NULL," + - "UNIQUE KEY `unique_player_currency` (`uuid`, `currency`)" + + "PRIMARY KEY `player_currency` (`uuid`, `currency`)" + ") CHARSET utf8mb4;"); try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_type;")) { while (set.next()) { CurrencyType type = new CurrencyType( set.getString("id"), set.getString("name"), - set.getBoolean("vault"), set.getBoolean("transferable") ); CURRENCY_TYPES.put(type.getId(), type); } } - try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_data ORDER BY `uuid`;")) { - if (set.next()) { - setPlayerCurrency( - UUID.fromString(set.getString("uuid")), - set.getString("currency"), - set.getDouble("amount"), - false - ); - } - } } } } - public static void loadPlayerData(@NotNull UUID uuid) throws SQLException { + public static Map loadPlayerData(@NotNull UUID uuid) throws SQLException { try (Connection connection = CoreAPI.getInstance().getConnection()) { try (PreparedStatement statement = connection.prepareStatement( "SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;" )) { statement.setString(1, uuid.toString()); try (ResultSet set = statement.executeQuery()) { - Map currency = getPlayerCurrency(uuid); + Map playerData = new ConcurrentHashMap<>(); while (set.next()) { - currency.put(set.getString("currency"), set.getDouble("amount")); + playerData.put( + set.getString("currency"), + set.getDouble("amount") + ); } + return playerData; } } } } @NotNull - public static Map getPlayerCurrency(@NotNull UUID uuid) { - return PLAYER_DATA.computeIfAbsent(uuid, o -> new ConcurrentHashMap<>()); + public static Map getPlayerCurrency(@NotNull UUID uuid) throws SQLException { + Map map = PLAYER_DATA.get(uuid); + if (map == null) { + map = loadPlayerData(uuid); + PLAYER_DATA.put(uuid, map); + } + return map; } - public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull String typeID) { + public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull String typeID) throws SQLException { return getPlayerCurrency(uuid).getOrDefault(typeID, 0d); } @@ -98,6 +94,7 @@ public final class CurrencyDataManager { statement.setString(1, uuid.toString()); statement.setString(2, typeID); statement.setDouble(3, amount); + statement.setDouble(4, amount); statement.executeUpdate(); } } @@ -141,12 +138,11 @@ public final class CurrencyDataManager { if (broadcast) { try (Connection connection = CoreAPI.getInstance().getConnection()) { try (PreparedStatement statement = connection.prepareStatement( - "REPLACE INTO hamster_currency_type VALUES(?, ?, ?, ?);" + "REPLACE INTO hamster_currency_type VALUES(?, ?, ?);" )) { statement.setString(1, type.getId()); statement.setString(2, type.getName()); - statement.setBoolean(3, type.isVault()); - statement.setBoolean(4, type.isTransferable()); + statement.setBoolean(3, type.isTransferable()); statement.executeUpdate(); } BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "updateCurrencyType", type); @@ -164,7 +160,7 @@ public final class CurrencyDataManager { statement.setString(1, typeID); statement.executeUpdate(); } - BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "setCurrencyType", typeID); + BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "deleteCurrencyType", typeID); } } } @@ -181,11 +177,6 @@ public final class CurrencyDataManager { @Nullable public static CurrencyType getVaultCurrencyType() { - for (CurrencyType type : CURRENCY_TYPES.values()) { - if (type.isVault()) { - return type; - } - } - return null; + return getCurrencyType(CurrencyConfigManager.getVaultCurrencyID()); } } diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyMessage.java b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyMessage.java index a883ec5..2ed0611 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyMessage.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/core/CurrencyMessage.java @@ -3,9 +3,9 @@ package cn.hamster3.mc.plugin.currency.core; import cn.hamster3.mc.plugin.core.bukkit.api.CoreBukkitAPI; import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; -import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.audience.Audience; -import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.TextReplacementConfig; import lombok.Getter; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.TextReplacementConfig; import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -17,6 +17,8 @@ import java.io.IOException; @Getter public enum CurrencyMessage { + ERROR_EXCEPTION("服务器处理出现异常,请联系服务器管理员反馈问题"), + ERROR_EXCEPTION_ADMIN("服务器处理出现异常,请查看服务器后台报错"), PLAYER_NOT_FOUND("§c未找到该玩家"), CURRENCY_TYPE_NOT_EXIST("§c未找到该货币类型"), CURRENCY_TYPE_EXISTED("§c该货币 ID 已存在"), diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java b/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java index 802605c..5a92afd 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/data/CurrencyType.java @@ -14,10 +14,6 @@ public class CurrencyType { * 显示名称 */ private String name; - /** - * 是否挂接到 vault - */ - private boolean vault; /** * 是否允许转账 */ diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyEvent.java b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyEvent.java new file mode 100644 index 0000000..846501e --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyEvent.java @@ -0,0 +1,36 @@ +package cn.hamster3.mc.plugin.currency.event; + +import cn.hamster3.mc.plugin.currency.data.CurrencyType; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +@Getter +@Setter +public class CurrencyEvent extends Event { + @Getter + private static HandlerList handlerList = new HandlerList(); + @NotNull + private UUID uuid; + @NotNull + private CurrencyType type; + private double amount; + + public CurrencyEvent(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) { + super(true); + this.uuid = uuid; + this.type = type; + this.amount = amount; + } + + + @NotNull + @Override + public HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyGiveEvent.java b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyGiveEvent.java new file mode 100644 index 0000000..4d51890 --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyGiveEvent.java @@ -0,0 +1,12 @@ +package cn.hamster3.mc.plugin.currency.event; + +import cn.hamster3.mc.plugin.currency.data.CurrencyType; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public class CurrencyGiveEvent extends CurrencyEvent { + public CurrencyGiveEvent(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) { + super(uuid, type, amount); + } +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencySetEvent.java b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencySetEvent.java new file mode 100644 index 0000000..66522d6 --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencySetEvent.java @@ -0,0 +1,12 @@ +package cn.hamster3.mc.plugin.currency.event; + +import cn.hamster3.mc.plugin.currency.data.CurrencyType; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public class CurrencySetEvent extends CurrencyEvent { + public CurrencySetEvent(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) { + super(uuid, type, amount); + } +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyTakeEvent.java b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyTakeEvent.java new file mode 100644 index 0000000..9dd0696 --- /dev/null +++ b/src/main/java/cn/hamster3/mc/plugin/currency/event/CurrencyTakeEvent.java @@ -0,0 +1,12 @@ +package cn.hamster3.mc.plugin.currency.event; + +import cn.hamster3.mc.plugin.currency.data.CurrencyType; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public class CurrencyTakeEvent extends CurrencyEvent { + public CurrencyTakeEvent(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) { + super(uuid, type, amount); + } +} diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/hook/PlaceholderHook.java b/src/main/java/cn/hamster3/mc/plugin/currency/hook/PlaceholderHook.java index efab861..533931a 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/hook/PlaceholderHook.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/hook/PlaceholderHook.java @@ -1,6 +1,6 @@ package cn.hamster3.mc.plugin.currency.hook; -import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; +import cn.hamster3.mc.plugin.currency.api.CurrencyAPI; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -32,7 +32,7 @@ public final class PlaceholderHook extends PlaceholderExpansion { @Override public String onRequest(OfflinePlayer player, @NotNull String params) { - return String.format("%.2f", CurrencyDataManager.getPlayerCurrency(player.getUniqueId(), params)); + return String.format("%.2f", CurrencyAPI.getPlayerCurrency(player.getUniqueId(), params).getOrDefault(0D)); } @Override diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/hook/VaultEconomyHook.java b/src/main/java/cn/hamster3/mc/plugin/currency/hook/VaultEconomyHook.java index 8542697..6509e43 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/hook/VaultEconomyHook.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/hook/VaultEconomyHook.java @@ -1,34 +1,24 @@ package cn.hamster3.mc.plugin.currency.hook; import cn.hamster3.mc.plugin.ball.common.api.BallAPI; -import cn.hamster3.mc.plugin.currency.CurrencyPlugin; -import cn.hamster3.mc.plugin.currency.core.ConfigManager; +import cn.hamster3.mc.plugin.currency.api.CurrencyAPI; +import cn.hamster3.mc.plugin.currency.core.CurrencyConfigManager; import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager; import cn.hamster3.mc.plugin.currency.data.CurrencyType; import net.milkbowl.vault.economy.AbstractEconomy; import net.milkbowl.vault.economy.EconomyResponse; import org.bukkit.OfflinePlayer; -import org.jetbrains.annotations.NotNull; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.UUID; -@SuppressWarnings("CallToPrintStackTrace") public class VaultEconomyHook extends AbstractEconomy { - private static final EconomyResponse NOT_IMPLEMENTED_RESPONSE = - new EconomyResponse( - 0, - 0, - EconomyResponse.ResponseType.NOT_IMPLEMENTED, - "HamsterCurrency 未实现该功能~" - ); - @NotNull - private final CurrencyType currencyType; + public static final VaultEconomyHook INSTANCE = new VaultEconomyHook(); + private static final EconomyResponse NOT_IMPLEMENTED = new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "HamsterCurrency 未实现该功能~"); + private static final EconomyResponse CURRENCY_TYPE_NOT_FOUND = new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Vault 货币类型设置出错"); - public VaultEconomyHook(@NotNull CurrencyType currencyType) { - this.currencyType = currencyType; + private VaultEconomyHook() { } @Override @@ -53,23 +43,31 @@ public class VaultEconomyHook extends AbstractEconomy { @Override public String currencyNamePlural() { - return ConfigManager.getVaultNamePlural(); + return CurrencyConfigManager.getVaultNamePlural(); } @Override public String currencyNameSingular() { - return ConfigManager.getVaultNameSingular(); + return CurrencyConfigManager.getVaultNameSingular(); } @Override public double getBalance(String playerName) { UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid(); - return CurrencyDataManager.getPlayerCurrency(uuid, currencyType.getId()); + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return 0D; + } + return CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent(); } @Override public double getBalance(OfflinePlayer player) { - return CurrencyDataManager.getPlayerCurrency(player.getUniqueId(), currencyType.getId()); + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return 0D; + } + return CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type.getId()).getSilent(); } @Override @@ -104,37 +102,33 @@ public class VaultEconomyHook extends AbstractEconomy { @Override public EconomyResponse withdrawPlayer(String playerName, double amount) { + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return CURRENCY_TYPE_NOT_FOUND; + } UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid(); - double balance = CurrencyDataManager.getPlayerCurrency(uuid, currencyType.getId()); + double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent(); if (balance < amount) { return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.FAILURE, "余额不足"); } double result = balance - amount; - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.setPlayerCurrency(uuid, currencyType.getId(), result, true); - } catch (SQLException e) { - e.printStackTrace(); - } - }); + CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent(); return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "扣款成功"); } @Override public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) { + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return CURRENCY_TYPE_NOT_FOUND; + } UUID uuid = player.getUniqueId(); - double balance = CurrencyDataManager.getPlayerCurrency(uuid, currencyType.getId()); + double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent(); if (balance < amount) { return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.FAILURE, "余额不足"); } double result = balance - amount; - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.setPlayerCurrency(uuid, currencyType.getId(), result, true); - } catch (SQLException e) { - e.printStackTrace(); - } - }); + CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent(); return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "扣款成功"); } @@ -150,31 +144,27 @@ public class VaultEconomyHook extends AbstractEconomy { @Override public EconomyResponse depositPlayer(String playerName, double amount) { + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return CURRENCY_TYPE_NOT_FOUND; + } UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid(); - double balance = CurrencyDataManager.getPlayerCurrency(uuid, currencyType.getId()); + double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent(); double result = balance + amount; - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.setPlayerCurrency(uuid, currencyType.getId(), result, true); - } catch (SQLException e) { - e.printStackTrace(); - } - }); + CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent(); return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, ""); } @Override public EconomyResponse depositPlayer(OfflinePlayer player, double amount) { + CurrencyType type = CurrencyDataManager.getVaultCurrencyType(); + if (type == null) { + return CURRENCY_TYPE_NOT_FOUND; + } UUID uuid = player.getUniqueId(); - double balance = CurrencyDataManager.getPlayerCurrency(uuid, currencyType.getId()); + double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent(); double result = balance + amount; - CurrencyPlugin.async(() -> { - try { - CurrencyDataManager.setPlayerCurrency(uuid, currencyType.getId(), result, true); - } catch (SQLException e) { - e.printStackTrace(); - } - }); + CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent(); return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, ""); } @@ -205,42 +195,42 @@ public class VaultEconomyHook extends AbstractEconomy { @Override public EconomyResponse createBank(String name, String player) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse deleteBank(String name) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse bankBalance(String name) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse bankHas(String name, double amount) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse bankWithdraw(String name, double amount) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse bankDeposit(String name, double amount) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse isBankOwner(String name, String playerName) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override public EconomyResponse isBankMember(String name, String playerName) { - return NOT_IMPLEMENTED_RESPONSE; + return NOT_IMPLEMENTED; } @Override diff --git a/src/main/java/cn/hamster3/mc/plugin/currency/listener/BallListener.java b/src/main/java/cn/hamster3/mc/plugin/currency/listener/BallListener.java index b50bafc..0d6ab60 100644 --- a/src/main/java/cn/hamster3/mc/plugin/currency/listener/BallListener.java +++ b/src/main/java/cn/hamster3/mc/plugin/currency/listener/BallListener.java @@ -56,7 +56,7 @@ public class BallListener { } break; } - case "removeCurrencyType": { + case "deleteCurrencyType": { try { CurrencyDataManager.deleteCurrencyType(event.getContentAsString(), false); } catch (SQLException e) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ffedbe5..10fb10a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,12 +8,12 @@ vault-settings: # Vault 显示货币名称(复数) currency-name-plural: 金币 -# 是否预加载玩家数据 -# ALL:对于所有货币类型都进行预加载 -# VAULT:只对绑定于 VaultAPI 的货币进行预加载 -# NONE:对任何货币都不进行预加载 -# 预加载是指服务端启动时将数据库中所有玩家的货币余额都从数据库中加载到内存 -# 开启预加载时,如果玩家数量过多,则可能会减慢服务器启动速度 +# 是否提前加载玩家数据 +# ALL:对于所有货币类型都进行提前 +# VAULT:只对绑定于 VaultAPI 的货币进行提前 +# NONE:对任何货币都不进行提前 +# 提前是指服务端启动时将数据库中所有玩家的货币余额都从数据库中加载到内存 +# 开启提前加载时,如果玩家数量过多,则可能会减慢服务器启动速度 # 一般来说,设置为 VAULT 即可 # 该功能暂未实现 pre-load-data: VAULT