From a0acd6e76093a1c688a911d0561df35b79343b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Thu, 28 Sep 2023 16:23:10 +0200 Subject: [PATCH 01/24] docs(README): fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7029583..04969e7 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - **Multi-currency** - LightEco supports creating your own currencies and using them in your server - **Local and global currencies** - LightEco offers support for both local and global currencies, enabling you to create server-specific or proxy-wide currencies. - **Storage** - LightEco offers support for multiple storage types. -- **Open-source** - LightEco is open-source, and is available on [GitHub](https://github.com/xHyroM/lightecp) +- **Open-source** - LightEco is open-source, and is available on [GitHub](https://github.com/xHyroM/lighteco) ## Development @@ -23,4 +23,4 @@ Monorepo structure: ## License -LightEco is licensed under the [Apache-2.0 License](./LICENSE). \ No newline at end of file +LightEco is licensed under the [Apache-2.0 License](./LICENSE). From 13c8e75402e90f07b33785ab34e553ab4af48458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Thu, 28 Sep 2023 16:23:38 +0200 Subject: [PATCH 02/24] d --- currency-money/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/currency-money/README.md b/currency-money/README.md index e788a02..0ea6d3b 100644 --- a/currency-money/README.md +++ b/currency-money/README.md @@ -15,8 +15,8 @@ - **Vault support** - Money supports Vault, allowing you to use other economy plugins with Money. - **Configurable** - Money is configurable, allowing you to change the currency's name, symbol, and more. - **Lightweight** - Money is lightweight, and is designed to be as efficient as possible. -- **Open-source** - Money is open-source, and is available on [GitHub](https://github.com/xHyroM/tree/main/currency-money) +- **Open-source** - Money is open-source, and is available on [GitHub](https://github.com/xHyroM/lighteco/tree/main/currency-money) ## License -Money is licensed under the [Apache-2.0 License](./LICENSE). \ No newline at end of file +Money is licensed under the [Apache-2.0 License](./LICENSE). From 1d808680d13e7bed171bbbfaaaf03f4b5f3fde28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Thu, 28 Sep 2023 17:01:16 +0200 Subject: [PATCH 03/24] ci: add publishing to repo.jopga.me --- .../kotlin/lighteco.base-logic.gradle.kts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts index a799e87..3ef4159 100644 --- a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts +++ b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts @@ -23,4 +23,28 @@ tasks { compileJava { options.encoding = Charsets.UTF_8.name() } +} + +publishing { + // Publishing to repo.jopga.me + publications.create("mavenJava") { + repositories.maven { + url = uri("https://repo.jopga.me/releases") + + credentials(PasswordCredentials::class) + authentication { + create("basic") + } + } + + groupId = rootProject.group as String + artifactId = project.name + version = rootProject.version as String + + pom { + name.set("LightEco") + } + + artifact(tasks.named("jar")) + } } \ No newline at end of file From 1f0f663b061c734cc0b0a9afadddd634c71e5993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Fri, 29 Sep 2023 14:01:31 +0200 Subject: [PATCH 04/24] ci: publish sources jar --- buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts index 3ef4159..6e0eab4 100644 --- a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts +++ b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts @@ -43,8 +43,13 @@ publishing { pom { name.set("LightEco") + } - artifact(tasks.named("jar")) + artifact(tasks.named("jar")) { + classifier = "" + } + + artifact(tasks.named("sourcesJar")) } } \ No newline at end of file From 2b9beb74da7a7284d21adb167ac63618e4bb2aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Fri, 29 Sep 2023 14:16:14 +0200 Subject: [PATCH 05/24] ci: more things in pom, set project descriptions --- build.gradle.kts | 2 ++ .../kotlin/lighteco.base-logic.gradle.kts | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4335316..98173ed 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,9 +13,11 @@ val commitHash = determineCommitHash(project) allprojects { group = "dev.xhyrom" version = "$majorVersion.$minorVersion.$patchVersion" + description = "Incredibly fast, lightweight, and modular plugin that excels across multiple platforms." ext { set("version", "$majorVersion.$minorVersion.$patchVersion+$commitHash") + set("description", description) } } diff --git a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts index 6e0eab4..3a09e7d 100644 --- a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts +++ b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts @@ -43,7 +43,37 @@ publishing { pom { name.set("LightEco") - + url.set("https://github.com/xHyroM/lighteco") + description.set(project.description) + organization { + name.set("xHyroM") + url.set("https://xhyrom.dev") + } + developers { + developer { + id.set("xHyroM") + name.set("xHyroM") + email.set("lol@xhyrom.dev") + timezone.set("Europe/Bratislava") + url.set("https://xhyrom.dev") + } + } + scm { + connection.set("scm:git:https://github.com/xHyroM/lighteco.git") + developerConnection.set("scm:git:git@github.com:xHyroM/lighteco.git") + url.set("https://github.com/xHyroM/lighteco") + } + licenses { + license { + name.set("Apache License 2.0") + url.set("https://github.com/xHyroM/lighteco/blob/main/LICENSE") + distribution.set("repo") + } + } + ciManagement { + system.set("GitHub Actions") + url.set("https://github.com/xHyroM/lighteco/actions") + } } artifact(tasks.named("jar")) { From af3e64063f002084e8df0e7c8e5621ae2bd85e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Fri, 29 Sep 2023 14:17:10 +0200 Subject: [PATCH 06/24] build: make currency-money description different --- currency-money/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/currency-money/build.gradle.kts b/currency-money/build.gradle.kts index e63ef0b..7c721b7 100644 --- a/currency-money/build.gradle.kts +++ b/currency-money/build.gradle.kts @@ -2,6 +2,8 @@ plugins { id("lighteco.addon-logic") } +description = "Addon that adds main currency to LightEco that uses Vault to hook into other economy plugins." + repositories { maven("https://repo.papermc.io/repository/maven-public/") maven("https://jitpack.io") From f4d4bbe8566ea0062c86c723a499e9a8065f1722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Fri, 29 Sep 2023 15:05:03 +0200 Subject: [PATCH 07/24] feat: currency identifier aliases --- .../lighteco/api/model/currency/Currency.java | 13 +++++ .../bukkit/manager/BukkitCommandManager.java | 47 +++++++++++-------- .../common/model/currency/Currency.java | 4 ++ .../money/common/currency/MoneyCurrency.java | 5 ++ 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java index 8f06741..308f086 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java @@ -13,6 +13,19 @@ public interface Currency { */ String getIdentifier(); + /** + * Returns the identifier aliases of the currency. + *

+ * Useful if you want multiple commands for the same currency. + * For example, you can have a command `/hyrocoins` but also `/hc`. + *

+ * + * @return the aliases + */ + default String[] getIdentifierAliases() { + return new String[0]; + }; + /** * Returns the type of the currency, either {@link Type#LOCAL} or {@link Type#GLOBAL} * 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 9b78ab5..53d1260 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 @@ -20,28 +20,11 @@ public class BukkitCommandManager extends AbstractCommandManager { @Override public void registerCurrencyCommand(@NonNull Currency currency) { - String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; + registerCommands(currency.getIdentifier(), currency); - // Balance - for (CommandAPICommand cmd : new BalanceCommand( - this, - currency.getIdentifier(), - currency, - permissionBase - ).multipleBuild()) { - cmd.register(); + for (String alias : currency.getIdentifierAliases()) { + registerCommands(alias, currency); } - - CommandAPICommand cmd = new CommandAPICommand(currency.getIdentifier()) - .withSubcommand(new SetCommand(this, currency, permissionBase).build()) - .withSubcommand(new GiveCommand(this, currency, permissionBase).build()) - .withSubcommand(new TakeCommand(this, currency, permissionBase).build()) - .withSubcommands(new BalanceCommand(this, "balance", currency, permissionBase).multipleBuild()); - - if (currency.isPayable()) - cmd = cmd.withSubcommand(new PayCommand(this, currency, permissionBase).build()); - - cmd.register(); } @Override @@ -69,6 +52,30 @@ public class BukkitCommandManager extends AbstractCommandManager { ).multipleBuild()) { cmd.register(); } + } + private void registerCommands(@NonNull String name, @NonNull Currency currency) { + String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; + + // Balance + for (CommandAPICommand cmd : new BalanceCommand( + this, + name, + currency, + permissionBase + ).multipleBuild()) { + cmd.register(); + } + + CommandAPICommand cmd = new CommandAPICommand(name) + .withSubcommand(new SetCommand(this, currency, permissionBase).build()) + .withSubcommand(new GiveCommand(this, currency, permissionBase).build()) + .withSubcommand(new TakeCommand(this, currency, permissionBase).build()) + .withSubcommands(new BalanceCommand(this, "balance", currency, permissionBase).multipleBuild()); + + if (currency.isPayable()) + cmd = cmd.withSubcommand(new PayCommand(this, currency, permissionBase).build()); + + cmd.register(); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java b/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java index 8946d49..cb978a4 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/model/currency/Currency.java @@ -16,6 +16,10 @@ public class Currency { return proxy.getIdentifier(); } + public String[] getIdentifierAliases() { + return proxy.getIdentifierAliases(); + } + public dev.xhyrom.lighteco.api.model.currency.Currency.Type getType() { return proxy.getType(); } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java index 5dbcef8..1858cf1 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java @@ -17,6 +17,11 @@ public class MoneyCurrency implements Currency { return "money"; } + @Override + public String[] getIdentifierAliases() { + return new String[]{"eco"}; + } + @Override public Type getType() { return Type.LOCAL; From 1e86142a58577757dfec716fd2a4bd872ae8da2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Thu, 5 Oct 2023 17:31:52 +0200 Subject: [PATCH 08/24] feat: handle maximum balance --- .../api/exception/CannotBeGreaterThan.java | 7 +++ .../api/exception/CannotBeNegative.java | 7 +++ .../lighteco/bukkit/commands/GiveCommand.java | 5 +- .../lighteco/bukkit/commands/PayCommand.java | 5 +- .../lighteco/bukkit/commands/SetCommand.java | 5 +- .../lighteco/bukkit/commands/TakeCommand.java | 5 +- .../dev/xhyrom/lighteco/bukkit/util/Util.java | 19 ++++++ .../xhyrom/lighteco/common/config/Config.java | 6 ++ .../config/message/CurrencyMessageConfig.java | 1 + .../command/AbstractCommandManager.java | 59 +++++++++++++++++-- .../lighteco/common/model/user/User.java | 24 ++++++-- .../provider/sql/SqlStorageProvider.java | 2 +- .../money/bukkit/hooks/vault/Vault.java | 10 ++-- 13 files changed, 132 insertions(+), 23 deletions(-) create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeGreaterThan.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeNegative.java create mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeGreaterThan.java b/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeGreaterThan.java new file mode 100644 index 0000000..45d6b92 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeGreaterThan.java @@ -0,0 +1,7 @@ +package dev.xhyrom.lighteco.api.exception; + +public class CannotBeGreaterThan extends IllegalArgumentException { + public CannotBeGreaterThan(String message) { + super(message); + } +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeNegative.java b/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeNegative.java new file mode 100644 index 0000000..4373dd9 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/exception/CannotBeNegative.java @@ -0,0 +1,7 @@ +package dev.xhyrom.lighteco.api.exception; + +public class CannotBeNegative extends IllegalArgumentException { + public CannotBeNegative(String message) { + super(message); + } +} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java index e138c0d..dbc6ab6 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java @@ -7,6 +7,7 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; +import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -27,8 +28,8 @@ public class GiveCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) + ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) + : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) ) .executes((sender, args) -> { this.handleGive(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java index 4e07a94..4a1f584 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java @@ -7,6 +7,7 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; +import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -27,8 +28,8 @@ public class PayCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) + ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) + : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) ) .executesPlayer((sender, args) -> { this.handlePay(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java index c45f9ff..ac410f0 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java @@ -7,6 +7,7 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; +import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -27,8 +28,8 @@ public class SetCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 0) - : new IntegerArgument("amount", 0) + ? new DoubleArgument("amount", 0, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) + : new IntegerArgument("amount", 0, this.manager.plugin.getConfig().maximumBalance.intValue()) ) .executes((sender, args) -> { this.handleSet(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java index 3f25263..015f6f9 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java @@ -7,6 +7,7 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; +import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -27,8 +28,8 @@ public class TakeCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) + ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) + : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) ) .executes((sender, args) -> { this.handleTake(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java new file mode 100644 index 0000000..d4da7cc --- /dev/null +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java @@ -0,0 +1,19 @@ +package dev.xhyrom.lighteco.bukkit.util; + +import lombok.experimental.UtilityClass; + +import java.math.BigDecimal; + +@UtilityClass +public class Util { + public double bigDecimalToDouble(final BigDecimal value) { + double amount = value.doubleValue(); + + // Don't return bigger balance than user actually has + if (BigDecimal.valueOf(amount).compareTo(value) > 0) { + amount = Math.nextAfter(amount, Double.NEGATIVE_INFINITY); + } + + return amount; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java index 7f1e85a..983c921 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java @@ -7,6 +7,8 @@ import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; import eu.okaeri.configs.annotation.Header; +import java.math.BigDecimal; + @Header("LightEco configuration file.") @Header("") public class Config extends OkaeriConfig { @@ -21,6 +23,10 @@ public class Config extends OkaeriConfig { @Comment("Save interval to storage in seconds.") public long saveInterval = 5L; + @Comment("Maximum allowed balance.") + @Comment("If you want to change this value, you must also change the data type in the database.") + public BigDecimal maximumBalance = BigDecimal.valueOf(999999999999999.99); + @Comment("Messages") public MessageConfig messages = new MessageConfig(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java index 3bc4c4b..7d6d645 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -18,4 +18,5 @@ public class CurrencyMessageConfig extends OkaeriConfig { public String wait = "Please wait a moment before using this command again."; public String notEnoughMoney = "You don't have enough money!"; public String cannotPaySelf = "You cannot pay yourself!"; + public String cannotBeGreaterThan = "Amount cannot be greater than "; } 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 3fef971..5853b0a 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,5 +1,6 @@ package dev.xhyrom.lighteco.common.manager.command; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -101,7 +102,19 @@ public abstract class AbstractCommandManager implements CommandManager { addToMustWait(sender.getUniqueId(), target.getUniqueId()); amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - target.setBalance(currency, amount); + try { + target.setBalance(currency, amount); + } catch (CannotBeGreaterThan e) { + removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); + sender.sendMessage( + miniMessage.deserialize( + this.getConfig(currency).cannotBeGreaterThan, + Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } sender.sendMessage( miniMessage.deserialize( @@ -120,7 +133,19 @@ public abstract class AbstractCommandManager implements CommandManager { addToMustWait(sender.getUniqueId(), target.getUniqueId()); amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - target.deposit(currency, amount); + try { + target.deposit(currency, amount); + } catch (CannotBeGreaterThan e) { + removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); + sender.sendMessage( + miniMessage.deserialize( + this.getConfig(currency).cannotBeGreaterThan, + Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } sender.sendMessage( miniMessage.deserialize( @@ -140,7 +165,19 @@ public abstract class AbstractCommandManager implements CommandManager { addToMustWait(sender.getUniqueId(), target.getUniqueId()); amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - target.withdraw(currency, amount); + try { + target.withdraw(currency, amount); + } catch (CannotBeGreaterThan e) { + removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); + sender.sendMessage( + miniMessage.deserialize( + this.getConfig(currency).cannotBeGreaterThan, + Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } sender.sendMessage( miniMessage.deserialize( @@ -186,8 +223,20 @@ public abstract class AbstractCommandManager implements CommandManager { // subtract tax from amount BigDecimal taxedAmount = amount.subtract(tax); - target.deposit(currency, taxedAmount); - user.withdraw(currency, amount); + try { + target.deposit(currency, taxedAmount); + user.withdraw(currency, amount); + } catch (CannotBeGreaterThan e) { + removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); + sender.sendMessage( + miniMessage.deserialize( + this.getConfig(currency).cannotBeGreaterThan, + Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } String template = tax.compareTo(BigDecimal.ZERO) > 0 ? this.getConfig(currency).payWithTax 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 bf0744b..4280c2d 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,5 +1,7 @@ package dev.xhyrom.lighteco.common.model.user; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.api.exception.CannotBeNegative; import dev.xhyrom.lighteco.common.api.impl.ApiUser; import dev.xhyrom.lighteco.common.cache.RedisBackedMap; import dev.xhyrom.lighteco.common.model.currency.Currency; @@ -46,13 +48,17 @@ public class User { return balances.getOrDefault(currency, currency.getDefaultBalance()); } - public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) { + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) throws CannotBeNegative, CannotBeGreaterThan { this.setBalance(currency, balance, false); } - public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force) { + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force) throws CannotBeNegative, CannotBeGreaterThan { if (balance.compareTo(BigDecimal.ZERO) < 0) { - throw new IllegalArgumentException("Balance cannot be negative"); + throw new CannotBeNegative("Balance cannot be negative"); + } + + if (balance.compareTo(this.plugin.getConfig().maximumBalance) > 0) { + throw new CannotBeGreaterThan("Balance cannot be greater than " + this.plugin.getConfig().maximumBalance); } balance = balance.setScale(currency.fractionalDigits(), RoundingMode.DOWN); @@ -62,19 +68,27 @@ public class User { this.setDirty(true); } - public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException { + public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws CannotBeNegative, CannotBeGreaterThan { if (amount.compareTo(BigDecimal.ZERO) < 0) { throw new IllegalArgumentException("Amount cannot be negative"); } + if (amount.compareTo(this.plugin.getConfig().maximumBalance) > 0) { + throw new CannotBeGreaterThan("Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); + } + this.setBalance(currency, this.getBalance(currency).add(amount)); } - public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException { + public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws CannotBeNegative, CannotBeGreaterThan { if (amount.compareTo(BigDecimal.ZERO) < 0) { throw new IllegalArgumentException("Amount cannot be negative"); } + if (amount.compareTo(this.plugin.getConfig().maximumBalance) > 0) { + throw new CannotBeGreaterThan("Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); + } + if (this.getBalance(currency).compareTo(amount) < 0) { // Withdraw all amount = this.getBalance(currency); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java index 94bbb89..afc900d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java @@ -31,7 +31,7 @@ public class SqlStorageProvider implements StorageProvider { private static final String CREATE_TABLE = """ CREATE TABLE IF NOT EXISTS '{prefix}_{table}' ( 'uuid' VARCHAR(36) NOT NULL, - 'balance' DECIMAL(10, 2) NOT NULL, + 'balance' DECIMAL(20, 2) NOT NULL, PRIMARY KEY (`uuid`) ); """.trim(); diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java index 1c19bd0..454cdf2 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java @@ -2,6 +2,8 @@ package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.api.exception.CannotBeNegative; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.user.User; import dev.xhyrom.lighteco.currency.money.common.Plugin; @@ -114,12 +116,12 @@ public class Vault extends AbstractEconomy { try { user.withdraw(currency, BigDecimal.valueOf(amount)); - } catch (IllegalArgumentException e) { + } catch (CannotBeGreaterThan | CannotBeNegative e) { return new EconomyResponse( amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.FAILURE, - "Cannot withdraw negative funds" + e.getMessage() ); } @@ -143,12 +145,12 @@ public class Vault extends AbstractEconomy { try { user.deposit(currency, BigDecimal.valueOf(amount)); - } catch (IllegalArgumentException e) { + } catch (CannotBeGreaterThan | CannotBeNegative e) { return new EconomyResponse( amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.FAILURE, - "Cannot deposit negative funds" + e.getMessage() ); } From 7ff6cbde47a9755a63ce02fa84ed3ca777883b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Thu, 5 Oct 2023 21:39:21 +0200 Subject: [PATCH 09/24] fix(currency-money): load at STARTUP --- currency-money/src/main/resources/plugin.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/currency-money/src/main/resources/plugin.yml b/currency-money/src/main/resources/plugin.yml index b5bdfbb..d1c95bb 100644 --- a/currency-money/src/main/resources/plugin.yml +++ b/currency-money/src/main/resources/plugin.yml @@ -4,6 +4,7 @@ version: ${version} main: dev.xhyrom.lighteco.currency.money.bukkit.BukkitMCLoader author: ${author} api-version: 1.20 +load: STARTUP softdepend: - Vault From 58cf77e2b9d8fd459f8d33458c4f8743f6201b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Thu, 5 Oct 2023 22:09:48 +0200 Subject: [PATCH 10/24] fix(currency-money): format double --- .../lighteco/currency/money/bukkit/hooks/vault/Vault.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java index 454cdf2..03888e4 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java @@ -52,7 +52,7 @@ public class Vault extends AbstractEconomy { NumberFormat format = NumberFormat.getInstance(); format.setCurrency(java.util.Currency.getInstance(this.plugin.getConfig().currencyCode)); - return format.format(BigDecimal.valueOf(amount)); + return format.format(amount); } @Override From 4c832f11f11b49234962b9aa9a0e5fc690cb82d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Sat, 7 Oct 2023 12:56:28 +0200 Subject: [PATCH 11/24] fix(bukkit): dont load commandapi twice --- .../dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java index da91c69..a8f76e9 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java @@ -1,7 +1,5 @@ package dev.xhyrom.lighteco.bukkit; -import dev.jorel.commandapi.CommandAPI; -import dev.jorel.commandapi.CommandAPIBukkitConfig; import org.bukkit.plugin.java.JavaPlugin; // Used inside plugin.yml @@ -16,20 +14,15 @@ public class BukkitLightEcoLoader extends JavaPlugin { @Override public void onLoad() { this.bootstrap.onLoad(); - CommandAPI.onLoad(new CommandAPIBukkitConfig(this) - .verboseOutput(this.bootstrap.getPlugin().getConfig().debug) - ); } @Override public void onEnable() { - CommandAPI.onEnable(); this.bootstrap.onEnable(); } @Override public void onDisable() { - CommandAPI.onDisable(); this.bootstrap.onDisable(); } } From ca5086621b17112a11f1e78b2ef00ad98dae8e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Sat, 7 Oct 2023 13:34:03 +0200 Subject: [PATCH 12/24] fix(bukkit): remove max value from commands --- .../lighteco/bukkit/commands/GiveCommand.java | 5 ++--- .../lighteco/bukkit/commands/PayCommand.java | 5 ++--- .../lighteco/bukkit/commands/SetCommand.java | 5 ++--- .../lighteco/bukkit/commands/TakeCommand.java | 5 ++--- .../dev/xhyrom/lighteco/bukkit/util/Util.java | 19 ------------------- 5 files changed, 8 insertions(+), 31 deletions(-) delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java index dbc6ab6..e138c0d 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java @@ -7,7 +7,6 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -28,8 +27,8 @@ public class GiveCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) - : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) ) .executes((sender, args) -> { this.handleGive(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java index 4a1f584..4e07a94 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java @@ -7,7 +7,6 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -28,8 +27,8 @@ public class PayCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) - : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) ) .executesPlayer((sender, args) -> { this.handlePay(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java index ac410f0..c45f9ff 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java @@ -7,7 +7,6 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -28,8 +27,8 @@ public class SetCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 0, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) - : new IntegerArgument("amount", 0, this.manager.plugin.getConfig().maximumBalance.intValue()) + ? new DoubleArgument("amount", 0) + : new IntegerArgument("amount", 0) ) .executes((sender, args) -> { this.handleSet(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java index 015f6f9..3f25263 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java @@ -7,7 +7,6 @@ import dev.jorel.commandapi.arguments.OfflinePlayerArgument; import dev.jorel.commandapi.executors.CommandArguments; import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.bukkit.util.Util; import dev.xhyrom.lighteco.common.model.currency.Currency; import lombok.RequiredArgsConstructor; import org.bukkit.OfflinePlayer; @@ -28,8 +27,8 @@ public class TakeCommand implements Command { .withArguments( new OfflinePlayerArgument("target"), currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1, Util.bigDecimalToDouble(this.manager.plugin.getConfig().maximumBalance)) - : new IntegerArgument("amount", 1, this.manager.plugin.getConfig().maximumBalance.intValue()) + ? new DoubleArgument("amount", 1) + : new IntegerArgument("amount", 1) ) .executes((sender, args) -> { this.handleTake(sender, args, currency); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java deleted file mode 100644 index d4da7cc..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/util/Util.java +++ /dev/null @@ -1,19 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.util; - -import lombok.experimental.UtilityClass; - -import java.math.BigDecimal; - -@UtilityClass -public class Util { - public double bigDecimalToDouble(final BigDecimal value) { - double amount = value.doubleValue(); - - // Don't return bigger balance than user actually has - if (BigDecimal.valueOf(amount).compareTo(value) > 0) { - amount = Math.nextAfter(amount, Double.NEGATIVE_INFINITY); - } - - return amount; - } -} From 9454f2db9026ec99d3545b4a812f599ea41cc6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Thu, 12 Oct 2023 18:12:31 +0200 Subject: [PATCH 13/24] docs: add SECURITY.md --- SECURITY.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..885d902 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,36 @@ +# Security Policy + +## Supported Versions + +LightEco is currently in alpha version, which means it's still not done. As such, it may contain bugs or other issues. We are working hard to improve the plugin and fix any problems that are found, but we encourage users to report any issues they encounter to us so that we can address them as quickly as possible. + +| Version | Supported | +| ------- | ------------------ | +| 0.x.x | :white_check_mark: | + +## Reporting a Vulnerability + +If you find a security vulnerability, please report it to us as soon as possible. We take security very seriously and we appreciate your help in keeping our plugin safe. + +**How to report a vulnerability** + +You can report a vulnerability to us by: + +* **Discord:** Contact me on Discord at [`xhyrom`](https://s.xhyrom.dev/discord-user). +* **Email:** Send an email to `github@xhyrom.dev`. + +**What to expect when reporting a vulnerability** + +We will investigate your report as soon as possible. We will typically provide you with an update within 24 hours. + +If the vulnerability is accepted, we will work to fix it as quickly as possible. We will notify you when the fix is released. + +If the vulnerability is declined, we will provide you with a reason for the decision. + +**What to include in your report** + +When reporting a vulnerability, please include the following information: + +* **A detailed description of the vulnerability.** This should include how the vulnerability can be exploited and what impact it could have. +* **Steps to reproduce the vulnerability.** This will help us to quickly investigate the issue. +* **Any additional information that you think may be helpful.** This could include screenshots, code samples, or other relevant data. From 4d192eefd57fd276b4b2e4645aa6a8cf23ab6883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Thu, 12 Oct 2023 22:20:59 +0200 Subject: [PATCH 14/24] build: bump commandapi --- bukkit/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index cbefc31..7fe7247 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -13,7 +13,7 @@ repositories { dependencies { implementation(project(":lighteco-common")) - implementation("dev.jorel:commandapi-bukkit-shade:9.1.0") + implementation("dev.jorel:commandapi-bukkit-shade:9.2.0") implementation("net.kyori:adventure-platform-bukkit:4.2.0") compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT") From a2be61eb95a91f16ac96396f122473fb82edcee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Mon, 16 Oct 2023 17:47:46 +0200 Subject: [PATCH 15/24] refactor: code cleanup --- .../bukkit/hooks/PlaceholderAPIExpansion.java | 13 ++++++------- common/build.gradle.kts | 2 +- .../lighteco/common/api/impl/ApiUserManager.java | 3 +-- .../provider/memory/MemoryStorageProvider.java | 3 +-- .../storage/provider/sql/SqlStorageProvider.java | 3 +-- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java index a4162ad..b0bbb01 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java @@ -6,31 +6,30 @@ import dev.xhyrom.lighteco.common.model.user.User; import lombok.RequiredArgsConstructor; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; @RequiredArgsConstructor public class PlaceholderAPIExpansion extends PlaceholderExpansion { private final BukkitLightEcoPlugin plugin; @Override - public @NotNull String getIdentifier() { + public @NonNull String getIdentifier() { return "lighteco"; } @Override - public @NotNull String getAuthor() { + public @NonNull String getAuthor() { return this.plugin.getBootstrap().getLoader().getDescription().getAuthors().toString(); } @Override - public @NotNull String getVersion() { + public @NonNull String getVersion() { return this.plugin.getBootstrap().getLoader().getDescription().getVersion(); } @Override - public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) { + public @Nullable String onRequest(OfflinePlayer player, @NonNull String params) { String[] args = params.split("_"); if (args.length < 2) return null; diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 91998ab..049f144 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -4,6 +4,7 @@ plugins { dependencies { api(project(":lighteco-api")) + api("org.checkerframework:checker-qual:3.8.0") api("net.kyori:adventure-api:4.12.0") { exclude(module = "adventure-bom") exclude(module = "checker-qual") @@ -23,6 +24,5 @@ dependencies { compileOnly("org.projectlombok:lombok:1.18.28") annotationProcessor("org.projectlombok:lombok:1.18.28") - compileOnly("org.checkerframework:checker-qual:3.8.0") compileOnly("org.jetbrains:annotations:20.1.0") } \ No newline at end of file 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 89098d2..c65d882 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 @@ -5,7 +5,6 @@ import dev.xhyrom.lighteco.api.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import org.jetbrains.annotations.NotNull; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -37,7 +36,7 @@ public class ApiUserManager extends ApiAbstractManager saveUsers(@NotNull @NonNull User... users) { + public @NonNull CompletableFuture saveUsers(@NonNull User... users) { return this.plugin.getStorage().saveUsers(users); } 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 bdea3e5..0658bf1 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 @@ -5,7 +5,6 @@ import dev.xhyrom.lighteco.api.storage.StorageProvider; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.UUID; @@ -43,7 +42,7 @@ public class MemoryStorageProvider implements StorageProvider { } @Override - public void saveUsers(@NotNull @NonNull User... users) { + public void saveUsers(@NonNull User... users) { for (User user : users) { this.userDatabase.put(user.getUniqueId(), user); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java index afc900d..1240330 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java @@ -8,7 +8,6 @@ import dev.xhyrom.lighteco.common.storage.StorageType; import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import org.jetbrains.annotations.NotNull; import java.math.BigDecimal; import java.sql.Connection; @@ -163,7 +162,7 @@ public class SqlStorageProvider implements StorageProvider { } @Override - public void saveUsers(@NotNull @NonNull User... users) throws Exception { + public void saveUsers(@NonNull User... users) throws Exception { // use transaction try (Connection c = this.connectionFactory.getConnection()) { try { From 572738dab56a6cb0adcf67a6af126c85083f673f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Mon, 30 Oct 2023 12:15:15 +0100 Subject: [PATCH 16/24] build: strip commit hash --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 98173ed..d196431 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,5 +56,5 @@ fun determineCommitHash(project: Project): String { standardOutput = commitHashInfo } - return commitHashInfo.toString() + return commitHashInfo.toString().strip() } \ No newline at end of file From b1d5ae7c0990ab61678e381f809c3d1779977610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 30 Oct 2023 16:08:31 +0100 Subject: [PATCH 17/24] feat(pay): send message to target (#6) * feat(pay): send message for target * fix: set audience on enable --- api/build.gradle.kts | 6 ++++++ .../dev/xhyrom/lighteco/api/model/user/User.java | 9 +++++++++ .../lighteco/bukkit/BukkitLightEcoBootstrap.java | 10 ++++++++++ .../xhyrom/lighteco/common/api/impl/ApiUser.java | 6 ++++++ .../config/message/CurrencyMessageConfig.java | 2 ++ .../manager/command/AbstractCommandManager.java | 15 +++++++++++++++ .../xhyrom/lighteco/common/model/user/User.java | 5 +++++ .../plugin/bootstrap/LightEcoBootstrap.java | 2 ++ .../lighteco/sponge/SpongeLightEcoBootstrap.java | 6 ++++++ 9 files changed, 61 insertions(+) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index 5c0dfcb..23c986e 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -3,6 +3,12 @@ plugins { } dependencies { + api("net.kyori:adventure-api:4.12.0") { + exclude(module = "adventure-bom") + exclude(module = "checker-qual") + exclude(module = "annotations") + } + compileOnly("org.projectlombok:lombok:1.18.28") annotationProcessor("org.projectlombok:lombok:1.18.28") diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java index 715dada..5bf88fe 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.model.user; import dev.xhyrom.lighteco.api.model.currency.Currency; +import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -59,4 +60,12 @@ public interface User { * @throws IllegalArgumentException if the amount is negative */ void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException; + + /** + * Send a message to this user. + * Message will be silently dropped if the user is offline. + * + * @param message the message + */ + void sendMessage(Component message); } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java index eafc476..a94bf72 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java @@ -8,6 +8,8 @@ import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; import lombok.Getter; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -27,6 +29,7 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr private final PluginLogger logger; @Getter private final SchedulerAdapter scheduler; + private BukkitAudiences audience; public BukkitLightEcoBootstrap(JavaPlugin loader) { this.loader = loader; @@ -46,6 +49,8 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr public void onEnable() { CommandAPI.onEnable(); this.plugin.enable(); + + this.audience = BukkitAudiences.create(loader); } @Override @@ -76,4 +81,9 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr public InputStream getResourceStream(String filename) { return this.loader.getResource(filename); } + + @Override + public Audience getPlayerAudience(UUID uniqueId) { + return audience.player(uniqueId); + } } 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 afc0f5c..a3d01d4 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 @@ -2,6 +2,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 net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -60,4 +61,9 @@ public class ApiUser implements User { this.handler.withdraw(internal, amount); } + + @Override + public void sendMessage(Component message) { + this.handler.sendMessage(message); + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java index 7d6d645..00380ce 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -14,6 +14,8 @@ public class CurrencyMessageConfig extends OkaeriConfig { public String pay = " | Paid to "; public String payWithTax = " | Paid to ( after tax)"; + public String payReceived = " | Received from "; + public String payReceivedWithTax = " | Received from ( after tax)"; public String wait = "Please wait a moment before using this command again."; public String notEnoughMoney = "You don't have enough money!"; 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 5853b0a..dec658f 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 @@ -242,6 +242,9 @@ public abstract class AbstractCommandManager implements CommandManager { ? this.getConfig(currency).payWithTax : this.getConfig(currency).pay; + String templateReceived = tax.compareTo(BigDecimal.ZERO) > 0 + ? this.getConfig(currency).payReceivedWithTax + : this.getConfig(currency).payReceived; sender.sendMessage( miniMessage.deserialize( @@ -255,6 +258,18 @@ public abstract class AbstractCommandManager implements CommandManager { ) ); + target.sendMessage( + miniMessage.deserialize( + templateReceived, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("sender", user.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()), + Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), + Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), + Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) + ) + ); + removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); } } 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 4280c2d..83069dc 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 @@ -8,6 +8,7 @@ import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; import lombok.Setter; +import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; @@ -96,4 +97,8 @@ public class User { this.setBalance(currency, this.getBalance(currency).subtract(amount)); } + + public void sendMessage(@NonNull Component message) { + this.plugin.getBootstrap().getPlayerAudience(this.getUniqueId()).sendMessage(message); + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java index ac16413..07b053a 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.plugin.bootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; +import net.kyori.adventure.audience.Audience; import java.io.InputStream; import java.nio.file.Path; @@ -16,4 +17,5 @@ public interface LightEcoBootstrap { boolean isPlayerOnline(UUID uniqueId); List getOnlinePlayers(); InputStream getResourceStream(String filename); + Audience getPlayerAudience(UUID uniqueId); } diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java index 469ab9f..ab281f0 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java @@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; +import net.kyori.adventure.audience.Audience; import org.apache.logging.log4j.Logger; import org.spongepowered.plugin.builtin.jvm.Plugin; @@ -73,4 +74,9 @@ public class SpongeLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr public InputStream getResourceStream(String filename) { return null; } + + @Override + public Audience getPlayerAudience(UUID uniqueId) { + return null; + } } From 35f94eac3b3cc381b5132a09841c1d6280a7a322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Sun, 24 Dec 2023 15:46:13 +0100 Subject: [PATCH 18/24] feat: messaging (#7) * feat: messaging * fix: some things * feat: make it actually working * feat: disable messaging by default * fix: avoid infinity loop * feat: multiple channels * remove todo message * fix: global channel should be lighteco:messages * fix: check if CHANNELS not null * refactor: set channels in constructor * fix: push user updates on quit when user is dirty --- .../dev/xhyrom/lighteco/api/LightEco.java | 10 ++ .../api/messaging/MessagingService.java | 8 + .../messenger/IncomingMessageConsumer.java | 10 ++ .../lighteco/api/messenger/Messenger.java | 12 ++ .../api/messenger/MessengerProvider.java | 7 + .../api/messenger/message/Message.java | 12 ++ .../messenger/message/OutgoingMessage.java | 15 ++ .../message/type/UserUpdateMessage.java | 23 +++ .../lighteco/bukkit/BukkitLightEcoPlugin.java | 6 + .../listeners/BukkitConnectionListener.java | 13 +- common/build.gradle.kts | 1 + .../lighteco/common/api/LightEcoApi.java | 8 + .../common/api/impl/ApiAbstractManager.java | 6 +- .../common/api/impl/ApiCommandManager.java | 8 +- .../common/api/impl/ApiCurrencyManager.java | 14 +- .../common/api/impl/ApiMessagingService.java | 23 +++ .../lighteco/common/api/impl/ApiUser.java | 36 +++-- .../common/api/impl/ApiUserManager.java | 14 +- .../xhyrom/lighteco/common/config/Config.java | 4 + .../config/messaging/MessagingConfig.java | 15 ++ .../config/messaging/MessagingDataConfig.java | 16 ++ .../common/dependencies/Dependency.java | 23 +++ .../dependencies/DependencyManager.java | 2 + .../dependencies/DependencyManagerImpl.java | 49 +++--- .../dependencies/DependencyRegistry.java | 15 ++ .../messaging/InternalMessagingService.java | 13 ++ .../messaging/LightEcoMessagingService.java | 145 ++++++++++++++++++ .../common/messaging/MessagingFactory.java | 38 +++++ .../common/messaging/MessagingType.java | 6 + .../messaging/message/AbstractMessage.java | 20 +++ .../common/messaging/message/MessageType.java | 5 + .../message/UserUpdateMessageImpl.java | 49 ++++++ .../messaging/type/redis/RedisMessenger.java | 94 ++++++++++++ .../type/redis/RedisMessengerProvider.java | 33 ++++ .../lighteco/common/model/user/User.java | 13 +- .../common/plugin/AbstractLightEcoPlugin.java | 28 +++- .../common/plugin/LightEcoPlugin.java | 5 + .../common/storage/StorageFactory.java | 3 +- .../common/util/gson/GsonProvider.java | 12 ++ .../lighteco/sponge/SpongeLightEcoPlugin.java | 6 + 40 files changed, 743 insertions(+), 77 deletions(-) create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messaging/MessagingService.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/MessengerProvider.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/Message.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/OutgoingMessage.java create mode 100644 api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiMessagingService.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingConfig.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingDataConfig.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/InternalMessagingService.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingFactory.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingType.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/MessageType.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/util/gson/GsonProvider.java diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java index 451c6e7..7b880b7 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java @@ -3,10 +3,13 @@ package dev.xhyrom.lighteco.api; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.manager.UserManager; +import dev.xhyrom.lighteco.api.messaging.MessagingService; import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.PlayerAdapter; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Optional; + public interface LightEco { /** * Gets the {@link Platform}, which represents the current platform the @@ -37,6 +40,13 @@ public interface LightEco { */ @NonNull CommandManager getCommandManager(); + /** + * Gets the {@link MessagingService}, which manages the messaging. + * + * @return the messaging service + */ + @NonNull Optional getMessagingService(); + /** * Gets the {@link PlayerAdapter} for a player class. * diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messaging/MessagingService.java b/api/src/main/java/dev/xhyrom/lighteco/api/messaging/MessagingService.java new file mode 100644 index 0000000..72cfdc1 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messaging/MessagingService.java @@ -0,0 +1,8 @@ +package dev.xhyrom.lighteco.api.messaging; + +import dev.xhyrom.lighteco.api.model.currency.Currency; +import dev.xhyrom.lighteco.api.model.user.User; + +public interface MessagingService { + void pushUserUpdate(User user, Currency currency); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java new file mode 100644 index 0000000..ff83582 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java @@ -0,0 +1,10 @@ +package dev.xhyrom.lighteco.api.messenger; + +import dev.xhyrom.lighteco.api.messenger.message.Message; +import org.checkerframework.checker.nullness.qual.NonNull; + +public interface IncomingMessageConsumer { + void consumeIncomingMessage(@NonNull Message message); + + void consumeRawIncomingMessage(@NonNull String message); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java new file mode 100644 index 0000000..ea6d7e3 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java @@ -0,0 +1,12 @@ +package dev.xhyrom.lighteco.api.messenger; + +import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; +import org.checkerframework.checker.nullness.qual.NonNull; + +public interface Messenger extends AutoCloseable { + void sendOutgoingMessage(@NonNull OutgoingMessage message, boolean global); + + @Override + default void close() { + } +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/MessengerProvider.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/MessengerProvider.java new file mode 100644 index 0000000..86cee89 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/MessengerProvider.java @@ -0,0 +1,7 @@ +package dev.xhyrom.lighteco.api.messenger; + +import org.checkerframework.checker.nullness.qual.NonNull; + +public interface MessengerProvider { + @NonNull Messenger obtain(@NonNull IncomingMessageConsumer consumer); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/Message.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/Message.java new file mode 100644 index 0000000..a1b9d67 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/Message.java @@ -0,0 +1,12 @@ +package dev.xhyrom.lighteco.api.messenger.message; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.UUID; + +/** + * Represents a message that can be received by a {@link dev.xhyrom.lighteco.api.messenger.Messenger}. + */ +public interface Message { + @NonNull UUID getId(); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/OutgoingMessage.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/OutgoingMessage.java new file mode 100644 index 0000000..bd7007e --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/OutgoingMessage.java @@ -0,0 +1,15 @@ +package dev.xhyrom.lighteco.api.messenger.message; + +import org.checkerframework.checker.nullness.qual.NonNull; + +/** + * Represents a message that can be sent via a {@link dev.xhyrom.lighteco.api.messenger.Messenger}. + */ +public interface OutgoingMessage extends Message { + /** + * Serializes message into a string. + * + * @return serialized message + */ + @NonNull String serialize(); +} diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java new file mode 100644 index 0000000..7d6b2e8 --- /dev/null +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java @@ -0,0 +1,23 @@ +package dev.xhyrom.lighteco.api.messenger.message.type; + +import dev.xhyrom.lighteco.api.messenger.message.Message; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.util.UUID; + +/** + * Represents a message that is sent when a user updates their profile. + */ +public interface UserUpdateMessage extends Message { + /** + * Gets the unique id of the user that updated their profile. + * + * @return the user's unique id + */ + @NonNull UUID getUserUniqueId(); + + @NonNull String getCurrencyIdentifier(); + + @NonNull BigDecimal getNewBalance(); +} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java index 63b5ce3..bca9b1f 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java @@ -8,6 +8,7 @@ import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener; import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; import dev.xhyrom.lighteco.bukkit.manager.BukkitContextManager; import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; +import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; import lombok.Getter; @@ -45,6 +46,11 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { this.contextManager = new BukkitContextManager(); } + @Override + protected MessagingFactory getMessagingFactory() { + return new MessagingFactory(this); + } + @Override protected void registerApiOnPlatform(LightEco api) { this.getBootstrap().getLoader().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap().getLoader(), ServicePriority.Normal); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java index c8176b2..2b79005 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java @@ -1,11 +1,11 @@ package dev.xhyrom.lighteco.bukkit.listeners; import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin; +import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -58,11 +58,10 @@ public class BukkitConnectionListener implements Listener { } this.plugin.getUserManager().saveUser(user) - .thenAccept(v -> { - // make sure the player is offline before unloading - if (Bukkit.getPlayer(uniqueId) != null) return; - - this.plugin.getUserManager().unload(uniqueId); - }); + .thenAccept(v -> this.plugin.getMessagingService().ifPresent(service -> { + for (Currency currency : this.plugin.getCurrencyManager().getRegisteredCurrencies()) { + service.pushUserUpdate(user, currency); + } + })); } } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 049f144..a2595f9 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -20,6 +20,7 @@ dependencies { implementation("eu.okaeri:okaeri-configs-validator-okaeri:5.0.0-beta.5") compileOnly("com.zaxxer:HikariCP:5.0.1") + compileOnly("redis.clients:jedis:5.1.0") compileOnly("org.projectlombok:lombok:1.18.28") annotationProcessor("org.projectlombok:lombok:1.18.28") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java index dbf396b..554241c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java @@ -4,12 +4,15 @@ import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.manager.UserManager; +import dev.xhyrom.lighteco.api.messaging.MessagingService; import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.PlayerAdapter; import dev.xhyrom.lighteco.common.api.impl.*; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Optional; + public class LightEcoApi implements LightEco { private final LightEcoPlugin plugin; @@ -49,6 +52,11 @@ public class LightEcoApi implements LightEco { return this.commandManager; } + @Override + public @NonNull Optional getMessagingService() { + return this.plugin.getMessagingService().map(ApiMessagingService::new); + } + @Override public @NonNull PlayerAdapter getPlayerAdapter(@NonNull Class playerClass) { Class expected = this.plugin.getContextManager().getPlayerClass(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java index 026b2a7..8c9e118 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiAbstractManager.java @@ -4,10 +4,10 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; public abstract class ApiAbstractManager { protected final LightEcoPlugin plugin; - protected final H handler; + protected final H handle; - protected ApiAbstractManager(LightEcoPlugin plugin, H handler) { + protected ApiAbstractManager(LightEcoPlugin plugin, H handle) { this.plugin = plugin; - this.handler = handler; + this.handle = handle; } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java index 0954065..bab2ecd 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java @@ -6,8 +6,8 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import org.checkerframework.checker.nullness.qual.NonNull; public class ApiCommandManager extends ApiAbstractManager implements CommandManager { - public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.command.CommandManager handler) { - super(plugin, handler); + public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.command.CommandManager handle) { + super(plugin, handle); } @Override @@ -15,7 +15,7 @@ public class ApiCommandManager extends ApiAbstractManager implements CurrencyManager { - public ApiCurrencyManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.currency.CurrencyManager handler) { - super(plugin, handler); + public ApiCurrencyManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.currency.CurrencyManager handle) { + super(plugin, handle); } - private Currency wrap(dev.xhyrom.lighteco.common.model.currency.Currency handler) { - return handler.getProxy(); + private Currency wrap(dev.xhyrom.lighteco.common.model.currency.Currency handle) { + return handle.getProxy(); } @Override public @NonNull Collection getRegisteredCurrencies() { - return this.handler.values() + return this.handle.values() .stream().map(this::wrap) .toList(); } @Override public Currency getCurrency(@NonNull String identifier) { - return wrap(this.handler.getIfLoaded(identifier)); + return wrap(this.handle.getIfLoaded(identifier)); } @Override public void registerCurrency(@NonNull Currency currency) { dev.xhyrom.lighteco.common.model.currency.Currency internal = new dev.xhyrom.lighteco.common.model.currency.Currency(currency); - this.handler.registerCurrency(internal); + this.handle.registerCurrency(internal); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiMessagingService.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiMessagingService.java new file mode 100644 index 0000000..2944469 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiMessagingService.java @@ -0,0 +1,23 @@ +package dev.xhyrom.lighteco.common.api.impl; + +import dev.xhyrom.lighteco.api.messaging.MessagingService; +import dev.xhyrom.lighteco.api.model.currency.Currency; +import dev.xhyrom.lighteco.api.model.user.User; +import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; + +public class ApiMessagingService implements MessagingService { + private final InternalMessagingService handle; + + public ApiMessagingService(InternalMessagingService handle) { + this.handle = handle; + } + + @Override + public void pushUserUpdate(User user, Currency currency) { + dev.xhyrom.lighteco.common.model.currency.Currency internalCurrency = this.handle.getPlugin() + .getCurrencyManager() + .getIfLoaded(currency.getIdentifier()); + + this.handle.pushUserUpdate(ApiUser.cast(user), internalCurrency); + } +} 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 a3d01d4..454cd5c 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 @@ -10,60 +10,68 @@ import java.math.BigDecimal; import java.util.UUID; public class ApiUser implements User { - private final dev.xhyrom.lighteco.common.model.user.User handler; + public static dev.xhyrom.lighteco.common.model.user.User cast(User u) { + if (u instanceof ApiUser) { + return ((ApiUser) u).handle; + } - public ApiUser(dev.xhyrom.lighteco.common.model.user.User handler) { - this.handler = handler; + throw new IllegalArgumentException("Cannot cast " + u.getClass().getName() + " to " + ApiUser.class.getName()); + } + + private final dev.xhyrom.lighteco.common.model.user.User handle; + + public ApiUser(dev.xhyrom.lighteco.common.model.user.User handle) { + this.handle = handle; } @Override public @NonNull UUID getUniqueId() { - return this.handler.getUniqueId(); + return this.handle.getUniqueId(); } @Override public @Nullable String getUsername() { - return this.handler.getUsername(); + return this.handle.getUsername(); } @Override public @NonNull BigDecimal getBalance(@NonNull Currency currency) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handle.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); - return this.handler.getBalance(internal); + return this.handle.getBalance(internal); } @Override public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handle.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); - this.handler.setBalance(internal, balance); + this.handle.setBalance(internal, balance); } @Override public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handle.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); - this.handler.deposit(internal, amount); + this.handle.deposit(internal, amount); } @Override public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handler.getPlugin() + dev.xhyrom.lighteco.common.model.currency.Currency internal = this.handle.getPlugin() .getCurrencyManager() .getIfLoaded(currency.getIdentifier()); - this.handler.withdraw(internal, amount); + this.handle.withdraw(internal, amount); } @Override public void sendMessage(Component message) { - this.handler.sendMessage(message); + this.handle.sendMessage(message); } } 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 c65d882..cb93789 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 @@ -10,13 +10,13 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; public class ApiUserManager extends ApiAbstractManager implements UserManager { - public ApiUserManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.user.UserManager handler) { - super(plugin, handler); + public ApiUserManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.user.UserManager handle) { + super(plugin, handle); } - private User wrap(dev.xhyrom.lighteco.common.model.user.User handler) { - this.plugin.getUserManager().getHousekeeper().registerUsage(handler.getUniqueId()); - return handler.getProxy(); + private User wrap(dev.xhyrom.lighteco.common.model.user.User handle) { + this.plugin.getUserManager().getHousekeeper().registerUsage(handle.getUniqueId()); + return handle.getProxy(); } @Override @@ -42,11 +42,11 @@ public class ApiUserManager extends ApiAbstractManager dependencies); void loadStorageDependencies(Set types); + void loadMessagingDependencies(Set types); ClassLoader obtainClassLoaderWith(Set dependencies); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java index b2bfc39..060837d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java @@ -5,10 +5,11 @@ import com.google.common.io.MoreFiles; import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.relocation.Relocation; import dev.xhyrom.lighteco.common.dependencies.relocation.RelocationHandler; +import dev.xhyrom.lighteco.common.messaging.MessagingType; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; -import dev.xhyrom.lighteco.common.util.URLClassLoaderAccess; import dev.xhyrom.lighteco.common.storage.StorageType; +import dev.xhyrom.lighteco.common.util.URLClassLoaderAccess; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import java.io.IOException; @@ -17,8 +18,6 @@ import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CountDownLatch; public class DependencyManagerImpl implements DependencyManager { private final EnumMap loaded = new EnumMap<>(Dependency.class); @@ -49,42 +48,29 @@ public class DependencyManagerImpl implements DependencyManager { @Override public void loadDependencies(Set dependencies) { - CountDownLatch latch = new CountDownLatch(dependencies.size()); - if (this.config.debug) this.logger.info("Loading dependencies: " + dependencies); for (Dependency dependency : dependencies) { if (this.loaded.containsKey(dependency)) { - latch.countDown(); continue; } - CompletableFuture.runAsync(() -> { - if (this.config.debug) - this.logger.info("Loading dependency " + dependency); - - try { - loadDependency(dependency); - } catch (Exception e) { - throw new RuntimeException("Failed to load dependency " + dependency, e); - } finally { - latch.countDown(); - - if (this.config.debug) - this.logger.info("Loaded dependency " + dependency); - } - }); - } - - try { - latch.await(); - if (this.config.debug) - this.logger.info("Loaded dependencies: " + dependencies); - } catch (InterruptedException e) { - throw new RuntimeException(e); + this.logger.info("Loading dependency " + dependency); + + try { + loadDependency(dependency); + } catch (Exception e) { + throw new RuntimeException("Failed to load dependency " + dependency, e); + } finally { + if (this.config.debug) + this.logger.info("Loaded dependency " + dependency); + } } + + if (this.config.debug) + this.logger.info("Loaded dependencies: " + dependencies); } private void loadDependency(Dependency dependency) throws Exception { @@ -142,6 +128,11 @@ public class DependencyManagerImpl implements DependencyManager { loadDependencies(this.registry.resolveStorageDependencies(types)); } + @Override + public void loadMessagingDependencies(Set types) { + loadDependencies(this.registry.resolveMessagingDependencies(types)); + } + @Override public ClassLoader obtainClassLoaderWith(Set dependencies) { ImmutableSet set = ImmutableSet.copyOf(dependencies); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java index b948bcb..88a0587 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.dependencies; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.SetMultimap; +import dev.xhyrom.lighteco.common.messaging.MessagingType; import dev.xhyrom.lighteco.common.storage.StorageType; import java.util.LinkedHashSet; @@ -16,6 +17,10 @@ public class DependencyRegistry { .putAll(StorageType.POSTGRESQL, Dependency.POSTGRESQL_DRIVER, Dependency.HIKARI) .build(); + private static final SetMultimap MESSAGING_DEPENDENCIES = ImmutableSetMultimap.builder() + .putAll(MessagingType.REDIS, Dependency.COMMONS_POOL_2, Dependency.JEDIS, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE) + .build(); + public Set resolveStorageDependencies(Set types) { Set dependencies = new LinkedHashSet<>(); @@ -26,6 +31,16 @@ public class DependencyRegistry { return dependencies; } + public Set resolveMessagingDependencies(Set types) { + Set dependencies = new LinkedHashSet<>(); + + for (MessagingType type : types) { + dependencies.addAll(MESSAGING_DEPENDENCIES.get(type)); + } + + return dependencies; + } + public boolean shouldAutoLoad(Dependency dependency) { return switch (dependency) { case H2_DRIVER, SQLITE_DRIVER -> false; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/InternalMessagingService.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/InternalMessagingService.java new file mode 100644 index 0000000..a778455 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/InternalMessagingService.java @@ -0,0 +1,13 @@ +package dev.xhyrom.lighteco.common.messaging; + +import dev.xhyrom.lighteco.common.model.currency.Currency; +import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +public interface InternalMessagingService { + LightEcoPlugin getPlugin(); + + void pushUserUpdate(User user, Currency currency); + + void shutdown(); +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java new file mode 100644 index 0000000..745a38d --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java @@ -0,0 +1,145 @@ +package dev.xhyrom.lighteco.common.messaging; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; +import dev.xhyrom.lighteco.api.messenger.Messenger; +import dev.xhyrom.lighteco.api.messenger.MessengerProvider; +import dev.xhyrom.lighteco.api.messenger.message.Message; +import dev.xhyrom.lighteco.api.messenger.message.type.UserUpdateMessage; +import dev.xhyrom.lighteco.common.cache.ExpiringSet; +import dev.xhyrom.lighteco.common.messaging.message.MessageType; +import dev.xhyrom.lighteco.common.messaging.message.UserUpdateMessageImpl; +import dev.xhyrom.lighteco.common.model.currency.Currency; +import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.common.util.gson.GsonProvider; +import lombok.Getter; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +public class LightEcoMessagingService implements InternalMessagingService, IncomingMessageConsumer { + @Getter + private final LightEcoPlugin plugin; + private final ExpiringSet receivedMessages; + + private final Messenger messenger; + private final MessengerProvider provider; + + public LightEcoMessagingService(LightEcoPlugin plugin, MessengerProvider provider) { + this.plugin = plugin; + + this.provider = provider; + this.messenger = provider.obtain(this); + this.receivedMessages = new ExpiringSet<>(30, TimeUnit.MINUTES); + } + + private UUID generateMessageId() { + UUID id = UUID.randomUUID(); + + this.receivedMessages.add(id); + return id; + } + + @Override + public void pushUserUpdate(User user, Currency currency) { + this.plugin.getBootstrap().getScheduler().async().execute(() -> + this.messenger.sendOutgoingMessage( + new UserUpdateMessageImpl(generateMessageId(), user.getUniqueId(), currency.getIdentifier(), user.getBalance(currency)), + currency.getType() == dev.xhyrom.lighteco.api.model.currency.Currency.Type.GLOBAL + ) + ); + } + + public static @NonNull String serialize(MessageType type, UUID id, JsonElement content) { + JsonObject data = new JsonObject(); + + data.add("i", new JsonPrimitive(id.toString())); + data.add("t", new JsonPrimitive(type.name())); + data.add("c", content); + + return GsonProvider.get().toJson(data); + } + + @Override + public void consumeIncomingMessage(@NonNull Message message) { + if (!this.receivedMessages.add(message.getId())) { + return; + } + + this.processIncomingMessage(message); + } + + @Override + public void consumeRawIncomingMessage(@NonNull String message) { + try { + deserializeAndConsumeRawIncomingMessage(message); + } catch (Exception e) { + this.plugin.getBootstrap().getLogger().warn("Failed to deserialize incoming message: " + message, e); + } + } + + private void deserializeAndConsumeRawIncomingMessage(@NonNull String message) { + JsonObject parsed = GsonProvider.get().fromJson(message, JsonObject.class); + + JsonElement idElement = parsed.get("i"); + if (idElement == null) { + throw new IllegalStateException("Missing message id: " + message); + } + + UUID id = UUID.fromString(idElement.getAsString()); + + if (!this.receivedMessages.add(id)) { + return; + } + + JsonElement typeElement = parsed.get("t"); + if (typeElement == null) { + throw new IllegalStateException("Missing message type: " + message); + } + + MessageType type = MessageType.valueOf(typeElement.getAsString()); + + @Nullable JsonElement contentElement = parsed.get("c"); + + Message deserialized; + switch (type) { + case USER_UPDATE: + deserialized = UserUpdateMessageImpl.deserialize(id, contentElement); + break; + default: + return; + } + + this.processIncomingMessage(deserialized); + } + + private void processIncomingMessage(Message message) { + if (message instanceof UserUpdateMessage userUpdateMessage) { + this.plugin.getBootstrap().getScheduler().async().execute(() -> { + User user = this.plugin.getUserManager().getIfLoaded(userUpdateMessage.getUserUniqueId()); + if (user == null) { + return; + } + + Currency currency = this.plugin.getCurrencyManager().getIfLoaded(userUpdateMessage.getCurrencyIdentifier()); + if (currency == null) { + return; + } + + user.setBalance(currency, userUpdateMessage.getNewBalance(), false, false); + }); + } else { + throw new IllegalStateException("Unknown message type: " + message.getClass().getName()); + } + } + + @Override + public void shutdown() { + this.messenger.close(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingFactory.java new file mode 100644 index 0000000..155d600 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingFactory.java @@ -0,0 +1,38 @@ +package dev.xhyrom.lighteco.common.messaging; + +import dev.xhyrom.lighteco.api.messenger.MessengerProvider; +import dev.xhyrom.lighteco.common.messaging.type.redis.RedisMessengerProvider; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +import java.util.Set; + +public class MessagingFactory { + private final LightEcoPlugin plugin; + + public MessagingFactory(LightEcoPlugin plugin) { + this.plugin = plugin; + } + + protected LightEcoPlugin getPlugin() { + return this.plugin; + } + + public Set getRequiredTypes() { + return Set.of(this.plugin.getConfig().messaging.provider); + } + + public InternalMessagingService get() { + MessagingType type = this.plugin.getConfig().messaging.provider; + MessengerProvider provider = this.createProvider(type); + if (provider == null) return null; + + return new LightEcoMessagingService(this.plugin, provider); + } + + private MessengerProvider createProvider(MessagingType type) { + return switch (type) { + case REDIS -> new RedisMessengerProvider(this.plugin); + default -> null; + }; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingType.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingType.java new file mode 100644 index 0000000..7eb1dd9 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/MessagingType.java @@ -0,0 +1,6 @@ +package dev.xhyrom.lighteco.common.messaging; + +public enum MessagingType { + NONE, + REDIS +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java new file mode 100644 index 0000000..e8c9cf2 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java @@ -0,0 +1,20 @@ +package dev.xhyrom.lighteco.common.messaging.message; + +import dev.xhyrom.lighteco.api.messenger.message.Message; +import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.UUID; + +public abstract class AbstractMessage implements Message, OutgoingMessage { + private final UUID id; + + protected AbstractMessage(UUID id) { + this.id = id; + } + + @Override + public @NonNull UUID getId() { + return this.id; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/MessageType.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/MessageType.java new file mode 100644 index 0000000..e25af3c --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/MessageType.java @@ -0,0 +1,5 @@ +package dev.xhyrom.lighteco.common.messaging.message; + +public enum MessageType { + USER_UPDATE; +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java new file mode 100644 index 0000000..1604865 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java @@ -0,0 +1,49 @@ +package dev.xhyrom.lighteco.common.messaging.message; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import dev.xhyrom.lighteco.api.messenger.message.type.UserUpdateMessage; +import dev.xhyrom.lighteco.common.messaging.LightEcoMessagingService; +import lombok.Getter; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.util.UUID; + +public class UserUpdateMessageImpl extends AbstractMessage implements UserUpdateMessage { + private static final MessageType TYPE = MessageType.USER_UPDATE; + + @Getter + private final UUID userUniqueId; + @Getter + private final String currencyIdentifier; + @Getter + private final BigDecimal newBalance; + + public static UserUpdateMessage deserialize(UUID id, @NonNull JsonElement data) { + JsonObject obj = data.getAsJsonObject(); + UUID userUniqueId = UUID.fromString(obj.get("u").getAsString()); + String currencyIdentifier = obj.get("c").getAsString(); + BigDecimal newBalance = obj.get("b").getAsBigDecimal(); + + return new UserUpdateMessageImpl(id, userUniqueId, currencyIdentifier, newBalance); + } + + public UserUpdateMessageImpl(UUID id, UUID userUniqueId, String currencyIdentifier, BigDecimal newBalance) { + super(id); + this.userUniqueId = userUniqueId; + this.currencyIdentifier = currencyIdentifier; + this.newBalance = newBalance; + } + + @Override + public @NonNull String serialize() { + JsonObject data = new JsonObject(); + data.add("u", new JsonPrimitive(this.userUniqueId.toString())); + data.add("c", new JsonPrimitive(this.currencyIdentifier)); + data.add("b", new JsonPrimitive(this.newBalance)); + + return LightEcoMessagingService.serialize(TYPE, getId(), data); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java new file mode 100644 index 0000000..82fe986 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java @@ -0,0 +1,94 @@ +package dev.xhyrom.lighteco.common.messaging.type.redis; + +import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; +import dev.xhyrom.lighteco.api.messenger.Messenger; +import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import lombok.Getter; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import redis.clients.jedis.*; + +public class RedisMessenger implements Messenger { + private static final String CHANNEL = "lighteco:{}:messages"; + @Getter + private final String[] channels; + + private final LightEcoPlugin plugin; + private final IncomingMessageConsumer consumer; + + private UnifiedJedis jedis; + private Subscription sub; + + public RedisMessenger(LightEcoPlugin plugin, IncomingMessageConsumer consumer) { + this.plugin = plugin; + this.consumer = consumer; + + this.channels = new String[] { + CHANNEL.replace("{}:", ""), + CHANNEL.replace("{}", this.plugin.getConfig().server) + }; + } + + public void init(@Nullable String address, @Nullable String username, String password, boolean ssl) { + this.init(new JedisPooled( + parseAddress(address), + jedisConfig(username, password, ssl) + )); + } + + private void init(UnifiedJedis jedis) { + this.jedis = jedis; + this.sub = new Subscription(this); + + this.plugin.getBootstrap().getScheduler().async().execute(() -> { + this.jedis.subscribe(this.sub, this.getChannels()); + }); + } + + private static JedisClientConfig jedisConfig(@Nullable String username, @Nullable String password, boolean ssl) { + return DefaultJedisClientConfig.builder() + .user(username) + .password(password) + .ssl(ssl) + .timeoutMillis(Protocol.DEFAULT_TIMEOUT) + .build(); + } + + private static HostAndPort parseAddress(String address) { + String[] addressSplit = address.split(":"); + String host = addressSplit[0]; + int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT; + + return new HostAndPort(host, port); + } + + @Override + public void sendOutgoingMessage(@NonNull OutgoingMessage message, boolean global) { + this.jedis.publish(global ? getChannels()[0] : getChannels()[1], message.serialize()); + } + + @Override + public void close() { + this.sub.unsubscribe(); + this.jedis.close(); + } + + private static class Subscription extends JedisPubSub { + private final RedisMessenger messenger; + + public Subscription(RedisMessenger messenger) { + this.messenger = messenger; + } + + @Override + public void onMessage(String channel, String message) { + String[] channels = this.messenger.getChannels(); + if (!channel.equals(channels[0]) && !channel.equals(channels[1])) { + return; + } + + this.messenger.consumer.consumeRawIncomingMessage(message); + } + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java new file mode 100644 index 0000000..2257453 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java @@ -0,0 +1,33 @@ +package dev.xhyrom.lighteco.common.messaging.type.redis; + +import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; +import dev.xhyrom.lighteco.api.messenger.Messenger; +import dev.xhyrom.lighteco.api.messenger.MessengerProvider; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import org.checkerframework.checker.nullness.qual.NonNull; + +public class RedisMessengerProvider implements MessengerProvider { + private final LightEcoPlugin plugin; + + public RedisMessengerProvider(LightEcoPlugin plugin) { + this.plugin = plugin; + } + + @Override + public @NonNull Messenger obtain(@NonNull IncomingMessageConsumer consumer) { + RedisMessenger messenger = new RedisMessenger(this.plugin, consumer); + + String address = this.plugin.getConfig().messaging.data.address; + String username = this.plugin.getConfig().messaging.data.username; + String password = this.plugin.getConfig().messaging.data.password; + + if (password.isEmpty()) password = null; + if (username.isEmpty()) username = null; + + boolean ssl = this.plugin.getConfig().messaging.data.ssl; + + messenger.init(address, username, password, ssl); + + return messenger; + } +} 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 83069dc..c3544f3 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 @@ -4,6 +4,7 @@ import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.api.exception.CannotBeNegative; import dev.xhyrom.lighteco.common.api.impl.ApiUser; import dev.xhyrom.lighteco.common.cache.RedisBackedMap; +import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; @@ -14,6 +15,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.HashMap; +import java.util.Optional; import java.util.UUID; @Getter @@ -50,10 +52,14 @@ public class User { } public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) throws CannotBeNegative, CannotBeGreaterThan { - this.setBalance(currency, balance, false); + this.setBalance(currency, balance, false, true); } public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force) throws CannotBeNegative, CannotBeGreaterThan { + this.setBalance(currency, balance, force, true); + } + + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force, boolean publish) throws CannotBeNegative, CannotBeGreaterThan { if (balance.compareTo(BigDecimal.ZERO) < 0) { throw new CannotBeNegative("Balance cannot be negative"); } @@ -67,6 +73,11 @@ public class User { if (!force) this.setDirty(true); + + if (publish) { + @NonNull Optional messagingService = this.plugin.getMessagingService(); + messagingService.ifPresent(internalMessagingService -> internalMessagingService.pushUserUpdate(this, currency)); + } } public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws CannotBeNegative, CannotBeGreaterThan { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index 4908b9c..ab03d48 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -6,13 +6,17 @@ import dev.xhyrom.lighteco.common.api.LightEcoApi; import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.DependencyManager; import dev.xhyrom.lighteco.common.dependencies.DependencyManagerImpl; +import dev.xhyrom.lighteco.common.messaging.MessagingFactory; +import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; import dev.xhyrom.lighteco.common.storage.Storage; import dev.xhyrom.lighteco.common.storage.StorageFactory; import dev.xhyrom.lighteco.common.task.UserSaveTask; import eu.okaeri.configs.ConfigManager; import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; import lombok.Getter; +import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Optional; import java.util.concurrent.TimeUnit; @Getter @@ -23,6 +27,7 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { @Getter private Storage storage; + private InternalMessagingService messagingService; private LightEcoApi api; private UserSaveTask userSaveTask; @@ -41,10 +46,18 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { public final void enable() { // setup storage - StorageFactory factory = new StorageFactory(this); - this.dependencyManager.loadStorageDependencies(factory.getRequiredTypes()); + StorageFactory storageFactory = new StorageFactory(this); + this.dependencyManager.loadStorageDependencies( + storageFactory.getRequiredTypes() + ); - this.storage = factory.get(); + MessagingFactory messagingFactory = this.getMessagingFactory(); + this.dependencyManager.loadMessagingDependencies( + messagingFactory.getRequiredTypes() + ); + + this.storage = storageFactory.get(); + this.messagingService = messagingFactory.get(); // register listeners this.registerListeners(); @@ -74,6 +87,9 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { // shutdown storage this.storage.shutdown(); + if (this.messagingService != null) + this.messagingService.shutdown(); + // close isolated class loaders this.dependencyManager.close(); } @@ -81,8 +97,14 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { protected abstract void registerListeners(); protected abstract void setupManagers(); + protected abstract MessagingFactory getMessagingFactory(); protected abstract void registerApiOnPlatform(LightEco api); protected abstract void registerPlatformHooks(); protected abstract void removePlatformHooks(); + + @Override + public @NonNull Optional getMessagingService() { + return Optional.ofNullable(this.messagingService); + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java index fe96969..0791a64 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java @@ -7,10 +7,13 @@ import dev.xhyrom.lighteco.common.dependencies.DependencyManager; import dev.xhyrom.lighteco.common.manager.command.CommandManager; import dev.xhyrom.lighteco.common.manager.currency.CurrencyManager; import dev.xhyrom.lighteco.common.manager.user.UserManager; +import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.storage.Storage; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Optional; + public interface LightEcoPlugin { Platform.@NonNull Type getPlatformType(); @@ -24,5 +27,7 @@ public interface LightEcoPlugin { @NonNull DependencyManager getDependencyManager(); + @NonNull Optional getMessagingService(); + @NonNull Storage getStorage(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java index fce72c5..9c774e1 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java @@ -1,6 +1,5 @@ package dev.xhyrom.lighteco.common.storage; -import com.google.common.collect.ImmutableSet; import dev.xhyrom.lighteco.api.storage.StorageProvider; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.storage.provider.memory.MemoryStorageProvider; @@ -21,7 +20,7 @@ public class StorageFactory { } public Set getRequiredTypes() { - return ImmutableSet.of(this.plugin.getConfig().storage.provider); + return Set.of(this.plugin.getConfig().storage.provider); } public Storage get() { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/util/gson/GsonProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/util/gson/GsonProvider.java new file mode 100644 index 0000000..be397dd --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/util/gson/GsonProvider.java @@ -0,0 +1,12 @@ +package dev.xhyrom.lighteco.common.util.gson; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public final class GsonProvider { + private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create(); + + public static Gson get() { + return GSON; + } +} diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java index ae1fef7..89646bb 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java @@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.common.manager.command.CommandManager; import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; +import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; import lombok.Getter; import org.checkerframework.checker.nullness.qual.NonNull; @@ -41,6 +42,11 @@ public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin { //this.contextManager = new BukkitContextManager(); } + @Override + protected MessagingFactory getMessagingFactory() { + return new MessagingFactory(this); + } + @Override protected void registerApiOnPlatform(LightEco api) { //this.getBootstrap().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap(), ServicePriority.Normal); From 9c0968ded2625aca36806ac3e206c9890c3b5a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Tue, 9 Jan 2024 21:28:03 +0100 Subject: [PATCH 19/24] build: bump deps --- bukkit/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index 7fe7247..e7d0197 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -13,7 +13,7 @@ repositories { dependencies { implementation(project(":lighteco-common")) - implementation("dev.jorel:commandapi-bukkit-shade:9.2.0") + implementation("dev.jorel:commandapi-bukkit-shade:9.3.0") implementation("net.kyori:adventure-platform-bukkit:4.2.0") compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT") From b7c90ae5c80804d5479cbdd3a443e54726066586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl=20=28xHyroM=29?= Date: Sun, 25 Feb 2024 18:00:28 +0100 Subject: [PATCH 20/24] fix: don't register balance commands as main currency command --- .../lighteco/bukkit/commands/BalanceCommand.java | 2 +- .../bukkit/manager/BukkitCommandManager.java | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java index 3c9e295..1324094 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java @@ -34,7 +34,7 @@ public class BalanceCommand implements Command { }; } - private void handleBalance(CommandSender originalSender, CommandArguments args, Currency currency) { + public void handleBalance(CommandSender originalSender, CommandArguments args, Currency currency) { BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); OfflinePlayer target = (OfflinePlayer) args.get("target"); 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 53d1260..9f10eb9 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 @@ -57,21 +57,17 @@ public class BukkitCommandManager extends AbstractCommandManager { private void registerCommands(@NonNull String name, @NonNull Currency currency) { String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; - // Balance - for (CommandAPICommand cmd : new BalanceCommand( - this, - name, - currency, - permissionBase - ).multipleBuild()) { - cmd.register(); - } + BalanceCommand balanceCommand = new BalanceCommand(this, "balance", currency, permissionBase); CommandAPICommand cmd = new CommandAPICommand(name) .withSubcommand(new SetCommand(this, currency, permissionBase).build()) .withSubcommand(new GiveCommand(this, currency, permissionBase).build()) .withSubcommand(new TakeCommand(this, currency, permissionBase).build()) - .withSubcommands(new BalanceCommand(this, "balance", currency, permissionBase).multipleBuild()); + .withSubcommands(balanceCommand.multipleBuild()) + // We want balance to be the default command + .executesPlayer((sender, args) -> { + balanceCommand.handleBalance(sender, args, currency); + }); if (currency.isPayable()) cmd = cmd.withSubcommand(new PayCommand(this, currency, permissionBase).build()); From 08050e15e5fedeae045d0ca71e8e5248486e6d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Wed, 8 May 2024 18:03:28 +0200 Subject: [PATCH 21/24] build: update grale --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9b17899..daeda4e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Sun Jul 30 15:47:43 CEST 2023 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From 75a1e8274af9cb04f9603f67646d38becd461a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Fri, 14 Jun 2024 22:18:08 +0200 Subject: [PATCH 22/24] build: support 1.21 --- bukkit/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bukkit/build.gradle.kts b/bukkit/build.gradle.kts index e7d0197..93903e5 100644 --- a/bukkit/build.gradle.kts +++ b/bukkit/build.gradle.kts @@ -13,7 +13,7 @@ repositories { dependencies { implementation(project(":lighteco-common")) - implementation("dev.jorel:commandapi-bukkit-shade:9.3.0") + implementation("dev.jorel:commandapi-bukkit-shade:9.5.0") implementation("net.kyori:adventure-platform-bukkit:4.2.0") compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT") From 23e73cb5da050b47275ec74b94a1ad219f6918cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 16 Jul 2024 18:04:37 +0200 Subject: [PATCH 23/24] feat!: better command abstraction (#4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: better command abstraction * feat: OfflineUserArgument type * feat: balance, balance other cmds in common & double, integer arg * feat: command manager, better arguments abstraction * feat: map args * feat: some things * feat(better command abstraction)!: switch to brigadier (#8) * feat: switch to brigadier * some updates * feat: make suggestion provider work * supress warnings * feat: pay & set command * feat: give & take command, better suggestions, permissions * refactor: cleanup * fix: dont register pay if currency is not payable * fix: send commands only if you have perms * make second test currency not payable * feat: use command's description & name * refactor: cleanup, rename bukkit to paper * refactor: cleanup, rename bukkit to paper * feat: add built-in info command * ci: build paper, not bukkit * refactor: better test organization * refactor: better test organization * feat: locks * feat: finish locking * feat: handle missing user * refactor: move methods to CommandHelper * refactor: change minimum api version * feat: console support --- .github/workflows/ci.yml | 3 +- .gitignore | 1 + .../lighteco/api/platform/Platform.java | 4 +- build.gradle.kts | 1 + .../kotlin/lighteco.base-logic.gradle.kts | 8 +- .../bukkit/commands/BalanceCommand.java | 59 ---- .../lighteco/bukkit/commands/Command.java | 13 - .../lighteco/bukkit/commands/GiveCommand.java | 54 ---- .../lighteco/bukkit/commands/PayCommand.java | 57 ---- .../lighteco/bukkit/commands/SetCommand.java | 54 ---- .../lighteco/bukkit/commands/TakeCommand.java | 54 ---- .../bukkit/manager/BukkitCommandManager.java | 77 ----- bukkittest/.gitignore | 44 --- common/build.gradle.kts | 2 + .../common/api/impl/ApiCommandManager.java | 8 +- .../common/command/CommandHelper.java | 55 ++++ .../common/command/CommandManager.java | 130 +++++++++ .../common/command/CommandSource.java | 8 + .../common/command/abstraction/Command.java | 36 +++ .../argument/type/OfflineUserArgument.java | 43 +++ .../exception/LockedUserException.java | 9 + .../type/OfflineUserSuggestionProvider.java | 34 +++ .../common/commands/BalanceCommand.java | 83 ++++++ .../commands/CurrencyParentCommand.java | 62 ++++ .../lighteco/common/commands/GiveCommand.java | 106 +++++++ .../lighteco/common/commands/InfoCommand.java | 36 +++ .../lighteco/common/commands/PayCommand.java | 150 ++++++++++ .../lighteco/common/commands/SetCommand.java | 106 +++++++ .../lighteco/common/commands/TakeCommand.java | 106 +++++++ .../config/message/CurrencyMessageConfig.java | 1 - .../common/config/message/MessageConfig.java | 3 + .../command/AbstractCommandManager.java | 275 ------------------ .../manager/command/CommandManager.java | 23 -- .../common/model/chat/CommandSender.java | 8 +- .../common/plugin/AbstractLightEcoPlugin.java | 3 + .../common/plugin/LightEcoPlugin.java | 2 +- .../plugin/bootstrap/LightEcoBootstrap.java | 5 + .../common/plugin/logger/PluginLogger.java | 3 + .../PaperMCLoader.java} | 8 +- .../{bukkit => paper}/hooks/vault/Vault.java | 2 +- .../hooks/vault/VaultFactory.java | 8 +- currency-money/src/main/resources/plugin.yml | 6 +- {bukkit => paper}/.gitignore | 0 {bukkit => paper}/README.md | 0 {bukkit => paper}/build.gradle.kts | 10 +- .../paper/PaperLightEcoBootstrap.java | 36 ++- .../lighteco/paper/PaperLightEcoLoader.java | 10 +- .../lighteco/paper/PaperLightEcoPlugin.java | 28 +- .../lighteco/paper/PaperSchedulerAdapter.java | 9 +- .../paper/chat/PaperCommandSender.java | 19 +- .../xhyrom/lighteco/paper}/hooks/Hooks.java | 6 +- .../paper}/hooks/PlaceholderAPIExpansion.java | 6 +- .../PaperCommandSuggestionsListener.java | 55 ++++ .../listeners/PaperConnectionListener.java | 10 +- .../lighteco/paper/logger/PaperLogger.java | 23 +- .../paper/manager/PaperCommandManager.java | 64 ++++ .../paper/manager/PaperContextManager.java | 4 +- .../lighteco/paper/util/PaperBrigadier.java | 41 +++ .../paper/util/PaperCommandMapUtil.java | 56 ++++ .../src/main/resources/plugin.yml | 6 +- settings.gradle.kts | 13 +- .../sponge/SpongeLightEcoBootstrap.java | 11 + .../lighteco/sponge/SpongeLightEcoPlugin.java | 2 +- {bukkittest => test/paper}/build.gradle.kts | 13 +- .../lighteco/test/paper}/TestCurrency.java | 2 +- .../lighteco/test/paper}/TestCurrency2.java | 4 +- .../lighteco/test/paper}/TestPlugin.java | 2 +- .../paper}/src/main/resources/plugin.yml | 4 +- 68 files changed, 1367 insertions(+), 817 deletions(-) delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/Command.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java delete mode 100644 bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java delete mode 100644 bukkittest/.gitignore create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java delete mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java delete mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java rename currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/{bukkit/BukkitMCLoader.java => paper/PaperMCLoader.java} (88%) rename currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/{bukkit => paper}/hooks/vault/Vault.java (99%) rename currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/{bukkit => paper}/hooks/vault/VaultFactory.java (76%) rename {bukkit => paper}/.gitignore (100%) rename {bukkit => paper}/README.md (100%) rename {bukkit => paper}/build.gradle.kts (81%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java => paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java (67%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java => paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoLoader.java (60%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java => paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java (64%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitSchedulerAdapter.java => paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java (87%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java => paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java (56%) rename {bukkit/src/main/java/dev/xhyrom/lighteco/bukkit => paper/src/main/java/dev/xhyrom/lighteco/paper}/hooks/Hooks.java (77%) rename {bukkit/src/main/java/dev/xhyrom/lighteco/bukkit => paper/src/main/java/dev/xhyrom/lighteco/paper}/hooks/PlaceholderAPIExpansion.java (91%) create mode 100644 paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java => paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java (89%) rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java => paper/src/main/java/dev/xhyrom/lighteco/paper/logger/PaperLogger.java (72%) create mode 100644 paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java rename bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitContextManager.java => paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java (77%) create mode 100644 paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java create mode 100644 paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java rename {bukkit => paper}/src/main/resources/plugin.yml (55%) rename {bukkittest => test/paper}/build.gradle.kts (61%) rename {bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest => test/paper/src/main/java/dev/xhyrom/lighteco/test/paper}/TestCurrency.java (92%) rename {bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest => test/paper/src/main/java/dev/xhyrom/lighteco/test/paper}/TestCurrency2.java (92%) rename {bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest => test/paper/src/main/java/dev/xhyrom/lighteco/test/paper}/TestPlugin.java (96%) rename {bukkittest => test/paper}/src/main/resources/plugin.yml (64%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eeb29cd..37ad61d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,8 +12,7 @@ jobs: distribution: 'adopt' - name: Build with Gradle run: | - ./gradlew shadowJar -p bukkittest - ./gradlew shadowJar -p bukkit + ./gradlew shadowJar -p paper ./gradlew shadowJar -p currency-money - uses: actions/upload-artifact@v3 with: diff --git a/.gitignore b/.gitignore index 1285e0e..2698b5a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ ### ForgeGradle ### # Minecraft client/server files run/ +runServer/ ### Intellij ### # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java b/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java index 0ede153..ed89fdf 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/platform/Platform.java @@ -11,7 +11,7 @@ public interface Platform { @NonNull Type getType(); enum Type { - BUKKIT("Bukkit"), + PAPER("Paper"), SPONGE("Sponge"), VELOCITY("Velocity"), BUNGEECORD("BungeeCord"); @@ -27,7 +27,7 @@ public interface Platform { } public boolean isLocal() { - return this == BUKKIT || this == SPONGE; + return this == PAPER || this == SPONGE; } public boolean isProxy() { diff --git a/build.gradle.kts b/build.gradle.kts index d196431..d46ab81 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,6 +28,7 @@ subprojects { repositories { mavenCentral() maven("https://storehouse.okaeri.eu/repository/maven-public/") + maven("https://libraries.minecraft.net") } } diff --git a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts index 3a09e7d..d50789b 100644 --- a/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts +++ b/buildSrc/src/main/kotlin/lighteco.base-logic.gradle.kts @@ -4,7 +4,12 @@ plugins { } java { - // toolchain.languageVersion.set(JavaLanguageVersion.of(17)) + targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_17 + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } + withSourcesJar() } @@ -13,6 +18,7 @@ tasks { filesMatching(listOf("plugin.yml")) { expand( "name" to project.name, + "coreName" to "LightEco", "version" to project.version, "description" to project.description, "author" to "xHyroM" diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java deleted file mode 100644 index 1324094..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/BalanceCommand.java +++ /dev/null @@ -1,59 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -import dev.jorel.commandapi.CommandAPICommand; -import dev.jorel.commandapi.arguments.OfflinePlayerArgument; -import dev.jorel.commandapi.executors.CommandArguments; -import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender; -import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import lombok.RequiredArgsConstructor; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; - -@RequiredArgsConstructor -public class BalanceCommand implements Command { - private final BukkitCommandManager manager; - private final String name; - private final Currency currency; - private final String permissionBase; - - @Override - public CommandAPICommand[] multipleBuild() { - return new CommandAPICommand[]{ - 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); - }) - }; - } - - public void handleBalance(CommandSender originalSender, CommandArguments args, Currency currency) { - BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); - OfflinePlayer target = (OfflinePlayer) args.get("target"); - - if (target == null) { - this.manager.onBalance(sender, currency); - - return; - } - - this.manager.plugin.getUserManager().loadUser(target.getUniqueId()) - .thenAccept(result -> { - String username = result.getUsername() == null ? - target.getName() != null - ? target.getName() - : args.getRaw("target") - : result.getUsername(); - result.setUsername(username); - - this.manager.onBalance(sender, currency, result); - }); - } -} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/Command.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/Command.java deleted file mode 100644 index 30f56b6..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/Command.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -import dev.jorel.commandapi.CommandAPICommand; - -public interface Command { - default CommandAPICommand build() { - return null; - } - - default CommandAPICommand[] multipleBuild() { - return new CommandAPICommand[0]; - } -} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java deleted file mode 100644 index e138c0d..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/GiveCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -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.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import lombok.RequiredArgsConstructor; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; - -import java.math.BigDecimal; - -@RequiredArgsConstructor -public class GiveCommand implements Command { - private final BukkitCommandManager manager; - private final Currency currency; - private final String permissionBase; - - @Override - public CommandAPICommand build() { - return new CommandAPICommand("give") - .withPermission(permissionBase + "give") - .withArguments( - new OfflinePlayerArgument("target"), - currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) - ) - .executes((sender, args) -> { - this.handleGive(sender, args, currency); - }); - } - - - private void handleGive(CommandSender originalSender, CommandArguments args, Currency currency) { - BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); - OfflinePlayer target = (OfflinePlayer) args.get("target"); - BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); - - if (!this.manager.canUse(sender, currency)) return; - - this.manager.plugin.getUserManager().loadUser(target.getUniqueId()) - .thenAccept(result -> { - String name = target.getName() != null ? target.getName() : args.getRaw("target"); - result.setUsername(name); - - this.manager.onGive(sender, currency, result, amount); - }); - } -} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java deleted file mode 100644 index 4e07a94..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/PayCommand.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -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.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import lombok.RequiredArgsConstructor; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; - -import java.math.BigDecimal; - -@RequiredArgsConstructor -public class PayCommand implements Command { - private final BukkitCommandManager manager; - private final Currency currency; - private final String permissionBase; - - @Override - public CommandAPICommand build() { - return new CommandAPICommand("pay") - .withPermission(permissionBase + "pay") - .withArguments( - new OfflinePlayerArgument("target"), - currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) - ) - .executesPlayer((sender, args) -> { - this.handlePay(sender, args, currency); - }); - } - - private void handlePay(CommandSender originalSender, CommandArguments args, Currency currency) { - BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); - OfflinePlayer target = (OfflinePlayer) args.get("target"); - BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); - - if (!this.manager.canUse(sender, currency)) return; - - this.manager.plugin.getUserManager().loadUser(target.getUniqueId()) - .thenAccept(result -> { - String username = result.getUsername() == null ? - target.getName() != null - ? target.getName() - : args.getRaw("target") - : result.getUsername(); - result.setUsername(username); - - this.manager.onPay(sender, currency, result, amount); - }); - } -} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java deleted file mode 100644 index c45f9ff..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/SetCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -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.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import lombok.RequiredArgsConstructor; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; - -import java.math.BigDecimal; - -@RequiredArgsConstructor -public class SetCommand implements Command { - private final BukkitCommandManager manager; - private final Currency currency; - private final String permissionBase; - - @Override - public CommandAPICommand build() { - return new CommandAPICommand("set") - .withPermission(permissionBase + "set") - .withArguments( - new OfflinePlayerArgument("target"), - currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 0) - : new IntegerArgument("amount", 0) - ) - .executes((sender, args) -> { - this.handleSet(sender, args, currency); - }); - } - - - private void handleSet(CommandSender originalSender, CommandArguments args, Currency currency) { - BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); - OfflinePlayer target = (OfflinePlayer) args.get("target"); - BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); - - if (!this.manager.canUse(sender, currency)) return; - - this.manager.plugin.getUserManager().loadUser(target.getUniqueId()) - .thenAccept(result -> { - String name = target.getName() != null ? target.getName() : args.getRaw("target"); - result.setUsername(name); - - this.manager.onSet(sender, currency, result, amount); - }); - } -} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java deleted file mode 100644 index 3f25263..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/commands/TakeCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.commands; - -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.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import lombok.RequiredArgsConstructor; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; - -import java.math.BigDecimal; - -@RequiredArgsConstructor -public class TakeCommand implements Command { - private final BukkitCommandManager manager; - private final Currency currency; - private final String permissionBase; - - @Override - public CommandAPICommand build() { - return new CommandAPICommand("take") - .withPermission(permissionBase + "take") - .withArguments( - new OfflinePlayerArgument("target"), - currency.getProxy().fractionalDigits() > 0 - ? new DoubleArgument("amount", 1) - : new IntegerArgument("amount", 1) - ) - .executes((sender, args) -> { - this.handleTake(sender, args, currency); - }); - } - - - private void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) { - BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.manager.audienceFactory); - OfflinePlayer target = (OfflinePlayer) args.get("target"); - BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount"))); - - if (!this.manager.canUse(sender, currency)) return; - - this.manager.plugin.getUserManager().loadUser(target.getUniqueId()) - .thenAccept(result -> { - String name = target.getName() != null ? target.getName() : args.getRaw("target"); - result.setUsername(name); - - this.manager.onTake(sender, currency, result, amount); - }); - } -} 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 deleted file mode 100644 index 9f10eb9..0000000 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitCommandManager.java +++ /dev/null @@ -1,77 +0,0 @@ -package dev.xhyrom.lighteco.bukkit.manager; - -import dev.jorel.commandapi.CommandAPICommand; -import dev.xhyrom.lighteco.bukkit.commands.*; -import dev.xhyrom.lighteco.common.manager.command.AbstractCommandManager; -import dev.xhyrom.lighteco.common.model.currency.Currency; -import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; -import net.kyori.adventure.platform.bukkit.BukkitAudiences; -import org.bukkit.plugin.java.JavaPlugin; -import org.checkerframework.checker.nullness.qual.NonNull; - -public class BukkitCommandManager extends AbstractCommandManager { - public final BukkitAudiences audienceFactory; - - public BukkitCommandManager(LightEcoPlugin plugin) { - super(plugin); - - this.audienceFactory = BukkitAudiences.create((JavaPlugin) this.plugin.getBootstrap().getLoader()); - } - - @Override - public void registerCurrencyCommand(@NonNull Currency currency) { - registerCommands(currency.getIdentifier(), currency); - - for (String alias : currency.getIdentifierAliases()) { - registerCommands(alias, currency); - } - } - - @Override - public void registerCurrencyCommand(@NonNull Currency currency, boolean main) { - if (!main) { - registerCurrencyCommand(currency); - return; - } - - String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; - - // Register main command - registerCurrencyCommand(currency); - - // Expose pay as main command - if (currency.isPayable()) - new PayCommand(this, currency, permissionBase).build().register(); - - // Expose balance as main command - for (CommandAPICommand cmd : new BalanceCommand( - this, - "balance", - currency, - permissionBase - ).multipleBuild()) { - cmd.register(); - } - } - - private void registerCommands(@NonNull String name, @NonNull Currency currency) { - String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; - - BalanceCommand balanceCommand = new BalanceCommand(this, "balance", currency, permissionBase); - - CommandAPICommand cmd = new CommandAPICommand(name) - .withSubcommand(new SetCommand(this, currency, permissionBase).build()) - .withSubcommand(new GiveCommand(this, currency, permissionBase).build()) - .withSubcommand(new TakeCommand(this, currency, permissionBase).build()) - .withSubcommands(balanceCommand.multipleBuild()) - // We want balance to be the default command - .executesPlayer((sender, args) -> { - balanceCommand.handleBalance(sender, args, currency); - }); - - if (currency.isPayable()) - cmd = cmd.withSubcommand(new PayCommand(this, currency, permissionBase).build()); - - cmd.register(); - } -} diff --git a/bukkittest/.gitignore b/bukkittest/.gitignore deleted file mode 100644 index 72a0aec..0000000 --- a/bukkittest/.gitignore +++ /dev/null @@ -1,44 +0,0 @@ -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store - -runServer/ \ No newline at end of file diff --git a/common/build.gradle.kts b/common/build.gradle.kts index a2595f9..f01959a 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -19,6 +19,8 @@ dependencies { implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:5.0.0-beta.5") implementation("eu.okaeri:okaeri-configs-validator-okaeri:5.0.0-beta.5") + compileOnly("com.mojang:brigadier:1.0.18") + compileOnly("com.zaxxer:HikariCP:5.0.1") compileOnly("redis.clients:jedis:5.1.0") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java index bab2ecd..bbfa9f4 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java @@ -5,8 +5,8 @@ import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import org.checkerframework.checker.nullness.qual.NonNull; -public class ApiCommandManager extends ApiAbstractManager implements CommandManager { - public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.command.CommandManager handle) { +public class ApiCommandManager extends ApiAbstractManager implements CommandManager { + public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.command.CommandManager handle) { super(plugin, handle); } @@ -15,7 +15,7 @@ public class ApiCommandManager extends ApiAbstractManager context) { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + User target = null; + try { + target = OfflineUserArgument.getOfflineUser(context, "target"); + } catch (LockedUserException e) { + sender.sendMessage(MiniMessage.miniMessage().deserialize( + plugin.getConfig().messages.wait + )); + } + + if (target == null || target.getUsername() == null) { + String userName = context.getArgument("target", String.class); + + sender.sendMessage(MiniMessage.miniMessage().deserialize( + plugin.getConfig().messages.userNotFound, + Placeholder.parsed("username", userName) + )); + return null; + } + + return target; + } + + public static CurrencyMessageConfig getCurrencyMessageConfig(LightEcoPlugin plugin, Currency currency) { + Map config = plugin.getConfig().messages.currency; + CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier()); + + if (currencyMessageConfig == null) { + return config.get("default"); + } + + return currencyMessageConfig; + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java new file mode 100644 index 0000000..f04c600 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java @@ -0,0 +1,130 @@ +package dev.xhyrom.lighteco.common.command; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.commands.BalanceCommand; +import dev.xhyrom.lighteco.common.commands.CurrencyParentCommand; +import dev.xhyrom.lighteco.common.commands.InfoCommand; +import dev.xhyrom.lighteco.common.commands.PayCommand; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; +import dev.xhyrom.lighteco.common.model.currency.Currency; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import lombok.Getter; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.MiniMessage; + +import java.util.*; +import java.util.concurrent.*; + +public class CommandManager { + protected final LightEcoPlugin plugin; + private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("lighteco-command-executor") + .build() + ); + + @Getter + private final CommandDispatcher dispatcher = new CommandDispatcher<>(); + + @Getter + private final Set locks = ConcurrentHashMap.newKeySet(); + private final Map locksMappings = new ConcurrentHashMap<>(); + + public CommandManager(LightEcoPlugin plugin) { + this.plugin = plugin; + } + + // Register built-in commands + public void register() { + register(new InfoCommand()); + } + + public void register(Currency currency, boolean main) { + register(new CurrencyParentCommand(currency)); + + if (main) { + register(BalanceCommand.create(currency)); + register(PayCommand.create(currency)); + } + } + + protected void register(Command command) { + dispatcher.getRoot().addChild(command.build()); + } + + public void execute(CommandSender sender, String name, String[] args) { + if (!sender.isConsole() && locks.contains(sender.getUniqueId())) { + sender.sendMessage(MiniMessage.miniMessage().deserialize( + this.plugin.getConfig().messages.wait + )); + return; + } + + final CommandSource source = new CommandSource(this.plugin, sender); + final ParseResults parseResults = dispatcher.parse( + name + (args.length > 0 ? " " + String.join(" ", args) : ""), + source + ); + + if (!sender.isConsole()) + locks.add(sender.getUniqueId()); + + CompletableFuture.runAsync(() -> { + try { + dispatcher.execute(parseResults); + } catch (CommandSyntaxException e) { + this.sendError(sender, name, e); + } finally { + if (!source.sender().isConsole()) { + this.plugin.getBootstrap().getLogger().debug("Removing lock for " + sender.getUsername()); + + UUID target = locksMappings.get(sender.getUniqueId()); + if (target != null) { + locks.remove(target); + locksMappings.remove(sender.getUniqueId()); + + this.plugin.getBootstrap().getLogger().debug("Removing lock caused by " + sender.getUsername() + " for " + target); + } + + locks.remove(sender.getUniqueId()); + } + } + }, executor); + } + + public void lockBySender(CommandSender sender, UUID target) { + locks.add(target); + locksMappings.put(sender.getUniqueId(), target); + } + + private void sendError(CommandSender sender, String name, CommandSyntaxException e) { + sender.sendMessage(Component.text(e.getRawMessage().getString(), NamedTextColor.RED)); + + if (e.getInput() != null && e.getCursor() >= 0) { + int j = Math.min(e.getInput().length(), e.getCursor()); + + Component msg = Component.empty().color(NamedTextColor.GRAY).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name)); + + if (j > 10) { + msg = msg.append(Component.text("...")); + } + + msg = msg.append(Component.text(e.getInput().substring(Math.max(0, j - 10), j))); + + if (j < e.getInput().length()) { + Component component = Component.text(e.getInput().substring(j)).color(NamedTextColor.RED).decorate(TextDecoration.UNDERLINED); + msg = msg.append(component); + } + + msg = msg.append(Component.translatable("command.context.here").color(NamedTextColor.RED).decorate(TextDecoration.ITALIC)); + sender.sendMessage(msg); + } + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java new file mode 100644 index 0000000..103ac6e --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java @@ -0,0 +1,8 @@ +package dev.xhyrom.lighteco.common.command; + +import dev.xhyrom.lighteco.common.model.chat.CommandSender; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import org.checkerframework.checker.nullness.qual.NonNull; + +public record CommandSource(@NonNull LightEcoPlugin plugin, @NonNull CommandSender sender) { +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java new file mode 100644 index 0000000..c44f2af --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java @@ -0,0 +1,36 @@ +package dev.xhyrom.lighteco.common.command.abstraction; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.common.command.CommandSource; +import lombok.Getter; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +@Getter +public abstract class Command { + @NonNull + protected final String name; + + @NonNull + private final String description; + + @NonNull + private final List aliases = new ArrayList<>(); + + public Command(@NonNull String name, @NonNull String description, String... aliases) { + this.name = name; + this.description = description; + + Collections.addAll(this.aliases, aliases); + } + + public abstract CommandNode build(); + + protected LiteralArgumentBuilder builder() { + return LiteralArgumentBuilder.literal(name); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java new file mode 100644 index 0000000..9416568 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java @@ -0,0 +1,43 @@ +package dev.xhyrom.lighteco.common.command.argument.type; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.exception.LockedUserException; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; +import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +import java.util.UUID; + +public class OfflineUserArgument implements ArgumentType { + private OfflineUserArgument() {} + + public static User getOfflineUser(CommandContext context, String name) throws LockedUserException { + String userName = context.getArgument(name, String.class); + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + UUID uniqueId = plugin.getBootstrap().lookupUniqueId(userName).orElse(null); + if (uniqueId == null) { + return null; + } + + if (sender.getUniqueId() != uniqueId && plugin.getCommandManager().getLocks().contains(uniqueId)) { + throw new LockedUserException(uniqueId); + } + + // Lock the user to prevent race conditions + if (!sender.isConsole()) + plugin.getCommandManager().lockBySender(context.getSource().sender(), uniqueId); + + return plugin.getUserManager().loadUser(uniqueId).join(); + } + + @Override + public String parse(StringReader reader) throws CommandSyntaxException { + return reader.readString(); + } +} \ No newline at end of file diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java new file mode 100644 index 0000000..994d0d1 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/exception/LockedUserException.java @@ -0,0 +1,9 @@ +package dev.xhyrom.lighteco.common.command.exception; + +import java.util.UUID; + +public class LockedUserException extends Exception { + public LockedUserException(UUID uniqueId) { + super("User with uuid " + uniqueId + " is currently locked thus cannot be modified"); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java new file mode 100644 index 0000000..9aa86d1 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java @@ -0,0 +1,34 @@ +package dev.xhyrom.lighteco.common.command.suggestion.type; + +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class OfflineUserSuggestionProvider implements SuggestionProvider { + public static OfflineUserSuggestionProvider create() { + return new OfflineUserSuggestionProvider(); + } + + @Override + public CompletableFuture getSuggestions(CommandContext context, SuggestionsBuilder builder) { + LightEcoPlugin plugin = context.getSource().plugin(); + + String remaining = builder.getRemaining(); + + for (UUID uniqueId : plugin.getBootstrap().getOnlinePlayers()) { + User user = plugin.getUserManager().getIfLoaded(uniqueId); + if (user == null) continue; + + builder.suggest(user.getUsername()); + } + + return builder.buildFuture(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java new file mode 100644 index 0000000..471b675 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java @@ -0,0 +1,83 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + +public class BalanceCommand extends Command { + private final Currency currency; + + public static BalanceCommand create(@NonNull Currency currency) { + return new BalanceCommand(currency); + } + + public BalanceCommand(@NonNull Currency currency) { + super("balance", "Check your balance"); + + this.currency = currency; + } + + @Override + public CommandNode build() { + return builder() + .then( + RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .requires((source) -> source.sender().eligible("lighteco.currency."+currency.getIdentifier()+".command.balance.others")) + .executes(context -> { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + final User target = getUser(context); + if (target == null) + return SINGLE_SUCCESS; + + BigDecimal balance = target.getBalance(currency); + + sender.sendMessage( + MiniMessage.miniMessage().deserialize( + getCurrencyMessageConfig(plugin, currency).balanceOthers, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("balance", balance.toPlainString()) + ) + ); + + return SINGLE_SUCCESS; + })) + .requires((source) -> source.sender().eligible("lighteco.currency."+currency.getIdentifier()+".command.balance")) + .executes(context -> { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + BigDecimal balance = user.getBalance(currency); + + sender.sendMessage( + MiniMessage.miniMessage().deserialize( + getCurrencyMessageConfig(plugin, currency).balance, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("balance", balance.toPlainString()) + ) + ); + + return SINGLE_SUCCESS; + }) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java new file mode 100644 index 0000000..84ef42b --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java @@ -0,0 +1,62 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.getCurrencyMessageConfig; + +public class CurrencyParentCommand extends Command { + private final Currency currency; + + public CurrencyParentCommand(@NonNull Currency currency) { + super( + currency.getIdentifier(), + currency.getIdentifier() + ); + + this.currency = currency; + } + + @Override + public CommandNode build() { + LiteralArgumentBuilder builder = builder() + .then(BalanceCommand.create(currency).build()) + .then(SetCommand.create(currency).build()) + .then(GiveCommand.create(currency).build()) + .then(TakeCommand.create(currency).build()) + .executes(context -> { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + BigDecimal balance = user.getBalance(currency); + + sender.sendMessage( + MiniMessage.miniMessage().deserialize( + getCurrencyMessageConfig(plugin, currency).balance, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("balance", balance.toPlainString()) + ) + ); + + return SINGLE_SUCCESS; + }); + + if (currency.isPayable()) + builder = builder.then(PayCommand.create(currency).build()); + + return builder.build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java new file mode 100644 index 0000000..7e247d5 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java @@ -0,0 +1,106 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + +public class GiveCommand extends Command { + private static final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final Currency currency; + + public static GiveCommand create(@NonNull Currency currency) { + return new GiveCommand(currency); + } + + public GiveCommand(@NonNull Currency currency) { + super("give", "Give a player some money"); + + this.currency = currency; + } + + private void execute(CommandContext context) { + final LightEcoPlugin plugin = context.getSource().plugin(); + final CommandSender sender = context.getSource().sender(); + + final User target = getUser(context); + if (target == null) + return; + + BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); + + amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); + + try { + target.deposit(currency, amount); + } catch (CannotBeGreaterThan e) { + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } + + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()) + ) + ); + } + + @Override + public CommandNode build() { + if (currency.fractionalDigits() > 0) { + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } + + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java new file mode 100644 index 0000000..fc4fe0f --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java @@ -0,0 +1,36 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.model.chat.CommandSender; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +public class InfoCommand extends Command { + public InfoCommand() { + super("lighteco", "LightEco information"); + } + + @Override + public CommandNode build() { + return builder() + .requires(source -> source.sender().eligible("lighteco.command.info")) + .executes(context -> { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); + + sender.sendMessage(MiniMessage.miniMessage().deserialize( + "<#fa5246>LightEco (<#d6766f>v) on <#d6766f>", + Placeholder.parsed("version", plugin.getBootstrap().getVersion()), + Placeholder.parsed("platform", plugin.getPlatformType().getName()) + )); + + return SINGLE_SUCCESS; + }) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java new file mode 100644 index 0000000..94db38c --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java @@ -0,0 +1,150 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + +public class PayCommand extends Command { + private static final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final Currency currency; + + public static PayCommand create(@NonNull Currency currency) { + return new PayCommand(currency); + } + + public PayCommand(@NonNull Currency currency) { + super("pay", "Pay a player"); + + this.currency = currency; + } + + private void execute(CommandContext context) { + final LightEcoPlugin plugin = context.getSource().plugin(); + final CommandSender sender = context.getSource().sender(); + + final User target = getUser(context); + if (target == null) + return; + + BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); + + amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); + + final User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId()); + if (user == null) { + return; + } + + if (user.getBalance(this.currency).compareTo(amount) < 0) { + sender.sendMessage( + miniMessage.deserialize(getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney) + ); + + return; + } + + // calculate tax using Currency#calculateTax + BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount); + tax = tax.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); + + // subtract tax from amount + BigDecimal taxedAmount = amount.subtract(tax); + + try { + target.deposit(currency, taxedAmount); + user.withdraw(currency, amount); + } catch (CannotBeGreaterThan e) { + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } + + String template = tax.compareTo(BigDecimal.ZERO) > 0 + ? getCurrencyMessageConfig(plugin, this.currency).payWithTax + : getCurrencyMessageConfig(plugin, this.currency).pay; + + String templateReceived = tax.compareTo(BigDecimal.ZERO) > 0 + ? getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax + : getCurrencyMessageConfig(plugin, this.currency).payReceived; + + sender.sendMessage( + miniMessage.deserialize( + template, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()), + Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), + Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), + Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) + ) + ); + + target.sendMessage( + miniMessage.deserialize( + templateReceived, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("sender", user.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()), + Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), + Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), + Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) + ) + ); + } + + @Override + public CommandNode build() { + if (currency.fractionalDigits() > 0) { + return builder() + .requires((source) -> !source.sender().isConsole() && source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } + + return builder() + .requires((source) -> !source.sender().isConsole() && source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java new file mode 100644 index 0000000..06b3ef2 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java @@ -0,0 +1,106 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + +public class SetCommand extends Command { + private static final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final Currency currency; + + public static SetCommand create(@NonNull Currency currency) { + return new SetCommand(currency); + } + + public SetCommand(@NonNull Currency currency) { + super("set", "Set a player's balance"); + + this.currency = currency; + } + + private void execute(CommandContext context) { + final LightEcoPlugin plugin = context.getSource().plugin(); + final CommandSender sender = context.getSource().sender(); + + final User target = getUser(context); + if (target == null) + return; + + BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); + + amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); + + try { + target.setBalance(currency, amount); + } catch (CannotBeGreaterThan e) { + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } + + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()) + ) + ); + } + + @Override + public CommandNode build() { + if (currency.fractionalDigits() > 0) { + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } + + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java new file mode 100644 index 0000000..75187b1 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java @@ -0,0 +1,106 @@ +package dev.xhyrom.lighteco.common.commands; + +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.tree.CommandNode; +import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; +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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + +public class TakeCommand extends Command { + private static final MiniMessage miniMessage = MiniMessage.miniMessage(); + private final Currency currency; + + public static TakeCommand create(@NonNull Currency currency) { + return new TakeCommand(currency); + } + + public TakeCommand(@NonNull Currency currency) { + super("take", "Take money from a player"); + + this.currency = currency; + } + + private void execute(CommandContext context) { + final LightEcoPlugin plugin = context.getSource().plugin(); + final CommandSender sender = context.getSource().sender(); + + final User target = getUser(context); + if (target == null) + return; + + BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); + + amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); + + try { + target.withdraw(currency, amount); + } catch (CannotBeGreaterThan e) { + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) + ) + ); + + return; + } + + sender.sendMessage( + miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()) + ) + ); + } + + @Override + public CommandNode build() { + if (currency.fractionalDigits() > 0) { + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } + + return builder() + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .executes(c -> { + execute(c); + return SINGLE_SUCCESS; + }))) + .build(); + } +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java index 00380ce..d5589a2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -17,7 +17,6 @@ public class CurrencyMessageConfig extends OkaeriConfig { public String payReceived = " | Received from "; public String payReceivedWithTax = " | Received from ( after tax)"; - public String wait = "Please wait a moment before using this command again."; public String notEnoughMoney = "You don't have enough money!"; public String cannotPaySelf = "You cannot pay yourself!"; public String cannotBeGreaterThan = "Amount cannot be greater than "; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java index 88be91a..1e0a5ef 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java @@ -7,4 +7,7 @@ import java.util.Map; public class MessageConfig extends OkaeriConfig { public Map currency = Collections.singletonMap("default", new CurrencyMessageConfig()); + + public String wait = "Please wait a moment before using this command again."; + public String userNotFound = "User not found."; } 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 deleted file mode 100644 index dec658f..0000000 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/AbstractCommandManager.java +++ /dev/null @@ -1,275 +0,0 @@ -package dev.xhyrom.lighteco.common.manager.command; - -import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; -import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; -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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; - -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.ArrayList; -import java.util.Map; -import java.util.UUID; - -public abstract class AbstractCommandManager implements CommandManager { - public final LightEcoPlugin plugin; - private final MiniMessage miniMessage = MiniMessage.miniMessage(); - private final Map config; - private final ArrayList mustWait = new ArrayList<>(); - - protected AbstractCommandManager(LightEcoPlugin plugin) { - this.plugin = plugin; - this.config = this.plugin.getConfig().messages.currency; - } - - @Override - public boolean canUse(CommandSender sender, Currency currency) { - // Console doesn't need to wait - if (sender.getUniqueId() == null) return true; - - if (mustWait.contains(sender.getUniqueId())) { - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).wait - ) - ); - - 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); - } - } - - private CurrencyMessageConfig getConfig(Currency currency) { - CurrencyMessageConfig currencyMessageConfig = this.config.get(currency.getIdentifier()); - - if (currencyMessageConfig == null) { - return this.config.get("default"); - } - - return currencyMessageConfig; - } - - @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( - getConfig(currency).balance, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("balance", balance.toPlainString()) - ) - ); - } - - @Override - public void onBalance(CommandSender sender, Currency currency, User target) { - BigDecimal balance = target.getBalance(currency); - - sender.sendMessage( - miniMessage.deserialize( - getConfig(currency).balanceOthers, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("balance", balance.toPlainString()) - ) - - ); - } - - @Override - public void onSet(CommandSender sender, Currency currency, User target, BigDecimal amount) { - addToMustWait(sender.getUniqueId(), target.getUniqueId()); - amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - - try { - target.setBalance(currency, amount); - } catch (CannotBeGreaterThan e) { - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).cannotBeGreaterThan, - Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) - ) - ); - - return; - } - - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).set, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()) - ) - ); - - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - } - - @Override - public void onGive(CommandSender sender, Currency currency, User target, BigDecimal amount) { - addToMustWait(sender.getUniqueId(), target.getUniqueId()); - amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - - try { - target.deposit(currency, amount); - } catch (CannotBeGreaterThan e) { - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).cannotBeGreaterThan, - Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) - ) - ); - - return; - } - - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).give, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("balance", target.getBalance(currency).toPlainString()) - ) - ); - - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - } - - @Override - public void onTake(CommandSender sender, Currency currency, User target, BigDecimal amount) { - addToMustWait(sender.getUniqueId(), target.getUniqueId()); - amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - - try { - target.withdraw(currency, amount); - } catch (CannotBeGreaterThan e) { - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).cannotBeGreaterThan, - Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) - ) - ); - - return; - } - - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).take, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("balance", target.getBalance(currency).toPlainString()) - ) - ); - - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - } - - @Override - public void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount) { - if (sender.getUniqueId() != null && (sender.getUniqueId() == target.getUniqueId())) { - sender.sendMessage( - miniMessage.deserialize(this.getConfig(currency).cannotPaySelf) - ); - - return; - } - - amount = amount.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - - User user = this.plugin.getUserManager().getIfLoaded(sender.getUniqueId()); - - if (user.getBalance(currency).compareTo(amount) < 0) { - sender.sendMessage( - miniMessage.deserialize(this.getConfig(currency).notEnoughMoney) - ); - - return; - } - - addToMustWait(sender.getUniqueId(), target.getUniqueId()); - - // calculate tax using Currency#calculateTax - BigDecimal tax = currency.getProxy().calculateTax(user.getProxy(), amount); - tax = tax.setScale(currency.getProxy().fractionalDigits(), RoundingMode.DOWN); - - // subtract tax from amount - BigDecimal taxedAmount = amount.subtract(tax); - - try { - target.deposit(currency, taxedAmount); - user.withdraw(currency, amount); - } catch (CannotBeGreaterThan e) { - removeFromMustWait(target.getUniqueId(), sender.getUniqueId()); - sender.sendMessage( - miniMessage.deserialize( - this.getConfig(currency).cannotBeGreaterThan, - Placeholder.parsed("max", this.plugin.getConfig().maximumBalance.toPlainString()) - ) - ); - - return; - } - - String template = tax.compareTo(BigDecimal.ZERO) > 0 - ? this.getConfig(currency).payWithTax - : this.getConfig(currency).pay; - - String templateReceived = tax.compareTo(BigDecimal.ZERO) > 0 - ? this.getConfig(currency).payReceivedWithTax - : this.getConfig(currency).payReceived; - - sender.sendMessage( - miniMessage.deserialize( - template, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), - Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), - Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) - ) - ); - - target.sendMessage( - miniMessage.deserialize( - templateReceived, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("sender", user.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), - Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), - Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) - ) - ); - - 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 deleted file mode 100644 index bcb3c5d..0000000 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/command/CommandManager.java +++ /dev/null @@ -1,23 +0,0 @@ -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 registerCurrencyCommand(@NonNull Currency currency, boolean main); - - boolean canUse(CommandSender sender, Currency currency); - - 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); - void onPay(CommandSender sender, Currency currency, User target, BigDecimal amount); -} 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 index 9827b2b..63b197a 100644 --- 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 @@ -1,12 +1,18 @@ package dev.xhyrom.lighteco.common.model.chat; import net.kyori.adventure.text.Component; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.UUID; public interface CommandSender { String getUsername(); - UUID getUniqueId(); + @Nullable UUID getUniqueId(); + boolean eligible(String permission); void sendMessage(Component message); + + default boolean isConsole() { + return getUniqueId() == null; + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index ab03d48..639716c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -65,6 +65,9 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { // setup managers this.setupManagers(); + // register built-in commands + this.getCommandManager().register(); + // register platform hooks this.registerPlatformHooks(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java index 0791a64..3953c3c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java @@ -2,9 +2,9 @@ package dev.xhyrom.lighteco.common.plugin; import dev.xhyrom.lighteco.api.manager.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; +import dev.xhyrom.lighteco.common.command.CommandManager; import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.DependencyManager; -import dev.xhyrom.lighteco.common.manager.command.CommandManager; import dev.xhyrom.lighteco.common.manager.currency.CurrencyManager; import dev.xhyrom.lighteco.common.manager.user.UserManager; import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java index 07b053a..0e349a8 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java @@ -7,6 +7,7 @@ import net.kyori.adventure.audience.Audience; import java.io.InputStream; import java.nio.file.Path; import java.util.List; +import java.util.Optional; import java.util.UUID; public interface LightEcoBootstrap { @@ -14,8 +15,12 @@ public interface LightEcoBootstrap { PluginLogger getLogger(); SchedulerAdapter getScheduler(); Path getDataDirectory(); + String getVersion(); + + Optional lookupUniqueId(String username); boolean isPlayerOnline(UUID uniqueId); List getOnlinePlayers(); + InputStream getResourceStream(String filename); Audience getPlayerAudience(UUID uniqueId); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java index ca1ab93..3f0c578 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java @@ -4,6 +4,9 @@ public interface PluginLogger { void info(String message); void info(String message, Object ...args); + void debug(String message); + void debug(String message, Object ...args); + void warn(String message); void warn(String message, Object ...args); diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java similarity index 88% rename from currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java rename to currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java index f19dc8b..51a39ad 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/BukkitMCLoader.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java @@ -1,23 +1,23 @@ -package dev.xhyrom.lighteco.currency.money.bukkit; +package dev.xhyrom.lighteco.currency.money.paper; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.model.currency.Currency; -import dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault.VaultFactory; +import dev.xhyrom.lighteco.currency.money.paper.hooks.vault.VaultFactory; import dev.xhyrom.lighteco.currency.money.common.AbstractPlugin; import dev.xhyrom.lighteco.currency.money.common.Plugin; import dev.xhyrom.lighteco.currency.money.common.currency.MoneyCurrency; import lombok.Getter; import org.bukkit.plugin.java.JavaPlugin; -public class BukkitMCLoader extends JavaPlugin { +public class PaperMCLoader extends JavaPlugin { private VaultFactory vaultFactory; @Getter private final Plugin plugin; - public BukkitMCLoader() { + public PaperMCLoader() { this.plugin = new AbstractPlugin(this.getDataFolder().toPath()); } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java similarity index 99% rename from currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java rename to currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java index 03888e4..cc2cb94 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/Vault.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java @@ -1,4 +1,4 @@ -package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault; +package dev.xhyrom.lighteco.currency.money.paper.hooks.vault; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java similarity index 76% rename from currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java rename to currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java index 8a72208..84d8829 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/bukkit/hooks/vault/VaultFactory.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java @@ -1,16 +1,16 @@ -package dev.xhyrom.lighteco.currency.money.bukkit.hooks.vault; +package dev.xhyrom.lighteco.currency.money.paper.hooks.vault; -import dev.xhyrom.lighteco.currency.money.bukkit.BukkitMCLoader; +import dev.xhyrom.lighteco.currency.money.paper.PaperMCLoader; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicesManager; public class VaultFactory { - private final BukkitMCLoader loader; + private final PaperMCLoader loader; private Vault vault; - public VaultFactory(BukkitMCLoader loader) { + public VaultFactory(PaperMCLoader loader) { this.loader = loader; } diff --git a/currency-money/src/main/resources/plugin.yml b/currency-money/src/main/resources/plugin.yml index d1c95bb..e893415 100644 --- a/currency-money/src/main/resources/plugin.yml +++ b/currency-money/src/main/resources/plugin.yml @@ -1,11 +1,11 @@ name: ${name} description: ${description} version: ${version} -main: dev.xhyrom.lighteco.currency.money.bukkit.BukkitMCLoader +main: dev.xhyrom.lighteco.currency.money.paper.PaperMCLoader author: ${author} -api-version: 1.20 +api-version: 1.17 load: STARTUP softdepend: - Vault - - lighteco-bukkit + - LightEco diff --git a/bukkit/.gitignore b/paper/.gitignore similarity index 100% rename from bukkit/.gitignore rename to paper/.gitignore diff --git a/bukkit/README.md b/paper/README.md similarity index 100% rename from bukkit/README.md rename to paper/README.md diff --git a/bukkit/build.gradle.kts b/paper/build.gradle.kts similarity index 81% rename from bukkit/build.gradle.kts rename to paper/build.gradle.kts index 93903e5..0456452 100644 --- a/bukkit/build.gradle.kts +++ b/paper/build.gradle.kts @@ -5,7 +5,7 @@ plugins { repositories { maven("https://repo.extendedclip.com/content/repositories/placeholderapi") - maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") + maven("https://repo.papermc.io/repository/maven-public/") maven("https://oss.sonatype.org/content/repositories/snapshots") maven("https://oss.sonatype.org/content/repositories/central") } @@ -13,10 +13,10 @@ repositories { dependencies { implementation(project(":lighteco-common")) - implementation("dev.jorel:commandapi-bukkit-shade:9.5.0") implementation("net.kyori:adventure-platform-bukkit:4.2.0") - compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT") + compileOnly("io.papermc.paper:paper-api:1.19.1-R0.1-SNAPSHOT") + compileOnly("io.papermc.paper:paper-mojangapi:1.19.1-R0.1-SNAPSHOT") // PlaceholderAPI compileOnly("me.clip:placeholderapi:2.11.3") @@ -29,7 +29,9 @@ dependencies { } tasks.shadowJar { - relocate("dev.jorel.commandapi", "dev.xhyrom.lighteco.libraries.commandapi") + dependencies { + exclude(dependency("com.mojang:brigadier")) + } // common relocate("eu.okaeri.configs", "dev.xhyrom.lighteco.libraries.okaeri.configs") diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java similarity index 67% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java index a94bf72..0695adb 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java @@ -1,13 +1,12 @@ -package dev.xhyrom.lighteco.bukkit; +package dev.xhyrom.lighteco.paper; -import dev.jorel.commandapi.CommandAPI; -import dev.jorel.commandapi.CommandAPIBukkitConfig; -import dev.xhyrom.lighteco.bukkit.logger.BukkitLogger; +import dev.xhyrom.lighteco.paper.logger.PaperLogger; import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; import lombok.Getter; +import org.bukkit.OfflinePlayer; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.entity.Entity; @@ -17,11 +16,12 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.InputStream; import java.nio.file.Path; import java.util.List; +import java.util.Optional; import java.util.UUID; @Getter -public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap { - private final BukkitLightEcoPlugin plugin = new BukkitLightEcoPlugin(this); +public class PaperLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstrap { + private final PaperLightEcoPlugin plugin = new PaperLightEcoPlugin(this); @Getter private final JavaPlugin loader; @@ -29,33 +29,29 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr private final PluginLogger logger; @Getter private final SchedulerAdapter scheduler; + @Getter private BukkitAudiences audience; - public BukkitLightEcoBootstrap(JavaPlugin loader) { + public PaperLightEcoBootstrap(JavaPlugin loader) { this.loader = loader; - this.logger = new BukkitLogger(loader.getLogger()); - this.scheduler = new BukkitSchedulerAdapter(this); + this.logger = new PaperLogger(this.plugin, loader.getLogger()); + this.scheduler = new PaperSchedulerAdapter(this); } @Override public void onLoad() { this.plugin.load(); - CommandAPI.onLoad(new CommandAPIBukkitConfig(this.loader) - .verboseOutput(this.getPlugin().getConfig().debug) - ); } @Override public void onEnable() { - CommandAPI.onEnable(); this.plugin.enable(); - this.audience = BukkitAudiences.create(loader); + this.audience = BukkitAudiences.create(this.loader); } @Override public void onDisable() { - CommandAPI.onDisable(); this.plugin.disable(); } @@ -64,6 +60,16 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr return this.loader.getDataFolder().toPath(); } + @Override + public String getVersion() { + return this.loader.getDescription().getVersion(); + } + + @Override + public Optional lookupUniqueId(String username) { + return Optional.of(this.loader.getServer().getOfflinePlayer(username)).map(OfflinePlayer::getUniqueId); + } + @Override public boolean isPlayerOnline(UUID uniqueId) { Player player = this.loader.getServer().getPlayer(uniqueId); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoLoader.java similarity index 60% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoLoader.java index a8f76e9..73b8ae1 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoLoader.java @@ -1,14 +1,14 @@ -package dev.xhyrom.lighteco.bukkit; +package dev.xhyrom.lighteco.paper; import org.bukkit.plugin.java.JavaPlugin; // Used inside plugin.yml @SuppressWarnings("unused") -public class BukkitLightEcoLoader extends JavaPlugin { - private final BukkitLightEcoBootstrap bootstrap; +public class PaperLightEcoLoader extends JavaPlugin { + private final PaperLightEcoBootstrap bootstrap; - public BukkitLightEcoLoader() { - this.bootstrap = new BukkitLightEcoBootstrap(this); + public PaperLightEcoLoader() { + this.bootstrap = new PaperLightEcoBootstrap(this); } @Override diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java similarity index 64% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java index bca9b1f..a3b5b82 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoPlugin.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java @@ -1,12 +1,13 @@ -package dev.xhyrom.lighteco.bukkit; +package dev.xhyrom.lighteco.paper; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.manager.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; -import dev.xhyrom.lighteco.bukkit.hooks.Hooks; -import dev.xhyrom.lighteco.bukkit.listeners.BukkitConnectionListener; -import dev.xhyrom.lighteco.bukkit.manager.BukkitCommandManager; -import dev.xhyrom.lighteco.bukkit.manager.BukkitContextManager; +import dev.xhyrom.lighteco.paper.hooks.Hooks; +import dev.xhyrom.lighteco.paper.listeners.PaperCommandSuggestionsListener; +import dev.xhyrom.lighteco.paper.listeners.PaperConnectionListener; +import dev.xhyrom.lighteco.paper.manager.PaperCommandManager; +import dev.xhyrom.lighteco.paper.manager.PaperContextManager; import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; @@ -17,33 +18,34 @@ import org.bukkit.plugin.ServicePriority; import org.checkerframework.checker.nullness.qual.NonNull; @Getter -public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { - private final BukkitLightEcoBootstrap bootstrap; +public class PaperLightEcoPlugin extends AbstractLightEcoPlugin { + private final PaperLightEcoBootstrap bootstrap; @Getter private StandardUserManager userManager; @Getter private StandardCurrencyManager currencyManager; @Getter - private BukkitCommandManager commandManager; + private PaperCommandManager commandManager; @Getter private ContextManager contextManager; - public BukkitLightEcoPlugin(BukkitLightEcoBootstrap bootstrap) { + public PaperLightEcoPlugin(PaperLightEcoBootstrap bootstrap) { this.bootstrap = bootstrap; } @Override protected void registerListeners() { - this.bootstrap.getLoader().getServer().getPluginManager().registerEvents(new BukkitConnectionListener(this), this.bootstrap.getLoader()); + this.bootstrap.getLoader().getServer().getPluginManager().registerEvents(new PaperConnectionListener(this), this.bootstrap.getLoader()); + this.bootstrap.getLoader().getServer().getPluginManager().registerEvents(new PaperCommandSuggestionsListener(this), this.bootstrap.getLoader()); } @Override public void setupManagers() { this.userManager = new StandardUserManager(this); this.currencyManager = new StandardCurrencyManager(this); - this.commandManager = new BukkitCommandManager(this); - this.contextManager = new BukkitContextManager(); + this.commandManager = new PaperCommandManager(this); + this.contextManager = new PaperContextManager(); } @Override @@ -68,6 +70,6 @@ public class BukkitLightEcoPlugin extends AbstractLightEcoPlugin { @Override public Platform.@NonNull Type getPlatformType() { - return Platform.Type.BUKKIT; + return Platform.Type.PAPER; } } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitSchedulerAdapter.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java similarity index 87% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitSchedulerAdapter.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java index 0312ffd..f051ba1 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitSchedulerAdapter.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java @@ -1,21 +1,20 @@ -package dev.xhyrom.lighteco.bukkit; +package dev.xhyrom.lighteco.paper; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerTask; -import lombok.Getter; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; -public class BukkitSchedulerAdapter implements SchedulerAdapter { +public class PaperSchedulerAdapter implements SchedulerAdapter { private final Executor async; - private final BukkitLightEcoBootstrap bootstrap; + private final PaperLightEcoBootstrap bootstrap; private final BukkitScheduler scheduler; - public BukkitSchedulerAdapter(BukkitLightEcoBootstrap bootstrap) { + public PaperSchedulerAdapter(PaperLightEcoBootstrap bootstrap) { this.bootstrap = bootstrap; this.scheduler = bootstrap.getLoader().getServer().getScheduler(); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java similarity index 56% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java index 9bdce61..f732850 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/chat/BukkitCommandSender.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java @@ -1,21 +1,15 @@ -package dev.xhyrom.lighteco.bukkit.chat; +package dev.xhyrom.lighteco.paper.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) { +public class PaperCommandSender extends AbstractCommandSender { + public PaperCommandSender(CommandSender sender) { super(sender); - - this.audience = audienceFactory.sender(sender); } @Override @@ -32,8 +26,13 @@ public class BukkitCommandSender extends AbstractCommandSender { return null; } + @Override + public boolean eligible(String permission) { + return this.delegate.hasPermission(permission); + } + @Override public void sendMessage(Component message) { - this.audience.sendMessage(message); + this.delegate.sendMessage(message); } } \ No newline at end of file diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java similarity index 77% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java index a820a55..8597548 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/Hooks.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java @@ -1,6 +1,6 @@ -package dev.xhyrom.lighteco.bukkit.hooks; +package dev.xhyrom.lighteco.paper.hooks; -import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; @@ -8,7 +8,7 @@ import org.bukkit.Bukkit; public class Hooks { private static PlaceholderAPIExpansion placeholderAPIExpansion; - public static void register(BukkitLightEcoPlugin plugin) { + public static void register(PaperLightEcoPlugin plugin) { if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { placeholderAPIExpansion = new PlaceholderAPIExpansion(plugin); placeholderAPIExpansion.register(); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java similarity index 91% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java index b0bbb01..7604fca 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/hooks/PlaceholderAPIExpansion.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java @@ -1,6 +1,6 @@ -package dev.xhyrom.lighteco.bukkit.hooks; +package dev.xhyrom.lighteco.paper.hooks; -import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import lombok.RequiredArgsConstructor; @@ -11,7 +11,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; @RequiredArgsConstructor public class PlaceholderAPIExpansion extends PlaceholderExpansion { - private final BukkitLightEcoPlugin plugin; + private final PaperLightEcoPlugin plugin; @Override public @NonNull String getIdentifier() { diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java new file mode 100644 index 0000000..2cf8a1f --- /dev/null +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java @@ -0,0 +1,55 @@ +package dev.xhyrom.lighteco.paper.listeners; + +import com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent; +import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; +import dev.xhyrom.lighteco.paper.util.PaperBrigadier; +import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; +import dev.xhyrom.lighteco.common.command.CommandSource; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class PaperCommandSuggestionsListener implements Listener { + private final PaperLightEcoPlugin plugin; + + public PaperCommandSuggestionsListener(PaperLightEcoPlugin plugin) { + this.plugin = plugin; + } + + @SuppressWarnings("deprecation") + @EventHandler + public void onPlayerSendCommandsEvent(AsyncPlayerSendCommandsEvent event) { + PaperCommandSender sender = new PaperCommandSender(event.getPlayer()); + CommandSource source = new CommandSource(this.plugin, sender); + if (event.isAsynchronous() || !event.hasFiredAsync()) { + for (CommandNode command : this.plugin.getCommandManager().getDispatcher().getRoot().getChildren()) { + PaperBrigadier.removeChild(event.getCommandNode(), command.getName()); + PaperBrigadier.removeChild(event.getCommandNode(), this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + command.getName()); + + if (!command.canUse(source)) continue; + + addChild(event, source, command, createClone(command)); + addChild(event, source, command, createClone(this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + command.getName(), command)); + } + } + } + + @SuppressWarnings({"rawtypes", "unchecked", "deprecation"}) + private void addChild(AsyncPlayerSendCommandsEvent event, CommandSource source, CommandNode command, CommandNode clone) { + for (CommandNode child : command.getChildren()) { + if (child.canUse(source)) + clone.addChild(child); + } + + event.getCommandNode().addChild((CommandNode) clone); + } + + private CommandNode createClone(CommandNode command) { + return createClone(command.getName(), command); + } + + private CommandNode createClone(String name, CommandNode command) { + return new LiteralCommandNode<>(name, command.getCommand(), command.getRequirement(), command.getRedirect(), command.getRedirectModifier(), command.isFork()); + } +} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java similarity index 89% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java index 2b79005..a8e511f 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/listeners/BukkitConnectionListener.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java @@ -1,6 +1,6 @@ -package dev.xhyrom.lighteco.bukkit.listeners; +package dev.xhyrom.lighteco.paper.listeners; -import dev.xhyrom.lighteco.bukkit.BukkitLightEcoPlugin; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import net.kyori.adventure.text.Component; @@ -14,11 +14,11 @@ import org.bukkit.event.player.PlayerQuitEvent; import java.util.UUID; -public class BukkitConnectionListener implements Listener { - private final BukkitLightEcoPlugin plugin; +public class PaperConnectionListener implements Listener { + private final PaperLightEcoPlugin plugin; private final MiniMessage miniMessage = MiniMessage.miniMessage(); - public BukkitConnectionListener(BukkitLightEcoPlugin plugin) { + public PaperConnectionListener(PaperLightEcoPlugin plugin) { this.plugin = plugin; } diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/logger/PaperLogger.java similarity index 72% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/logger/PaperLogger.java index 7d4d6f7..a423c0b 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/logger/PaperLogger.java @@ -1,14 +1,17 @@ -package dev.xhyrom.lighteco.bukkit.logger; +package dev.xhyrom.lighteco.paper.logger; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import java.util.logging.Level; import java.util.logging.Logger; -public class BukkitLogger implements PluginLogger { +public class PaperLogger implements PluginLogger { + private final LightEcoPlugin plugin; private final Logger logger; - public BukkitLogger(Logger logger) { + public PaperLogger(LightEcoPlugin plugin, Logger logger) { + this.plugin = plugin; this.logger = logger; } @@ -22,6 +25,20 @@ public class BukkitLogger implements PluginLogger { this.logger.info(String.format(message, args)); } + @Override + public void debug(String message) { + if (!this.plugin.getConfig().debug) return; + + info(message); + } + + @Override + public void debug(String message, Object... args) { + if (!this.plugin.getConfig().debug) return; + + info(message, args); + } + @Override public void warn(String message) { this.logger.log(Level.WARNING, message); diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java new file mode 100644 index 0000000..abbfb30 --- /dev/null +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java @@ -0,0 +1,64 @@ +package dev.xhyrom.lighteco.paper.manager; + +import com.mojang.brigadier.ParseResults; +import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; +import dev.xhyrom.lighteco.common.command.CommandManager; +import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.common.command.abstraction.Command; +import dev.xhyrom.lighteco.common.model.currency.Currency; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.paper.util.PaperCommandMapUtil; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.TabCompleter; + +import java.util.ArrayList; +import java.util.List; + +public class PaperCommandManager extends CommandManager { + private final PaperCommandMapUtil commandMapUtil; + + public PaperCommandManager(LightEcoPlugin plugin) { + super(plugin); + + this.commandMapUtil = new PaperCommandMapUtil(plugin); + } + + @Override + protected void register(Command command) { + super.register(command); + + CommandExecutor executor = (sender, bukkitCommand, s, args) -> { + if (s.startsWith("lighteco:")) { + s = s.substring(9); + } + + execute(new PaperCommandSender(sender), s, args); + return true; + }; + + TabCompleter completer = (sender, bukkitCommand, s, args) -> { + final List suggestions = new ArrayList<>(); + final CommandSource source = new CommandSource(plugin, new PaperCommandSender(sender)); + + final ParseResults parseResults = getDispatcher().parse( + command.getName() + (args.length > 0 ? " " + String.join(" ", args) : ""), + source + ); + + getDispatcher().getCompletionSuggestions( + parseResults + ).join().getList().forEach(suggestion -> suggestions.add(suggestion.getText())); + + return suggestions; + }; + + List aliases = new ArrayList<>(command.getAliases()); + aliases.add(command.getName()); + + this.commandMapUtil.register( + executor, + completer, + aliases + ); + } +} diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitContextManager.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java similarity index 77% rename from bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitContextManager.java rename to paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java index 1ec1a53..b63e8f8 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/manager/BukkitContextManager.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java @@ -1,4 +1,4 @@ -package dev.xhyrom.lighteco.bukkit.manager; +package dev.xhyrom.lighteco.paper.manager; import dev.xhyrom.lighteco.api.manager.ContextManager; import org.bukkit.entity.Player; @@ -6,7 +6,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.util.UUID; -public class BukkitContextManager implements ContextManager { +public class PaperContextManager implements ContextManager { @Override public @NonNull UUID getPlayerUniqueId(@NonNull Player player) { return player.getUniqueId(); diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java new file mode 100644 index 0000000..ad4a7bc --- /dev/null +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java @@ -0,0 +1,41 @@ +package dev.xhyrom.lighteco.paper.util; + +import com.mojang.brigadier.tree.CommandNode; +import com.mojang.brigadier.tree.RootCommandNode; + +import java.lang.reflect.Field; +import java.util.Map; + +public class PaperBrigadier { + private static final Field CHILDREN_FIELD; + private static final Field LITERALS_FIELD; + private static final Field ARGUMENTS_FIELD; + private static final Field[] CHILDREN_FIELDS; + + static { + try { + CHILDREN_FIELD = CommandNode.class.getDeclaredField("children"); + LITERALS_FIELD = CommandNode.class.getDeclaredField("literals"); + ARGUMENTS_FIELD = CommandNode.class.getDeclaredField("arguments"); + CHILDREN_FIELDS = new Field[]{CHILDREN_FIELD, LITERALS_FIELD, ARGUMENTS_FIELD}; + + for (Field field : CHILDREN_FIELDS) { + field.setAccessible(true); + } + } catch (ReflectiveOperationException e) { + throw new ExceptionInInitializerError(e); + } + } + + @SuppressWarnings("unchecked") + public static void removeChild(RootCommandNode root, String name) { + try { + for (Field field : CHILDREN_FIELDS) { + Map children = (Map) field.get(root); + children.remove(name); + } + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } +} diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java new file mode 100644 index 0000000..2e3b06c --- /dev/null +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java @@ -0,0 +1,56 @@ +package dev.xhyrom.lighteco.paper.util; + +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import org.bukkit.command.*; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; + +import java.lang.reflect.Constructor; +import java.util.List; +import java.util.Map; + +public class PaperCommandMapUtil { + private static final Constructor COMMAND_CONSTRUCTOR; + + static { + try { + COMMAND_CONSTRUCTOR = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + COMMAND_CONSTRUCTOR.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new ExceptionInInitializerError(e); + } + } + + private final LightEcoPlugin plugin; + public PaperCommandMapUtil(LightEcoPlugin plugin) { + this.plugin = plugin; + } + + public void register(CommandExecutor executor, TabCompleter completer, List aliases) { + Plugin bukkitPlugin = (JavaPlugin) this.plugin.getBootstrap().getLoader(); + CommandMap commandMap = bukkitPlugin.getServer().getCommandMap(); + Map knownCommands = commandMap.getKnownCommands(); + + for (String name : aliases) { + if (!name.toLowerCase().equals(name)) { + throw new IllegalArgumentException("Command aliases must be lowercase! (name: " + name + ")"); + } + + try { + PluginCommand command = COMMAND_CONSTRUCTOR.newInstance(name, bukkitPlugin); + + commandMap.register(bukkitPlugin.getName().toLowerCase(), command); + knownCommands.put(bukkitPlugin.getName().toLowerCase() + ":" + name.toLowerCase(), command); + knownCommands.put(name, command); + + command.setLabel(name); + command.setExecutor(executor); + command.setTabCompleter(completer); + + commandMap.register(bukkitPlugin.getName(), command); + } catch (ReflectiveOperationException e) { + this.plugin.getBootstrap().getLogger().error("Failed to register command: %s", e); + } + } + } +} diff --git a/bukkit/src/main/resources/plugin.yml b/paper/src/main/resources/plugin.yml similarity index 55% rename from bukkit/src/main/resources/plugin.yml rename to paper/src/main/resources/plugin.yml index 0d7f4be..6868c77 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/paper/src/main/resources/plugin.yml @@ -1,9 +1,9 @@ -name: ${name} +name: ${coreName} description: ${description} version: ${version} -main: dev.xhyrom.lighteco.bukkit.BukkitLightEcoLoader +main: dev.xhyrom.lighteco.paper.PaperLightEcoLoader author: ${author} -api-version: 1.16 +api-version: 1.17 load: STARTUP softdepend: diff --git a/settings.gradle.kts b/settings.gradle.kts index 854271b..1c0f551 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,15 +3,22 @@ rootProject.name = "lighteco-parent" sequenceOf( "api", "common", - "bukkit", - "bukkittest", + "paper", "sponge-8", - "currency-money" + "currency-money", + "test" ).forEach { include("lighteco-$it") project(":lighteco-$it").projectDir = file(it) } +sequenceOf( + "paper" +).forEach { + include("lighteco-test-$it") + project(":lighteco-test-$it").projectDir = file("test/$it") +} + pluginManagement { repositories { mavenLocal() diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java index ab281f0..c1f736d 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java @@ -12,6 +12,7 @@ import org.spongepowered.plugin.builtin.jvm.Plugin; import java.io.InputStream; import java.nio.file.Path; import java.util.List; +import java.util.Optional; import java.util.UUID; @Plugin("lighteco-sponge") @@ -60,6 +61,16 @@ public class SpongeLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr return null; } + @Override + public String getVersion() { + return null; + } + + @Override + public Optional lookupUniqueId(String username) { + return Optional.empty(); + } + @Override public boolean isPlayerOnline(UUID uniqueId) { return false; diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java index 89646bb..a80b1b6 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java @@ -3,7 +3,7 @@ package dev.xhyrom.lighteco.sponge; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.manager.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; -import dev.xhyrom.lighteco.common.manager.command.CommandManager; +import dev.xhyrom.lighteco.common.command.CommandManager; import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; import dev.xhyrom.lighteco.common.messaging.MessagingFactory; diff --git a/bukkittest/build.gradle.kts b/test/paper/build.gradle.kts similarity index 61% rename from bukkittest/build.gradle.kts rename to test/paper/build.gradle.kts index 545c292..fa27896 100644 --- a/bukkittest/build.gradle.kts +++ b/test/paper/build.gradle.kts @@ -6,8 +6,17 @@ plugins { } server { - version = "1.20.1" - type = "paper" + version = "1.20.6" + type = "purpur" + + // Minecraft 1.20.6 requires java 21 + java { + targetCompatibility = JavaVersion.VERSION_21 + sourceCompatibility = JavaVersion.VERSION_21 + toolchain { + languageVersion.set(JavaLanguageVersion.of(21)) + } + } } repositories { diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency.java similarity index 92% rename from bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java rename to test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency.java index 4652c83..c5534af 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency.java +++ b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency.java @@ -1,4 +1,4 @@ -package dev.xhyrom.lighteco.bukkittest; +package dev.xhyrom.lighteco.test.paper; import dev.xhyrom.lighteco.api.model.currency.Currency; diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java similarity index 92% rename from bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java rename to test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java index 63de38a..e501613 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestCurrency2.java +++ b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java @@ -1,4 +1,4 @@ -package dev.xhyrom.lighteco.bukkittest; +package dev.xhyrom.lighteco.test.paper; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.user.User; @@ -18,7 +18,7 @@ public class TestCurrency2 implements Currency { @Override public boolean isPayable() { - return true; + return false; } @Override diff --git a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java similarity index 96% rename from bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java rename to test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java index 26170e1..2b28756 100644 --- a/bukkittest/src/main/java/dev/xhyrom/lighteco/bukkittest/TestPlugin.java +++ b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java @@ -1,4 +1,4 @@ -package dev.xhyrom.lighteco.bukkittest; +package dev.xhyrom.lighteco.test.paper; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; diff --git a/bukkittest/src/main/resources/plugin.yml b/test/paper/src/main/resources/plugin.yml similarity index 64% rename from bukkittest/src/main/resources/plugin.yml rename to test/paper/src/main/resources/plugin.yml index 0fdea7e..31d3857 100644 --- a/bukkittest/src/main/resources/plugin.yml +++ b/test/paper/src/main/resources/plugin.yml @@ -1,10 +1,10 @@ name: ${name} description: ${description} version: ${version} -main: dev.xhyrom.lighteco.bukkittest.TestPlugin +main: dev.xhyrom.lighteco.test.paper.TestPlugin author: ${author} api-version: 1.20 softdepend: - Vault - - lighteco-bukkit \ No newline at end of file + - LightEco \ No newline at end of file From 6cd7ad090130e108deff1772a952652adf25fb61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 16 Jul 2024 18:22:32 +0200 Subject: [PATCH 24/24] build: add spotless formatter (#9) --- .../dev/xhyrom/lighteco/api/LightEco.java | 1 + .../xhyrom/lighteco/api/LightEcoProvider.java | 7 +- .../lighteco/api/manager/CommandManager.java | 1 + .../lighteco/api/manager/CurrencyManager.java | 1 + .../lighteco/api/manager/UserManager.java | 2 + .../messenger/IncomingMessageConsumer.java | 1 + .../lighteco/api/messenger/Messenger.java | 4 +- .../message/type/UserUpdateMessage.java | 1 + .../lighteco/api/model/currency/Currency.java | 6 +- .../xhyrom/lighteco/api/model/user/User.java | 8 +- .../lighteco/api/platform/PlayerAdapter.java | 1 + .../lighteco/api/storage/StorageProvider.java | 4 + build.gradle.kts | 19 ++++ .../lighteco/common/api/LightEcoApi.java | 4 +- .../common/api/impl/ApiCommandManager.java | 18 +-- .../common/api/impl/ApiCurrencyManager.java | 16 ++- .../common/api/impl/ApiMessagingService.java | 5 +- .../lighteco/common/api/impl/ApiPlatform.java | 1 + .../common/api/impl/ApiPlayerAdapter.java | 3 +- .../lighteco/common/api/impl/ApiUser.java | 25 ++--- .../common/api/impl/ApiUserManager.java | 11 +- .../lighteco/common/cache/ExpiringSet.java | 4 +- .../lighteco/common/cache/RedisBackedMap.java | 4 +- .../common/command/CommandHelper.java | 19 ++-- .../common/command/CommandManager.java | 84 ++++++++------ .../common/command/CommandSource.java | 4 +- .../common/command/abstraction/Command.java | 12 +- .../argument/type/OfflineUserArgument.java | 9 +- .../type/OfflineUserSuggestionProvider.java | 4 +- .../common/commands/BalanceCommand.java | 66 ++++++----- .../commands/CurrencyParentCommand.java | 23 ++-- .../lighteco/common/commands/GiveCommand.java | 72 +++++++----- .../lighteco/common/commands/InfoCommand.java | 18 +-- .../lighteco/common/commands/PayCommand.java | 106 ++++++++++-------- .../lighteco/common/commands/SetCommand.java | 71 +++++++----- .../lighteco/common/commands/TakeCommand.java | 72 +++++++----- .../xhyrom/lighteco/common/config/Config.java | 7 +- .../config/housekeeper/HousekeeperConfig.java | 2 + .../config/message/CurrencyMessageConfig.java | 28 +++-- .../common/config/message/MessageConfig.java | 3 +- .../config/messaging/MessagingConfig.java | 1 + .../config/messaging/MessagingDataConfig.java | 1 + .../common/config/storage/StorageConfig.java | 1 + .../config/storage/StorageDataConfig.java | 1 + .../common/dependencies/Dependency.java | 78 ++++--------- .../dependencies/DependencyManager.java | 1 + .../dependencies/DependencyManagerImpl.java | 17 ++- .../dependencies/DependencyRegistry.java | 28 +++-- .../dependencies/IsolatedClassLoader.java | 3 +- .../common/dependencies/package-info.java | 2 +- .../dependencies/relocation/Relocation.java | 1 - .../relocation/RelocationHandler.java | 13 ++- .../lighteco/common/manager/Manager.java | 1 + .../manager/currency/CurrencyManager.java | 1 + .../currency/StandardCurrencyManager.java | 12 +- .../manager/user/StandardUserManager.java | 36 +++--- .../common/manager/user/UserHousekeeper.java | 8 +- .../common/manager/user/UserManager.java | 2 + .../messaging/LightEcoMessagingService.java | 38 +++++-- .../messaging/message/AbstractMessage.java | 1 + .../message/UserUpdateMessageImpl.java | 8 +- .../messaging/type/redis/RedisMessenger.java | 21 ++-- .../type/redis/RedisMessengerProvider.java | 1 + .../model/chat/AbstractCommandSender.java | 8 +- .../common/model/chat/CommandSender.java | 3 + .../lighteco/common/model/user/User.java | 35 ++++-- .../xhyrom/lighteco/common/package-info.java | 2 +- .../common/plugin/AbstractLightEcoPlugin.java | 25 +++-- .../common/plugin/LightEcoPlugin.java | 5 + .../plugin/bootstrap/LightEcoBootstrap.java | 8 ++ .../plugin/bootstrap/LoaderBootstrap.java | 2 + .../common/plugin/logger/PluginLogger.java | 18 ++- .../lighteco/common/storage/Storage.java | 3 +- .../common/storage/StorageFactory.java | 26 +++-- .../memory/MemoryStorageProvider.java | 8 +- .../common/storage/provider/package-info.java | 2 +- .../storage/provider/sql/SqlStatements.java | 18 ++- .../provider/sql/SqlStorageProvider.java | 97 ++++++++-------- .../sql/connection/ConnectionFactory.java | 1 + .../connection/file/H2ConnectionFactory.java | 14 ++- .../file/SqliteConnectionFactory.java | 11 +- .../DriverBasedHikariConnectionFactory.java | 15 ++- .../hikari/HikariConnectionFactory.java | 26 +++-- .../lighteco/common/task/UserSaveTask.java | 8 +- .../common/util/ThrowableRunnable.java | 1 - .../common/util/URLClassLoaderAccess.java | 14 ++- .../currency/money/common/AbstractPlugin.java | 2 + .../currency/money/common/config/Config.java | 2 +- .../money/common/currency/MoneyCurrency.java | 4 +- .../currency/money/paper/PaperMCLoader.java | 6 +- .../money/paper/hooks/vault/Vault.java | 16 ++- .../money/paper/hooks/vault/VaultFactory.java | 5 +- .../paper/PaperLightEcoBootstrap.java | 13 ++- .../lighteco/paper/PaperLightEcoPlugin.java | 36 ++++-- .../lighteco/paper/PaperSchedulerAdapter.java | 24 ++-- .../paper/chat/PaperCommandSender.java | 4 +- .../xhyrom/lighteco/paper/hooks/Hooks.java | 2 + .../paper/hooks/PlaceholderAPIExpansion.java | 12 +- .../PaperCommandSuggestionsListener.java | 45 ++++++-- .../listeners/PaperConnectionListener.java | 33 +++--- .../paper/manager/PaperCommandManager.java | 34 +++--- .../paper/manager/PaperContextManager.java | 1 + .../lighteco/paper/util/PaperBrigadier.java | 2 +- .../paper/util/PaperCommandMapUtil.java | 11 +- .../sponge/SpongeLightEcoBootstrap.java | 3 + .../lighteco/sponge/SpongeLightEcoPlugin.java | 23 ++-- .../lighteco/test/paper/TestCurrency2.java | 2 +- .../lighteco/test/paper/TestPlugin.java | 5 +- 108 files changed, 986 insertions(+), 672 deletions(-) diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java index 7b880b7..8d781d4 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/LightEco.java @@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.api.manager.UserManager; import dev.xhyrom.lighteco.api.messaging.MessagingService; import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.PlayerAdapter; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Optional; diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/LightEcoProvider.java b/api/src/main/java/dev/xhyrom/lighteco/api/LightEcoProvider.java index b321054..7960d40 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/LightEcoProvider.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/LightEcoProvider.java @@ -1,10 +1,11 @@ package dev.xhyrom.lighteco.api; -import lombok.experimental.UtilityClass; -import org.checkerframework.checker.nullness.qual.NonNull; - import static org.jetbrains.annotations.ApiStatus.Internal; +import lombok.experimental.UtilityClass; + +import org.checkerframework.checker.nullness.qual.NonNull; + @UtilityClass public final class LightEcoProvider { private static LightEco instance; diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/manager/CommandManager.java b/api/src/main/java/dev/xhyrom/lighteco/api/manager/CommandManager.java index 9d7401f..f003212 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/manager/CommandManager.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/manager/CommandManager.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.manager; import dev.xhyrom.lighteco.api.model.currency.Currency; + import org.checkerframework.checker.nullness.qual.NonNull; public interface CommandManager { diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/manager/CurrencyManager.java b/api/src/main/java/dev/xhyrom/lighteco/api/manager/CurrencyManager.java index b9113e9..adbf759 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/manager/CurrencyManager.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/manager/CurrencyManager.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.manager; import dev.xhyrom.lighteco.api.model.currency.Currency; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/manager/UserManager.java b/api/src/main/java/dev/xhyrom/lighteco/api/manager/UserManager.java index eb02103..c8b6217 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/manager/UserManager.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/manager/UserManager.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.manager; import dev.xhyrom.lighteco.api.model.user.User; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -9,6 +10,7 @@ import java.util.concurrent.CompletableFuture; public interface UserManager { @NonNull CompletableFuture loadUser(@NonNull UUID uniqueId); + @NonNull CompletableFuture loadUser(@NonNull UUID uniqueId, String username); @NonNull CompletableFuture saveUser(@NonNull User user); diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java index ff83582..4f62243 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/IncomingMessageConsumer.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.messenger; import dev.xhyrom.lighteco.api.messenger.message.Message; + import org.checkerframework.checker.nullness.qual.NonNull; public interface IncomingMessageConsumer { diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java index ea6d7e3..29f296b 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/Messenger.java @@ -1,12 +1,12 @@ package dev.xhyrom.lighteco.api.messenger; import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; + import org.checkerframework.checker.nullness.qual.NonNull; public interface Messenger extends AutoCloseable { void sendOutgoingMessage(@NonNull OutgoingMessage message, boolean global); @Override - default void close() { - } + default void close() {} } diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java index 7d6b2e8..54a7fc0 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/messenger/message/type/UserUpdateMessage.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.messenger.message.type; import dev.xhyrom.lighteco.api.messenger.message.Message; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java index 308f086..e6f3ed0 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/currency/Currency.java @@ -24,7 +24,8 @@ public interface Currency { */ default String[] getIdentifierAliases() { return new String[0]; - }; + } + ; /** * Returns the type of the currency, either {@link Type#LOCAL} or {@link Type#GLOBAL} @@ -79,7 +80,8 @@ public interface Currency { */ default BigDecimal calculateTax(User user, BigDecimal amount) { return BigDecimal.ZERO; - }; + } + ; /** * Represents the type of currency diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java index 5bf88fe..3814c59 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/model/user/User.java @@ -1,7 +1,9 @@ package dev.xhyrom.lighteco.api.model.user; import dev.xhyrom.lighteco.api.model.currency.Currency; + import net.kyori.adventure.text.Component; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -50,7 +52,8 @@ public interface User { * @param amount the amount * @throws IllegalArgumentException if the amount is negative */ - void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException; + void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) + throws IllegalArgumentException; /** * Subtract the specified amount from the balance of this user for the specified currency. @@ -59,7 +62,8 @@ public interface User { * @param amount the amount * @throws IllegalArgumentException if the amount is negative */ - void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws IllegalArgumentException; + void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) + throws IllegalArgumentException; /** * Send a message to this user. diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/platform/PlayerAdapter.java b/api/src/main/java/dev/xhyrom/lighteco/api/platform/PlayerAdapter.java index ddd8efc..6f3f8ee 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/platform/PlayerAdapter.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/platform/PlayerAdapter.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.api.platform; import dev.xhyrom.lighteco.api.model.user.User; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.concurrent.CompletableFuture; diff --git a/api/src/main/java/dev/xhyrom/lighteco/api/storage/StorageProvider.java b/api/src/main/java/dev/xhyrom/lighteco/api/storage/StorageProvider.java index e4c199b..bda6b33 100644 --- a/api/src/main/java/dev/xhyrom/lighteco/api/storage/StorageProvider.java +++ b/api/src/main/java/dev/xhyrom/lighteco/api/storage/StorageProvider.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.api.storage; 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; @@ -23,7 +24,10 @@ public interface StorageProvider { void shutdown() throws Exception; default void registerCurrency(@NonNull Currency currency) throws Exception {} + @NonNull User loadUser(@NonNull UUID uniqueId, @Nullable String username) throws Exception; + void saveUser(@NonNull User user) throws Exception; + void saveUsers(@NonNull User... users) throws Exception; } diff --git a/build.gradle.kts b/build.gradle.kts index d46ab81..5beb692 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import java.io.ByteArrayOutputStream plugins { id("java") id("org.sonarqube") version "4.2.1.3168" + id("com.diffplug.spotless") version "6.25.0" } val majorVersion = 0 @@ -10,6 +11,12 @@ val minorVersion = 1 val patchVersion = determinePatchVersion(project) val commitHash = determineCommitHash(project) +defaultTasks("spotlessApply") + +repositories { + mavenCentral() +} + allprojects { group = "dev.xhyrom" version = "$majorVersion.$minorVersion.$patchVersion" @@ -32,6 +39,18 @@ subprojects { } } +spotless { + java { + importOrder() + removeUnusedImports() + + palantirJavaFormat().style("AOSP") + formatAnnotations() + + target("api/src/main/java/**", "common/src/main/java/**", "currency-money/src/main/java/**", "paper/src/main/java/**", "sponge-8/src/main/java/**", "test/**/src/main/java/**") + } +} + fun determinePatchVersion(project: Project): Int { val tagInfo = ByteArrayOutputStream() diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java index 554241c..3ffce80 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/LightEcoApi.java @@ -9,6 +9,7 @@ import dev.xhyrom.lighteco.api.platform.Platform; import dev.xhyrom.lighteco.api.platform.PlayerAdapter; import dev.xhyrom.lighteco.common.api.impl.*; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Optional; @@ -62,7 +63,8 @@ public class LightEcoApi implements LightEco { Class expected = this.plugin.getContextManager().getPlayerClass(); if (!expected.equals(playerClass)) { - throw new IllegalArgumentException("Expected player class " + expected.getName() + ", got " + playerClass.getName()); + throw new IllegalArgumentException("Expected player class " + expected.getName() + + ", got " + playerClass.getName()); } return (PlayerAdapter) this.playerAdapter; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java index bbfa9f4..5b75167 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCommandManager.java @@ -3,26 +3,30 @@ package dev.xhyrom.lighteco.common.api.impl; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; -public class ApiCommandManager extends ApiAbstractManager implements CommandManager { - public ApiCommandManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.command.CommandManager handle) { +public class ApiCommandManager + extends ApiAbstractManager + implements CommandManager { + public ApiCommandManager( + LightEcoPlugin plugin, dev.xhyrom.lighteco.common.command.CommandManager handle) { super(plugin, handle); } @Override public void registerCurrencyCommand(@NonNull Currency currency) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.plugin.getCurrencyManager() - .getIfLoaded(currency.getIdentifier()); + dev.xhyrom.lighteco.common.model.currency.Currency internal = + this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier()); this.handle.register(internal, false); } @Override public void registerCurrencyCommand(@NonNull Currency currency, boolean main) { - dev.xhyrom.lighteco.common.model.currency.Currency internal = this.plugin.getCurrencyManager() - .getIfLoaded(currency.getIdentifier()); + dev.xhyrom.lighteco.common.model.currency.Currency internal = + this.plugin.getCurrencyManager().getIfLoaded(currency.getIdentifier()); this.handle.register(internal, main); } -} \ No newline at end of file +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java index c3dda44..b77c662 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/api/impl/ApiCurrencyManager.java @@ -3,12 +3,17 @@ package dev.xhyrom.lighteco.common.api.impl; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Collection; -public class ApiCurrencyManager extends ApiAbstractManager implements CurrencyManager { - public ApiCurrencyManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.currency.CurrencyManager handle) { +public class ApiCurrencyManager + extends ApiAbstractManager + implements CurrencyManager { + public ApiCurrencyManager( + LightEcoPlugin plugin, + dev.xhyrom.lighteco.common.manager.currency.CurrencyManager handle) { super(plugin, handle); } @@ -18,9 +23,7 @@ public class ApiCurrencyManager extends ApiAbstractManager getRegisteredCurrencies() { - return this.handle.values() - .stream().map(this::wrap) - .toList(); + return this.handle.values().stream().map(this::wrap).toList(); } @Override @@ -30,7 +33,8 @@ public class ApiCurrencyManager extends ApiAbstractManager implements UserManager { - public ApiUserManager(LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.user.UserManager handle) { +public class ApiUserManager + extends ApiAbstractManager + implements UserManager { + public ApiUserManager( + LightEcoPlugin plugin, dev.xhyrom.lighteco.common.manager.user.UserManager handle) { super(plugin, handle); } @@ -26,8 +30,7 @@ public class ApiUserManager extends ApiAbstractManager loadUser(@NonNull UUID uniqueId, String username) { - return this.plugin.getStorage().loadUser(uniqueId, username) - .thenApply(this::wrap); + return this.plugin.getStorage().loadUser(uniqueId, username).thenApply(this::wrap); } @Override diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/cache/ExpiringSet.java b/common/src/main/java/dev/xhyrom/lighteco/common/cache/ExpiringSet.java index 22c95e3..032a60f 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/cache/ExpiringSet.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/cache/ExpiringSet.java @@ -10,9 +10,7 @@ public class ExpiringSet { private final long lifetime; public ExpiringSet(long duration, TimeUnit unit) { - this.cache = CacheBuilder.newBuilder() - .expireAfterWrite(duration, unit) - .build(); + this.cache = CacheBuilder.newBuilder().expireAfterWrite(duration, unit).build(); this.lifetime = unit.toMillis(duration); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/cache/RedisBackedMap.java b/common/src/main/java/dev/xhyrom/lighteco/common/cache/RedisBackedMap.java index 7fd2df8..5b965c1 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/cache/RedisBackedMap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/cache/RedisBackedMap.java @@ -2,6 +2,4 @@ package dev.xhyrom.lighteco.common.cache; import java.util.HashMap; -public class RedisBackedMap extends HashMap { - -} +public class RedisBackedMap extends HashMap {} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java index acc6359..73b3894 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandHelper.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.command; import com.mojang.brigadier.context.CommandContext; + import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument; import dev.xhyrom.lighteco.common.command.exception.LockedUserException; import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig; @@ -8,7 +9,9 @@ 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 lombok.experimental.UtilityClass; + import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -24,25 +27,25 @@ public class CommandHelper { try { target = OfflineUserArgument.getOfflineUser(context, "target"); } catch (LockedUserException e) { - sender.sendMessage(MiniMessage.miniMessage().deserialize( - plugin.getConfig().messages.wait - )); + sender.sendMessage( + MiniMessage.miniMessage().deserialize(plugin.getConfig().messages.wait)); } if (target == null || target.getUsername() == null) { String userName = context.getArgument("target", String.class); - sender.sendMessage(MiniMessage.miniMessage().deserialize( - plugin.getConfig().messages.userNotFound, - Placeholder.parsed("username", userName) - )); + sender.sendMessage(MiniMessage.miniMessage() + .deserialize( + plugin.getConfig().messages.userNotFound, + Placeholder.parsed("username", userName))); return null; } return target; } - public static CurrencyMessageConfig getCurrencyMessageConfig(LightEcoPlugin plugin, Currency currency) { + public static CurrencyMessageConfig getCurrencyMessageConfig( + LightEcoPlugin plugin, Currency currency) { Map config = plugin.getConfig().messages.currency; CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier()); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java index f04c600..aa6c2cd 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java @@ -4,6 +4,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.exceptions.CommandSyntaxException; + import dev.xhyrom.lighteco.common.command.abstraction.Command; import dev.xhyrom.lighteco.common.commands.BalanceCommand; import dev.xhyrom.lighteco.common.commands.CurrencyParentCommand; @@ -12,7 +13,9 @@ import dev.xhyrom.lighteco.common.commands.PayCommand; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import lombok.Getter; + import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; @@ -24,17 +27,18 @@ import java.util.concurrent.*; public class CommandManager { protected final LightEcoPlugin plugin; - private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("lighteco-command-executor") - .build() - ); + private final ExecutorService executor = + Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("lighteco-command-executor") + .build()); @Getter private final CommandDispatcher dispatcher = new CommandDispatcher<>(); @Getter private final Set locks = ConcurrentHashMap.newKeySet(); + private final Map locksMappings = new ConcurrentHashMap<>(); public CommandManager(LightEcoPlugin plugin) { @@ -61,42 +65,47 @@ public class CommandManager { public void execute(CommandSender sender, String name, String[] args) { if (!sender.isConsole() && locks.contains(sender.getUniqueId())) { - sender.sendMessage(MiniMessage.miniMessage().deserialize( - this.plugin.getConfig().messages.wait - )); + sender.sendMessage( + MiniMessage.miniMessage().deserialize(this.plugin.getConfig().messages.wait)); return; } final CommandSource source = new CommandSource(this.plugin, sender); final ParseResults parseResults = dispatcher.parse( - name + (args.length > 0 ? " " + String.join(" ", args) : ""), - source - ); + name + (args.length > 0 ? " " + String.join(" ", args) : ""), source); - if (!sender.isConsole()) - locks.add(sender.getUniqueId()); + if (!sender.isConsole()) locks.add(sender.getUniqueId()); - CompletableFuture.runAsync(() -> { - try { - dispatcher.execute(parseResults); - } catch (CommandSyntaxException e) { - this.sendError(sender, name, e); - } finally { - if (!source.sender().isConsole()) { - this.plugin.getBootstrap().getLogger().debug("Removing lock for " + sender.getUsername()); + CompletableFuture.runAsync( + () -> { + try { + dispatcher.execute(parseResults); + } catch (CommandSyntaxException e) { + this.sendError(sender, name, e); + } finally { + if (!source.sender().isConsole()) { + this.plugin + .getBootstrap() + .getLogger() + .debug("Removing lock for " + sender.getUsername()); - UUID target = locksMappings.get(sender.getUniqueId()); - if (target != null) { - locks.remove(target); - locksMappings.remove(sender.getUniqueId()); + UUID target = locksMappings.get(sender.getUniqueId()); + if (target != null) { + locks.remove(target); + locksMappings.remove(sender.getUniqueId()); - this.plugin.getBootstrap().getLogger().debug("Removing lock caused by " + sender.getUsername() + " for " + target); + this.plugin + .getBootstrap() + .getLogger() + .debug("Removing lock caused by " + sender.getUsername() + + " for " + target); + } + + locks.remove(sender.getUniqueId()); + } } - - locks.remove(sender.getUniqueId()); - } - } - }, executor); + }, + executor); } public void lockBySender(CommandSender sender, UUID target) { @@ -110,7 +119,10 @@ public class CommandManager { if (e.getInput() != null && e.getCursor() >= 0) { int j = Math.min(e.getInput().length(), e.getCursor()); - Component msg = Component.empty().color(NamedTextColor.GRAY).clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name)); + Component msg = Component.empty() + .color(NamedTextColor.GRAY) + .clickEvent( + ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + name)); if (j > 10) { msg = msg.append(Component.text("...")); @@ -119,11 +131,15 @@ public class CommandManager { msg = msg.append(Component.text(e.getInput().substring(Math.max(0, j - 10), j))); if (j < e.getInput().length()) { - Component component = Component.text(e.getInput().substring(j)).color(NamedTextColor.RED).decorate(TextDecoration.UNDERLINED); + Component component = Component.text(e.getInput().substring(j)) + .color(NamedTextColor.RED) + .decorate(TextDecoration.UNDERLINED); msg = msg.append(component); } - msg = msg.append(Component.translatable("command.context.here").color(NamedTextColor.RED).decorate(TextDecoration.ITALIC)); + msg = msg.append(Component.translatable("command.context.here") + .color(NamedTextColor.RED) + .decorate(TextDecoration.ITALIC)); sender.sendMessage(msg); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java index 103ac6e..a0095ac 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandSource.java @@ -2,7 +2,7 @@ package dev.xhyrom.lighteco.common.command; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; -public record CommandSource(@NonNull LightEcoPlugin plugin, @NonNull CommandSender sender) { -} +public record CommandSource(@NonNull LightEcoPlugin plugin, @NonNull CommandSender sender) {} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java index c44f2af..b915fb1 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/Command.java @@ -2,8 +2,11 @@ package dev.xhyrom.lighteco.common.command.abstraction; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.common.command.CommandSource; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.ArrayList; @@ -12,14 +15,11 @@ import java.util.List; @Getter public abstract class Command { - @NonNull - protected final String name; + @NonNull protected final String name; - @NonNull - private final String description; + @NonNull private final String description; - @NonNull - private final List aliases = new ArrayList<>(); + @NonNull private final List aliases = new ArrayList<>(); public Command(@NonNull String name, @NonNull String description, String... aliases) { this.name = name; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java index 9416568..8177ead 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/type/OfflineUserArgument.java @@ -4,6 +4,7 @@ import com.mojang.brigadier.StringReader; import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; + import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.exception.LockedUserException; import dev.xhyrom.lighteco.common.model.chat.CommandSender; @@ -15,7 +16,8 @@ import java.util.UUID; public class OfflineUserArgument implements ArgumentType { private OfflineUserArgument() {} - public static User getOfflineUser(CommandContext context, String name) throws LockedUserException { + public static User getOfflineUser(CommandContext context, String name) + throws LockedUserException { String userName = context.getArgument(name, String.class); LightEcoPlugin plugin = context.getSource().plugin(); CommandSender sender = context.getSource().sender(); @@ -25,7 +27,8 @@ public class OfflineUserArgument implements ArgumentType { return null; } - if (sender.getUniqueId() != uniqueId && plugin.getCommandManager().getLocks().contains(uniqueId)) { + if (sender.getUniqueId() != uniqueId + && plugin.getCommandManager().getLocks().contains(uniqueId)) { throw new LockedUserException(uniqueId); } @@ -40,4 +43,4 @@ public class OfflineUserArgument implements ArgumentType { public String parse(StringReader reader) throws CommandSyntaxException { return reader.readString(); } -} \ No newline at end of file +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java index 9aa86d1..cef1398 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/suggestion/type/OfflineUserSuggestionProvider.java @@ -4,6 +4,7 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; + import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; @@ -17,7 +18,8 @@ public class OfflineUserSuggestionProvider implements SuggestionProvider getSuggestions(CommandContext context, SuggestionsBuilder builder) { + public CompletableFuture getSuggestions( + CommandContext context, SuggestionsBuilder builder) { LightEcoPlugin plugin = context.getSource().plugin(); String remaining = builder.getRemaining(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java index 471b675..57c2d10 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/BalanceCommand.java @@ -1,8 +1,13 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; import dev.xhyrom.lighteco.common.command.suggestion.type.OfflineUserSuggestionProvider; @@ -10,15 +15,14 @@ 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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.*; - public class BalanceCommand extends Command { private final Currency currency; @@ -35,32 +39,36 @@ public class BalanceCommand extends Command { @Override public CommandNode build() { return builder() - .then( - RequiredArgumentBuilder.argument("target", StringArgumentType.word()) - .suggests(OfflineUserSuggestionProvider.create()) - .requires((source) -> source.sender().eligible("lighteco.currency."+currency.getIdentifier()+".command.balance.others")) - .executes(context -> { - LightEcoPlugin plugin = context.getSource().plugin(); - CommandSender sender = context.getSource().sender(); + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) + .suggests(OfflineUserSuggestionProvider.create()) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.balance.others")) + .executes(context -> { + LightEcoPlugin plugin = context.getSource().plugin(); + CommandSender sender = context.getSource().sender(); - final User target = getUser(context); - if (target == null) - return SINGLE_SUCCESS; + final User target = getUser(context); + if (target == null) return SINGLE_SUCCESS; - BigDecimal balance = target.getBalance(currency); + BigDecimal balance = target.getBalance(currency); - sender.sendMessage( - MiniMessage.miniMessage().deserialize( - getCurrencyMessageConfig(plugin, currency).balanceOthers, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("balance", balance.toPlainString()) - ) - ); + sender.sendMessage(MiniMessage.miniMessage() + .deserialize( + getCurrencyMessageConfig(plugin, currency) + .balanceOthers, + Placeholder.parsed( + "currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed( + "balance", balance.toPlainString()))); - return SINGLE_SUCCESS; - })) - .requires((source) -> source.sender().eligible("lighteco.currency."+currency.getIdentifier()+".command.balance")) + return SINGLE_SUCCESS; + })) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.balance")) .executes(context -> { LightEcoPlugin plugin = context.getSource().plugin(); CommandSender sender = context.getSource().sender(); @@ -68,13 +76,11 @@ public class BalanceCommand extends Command { User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId()); BigDecimal balance = user.getBalance(currency); - sender.sendMessage( - MiniMessage.miniMessage().deserialize( + sender.sendMessage(MiniMessage.miniMessage() + .deserialize( getCurrencyMessageConfig(plugin, currency).balance, Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("balance", balance.toPlainString()) - ) - ); + Placeholder.parsed("balance", balance.toPlainString()))); return SINGLE_SUCCESS; }) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java index 84ef42b..2da1974 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/CurrencyParentCommand.java @@ -1,30 +1,31 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.getCurrencyMessageConfig; + import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.getCurrencyMessageConfig; - public class CurrencyParentCommand extends Command { private final Currency currency; public CurrencyParentCommand(@NonNull Currency currency) { - super( - currency.getIdentifier(), - currency.getIdentifier() - ); + super(currency.getIdentifier(), currency.getIdentifier()); this.currency = currency; } @@ -43,13 +44,11 @@ public class CurrencyParentCommand extends Command { User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId()); BigDecimal balance = user.getBalance(currency); - sender.sendMessage( - MiniMessage.miniMessage().deserialize( + sender.sendMessage(MiniMessage.miniMessage() + .deserialize( getCurrencyMessageConfig(plugin, currency).balance, Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("balance", balance.toPlainString()) - ) - ); + Placeholder.parsed("balance", balance.toPlainString()))); return SINGLE_SUCCESS; }); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java index 7e247d5..34d8237 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/GiveCommand.java @@ -1,11 +1,16 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; @@ -14,16 +19,15 @@ 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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import java.math.RoundingMode; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.*; - public class GiveCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); private final Currency currency; @@ -43,60 +47,66 @@ public class GiveCommand extends Command { final CommandSender sender = context.getSource().sender(); final User target = getUser(context); - if (target == null) - return; + if (target == null) return; - BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 - ? context.getArgument("amount", Double.class) - : context.getArgument("amount", Integer.class)); + BigDecimal amount = BigDecimal.valueOf( + currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); try { target.deposit(currency, amount); } catch (CannotBeGreaterThan e) { - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, - Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()))); return; } - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).set, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()))); } @Override public CommandNode build() { if (currency.fractionalDigits() > 0) { return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.give")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .then(RequiredArgumentBuilder.argument( + "amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + + currency.getIdentifier() + ".command.give")) .executes(c -> { execute(c); return SINGLE_SUCCESS; }))) - .build(); + .build(); } return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible( + "lighteco.currency." + currency.getIdentifier() + ".command.give")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.give")) + .then(RequiredArgumentBuilder.argument( + "amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.give")) .executes(c -> { execute(c); return SINGLE_SUCCESS; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java index fc4fe0f..03ad1cd 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/InfoCommand.java @@ -1,15 +1,17 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; - public class InfoCommand extends Command { public InfoCommand() { super("lighteco", "LightEco information"); @@ -23,11 +25,13 @@ public class InfoCommand extends Command { LightEcoPlugin plugin = context.getSource().plugin(); CommandSender sender = context.getSource().sender(); - sender.sendMessage(MiniMessage.miniMessage().deserialize( - "<#fa5246>LightEco (<#d6766f>v) on <#d6766f>", - Placeholder.parsed("version", plugin.getBootstrap().getVersion()), - Placeholder.parsed("platform", plugin.getPlatformType().getName()) - )); + sender.sendMessage(MiniMessage.miniMessage() + .deserialize( + "<#fa5246>LightEco (<#d6766f>v) on <#d6766f>", + Placeholder.parsed( + "version", plugin.getBootstrap().getVersion()), + Placeholder.parsed( + "platform", plugin.getPlatformType().getName()))); return SINGLE_SUCCESS; }) diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java index 94db38c..44148cc 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/PayCommand.java @@ -1,11 +1,16 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; @@ -14,16 +19,15 @@ 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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import java.math.RoundingMode; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.*; - public class PayCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); private final Currency currency; @@ -43,12 +47,12 @@ public class PayCommand extends Command { final CommandSender sender = context.getSource().sender(); final User target = getUser(context); - if (target == null) - return; + if (target == null) return; - BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 - ? context.getArgument("amount", Double.class) - : context.getArgument("amount", Integer.class)); + BigDecimal amount = BigDecimal.valueOf( + currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); @@ -58,9 +62,8 @@ public class PayCommand extends Command { } if (user.getBalance(this.currency).compareTo(amount) < 0) { - sender.sendMessage( - miniMessage.deserialize(getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney)); return; } @@ -76,12 +79,9 @@ public class PayCommand extends Command { target.deposit(currency, taxedAmount); user.withdraw(currency, amount); } catch (CannotBeGreaterThan e) { - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, - Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()))); return; } @@ -94,53 +94,63 @@ public class PayCommand extends Command { ? getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax : getCurrencyMessageConfig(plugin, this.currency).payReceived; - sender.sendMessage( - miniMessage.deserialize( - template, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), - Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), - Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + template, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()), + Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), + Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), + Placeholder.parsed( + "receiver_balance", target.getBalance(currency).toPlainString()))); - target.sendMessage( - miniMessage.deserialize( - templateReceived, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("sender", user.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()), - Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), - Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), - Placeholder.parsed("receiver_balance", target.getBalance(currency).toPlainString()) - ) - ); + target.sendMessage(miniMessage.deserialize( + templateReceived, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("sender", user.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()), + Placeholder.parsed("taxed_amount", taxedAmount.toPlainString()), + Placeholder.parsed("sender_balance", user.getBalance(currency).toPlainString()), + Placeholder.parsed( + "receiver_balance", target.getBalance(currency).toPlainString()))); } @Override public CommandNode build() { if (currency.fractionalDigits() > 0) { return builder() - .requires((source) -> !source.sender().isConsole() && source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> !source.sender().isConsole() + && source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.pay")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .then(RequiredArgumentBuilder.argument( + "amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + + currency.getIdentifier() + ".command.pay")) .executes(c -> { execute(c); return SINGLE_SUCCESS; }))) - .build(); + .build(); } return builder() - .requires((source) -> !source.sender().isConsole() && source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> !source.sender().isConsole() + && source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.pay")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.pay")) + .then(RequiredArgumentBuilder.argument( + "amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.pay")) .executes(c -> { execute(c); return SINGLE_SUCCESS; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java index 06b3ef2..7bca726 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/SetCommand.java @@ -1,11 +1,16 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; @@ -14,16 +19,15 @@ 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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import java.math.RoundingMode; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.*; - public class SetCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); private final Currency currency; @@ -43,60 +47,65 @@ public class SetCommand extends Command { final CommandSender sender = context.getSource().sender(); final User target = getUser(context); - if (target == null) - return; + if (target == null) return; - BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 - ? context.getArgument("amount", Double.class) - : context.getArgument("amount", Integer.class)); + BigDecimal amount = BigDecimal.valueOf( + currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); try { target.setBalance(currency, amount); } catch (CannotBeGreaterThan e) { - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, - Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()))); return; } - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).set, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()))); } @Override public CommandNode build() { if (currency.fractionalDigits() > 0) { return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.set")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .then(RequiredArgumentBuilder.argument( + "amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + + currency.getIdentifier() + ".command.set")) .executes(c -> { execute(c); return SINGLE_SUCCESS; }))) - .build(); + .build(); } return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.set")) + .then(RequiredArgumentBuilder.argument( + "amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.set")) .executes(c -> { execute(c); return SINGLE_SUCCESS; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java index 75187b1..aa03ef0 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/commands/TakeCommand.java @@ -1,11 +1,16 @@ package dev.xhyrom.lighteco.common.commands; +import static com.mojang.brigadier.Command.SINGLE_SUCCESS; + +import static dev.xhyrom.lighteco.common.command.CommandHelper.*; + import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.tree.CommandNode; + import dev.xhyrom.lighteco.api.exception.CannotBeGreaterThan; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; @@ -14,16 +19,15 @@ 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 net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; import java.math.RoundingMode; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static dev.xhyrom.lighteco.common.command.CommandHelper.*; - public class TakeCommand extends Command { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); private final Currency currency; @@ -43,60 +47,66 @@ public class TakeCommand extends Command { final CommandSender sender = context.getSource().sender(); final User target = getUser(context); - if (target == null) - return; + if (target == null) return; - BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0 - ? context.getArgument("amount", Double.class) - : context.getArgument("amount", Integer.class)); + BigDecimal amount = BigDecimal.valueOf( + currency.fractionalDigits() > 0 + ? context.getArgument("amount", Double.class) + : context.getArgument("amount", Integer.class)); amount = amount.setScale(currency.fractionalDigits(), RoundingMode.DOWN); try { target.withdraw(currency, amount); } catch (CannotBeGreaterThan e) { - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, - Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan, + Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString()))); return; } - sender.sendMessage( - miniMessage.deserialize( - getCurrencyMessageConfig(plugin, this.currency).set, - Placeholder.parsed("currency", currency.getIdentifier()), - Placeholder.parsed("target", target.getUsername()), - Placeholder.parsed("amount", amount.toPlainString()) - ) - ); + sender.sendMessage(miniMessage.deserialize( + getCurrencyMessageConfig(plugin, this.currency).set, + Placeholder.parsed("currency", currency.getIdentifier()), + Placeholder.parsed("target", target.getUsername()), + Placeholder.parsed("amount", amount.toPlainString()))); } @Override public CommandNode build() { if (currency.fractionalDigits() > 0) { return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.take")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", DoubleArgumentType.doubleArg(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .then(RequiredArgumentBuilder.argument( + "amount", DoubleArgumentType.doubleArg(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + + currency.getIdentifier() + ".command.take")) .executes(c -> { execute(c); return SINGLE_SUCCESS; }))) - .build(); + .build(); } return builder() - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) - .then(RequiredArgumentBuilder.argument("target", StringArgumentType.word()) + .requires((source) -> source.sender() + .eligible( + "lighteco.currency." + currency.getIdentifier() + ".command.take")) + .then(RequiredArgumentBuilder.argument( + "target", StringArgumentType.word()) .suggests(OfflineUserSuggestionProvider.create()) - .then(RequiredArgumentBuilder.argument("amount", IntegerArgumentType.integer(1)) - .requires((source) -> source.sender().eligible("lighteco.currency." + currency.getIdentifier() + ".command.take")) + .then(RequiredArgumentBuilder.argument( + "amount", IntegerArgumentType.integer(1)) + .requires((source) -> source.sender() + .eligible("lighteco.currency." + currency.getIdentifier() + + ".command.take")) .executes(c -> { execute(c); return SINGLE_SUCCESS; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java index e8a3797..8f21694 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java @@ -4,6 +4,7 @@ import dev.xhyrom.lighteco.common.config.housekeeper.HousekeeperConfig; import dev.xhyrom.lighteco.common.config.message.MessageConfig; import dev.xhyrom.lighteco.common.config.messaging.MessagingConfig; import dev.xhyrom.lighteco.common.config.storage.StorageConfig; + import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; import eu.okaeri.configs.annotation.Header; @@ -14,7 +15,8 @@ import java.math.BigDecimal; @Header("") public class Config extends OkaeriConfig { @Comment("This property must be unique for each server.") - @Comment("If you have multiple servers, you must set this property to a different value for each server.") + @Comment( + "If you have multiple servers, you must set this property to a different value for each server.") @Comment("Used for local currencies.") public String server = "none"; @@ -28,7 +30,8 @@ public class Config extends OkaeriConfig { public long saveInterval = 5L; @Comment("Maximum allowed balance.") - @Comment("If you want to change this value, you must also change the data type in the database.") + @Comment( + "If you want to change this value, you must also change the data type in the database.") public BigDecimal maximumBalance = BigDecimal.valueOf(999999999999999.99); @Comment("Messages") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/housekeeper/HousekeeperConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/housekeeper/HousekeeperConfig.java index f15e82f..0e652e6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/housekeeper/HousekeeperConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/housekeeper/HousekeeperConfig.java @@ -8,9 +8,11 @@ import java.util.concurrent.TimeUnit; public class HousekeeperConfig extends OkaeriConfig { @Comment("How long should the cache be kept after the last write") public int expireAfterWrite = 300; + public TimeUnit expireAfterWriteUnit = TimeUnit.SECONDS; @Comment("How often should housekeeper run") public int runInterval = 60; + public TimeUnit runIntervalUnit = TimeUnit.SECONDS; } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java index d5589a2..87f745e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/CurrencyMessageConfig.java @@ -4,18 +4,28 @@ import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Variable; public class CurrencyMessageConfig extends OkaeriConfig { - public String balance = " | Your balance: "; + public String balance = + " | Your balance: "; + @Variable("balance-others") - public String balanceOthers = " | Balance of | "; + public String balanceOthers = + " | Balance of | "; - public String set = " | Set balance of to "; - public String give = " | Gave | "; - public String take = " | Took from "; + public String set = + " | Set balance of to "; + public String give = + " | Gave | "; + public String take = + " | Took from "; - public String pay = " | Paid to "; - public String payWithTax = " | Paid to ( after tax)"; - public String payReceived = " | Received from "; - public String payReceivedWithTax = " | Received from ( after tax)"; + public String pay = + " | Paid to "; + public String payWithTax = + " | Paid to ( after tax)"; + public String payReceived = + " | Received from "; + public String payReceivedWithTax = + " | Received from ( after tax)"; public String notEnoughMoney = "You don't have enough money!"; public String cannotPaySelf = "You cannot pay yourself!"; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java index 1e0a5ef..7a080e4 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/message/MessageConfig.java @@ -6,7 +6,8 @@ import java.util.Collections; import java.util.Map; public class MessageConfig extends OkaeriConfig { - public Map currency = Collections.singletonMap("default", new CurrencyMessageConfig()); + public Map currency = + Collections.singletonMap("default", new CurrencyMessageConfig()); public String wait = "Please wait a moment before using this command again."; public String userNotFound = "User not found."; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingConfig.java index 217beca..2989b07 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingConfig.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.config.messaging; import dev.xhyrom.lighteco.common.messaging.MessagingType; + import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingDataConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingDataConfig.java index 124c62e..d3a880e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingDataConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/messaging/MessagingDataConfig.java @@ -9,6 +9,7 @@ public class MessagingDataConfig extends OkaeriConfig { @Comment("Credentials for connecting to the messaging service.") public String username = "root"; + public String password = "password"; @Comment("Whether to use SSL to connect to the messaging service.") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageConfig.java index 12897ce..7746351 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageConfig.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.config.storage; import dev.xhyrom.lighteco.common.storage.StorageType; + import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.annotation.Comment; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageDataConfig.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageDataConfig.java index 614eef6..80174e6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageDataConfig.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/storage/StorageDataConfig.java @@ -12,6 +12,7 @@ public class StorageDataConfig extends OkaeriConfig { @Comment("Credentials for connecting to the database.") public String username = "root"; + public String password = "password"; @Comment("Maximum number of connections in the pool.") diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/Dependency.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/Dependency.java index 0c8bf58..856ca2d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/Dependency.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/Dependency.java @@ -1,7 +1,9 @@ package dev.xhyrom.lighteco.common.dependencies; import com.google.common.collect.ImmutableList; + import dev.xhyrom.lighteco.common.dependencies.relocation.Relocation; + import lombok.Getter; import java.util.List; @@ -12,82 +14,40 @@ public enum Dependency { * Somewhere we use brackets instad of dots, so we need to rewrite them * This is because gradle's shadow plugin relocates using replacing full paths (dots) */ - - ASM( - "org.ow2.asm", - "asm", - "9.1" - ), - ASM_COMMONS( - "org.ow2.asm", - "asm-commons", - "9.1" - ), - JAR_RELOCATOR( - "me.lucko", - "jar-relocator", - "1.7" - ), - HIKARI( - "com{}zaxxer", - "HikariCP", - "5.0.1", - Relocation.of("hikari", "com{}zaxxer{}hikari") - ), - H2_DRIVER( - "com.h2database", - "h2", - "2.1.214" - ), - SQLITE_DRIVER( - "org.xerial", - "sqlite-jdbc", - "3.28.0" - ), + ASM("org.ow2.asm", "asm", "9.1"), + ASM_COMMONS("org.ow2.asm", "asm-commons", "9.1"), + JAR_RELOCATOR("me.lucko", "jar-relocator", "1.7"), + HIKARI("com{}zaxxer", "HikariCP", "5.0.1", Relocation.of("hikari", "com{}zaxxer{}hikari")), + H2_DRIVER("com.h2database", "h2", "2.1.214"), + SQLITE_DRIVER("org.xerial", "sqlite-jdbc", "3.28.0"), MARIADB_DRIVER( "org{}mariadb{}jdbc", "mariadb-java-client", "3.1.3", - Relocation.of("mariadb", "org{}mariadb{}jdbc") - ), - MYSQL_DRIVER( - "mysql", - "mysql-connector-java", - "8.0.23", - Relocation.of("mysql", "com{}mysql") - ), + Relocation.of("mariadb", "org{}mariadb{}jdbc")), + MYSQL_DRIVER("mysql", "mysql-connector-java", "8.0.23", Relocation.of("mysql", "com{}mysql")), POSTGRESQL_DRIVER( "org{}postgresql", "postgresql", "42.6.0", - Relocation.of("postgresql", "org{}postgresql") - ), + Relocation.of("postgresql", "org{}postgresql")), JEDIS( "redis.clients", "jedis", "5.1.0", Relocation.of("jedis", "redis{}clients{}jedis"), - Relocation.of("commonspool2", "org{}apache{}commons{}pool2") - ), - SLF4J_SIMPLE( - "org.slf4j", - "slf4j-simple", - "1.7.30" - ), - SLF4J_API( - "org.slf4j", - "slf4j-api", - "1.7.30" - ), + Relocation.of("commonspool2", "org{}apache{}commons{}pool2")), + SLF4J_SIMPLE("org.slf4j", "slf4j-simple", "1.7.30"), + SLF4J_API("org.slf4j", "slf4j-api", "1.7.30"), COMMONS_POOL_2( "org.apache.commons", "commons-pool2", "2.9.0", - Relocation.of("commonspool2", "org{}apache{}commons{}pool2") - ); + Relocation.of("commonspool2", "org{}apache{}commons{}pool2")); private final String fullPath; private final String version; + @Getter private final List relocations; @@ -98,13 +58,13 @@ public enum Dependency { } Dependency(String groupId, String artifactId, String version, Relocation... relocations) { - this.fullPath = String.format(MAVEN_FORMAT, + this.fullPath = String.format( + MAVEN_FORMAT, rewriteEscape(groupId).replace('.', '/'), rewriteEscape(artifactId), version, rewriteEscape(artifactId), - version - ); + version); this.version = version; this.relocations = ImmutableList.copyOf(relocations); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java index d7b044c..ad4ad1a 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManager.java @@ -9,6 +9,7 @@ public interface DependencyManager extends AutoCloseable { void loadDependencies(Set dependencies); void loadStorageDependencies(Set types); + void loadMessagingDependencies(Set types); ClassLoader obtainClassLoaderWith(Set dependencies); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java index 060837d..24c1a75 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyManagerImpl.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.dependencies; import com.google.common.collect.ImmutableSet; import com.google.common.io.MoreFiles; + import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.relocation.Relocation; import dev.xhyrom.lighteco.common.dependencies.relocation.RelocationHandler; @@ -10,6 +11,7 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.storage.StorageType; import dev.xhyrom.lighteco.common.util.URLClassLoaderAccess; + import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import java.io.IOException; @@ -35,7 +37,8 @@ public class DependencyManagerImpl implements DependencyManager { this.logger = plugin.getBootstrap().getLogger(); this.registry = new DependencyRegistry(); this.cacheDirectory = setupCacheDirectory(plugin); - this.classLoader = URLClassLoaderAccess.create((URLClassLoader) plugin.getBootstrap().getClass().getClassLoader()); + this.classLoader = URLClassLoaderAccess.create( + (URLClassLoader) plugin.getBootstrap().getClass().getClassLoader()); } private synchronized RelocationHandler getRelocationHandler() { @@ -48,29 +51,25 @@ public class DependencyManagerImpl implements DependencyManager { @Override public void loadDependencies(Set dependencies) { - if (this.config.debug) - this.logger.info("Loading dependencies: " + dependencies); + if (this.config.debug) this.logger.info("Loading dependencies: " + dependencies); for (Dependency dependency : dependencies) { if (this.loaded.containsKey(dependency)) { continue; } - if (this.config.debug) - this.logger.info("Loading dependency " + dependency); + if (this.config.debug) this.logger.info("Loading dependency " + dependency); try { loadDependency(dependency); } catch (Exception e) { throw new RuntimeException("Failed to load dependency " + dependency, e); } finally { - if (this.config.debug) - this.logger.info("Loaded dependency " + dependency); + if (this.config.debug) this.logger.info("Loaded dependency " + dependency); } } - if (this.config.debug) - this.logger.info("Loaded dependencies: " + dependencies); + if (this.config.debug) this.logger.info("Loaded dependencies: " + dependencies); } private void loadDependency(Dependency dependency) throws Exception { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java index 88a0587..afd229a 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/DependencyRegistry.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.dependencies; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.SetMultimap; + import dev.xhyrom.lighteco.common.messaging.MessagingType; import dev.xhyrom.lighteco.common.storage.StorageType; @@ -9,17 +10,24 @@ import java.util.LinkedHashSet; import java.util.Set; public class DependencyRegistry { - private static final SetMultimap STORAGE_DEPENDENCIES = ImmutableSetMultimap.builder() - .putAll(StorageType.SQLITE, Dependency.SQLITE_DRIVER) - .putAll(StorageType.H2, Dependency.H2_DRIVER) - .putAll(StorageType.MYSQL, Dependency.MYSQL_DRIVER, Dependency.HIKARI) - .putAll(StorageType.MARIADB, Dependency.MARIADB_DRIVER, Dependency.HIKARI) - .putAll(StorageType.POSTGRESQL, Dependency.POSTGRESQL_DRIVER, Dependency.HIKARI) - .build(); + private static final SetMultimap STORAGE_DEPENDENCIES = + ImmutableSetMultimap.builder() + .putAll(StorageType.SQLITE, Dependency.SQLITE_DRIVER) + .putAll(StorageType.H2, Dependency.H2_DRIVER) + .putAll(StorageType.MYSQL, Dependency.MYSQL_DRIVER, Dependency.HIKARI) + .putAll(StorageType.MARIADB, Dependency.MARIADB_DRIVER, Dependency.HIKARI) + .putAll(StorageType.POSTGRESQL, Dependency.POSTGRESQL_DRIVER, Dependency.HIKARI) + .build(); - private static final SetMultimap MESSAGING_DEPENDENCIES = ImmutableSetMultimap.builder() - .putAll(MessagingType.REDIS, Dependency.COMMONS_POOL_2, Dependency.JEDIS, Dependency.SLF4J_API, Dependency.SLF4J_SIMPLE) - .build(); + private static final SetMultimap MESSAGING_DEPENDENCIES = + ImmutableSetMultimap.builder() + .putAll( + MessagingType.REDIS, + Dependency.COMMONS_POOL_2, + Dependency.JEDIS, + Dependency.SLF4J_API, + Dependency.SLF4J_SIMPLE) + .build(); public Set resolveStorageDependencies(Set types) { Set dependencies = new LinkedHashSet<>(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/IsolatedClassLoader.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/IsolatedClassLoader.java index e9568ed..21f060d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/IsolatedClassLoader.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/IsolatedClassLoader.java @@ -11,5 +11,4 @@ public class IsolatedClassLoader extends URLClassLoader { public IsolatedClassLoader(URL[] urls) { super(urls, ClassLoader.getSystemClassLoader().getParent()); } - -} \ No newline at end of file +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/package-info.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/package-info.java index c4a2b90..42fb8da 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/package-info.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/package-info.java @@ -4,4 +4,4 @@ // Copyright (c) contributors // Under MIT License -package dev.xhyrom.lighteco.common.dependencies; \ No newline at end of file +package dev.xhyrom.lighteco.common.dependencies; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/Relocation.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/Relocation.java index e1679a0..a738532 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/Relocation.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/Relocation.java @@ -17,5 +17,4 @@ public class Relocation { this.pattern = pattern; this.relocatedPattern = relocatedPattern; } - } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/RelocationHandler.java b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/RelocationHandler.java index 15ff554..7e21f3e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/RelocationHandler.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/dependencies/relocation/RelocationHandler.java @@ -12,7 +12,8 @@ import java.nio.file.Path; import java.util.*; public class RelocationHandler { - public static final Set DEPENDENCIES = EnumSet.of(Dependency.ASM, Dependency.ASM_COMMONS, Dependency.JAR_RELOCATOR); + public static final Set DEPENDENCIES = + EnumSet.of(Dependency.ASM, Dependency.ASM_COMMONS, Dependency.JAR_RELOCATOR); private static final String JAR_RELOCATOR_CLASS = "me.lucko.jarrelocator.JarRelocator"; private static final String JAR_RELOCATOR_RUN_METHOD = "run"; @@ -31,10 +32,12 @@ public class RelocationHandler { Class jarRelocatorClass = classLoader.loadClass(JAR_RELOCATOR_CLASS); // prepare the reflected constructor & method instances - this.jarRelocatorConstructor = jarRelocatorClass.getDeclaredConstructor(File.class, File.class, Map.class); + this.jarRelocatorConstructor = + jarRelocatorClass.getDeclaredConstructor(File.class, File.class, Map.class); this.jarRelocatorConstructor.setAccessible(true); - this.jarRelocatorRunMethod = jarRelocatorClass.getDeclaredMethod(JAR_RELOCATOR_RUN_METHOD); + this.jarRelocatorRunMethod = + jarRelocatorClass.getDeclaredMethod(JAR_RELOCATOR_RUN_METHOD); this.jarRelocatorRunMethod.setAccessible(true); } catch (Exception e) { try { @@ -56,8 +59,8 @@ public class RelocationHandler { } // create and invoke a new relocator - Object relocator = this.jarRelocatorConstructor.newInstance(input.toFile(), output.toFile(), mappings); + Object relocator = + this.jarRelocatorConstructor.newInstance(input.toFile(), output.toFile(), mappings); this.jarRelocatorRunMethod.invoke(relocator); } - } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/Manager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/Manager.java index 7734c22..0cc55b7 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/Manager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/Manager.java @@ -6,6 +6,7 @@ public interface Manager { T apply(I identifier); Collection keys(); + Collection values(); T getOrMake(I identifier); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/CurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/CurrencyManager.java index c2675f9..ba1274e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/CurrencyManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/CurrencyManager.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.manager.currency; import dev.xhyrom.lighteco.common.manager.Manager; import dev.xhyrom.lighteco.common.model.currency.Currency; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Collection; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/StandardCurrencyManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/StandardCurrencyManager.java index f15f5d8..80f3058 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/StandardCurrencyManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/currency/StandardCurrencyManager.java @@ -3,11 +3,13 @@ package dev.xhyrom.lighteco.common.manager.currency; import dev.xhyrom.lighteco.common.manager.SingleManager; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Collection; -public class StandardCurrencyManager extends SingleManager implements CurrencyManager { +public class StandardCurrencyManager extends SingleManager + implements CurrencyManager { private final LightEcoPlugin plugin; public StandardCurrencyManager(LightEcoPlugin plugin) { @@ -32,10 +34,14 @@ public class StandardCurrencyManager extends SingleManager imp @Override public void registerCurrency(@NonNull Currency currency) { if (this.isLoaded(currency.getIdentifier())) - throw new IllegalArgumentException("Currency with identifier " + currency.getIdentifier() + " already registered"); + throw new IllegalArgumentException( + "Currency with identifier " + currency.getIdentifier() + " already registered"); if (this.plugin.getConfig().debug) - this.plugin.getBootstrap().getLogger().info("Registering currency " + currency.getIdentifier()); + this.plugin + .getBootstrap() + .getLogger() + .info("Registering currency " + currency.getIdentifier()); this.plugin.getStorage().registerCurrencySync(currency.getProxy()); this.map.put(currency.getIdentifier(), currency); 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 deff464..11610b2 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 @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.common.manager.user; import dev.xhyrom.lighteco.common.manager.ConcurrentManager; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import lombok.Getter; import java.util.Arrays; @@ -11,21 +12,26 @@ import java.util.concurrent.CompletableFuture; public class StandardUserManager extends ConcurrentManager implements UserManager { private final LightEcoPlugin plugin; + @Getter private final UserHousekeeper housekeeper; public StandardUserManager(LightEcoPlugin plugin) { this.plugin = plugin; - this.housekeeper = new UserHousekeeper(plugin, this, UserHousekeeper.timeoutSettings( - this.plugin.getConfig().housekeeper.expireAfterWrite, - this.plugin.getConfig().housekeeper.expireAfterWriteUnit - )); + this.housekeeper = new UserHousekeeper( + plugin, + this, + UserHousekeeper.timeoutSettings( + this.plugin.getConfig().housekeeper.expireAfterWrite, + this.plugin.getConfig().housekeeper.expireAfterWriteUnit)); - this.plugin.getBootstrap().getScheduler().asyncRepeating( - this.housekeeper, - this.plugin.getConfig().housekeeper.runInterval, - this.plugin.getConfig().housekeeper.runIntervalUnit - ); + this.plugin + .getBootstrap() + .getScheduler() + .asyncRepeating( + this.housekeeper, + this.plugin.getConfig().housekeeper.runInterval, + this.plugin.getConfig().housekeeper.runIntervalUnit); } @Override @@ -49,15 +55,15 @@ public class StandardUserManager extends ConcurrentManager implement @Override public CompletableFuture saveUser(User user) { - return this.plugin.getStorage().saveUser(user.getProxy()); + return this.plugin.getStorage().saveUser(user.getProxy()); } @Override public CompletableFuture saveUsers(User... users) { - return this.plugin.getStorage().saveUsers( - Arrays.stream(users) + return this.plugin + .getStorage() + .saveUsers(Arrays.stream(users) .map(User::getProxy) - .toArray(dev.xhyrom.lighteco.api.model.user.User[]::new) - ); + .toArray(dev.xhyrom.lighteco.api.model.user.User[]::new)); } -} \ No newline at end of file +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserHousekeeper.java b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserHousekeeper.java index 3b5c080..806cbc7 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserHousekeeper.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/manager/user/UserHousekeeper.java @@ -13,7 +13,8 @@ public class UserHousekeeper implements Runnable { private final ExpiringSet recentlyUsed; - public UserHousekeeper(LightEcoPlugin plugin, UserManager userManager, TimeoutSettings timeoutSettings) { + public UserHousekeeper( + LightEcoPlugin plugin, UserManager userManager, TimeoutSettings timeoutSettings) { this.plugin = plugin; this.userManager = userManager; this.recentlyUsed = new ExpiringSet<>(timeoutSettings.duration, timeoutSettings.unit); @@ -41,8 +42,7 @@ public class UserHousekeeper implements Runnable { } // If the user is dirty (has unsaved changes), don't unload - if (user.isDirty()) - return; + if (user.isDirty()) return; if (this.plugin.getConfig().debug) { this.plugin.getBootstrap().getLogger().info("Unloading data for " + uuid); @@ -64,4 +64,4 @@ public class UserHousekeeper implements Runnable { this.unit = unit; } } -} \ No newline at end of file +} 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 3d50fcc..f1f4837 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 @@ -10,8 +10,10 @@ public interface UserManager extends Manager { UserHousekeeper getHousekeeper(); CompletableFuture saveUser(User user); + CompletableFuture saveUsers(User... users); CompletableFuture loadUser(UUID uniqueId); + CompletableFuture loadUser(UUID uniqueId, String username); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java index 745a38d..876fee2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/LightEcoMessagingService.java @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.common.messaging; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; + import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; import dev.xhyrom.lighteco.api.messenger.Messenger; import dev.xhyrom.lighteco.api.messenger.MessengerProvider; @@ -15,7 +16,9 @@ import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.util.gson.GsonProvider; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -25,6 +28,7 @@ import java.util.concurrent.TimeUnit; public class LightEcoMessagingService implements InternalMessagingService, IncomingMessageConsumer { @Getter private final LightEcoPlugin plugin; + private final ExpiringSet receivedMessages; private final Messenger messenger; @@ -47,12 +51,18 @@ public class LightEcoMessagingService implements InternalMessagingService, Incom @Override public void pushUserUpdate(User user, Currency currency) { - this.plugin.getBootstrap().getScheduler().async().execute(() -> - this.messenger.sendOutgoingMessage( - new UserUpdateMessageImpl(generateMessageId(), user.getUniqueId(), currency.getIdentifier(), user.getBalance(currency)), - currency.getType() == dev.xhyrom.lighteco.api.model.currency.Currency.Type.GLOBAL - ) - ); + this.plugin + .getBootstrap() + .getScheduler() + .async() + .execute(() -> this.messenger.sendOutgoingMessage( + new UserUpdateMessageImpl( + generateMessageId(), + user.getUniqueId(), + currency.getIdentifier(), + user.getBalance(currency)), + currency.getType() + == dev.xhyrom.lighteco.api.model.currency.Currency.Type.GLOBAL)); } public static @NonNull String serialize(MessageType type, UUID id, JsonElement content) { @@ -79,7 +89,10 @@ public class LightEcoMessagingService implements InternalMessagingService, Incom try { deserializeAndConsumeRawIncomingMessage(message); } catch (Exception e) { - this.plugin.getBootstrap().getLogger().warn("Failed to deserialize incoming message: " + message, e); + this.plugin + .getBootstrap() + .getLogger() + .warn("Failed to deserialize incoming message: " + message, e); } } @@ -121,12 +134,16 @@ public class LightEcoMessagingService implements InternalMessagingService, Incom private void processIncomingMessage(Message message) { if (message instanceof UserUpdateMessage userUpdateMessage) { this.plugin.getBootstrap().getScheduler().async().execute(() -> { - User user = this.plugin.getUserManager().getIfLoaded(userUpdateMessage.getUserUniqueId()); + User user = this.plugin + .getUserManager() + .getIfLoaded(userUpdateMessage.getUserUniqueId()); if (user == null) { return; } - Currency currency = this.plugin.getCurrencyManager().getIfLoaded(userUpdateMessage.getCurrencyIdentifier()); + Currency currency = this.plugin + .getCurrencyManager() + .getIfLoaded(userUpdateMessage.getCurrencyIdentifier()); if (currency == null) { return; } @@ -134,7 +151,8 @@ public class LightEcoMessagingService implements InternalMessagingService, Incom user.setBalance(currency, userUpdateMessage.getNewBalance(), false, false); }); } else { - throw new IllegalStateException("Unknown message type: " + message.getClass().getName()); + throw new IllegalStateException( + "Unknown message type: " + message.getClass().getName()); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java index e8c9cf2..7fa5059 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/AbstractMessage.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.messaging.message; import dev.xhyrom.lighteco.api.messenger.message.Message; import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.UUID; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java index 1604865..cdadf8d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/message/UserUpdateMessageImpl.java @@ -3,9 +3,12 @@ package dev.xhyrom.lighteco.common.messaging.message; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; + import dev.xhyrom.lighteco.api.messenger.message.type.UserUpdateMessage; import dev.xhyrom.lighteco.common.messaging.LightEcoMessagingService; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; @@ -16,8 +19,10 @@ public class UserUpdateMessageImpl extends AbstractMessage implements UserUpdate @Getter private final UUID userUniqueId; + @Getter private final String currencyIdentifier; + @Getter private final BigDecimal newBalance; @@ -30,7 +35,8 @@ public class UserUpdateMessageImpl extends AbstractMessage implements UserUpdate return new UserUpdateMessageImpl(id, userUniqueId, currencyIdentifier, newBalance); } - public UserUpdateMessageImpl(UUID id, UUID userUniqueId, String currencyIdentifier, BigDecimal newBalance) { + public UserUpdateMessageImpl( + UUID id, UUID userUniqueId, String currencyIdentifier, BigDecimal newBalance) { super(id); this.userUniqueId = userUniqueId; this.currencyIdentifier = currencyIdentifier; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java index 82fe986..3c5162d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessenger.java @@ -4,13 +4,17 @@ import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; import dev.xhyrom.lighteco.api.messenger.Messenger; import dev.xhyrom.lighteco.api.messenger.message.OutgoingMessage; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; + import redis.clients.jedis.*; public class RedisMessenger implements Messenger { private static final String CHANNEL = "lighteco:{}:messages"; + @Getter private final String[] channels; @@ -25,16 +29,13 @@ public class RedisMessenger implements Messenger { this.consumer = consumer; this.channels = new String[] { - CHANNEL.replace("{}:", ""), - CHANNEL.replace("{}", this.plugin.getConfig().server) + CHANNEL.replace("{}:", ""), CHANNEL.replace("{}", this.plugin.getConfig().server) }; } - public void init(@Nullable String address, @Nullable String username, String password, boolean ssl) { - this.init(new JedisPooled( - parseAddress(address), - jedisConfig(username, password, ssl) - )); + public void init( + @Nullable String address, @Nullable String username, String password, boolean ssl) { + this.init(new JedisPooled(parseAddress(address), jedisConfig(username, password, ssl))); } private void init(UnifiedJedis jedis) { @@ -46,7 +47,8 @@ public class RedisMessenger implements Messenger { }); } - private static JedisClientConfig jedisConfig(@Nullable String username, @Nullable String password, boolean ssl) { + private static JedisClientConfig jedisConfig( + @Nullable String username, @Nullable String password, boolean ssl) { return DefaultJedisClientConfig.builder() .user(username) .password(password) @@ -58,7 +60,8 @@ public class RedisMessenger implements Messenger { private static HostAndPort parseAddress(String address) { String[] addressSplit = address.split(":"); String host = addressSplit[0]; - int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT; + int port = + addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : Protocol.DEFAULT_PORT; return new HostAndPort(host, port); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java index 2257453..8448456 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/messaging/type/redis/RedisMessengerProvider.java @@ -4,6 +4,7 @@ import dev.xhyrom.lighteco.api.messenger.IncomingMessageConsumer; import dev.xhyrom.lighteco.api.messenger.Messenger; import dev.xhyrom.lighteco.api.messenger.MessengerProvider; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; public class RedisMessengerProvider implements MessengerProvider { 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 index b3f8cbd..8bec875 100644 --- 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 @@ -1,9 +1,9 @@ package dev.xhyrom.lighteco.common.model.chat; public abstract class AbstractCommandSender implements CommandSender { - protected final T delegate; + protected final T delegate; - protected AbstractCommandSender(T delegate) { - this.delegate = delegate; - } + protected 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 index 63b197a..4bda806 100644 --- 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 @@ -1,15 +1,18 @@ package dev.xhyrom.lighteco.common.model.chat; import net.kyori.adventure.text.Component; + import org.checkerframework.checker.nullness.qual.Nullable; import java.util.UUID; public interface CommandSender { String getUsername(); + @Nullable UUID getUniqueId(); boolean eligible(String permission); + void sendMessage(Component message); default boolean isConsole() { 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 c3544f3..6460d03 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 @@ -7,9 +7,12 @@ import dev.xhyrom.lighteco.common.cache.RedisBackedMap; import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import lombok.Getter; import lombok.Setter; + import net.kyori.adventure.text.Component; + import org.checkerframework.checker.nullness.qual.NonNull; import java.math.BigDecimal; @@ -31,6 +34,7 @@ public class User { @Getter @Setter private boolean dirty = false; + @Getter @Setter private String username; @@ -51,54 +55,63 @@ public class User { return balances.getOrDefault(currency, currency.getDefaultBalance()); } - public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) throws CannotBeNegative, CannotBeGreaterThan { + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance) + throws CannotBeNegative, CannotBeGreaterThan { this.setBalance(currency, balance, false, true); } - public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force) throws CannotBeNegative, CannotBeGreaterThan { + public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force) + throws CannotBeNegative, CannotBeGreaterThan { this.setBalance(currency, balance, force, true); } - public void setBalance(@NonNull Currency currency, @NonNull BigDecimal balance, boolean force, boolean publish) throws CannotBeNegative, CannotBeGreaterThan { + public void setBalance( + @NonNull Currency currency, @NonNull BigDecimal balance, boolean force, boolean publish) + throws CannotBeNegative, CannotBeGreaterThan { if (balance.compareTo(BigDecimal.ZERO) < 0) { throw new CannotBeNegative("Balance cannot be negative"); } if (balance.compareTo(this.plugin.getConfig().maximumBalance) > 0) { - throw new CannotBeGreaterThan("Balance cannot be greater than " + this.plugin.getConfig().maximumBalance); + throw new CannotBeGreaterThan( + "Balance cannot be greater than " + this.plugin.getConfig().maximumBalance); } balance = balance.setScale(currency.fractionalDigits(), RoundingMode.DOWN); balances.put(currency, balance); - if (!force) - this.setDirty(true); + if (!force) this.setDirty(true); if (publish) { @NonNull Optional messagingService = this.plugin.getMessagingService(); - messagingService.ifPresent(internalMessagingService -> internalMessagingService.pushUserUpdate(this, currency)); + messagingService.ifPresent(internalMessagingService -> + internalMessagingService.pushUserUpdate(this, currency)); } } - public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) throws CannotBeNegative, CannotBeGreaterThan { + public void deposit(@NonNull Currency currency, @NonNull BigDecimal amount) + throws CannotBeNegative, CannotBeGreaterThan { if (amount.compareTo(BigDecimal.ZERO) < 0) { throw new IllegalArgumentException("Amount cannot be negative"); } if (amount.compareTo(this.plugin.getConfig().maximumBalance) > 0) { - throw new CannotBeGreaterThan("Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); + throw new CannotBeGreaterThan( + "Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); } this.setBalance(currency, this.getBalance(currency).add(amount)); } - public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) throws CannotBeNegative, CannotBeGreaterThan { + public void withdraw(@NonNull Currency currency, @NonNull BigDecimal amount) + throws CannotBeNegative, CannotBeGreaterThan { if (amount.compareTo(BigDecimal.ZERO) < 0) { throw new IllegalArgumentException("Amount cannot be negative"); } if (amount.compareTo(this.plugin.getConfig().maximumBalance) > 0) { - throw new CannotBeGreaterThan("Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); + throw new CannotBeGreaterThan( + "Amount cannot be greater than " + this.plugin.getConfig().maximumBalance); } if (this.getBalance(currency).compareTo(amount) < 0) { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/package-info.java b/common/src/main/java/dev/xhyrom/lighteco/common/package-info.java index 38a7213..0b5a2b2 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/package-info.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/package-info.java @@ -1,4 +1,4 @@ /** * Common classes used by all platform-specific implementations. */ -package dev.xhyrom.lighteco.common; \ No newline at end of file +package dev.xhyrom.lighteco.common; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index 639716c..7c1fa09 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -6,14 +6,17 @@ import dev.xhyrom.lighteco.common.api.LightEcoApi; import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.DependencyManager; import dev.xhyrom.lighteco.common.dependencies.DependencyManagerImpl; -import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; +import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.storage.Storage; import dev.xhyrom.lighteco.common.storage.StorageFactory; import dev.xhyrom.lighteco.common.task.UserSaveTask; + import eu.okaeri.configs.ConfigManager; import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Optional; @@ -22,11 +25,13 @@ import java.util.concurrent.TimeUnit; @Getter public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { private DependencyManager dependencyManager; + @Getter private Config config; @Getter private Storage storage; + private InternalMessagingService messagingService; private LightEcoApi api; @@ -47,14 +52,10 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { public final void enable() { // setup storage StorageFactory storageFactory = new StorageFactory(this); - this.dependencyManager.loadStorageDependencies( - storageFactory.getRequiredTypes() - ); + this.dependencyManager.loadStorageDependencies(storageFactory.getRequiredTypes()); MessagingFactory messagingFactory = this.getMessagingFactory(); - this.dependencyManager.loadMessagingDependencies( - messagingFactory.getRequiredTypes() - ); + this.dependencyManager.loadMessagingDependencies(messagingFactory.getRequiredTypes()); this.storage = storageFactory.get(); this.messagingService = messagingFactory.get(); @@ -77,7 +78,9 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { this.registerApiOnPlatform(this.api); this.userSaveTask = new UserSaveTask(this); - this.getBootstrap().getScheduler().asyncRepeating(userSaveTask, this.config.saveInterval, TimeUnit.SECONDS); + this.getBootstrap() + .getScheduler() + .asyncRepeating(userSaveTask, this.config.saveInterval, TimeUnit.SECONDS); } public final void disable() { @@ -90,8 +93,7 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { // shutdown storage this.storage.shutdown(); - if (this.messagingService != null) - this.messagingService.shutdown(); + if (this.messagingService != null) this.messagingService.shutdown(); // close isolated class loaders this.dependencyManager.close(); @@ -100,10 +102,13 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { protected abstract void registerListeners(); protected abstract void setupManagers(); + protected abstract MessagingFactory getMessagingFactory(); + protected abstract void registerApiOnPlatform(LightEco api); protected abstract void registerPlatformHooks(); + protected abstract void removePlatformHooks(); @Override diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java index 3953c3c..ee2f0c6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/LightEcoPlugin.java @@ -10,6 +10,7 @@ import dev.xhyrom.lighteco.common.manager.user.UserManager; import dev.xhyrom.lighteco.common.messaging.InternalMessagingService; import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.storage.Storage; + import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Optional; @@ -20,9 +21,13 @@ public interface LightEcoPlugin { @NonNull LightEcoBootstrap getBootstrap(); @NonNull Config getConfig(); + @NonNull UserManager getUserManager(); + @NonNull CurrencyManager getCurrencyManager(); + @NonNull CommandManager getCommandManager(); + @NonNull ContextManager getContextManager(); @NonNull DependencyManager getDependencyManager(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java index 0e349a8..4bb69ed 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LightEcoBootstrap.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.plugin.bootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; + import net.kyori.adventure.audience.Audience; import java.io.InputStream; @@ -12,15 +13,22 @@ import java.util.UUID; public interface LightEcoBootstrap { Object getLoader(); + PluginLogger getLogger(); + SchedulerAdapter getScheduler(); + Path getDataDirectory(); + String getVersion(); Optional lookupUniqueId(String username); + boolean isPlayerOnline(UUID uniqueId); + List getOnlinePlayers(); InputStream getResourceStream(String filename); + Audience getPlayerAudience(UUID uniqueId); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LoaderBootstrap.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LoaderBootstrap.java index b2c7858..a05e4d3 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LoaderBootstrap.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/bootstrap/LoaderBootstrap.java @@ -2,6 +2,8 @@ package dev.xhyrom.lighteco.common.plugin.bootstrap; public interface LoaderBootstrap { void onLoad(); + void onEnable(); + void onDisable(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java index 3f0c578..83f2f84 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/logger/PluginLogger.java @@ -2,20 +2,26 @@ package dev.xhyrom.lighteco.common.plugin.logger; public interface PluginLogger { void info(String message); - void info(String message, Object ...args); + + void info(String message, Object... args); void debug(String message); - void debug(String message, Object ...args); + + void debug(String message, Object... args); void warn(String message); - void warn(String message, Object ...args); + + void warn(String message, Object... args); void warn(String message, Throwable throwable); - void warn(String message, Throwable throwable, Object ...args); + + void warn(String message, Throwable throwable, Object... args); void error(String message); - void error(String message, Object ...args); + + void error(String message, Object... args); void error(String message, Throwable throwable); - void error(String message, Throwable throwable, Object ...args); + + void error(String message, Throwable throwable, Object... args); } 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 8faae9a..f397c9a 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 @@ -75,7 +75,8 @@ public class Storage { } return future(() -> this.provider.loadUser(uniqueId, username)) - .thenApply(apiUser -> this.plugin.getUserManager().getIfLoaded(apiUser.getUniqueId())); + .thenApply( + apiUser -> this.plugin.getUserManager().getIfLoaded(apiUser.getUniqueId())); } public CompletableFuture saveUser(dev.xhyrom.lighteco.api.model.user.User user) { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java index 9c774e1..e2d51d0 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/StorageFactory.java @@ -37,25 +37,27 @@ public class StorageFactory { case MEMORY -> new MemoryStorageProvider(this.plugin); case H2 -> new SqlStorageProvider( this.plugin, - new H2ConnectionFactory(this.plugin.getBootstrap().getDataDirectory().resolve("lighteco-h2").toAbsolutePath()) - ); + new H2ConnectionFactory(this.plugin + .getBootstrap() + .getDataDirectory() + .resolve("lighteco-h2") + .toAbsolutePath())); case SQLITE -> new SqlStorageProvider( this.plugin, - new SqliteConnectionFactory(this.plugin.getBootstrap().getDataDirectory().resolve("lighteco-sqlite.db")) - ); + new SqliteConnectionFactory(this.plugin + .getBootstrap() + .getDataDirectory() + .resolve("lighteco-sqlite.db"))); case MYSQL -> new SqlStorageProvider( - this.plugin, - new MySQLConnectionFactory(this.plugin.getConfig().storage.data) - ); + this.plugin, new MySQLConnectionFactory(this.plugin.getConfig().storage.data)); case MARIADB -> new SqlStorageProvider( this.plugin, - new MariaDBConnectionFactory(this.plugin.getConfig().storage.data) - ); + new MariaDBConnectionFactory(this.plugin.getConfig().storage.data)); case POSTGRESQL -> new SqlStorageProvider( this.plugin, - new PostgreSQLConnectionFactory(this.plugin.getConfig().storage.data) - ); - default -> throw new IllegalArgumentException("Unknown storage provider: " + type.name()); + new PostgreSQLConnectionFactory(this.plugin.getConfig().storage.data)); + default -> throw new IllegalArgumentException( + "Unknown storage provider: " + type.name()); }; } } 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 0658bf1..e6d9141 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 @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.common.storage.provider.memory; import dev.xhyrom.lighteco.api.model.user.User; import dev.xhyrom.lighteco.api.storage.StorageProvider; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -13,6 +14,7 @@ public class MemoryStorageProvider implements StorageProvider { private HashMap userDatabase; private final LightEcoPlugin plugin; + public MemoryStorageProvider(LightEcoPlugin plugin) { this.plugin = plugin; } @@ -49,9 +51,9 @@ public class MemoryStorageProvider implements StorageProvider { } private User createUser(UUID uniqueId, String username, User data) { - dev.xhyrom.lighteco.common.model.user.User user = this.plugin.getUserManager().getOrMake(uniqueId); - if (username != null) - user.setUsername(username); + dev.xhyrom.lighteco.common.model.user.User user = + this.plugin.getUserManager().getOrMake(uniqueId); + if (username != null) user.setUsername(username); return user.getProxy(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/package-info.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/package-info.java index bf9dc2e..151237b 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/package-info.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/package-info.java @@ -4,4 +4,4 @@ // Copyright (c) contributors // Under MIT License -package dev.xhyrom.lighteco.common.storage.provider; \ No newline at end of file +package dev.xhyrom.lighteco.common.storage.provider; diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java index 1577d8c..f2b9075 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStatements.java @@ -7,26 +7,22 @@ public enum SqlStatements { "INSERT INTO '{prefix}_local_{context}_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;", "INSERT INTO '{prefix}_local_{context}_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON DUPLICATE KEY UPDATE balance=?2;", "INSERT INTO '{prefix}_local_{context}_{currency}_users' (uuid, balance) VALUES (?, ?) ON DUPLICATE KEY UPDATE balance=?;", - "INSERT INTO '{prefix}_local_{context}_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;" - ), + "INSERT INTO '{prefix}_local_{context}_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;"), SAVE_USER_GLOBAL_CURRENCY( "INSERT INTO '{prefix}_global_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;", "INSERT INTO '{prefix}_global_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON DUPLICATE KEY UPDATE balance=?2;", "INSERT INTO '{prefix}_global_{currency}_users' (uuid, balance) VALUES (?, ?) ON DUPLICATE KEY UPDATE balance=?;", - "INSERT INTO '{prefix}_global_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;" - ), + "INSERT INTO '{prefix}_global_{currency}_users' (uuid, balance) VALUES (?1, ?2) ON CONFLICT (uuid) DO UPDATE SET balance=?2;"), LOAD_LOCAL_CURRENCY_USER( "SELECT {identifier} AS name, balance FROM '{prefix}_local_{context}_{currency}_users' WHERE uuid = ?1", "SELECT {identifier} AS name, balance FROM '{prefix}_local_{context}_{currency}_users' WHERE uuid = ?1", "SELECT {identifier} AS name, balance FROM '{prefix}_local_{context}_{currency}_users' WHERE uuid = ?", - "SELECT {identifier} AS name, balance FROM '{prefix}_local_{context}_{currency}_users' WHERE uuid = ?1" - ), + "SELECT {identifier} AS name, balance FROM '{prefix}_local_{context}_{currency}_users' WHERE uuid = ?1"), LOAD_GLOBAL_CURRENCY_USER( "SELECT {identifier} AS name, balance FROM '{prefix}_global_{currency}_users' WHERE uuid = ?1", "SELECT {identifier} AS name, balance FROM '{prefix}_global_{currency}_users' WHERE uuid = ?1", "SELECT {identifier} AS name, balance FROM '{prefix}_global_{currency}_users' WHERE uuid = ?", - "SELECT {identifier} AS name, balance FROM '{prefix}_global_{currency}_users' WHERE uuid = ?1" - ); + "SELECT {identifier} AS name, balance FROM '{prefix}_global_{currency}_users' WHERE uuid = ?1"); public final String sqlite; public final String mysql; @@ -54,11 +50,13 @@ public enum SqlStatements { case POSTGRESQL -> { return this.postgresql; } - default -> throw new IllegalArgumentException("Unknown implementation: " + implementationName); + default -> throw new IllegalArgumentException( + "Unknown implementation: " + implementationName); } } public static boolean mustDuplicateParameters(StorageType implementationName) { - return implementationName == StorageType.MARIADB || implementationName == StorageType.POSTGRESQL; + return implementationName == StorageType.MARIADB + || implementationName == StorageType.POSTGRESQL; } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java index 1240330..d7a99a6 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/SqlStorageProvider.java @@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.storage.StorageType; import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory; + import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -25,15 +26,19 @@ public class SqlStorageProvider implements StorageProvider { private static String LOAD_LOCAL_CURRENCY_USER; private static String LOAD_GLOBAL_CRRENCY_USER; - private static final String DELETE_LOCAL_USER = "DELETE FROM {prefix}_local_{context}_{currency}_users WHERE uuid = ?;"; - private static final String DELETE_GLOBAL_USER = "DELETE FROM {prefix}_global_{currency}_users WHERE uuid = ?;"; - private static final String CREATE_TABLE = """ + private static final String DELETE_LOCAL_USER = + "DELETE FROM {prefix}_local_{context}_{currency}_users WHERE uuid = ?;"; + private static final String DELETE_GLOBAL_USER = + "DELETE FROM {prefix}_global_{currency}_users WHERE uuid = ?;"; + private static final String CREATE_TABLE = + """ CREATE TABLE IF NOT EXISTS '{prefix}_{table}' ( 'uuid' VARCHAR(36) NOT NULL, 'balance' DECIMAL(20, 2) NOT NULL, PRIMARY KEY (`uuid`) ); - """.trim(); + """ + .trim(); private final LightEcoPlugin plugin; private final ConnectionFactory connectionFactory; @@ -42,11 +47,9 @@ public class SqlStorageProvider implements StorageProvider { public SqlStorageProvider(LightEcoPlugin plugin, ConnectionFactory connectionFactory) { this.plugin = plugin; this.connectionFactory = connectionFactory; - this.statementProcessor = connectionFactory.getStatementProcessor().compose( - s -> s - .replace("{prefix}", plugin.getConfig().storage.tablePrefix) - .replace("{context}", plugin.getConfig().server) - ); + this.statementProcessor = connectionFactory.getStatementProcessor().compose(s -> s.replace( + "{prefix}", plugin.getConfig().storage.tablePrefix) + .replace("{context}", plugin.getConfig().server)); final StorageType implementationName = this.connectionFactory.getImplementationName(); SAVE_USER_LOCAL_CURRENCY = SqlStatements.SAVE_USER_LOCAL_CURRENCY.get(implementationName); @@ -66,7 +69,8 @@ public class SqlStorageProvider implements StorageProvider { } @Override - public void registerCurrency(dev.xhyrom.lighteco.api.model.currency.@NonNull Currency currency) throws Exception { + public void registerCurrency(dev.xhyrom.lighteco.api.model.currency.@NonNull Currency currency) + throws Exception { StringBuilder tableName = new StringBuilder(); if (currency.getType() == dev.xhyrom.lighteco.api.model.currency.Currency.Type.LOCAL) { @@ -80,41 +84,39 @@ public class SqlStorageProvider implements StorageProvider { tableName.append("_users"); try (Connection c = this.connectionFactory.getConnection()) { - try (PreparedStatement ps = c.prepareStatement( - this.statementProcessor.apply(CREATE_TABLE - .replace("{table}", tableName.toString()) - ) - )) { + try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply( + CREATE_TABLE.replace("{table}", tableName.toString())))) { ps.execute(); } } } @Override - public @NonNull User loadUser(@NonNull UUID uniqueId, @Nullable String username) throws Exception { + public @NonNull User loadUser(@NonNull UUID uniqueId, @Nullable String username) + throws Exception { String uniqueIdString = uniqueId.toString(); - dev.xhyrom.lighteco.common.model.user.User user = this.plugin.getUserManager().getOrMake(uniqueId); - if (username != null) - user.setUsername(username); + dev.xhyrom.lighteco.common.model.user.User user = + this.plugin.getUserManager().getOrMake(uniqueId); + if (username != null) user.setUsername(username); StringBuilder query = new StringBuilder(); - List currencies = this.plugin.getCurrencyManager().getRegisteredCurrencies().stream().toList(); + List currencies = + this.plugin.getCurrencyManager().getRegisteredCurrencies().stream() + .toList(); int size = this.plugin.getCurrencyManager().getRegisteredCurrencies().size(); for (int i = 0; i < size; i++) { Currency currency = currencies.get(i); switch (currency.getType()) { - case GLOBAL -> query.append( - this.statementProcessor.apply(LOAD_GLOBAL_CRRENCY_USER - .replace("{currency}", currency.getIdentifier()) - ).replace("{identifier}", "'"+currency.getIdentifier()+"'") - ); - case LOCAL -> query.append( - this.statementProcessor.apply(LOAD_LOCAL_CURRENCY_USER - .replace("{currency}", currency.getIdentifier()) - ).replace("{identifier}", "'"+currency.getIdentifier()+"'") - ); + case GLOBAL -> query.append(this.statementProcessor + .apply(LOAD_GLOBAL_CRRENCY_USER.replace( + "{currency}", currency.getIdentifier())) + .replace("{identifier}", "'" + currency.getIdentifier() + "'")); + case LOCAL -> query.append(this.statementProcessor + .apply(LOAD_LOCAL_CURRENCY_USER.replace( + "{currency}", currency.getIdentifier())) + .replace("{identifier}", "'" + currency.getIdentifier() + "'")); } if (i != size - 1) { @@ -124,7 +126,8 @@ public class SqlStorageProvider implements StorageProvider { try (Connection c = this.connectionFactory.getConnection()) { try (PreparedStatement ps = c.prepareStatement(query.toString())) { - if (SqlStatements.mustDuplicateParameters(this.connectionFactory.getImplementationName())) { + if (SqlStatements.mustDuplicateParameters( + this.connectionFactory.getImplementationName())) { for (int i = 0; i < size; i++) { ps.setString(i + 1, uniqueIdString); } @@ -186,9 +189,9 @@ public class SqlStorageProvider implements StorageProvider { saveBalances(c, user, uniqueIdString, true); } - private void saveBalances(Connection c, User user, String uniqueIdString, boolean transactions) throws SQLException { - if (transactions) - c.setAutoCommit(false); + private void saveBalances(Connection c, User user, String uniqueIdString, boolean transactions) + throws SQLException { + if (transactions) c.setAutoCommit(false); for (Currency currency : this.plugin.getCurrencyManager().getRegisteredCurrencies()) { BigDecimal balance = user.getBalance(currency.getProxy()); @@ -196,16 +199,18 @@ public class SqlStorageProvider implements StorageProvider { if (balance.compareTo(BigDecimal.ZERO) == 0) { switch (currency.getType()) { case GLOBAL -> { - try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(DELETE_GLOBAL_USER - .replace("{currency}", currency.getIdentifier())))) { + try (PreparedStatement ps = c.prepareStatement( + this.statementProcessor.apply(DELETE_GLOBAL_USER.replace( + "{currency}", currency.getIdentifier())))) { ps.setString(1, uniqueIdString); ps.execute(); } } case LOCAL -> { - try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(DELETE_LOCAL_USER - .replace("{currency}", currency.getIdentifier())))) { + try (PreparedStatement ps = c.prepareStatement( + this.statementProcessor.apply(DELETE_LOCAL_USER.replace( + "{currency}", currency.getIdentifier())))) { ps.setString(1, uniqueIdString); ps.execute(); @@ -218,22 +223,26 @@ public class SqlStorageProvider implements StorageProvider { switch (currency.getType()) { case GLOBAL -> { - try (PreparedStatement ps = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_GLOBAL_CURRENCY - .replace("{currency}", currency.getIdentifier())))) { + try (PreparedStatement ps = c.prepareStatement( + this.statementProcessor.apply(SAVE_USER_GLOBAL_CURRENCY.replace( + "{currency}", currency.getIdentifier())))) { ps.setString(1, uniqueIdString); ps.setBigDecimal(2, balance); - if (SqlStatements.mustDuplicateParameters(this.connectionFactory.getImplementationName())) + if (SqlStatements.mustDuplicateParameters( + this.connectionFactory.getImplementationName())) ps.setBigDecimal(3, balance); ps.execute(); } } case LOCAL -> { - try (PreparedStatement psLocal = c.prepareStatement(this.statementProcessor.apply(SAVE_USER_LOCAL_CURRENCY - .replace("{currency}", currency.getIdentifier())))) { + try (PreparedStatement psLocal = c.prepareStatement( + this.statementProcessor.apply(SAVE_USER_LOCAL_CURRENCY.replace( + "{currency}", currency.getIdentifier())))) { psLocal.setString(1, uniqueIdString); psLocal.setBigDecimal(2, balance); - if (SqlStatements.mustDuplicateParameters(this.connectionFactory.getImplementationName())) + if (SqlStatements.mustDuplicateParameters( + this.connectionFactory.getImplementationName())) psLocal.setBigDecimal(3, balance); psLocal.execute(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java index c44ebdc..cb09105 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/ConnectionFactory.java @@ -10,6 +10,7 @@ public interface ConnectionFactory { StorageType getImplementationName(); void init(LightEcoPlugin plugin); + void shutdown() throws Exception; Function getStatementProcessor(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java index 46b99b1..34739c3 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java @@ -26,11 +26,13 @@ public class H2ConnectionFactory extends FileConnectionFactory { @Override public void init(LightEcoPlugin plugin) { - ClassLoader classLoader = plugin.getDependencyManager().obtainClassLoaderWith(EnumSet.of(Dependency.H2_DRIVER)); + ClassLoader classLoader = plugin.getDependencyManager() + .obtainClassLoaderWith(EnumSet.of(Dependency.H2_DRIVER)); try { Class connectionClass = classLoader.loadClass("org.h2.jdbc.JdbcConnection"); - this.connectionConstructor = connectionClass.getConstructor(String.class, Properties.class, String.class, Object.class, boolean.class); + this.connectionConstructor = connectionClass.getConstructor( + String.class, Properties.class, String.class, Object.class, boolean.class); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } @@ -42,8 +44,9 @@ public class H2ConnectionFactory extends FileConnectionFactory { return (Connection) this.connectionConstructor.newInstance( "jdbc:h2:" + file.toString() + ";MODE=MySQL;DATABASE_TO_LOWER=TRUE", new Properties(), - null, null, false - ); + null, + null, + false); } catch (Exception e) { if (e.getCause() instanceof SQLException) { throw (SQLException) e.getCause(); @@ -55,8 +58,7 @@ public class H2ConnectionFactory extends FileConnectionFactory { @Override public Function getStatementProcessor() { - return s -> s - .replace('\'', '`') + return s -> s.replace('\'', '`') .replace("LIKE", "ILIKE") .replace("value", "`value`") .replace("``value``", "`value`"); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java index 10fa8ba..3aeb891 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java @@ -26,11 +26,13 @@ public class SqliteConnectionFactory extends FileConnectionFactory { @Override public void init(LightEcoPlugin plugin) { - ClassLoader classLoader = plugin.getDependencyManager().obtainClassLoaderWith(EnumSet.of(Dependency.SQLITE_DRIVER)); + ClassLoader classLoader = plugin.getDependencyManager() + .obtainClassLoaderWith(EnumSet.of(Dependency.SQLITE_DRIVER)); try { Class connectionClass = classLoader.loadClass("org.sqlite.jdbc4.JDBC4Connection"); - this.connectionConstructor = connectionClass.getConstructor(String.class, String.class, Properties.class); + this.connectionConstructor = + connectionClass.getConstructor(String.class, String.class, Properties.class); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } @@ -40,10 +42,7 @@ public class SqliteConnectionFactory extends FileConnectionFactory { protected Connection createConnection(Path file) throws SQLException { try { return (Connection) this.connectionConstructor.newInstance( - "jdbc:sqlite:" + file, - file.toString(), - new Properties() - ); + "jdbc:sqlite:" + file, file.toString(), new Properties()); } catch (Exception e) { if (e.getCause() instanceof SQLException) { throw (SQLException) e.getCause(); diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/DriverBasedHikariConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/DriverBasedHikariConnectionFactory.java index 45bb553..319cb09 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/DriverBasedHikariConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/DriverBasedHikariConnectionFactory.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection.hikari; import com.zaxxer.hikari.HikariConfig; + import dev.xhyrom.lighteco.common.config.storage.StorageDataConfig; import java.sql.Driver; @@ -18,9 +19,16 @@ public abstract class DriverBasedHikariConnectionFactory extends HikariConnectio protected abstract String driverJdbcIdentifier(); @Override - protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) { + protected void configureDatabase( + HikariConfig config, + String address, + String port, + String databaseName, + String username, + String password) { config.setDriverClassName(driverClassName()); - config.setJdbcUrl(String.format("jdbc:%s://%s:%s/%s", driverJdbcIdentifier(), address, port, databaseName)); + config.setJdbcUrl(String.format( + "jdbc:%s://%s:%s/%s", driverJdbcIdentifier(), address, port, databaseName)); config.setUsername(username); config.setPassword(password); } @@ -39,7 +47,8 @@ public abstract class DriverBasedHikariConnectionFactory extends HikariConnectio if (driver.getClass().getName().equals(driverClassName)) { try { DriverManager.deregisterDriver(driver); - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } } } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/HikariConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/HikariConnectionFactory.java index 303b9cc..7d4c61e 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/HikariConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/hikari/HikariConnectionFactory.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.common.storage.provider.sql.connection.hikari; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; + import dev.xhyrom.lighteco.common.config.storage.StorageDataConfig; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFactory; @@ -23,7 +24,13 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { protected abstract String defaultPort(); - protected abstract void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password); + protected abstract void configureDatabase( + HikariConfig config, + String address, + String port, + String databaseName, + String username, + String password); protected void overrideProperties(Map properties) { // https://github.com/brettwooldridge/HikariCP/wiki/Rapid-Recovery @@ -39,9 +46,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { /** * Called after the Hikari pool has been initialised */ - protected void postInitialize() { - - } + protected void postInitialize() {} @Override public void init(LightEcoPlugin plugin) { @@ -56,7 +61,13 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { String port = addressSplit.length > 1 ? addressSplit[1] : defaultPort(); // allow the implementation to configure the HikariConfig appropriately with these values - configureDatabase(config, address, port, this.configuration.database, this.configuration.username, this.configuration.password); + configureDatabase( + config, + address, + port, + this.configuration.database, + this.configuration.username, + this.configuration.password); Map properties = new HashMap<>(); @@ -91,7 +102,8 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { Connection connection = this.hikari.getConnection(); if (connection == null) { - throw new SQLException("Unable to get a connection from the pool. (getConnection returned null)"); + throw new SQLException( + "Unable to get a connection from the pool. (getConnection returned null)"); } return connection; @@ -101,4 +113,4 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { public Function getStatementProcessor() { return s -> s.replace('\'', '`'); } -} \ No newline at end of file +} diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java b/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java index ad99adc..c93406c 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java @@ -30,11 +30,11 @@ public class UserSaveTask implements Runnable { user.setDirty(false); } - this.plugin.getStorage().saveUsersSync( - Arrays.stream(users) + this.plugin + .getStorage() + .saveUsersSync(Arrays.stream(users) .map(User::getProxy) - .toArray(dev.xhyrom.lighteco.api.model.user.User[]::new) - ); + .toArray(dev.xhyrom.lighteco.api.model.user.User[]::new)); } catch (RuntimeException e) { this.plugin.getBootstrap().getLogger().error("Failed to save users", e); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/util/ThrowableRunnable.java b/common/src/main/java/dev/xhyrom/lighteco/common/util/ThrowableRunnable.java index fcf57ba..3e70ca5 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/util/ThrowableRunnable.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/util/ThrowableRunnable.java @@ -4,4 +4,3 @@ package dev.xhyrom.lighteco.common.util; public interface ThrowableRunnable { void run() throws Exception; } - diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/util/URLClassLoaderAccess.java b/common/src/main/java/dev/xhyrom/lighteco/common/util/URLClassLoaderAccess.java index 0af4715..b17238d 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/util/URLClassLoaderAccess.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/util/URLClassLoaderAccess.java @@ -7,6 +7,7 @@ package dev.xhyrom.lighteco.common.util; import dev.xhyrom.lighteco.common.util.exception.UnableToInjectException; + import org.checkerframework.checker.nullness.qual.NonNull; import java.lang.reflect.Field; @@ -22,9 +23,10 @@ public abstract class URLClassLoaderAccess { } else if (Unsafe.isSupported()) { return new Unsafe(classLoader); } else { - throw new UnableToInjectException("LightEco is unable to inject dependencies into the plugin class loader.\n" + - "To fix this, please add '--add-opens java.base/java.lang=ALL-UNNAMED' to your JVM arguments." + - "If it still doesn't work, please report this on https://github.com/xHyroM/lighteco/issues"); + throw new UnableToInjectException( + "LightEco is unable to inject dependencies into the plugin class loader.\n" + + "To fix this, please add '--add-opens java.base/java.lang=ALL-UNNAMED' to your JVM arguments." + + "If it still doesn't work, please report this on https://github.com/xHyroM/lighteco/issues"); } } @@ -109,7 +111,9 @@ public abstract class URLClassLoaderAccess { this.pathURLs = pathURLs; } - private static Object fetchField(final Class clazz, final Object object, final String name) throws NoSuchFieldException { + private static Object fetchField( + final Class clazz, final Object object, final String name) + throws NoSuchFieldException { Field field = clazz.getDeclaredField(name); long offset = UNSAFE.objectFieldOffset(field); return UNSAFE.getObject(object, offset); @@ -121,7 +125,7 @@ public abstract class URLClassLoaderAccess { throw new NullPointerException("unopenedURLs or pathURLs"); } - synchronized (this.unopenedURLs) { + synchronized (this.unopenedURLs) { this.unopenedURLs.add(url); this.pathURLs.add(url); } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/AbstractPlugin.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/AbstractPlugin.java index ad483e1..c29dbb7 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/AbstractPlugin.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/AbstractPlugin.java @@ -1,8 +1,10 @@ package dev.xhyrom.lighteco.currency.money.common; import dev.xhyrom.lighteco.currency.money.common.config.Config; + import eu.okaeri.configs.ConfigManager; import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer; + import lombok.Getter; import java.nio.file.Path; diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/config/Config.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/config/Config.java index 0134dc8..36e1d65 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/config/Config.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/config/Config.java @@ -5,9 +5,9 @@ import eu.okaeri.configs.annotation.Comment; public class Config extends OkaeriConfig { @Comment("Currency name") - @Comment("In singular form") public String currencyNameSingular = "Dollar"; + @Comment("In plural form") public String currencyNamePlural = "Dollars"; diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java index 1858cf1..4a1ca80 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/common/currency/MoneyCurrency.java @@ -19,7 +19,7 @@ public class MoneyCurrency implements Currency { @Override public String[] getIdentifierAliases() { - return new String[]{"eco"}; + return new String[] {"eco"}; } @Override @@ -39,6 +39,6 @@ public class MoneyCurrency implements Currency { @Override public int fractionalDigits() { - return this.plugin.getConfig().fractionalDigits; + return this.plugin.getConfig().fractionalDigits; } } diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java index 51a39ad..b2533d7 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/PaperMCLoader.java @@ -5,15 +5,18 @@ import dev.xhyrom.lighteco.api.LightEcoProvider; import dev.xhyrom.lighteco.api.manager.CommandManager; import dev.xhyrom.lighteco.api.manager.CurrencyManager; import dev.xhyrom.lighteco.api.model.currency.Currency; -import dev.xhyrom.lighteco.currency.money.paper.hooks.vault.VaultFactory; import dev.xhyrom.lighteco.currency.money.common.AbstractPlugin; import dev.xhyrom.lighteco.currency.money.common.Plugin; import dev.xhyrom.lighteco.currency.money.common.currency.MoneyCurrency; +import dev.xhyrom.lighteco.currency.money.paper.hooks.vault.VaultFactory; + import lombok.Getter; + import org.bukkit.plugin.java.JavaPlugin; public class PaperMCLoader extends JavaPlugin { private VaultFactory vaultFactory; + @Getter private final Plugin plugin; @@ -50,4 +53,3 @@ public class PaperMCLoader extends JavaPlugin { this.vaultFactory.unhook(); } } - diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java index cc2cb94..622c43b 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/Vault.java @@ -7,8 +7,10 @@ import dev.xhyrom.lighteco.api.exception.CannotBeNegative; import dev.xhyrom.lighteco.api.model.currency.Currency; import dev.xhyrom.lighteco.api.model.user.User; import dev.xhyrom.lighteco.currency.money.common.Plugin; + import net.milkbowl.vault.economy.AbstractEconomy; import net.milkbowl.vault.economy.EconomyResponse; + import org.bukkit.Bukkit; import java.math.BigDecimal; @@ -93,7 +95,7 @@ public class Vault extends AbstractEconomy { @Override public boolean has(String playerName, double amount) { - return has(playerName, null, amount); + return has(playerName, null, amount); } @Override @@ -121,16 +123,14 @@ public class Vault extends AbstractEconomy { amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.FAILURE, - e.getMessage() - ); + e.getMessage()); } return new EconomyResponse( amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.SUCCESS, - "" - ); + ""); } @Override @@ -150,16 +150,14 @@ public class Vault extends AbstractEconomy { amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.FAILURE, - e.getMessage() - ); + e.getMessage()); } return new EconomyResponse( amount, bigDecimalToDouble(user.getBalance(currency)), EconomyResponse.ResponseType.SUCCESS, - "" - ); + ""); } @Override diff --git a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java index 84d8829..5797080 100644 --- a/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java +++ b/currency-money/src/main/java/dev/xhyrom/lighteco/currency/money/paper/hooks/vault/VaultFactory.java @@ -1,7 +1,9 @@ package dev.xhyrom.lighteco.currency.money.paper.hooks.vault; import dev.xhyrom.lighteco.currency.money.paper.PaperMCLoader; + import net.milkbowl.vault.economy.Economy; + import org.bukkit.Bukkit; import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicesManager; @@ -15,8 +17,7 @@ public class VaultFactory { } public void hook() { - if (this.vault == null) - vault = new Vault(this.loader.getPlugin()); + if (this.vault == null) vault = new Vault(this.loader.getPlugin()); ServicesManager manager = Bukkit.getServicesManager(); manager.register(Economy.class, vault, this.loader, ServicePriority.Highest); diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java index 0695adb..e94451d 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoBootstrap.java @@ -1,14 +1,17 @@ package dev.xhyrom.lighteco.paper; -import dev.xhyrom.lighteco.paper.logger.PaperLogger; import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; +import dev.xhyrom.lighteco.paper.logger.PaperLogger; + import lombok.Getter; -import org.bukkit.OfflinePlayer; + import net.kyori.adventure.audience.Audience; import net.kyori.adventure.platform.bukkit.BukkitAudiences; + +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -25,10 +28,13 @@ public class PaperLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstra @Getter private final JavaPlugin loader; + @Getter private final PluginLogger logger; + @Getter private final SchedulerAdapter scheduler; + @Getter private BukkitAudiences audience; @@ -67,7 +73,8 @@ public class PaperLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstra @Override public Optional lookupUniqueId(String username) { - return Optional.of(this.loader.getServer().getOfflinePlayer(username)).map(OfflinePlayer::getUniqueId); + return Optional.of(this.loader.getServer().getOfflinePlayer(username)) + .map(OfflinePlayer::getUniqueId); } @Override diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java index a3b5b82..f08cfe6 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperLightEcoPlugin.java @@ -3,16 +3,18 @@ package dev.xhyrom.lighteco.paper; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.manager.ContextManager; import dev.xhyrom.lighteco.api.platform.Platform; +import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; +import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; +import dev.xhyrom.lighteco.common.messaging.MessagingFactory; +import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; import dev.xhyrom.lighteco.paper.hooks.Hooks; import dev.xhyrom.lighteco.paper.listeners.PaperCommandSuggestionsListener; import dev.xhyrom.lighteco.paper.listeners.PaperConnectionListener; import dev.xhyrom.lighteco.paper.manager.PaperCommandManager; import dev.xhyrom.lighteco.paper.manager.PaperContextManager; -import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; -import dev.xhyrom.lighteco.common.messaging.MessagingFactory; -import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; -import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; + import lombok.Getter; + import org.bukkit.entity.Player; import org.bukkit.plugin.ServicePriority; import org.checkerframework.checker.nullness.qual.NonNull; @@ -23,10 +25,13 @@ public class PaperLightEcoPlugin extends AbstractLightEcoPlugin { @Getter private StandardUserManager userManager; + @Getter private StandardCurrencyManager currencyManager; + @Getter private PaperCommandManager commandManager; + @Getter private ContextManager contextManager; @@ -36,8 +41,17 @@ public class PaperLightEcoPlugin extends AbstractLightEcoPlugin { @Override protected void registerListeners() { - this.bootstrap.getLoader().getServer().getPluginManager().registerEvents(new PaperConnectionListener(this), this.bootstrap.getLoader()); - this.bootstrap.getLoader().getServer().getPluginManager().registerEvents(new PaperCommandSuggestionsListener(this), this.bootstrap.getLoader()); + this.bootstrap + .getLoader() + .getServer() + .getPluginManager() + .registerEvents(new PaperConnectionListener(this), this.bootstrap.getLoader()); + this.bootstrap + .getLoader() + .getServer() + .getPluginManager() + .registerEvents( + new PaperCommandSuggestionsListener(this), this.bootstrap.getLoader()); } @Override @@ -55,7 +69,15 @@ public class PaperLightEcoPlugin extends AbstractLightEcoPlugin { @Override protected void registerApiOnPlatform(LightEco api) { - this.getBootstrap().getLoader().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap().getLoader(), ServicePriority.Normal); + this.getBootstrap() + .getLoader() + .getServer() + .getServicesManager() + .register( + LightEco.class, + api, + this.getBootstrap().getLoader(), + ServicePriority.Normal); } @Override diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java index f051ba1..656216e 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/PaperSchedulerAdapter.java @@ -2,6 +2,7 @@ package dev.xhyrom.lighteco.paper; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerTask; + import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; @@ -18,7 +19,8 @@ public class PaperSchedulerAdapter implements SchedulerAdapter { this.bootstrap = bootstrap; this.scheduler = bootstrap.getLoader().getServer().getScheduler(); - this.async = runnable -> this.scheduler.runTaskAsynchronously(this.bootstrap.getLoader(), runnable); + this.async = runnable -> + this.scheduler.runTaskAsynchronously(this.bootstrap.getLoader(), runnable); } public Executor async() { @@ -27,29 +29,19 @@ public class PaperSchedulerAdapter implements SchedulerAdapter { @Override public SchedulerTask asyncLater(Runnable runnable, long delay, TimeUnit unit) { - return new Task( - this.scheduler.runTaskLaterAsynchronously( - this.bootstrap.getLoader(), - runnable, - unit.toSeconds(delay) * 20 - ) - ); + return new Task(this.scheduler.runTaskLaterAsynchronously( + this.bootstrap.getLoader(), runnable, unit.toSeconds(delay) * 20)); } @Override public SchedulerTask asyncRepeating(Runnable runnable, long interval, TimeUnit unit) { - return new Task( - this.scheduler.runTaskTimerAsynchronously( - this.bootstrap.getLoader(), - runnable, - 0, - unit.toSeconds(interval) * 20 - ) - ); + return new Task(this.scheduler.runTaskTimerAsynchronously( + this.bootstrap.getLoader(), runnable, 0, unit.toSeconds(interval) * 20)); } public class Task implements SchedulerTask { private final BukkitTask task; + public Task(BukkitTask task) { this.task = task; } diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java index f732850..8678c2f 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/chat/PaperCommandSender.java @@ -1,7 +1,9 @@ package dev.xhyrom.lighteco.paper.chat; import dev.xhyrom.lighteco.common.model.chat.AbstractCommandSender; + import net.kyori.adventure.text.Component; + import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -35,4 +37,4 @@ public class PaperCommandSender extends AbstractCommandSender { public void sendMessage(Component message) { this.delegate.sendMessage(message); } -} \ No newline at end of file +} diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java index 8597548..a8d3893 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/Hooks.java @@ -1,7 +1,9 @@ package dev.xhyrom.lighteco.paper.hooks; import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; + import lombok.experimental.UtilityClass; + import org.bukkit.Bukkit; @UtilityClass diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java index 7604fca..0471401 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/hooks/PlaceholderAPIExpansion.java @@ -1,10 +1,13 @@ package dev.xhyrom.lighteco.paper.hooks; -import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; + import lombok.RequiredArgsConstructor; + import me.clip.placeholderapi.expansion.PlaceholderExpansion; + import org.bukkit.OfflinePlayer; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -20,7 +23,12 @@ public class PlaceholderAPIExpansion extends PlaceholderExpansion { @Override public @NonNull String getAuthor() { - return this.plugin.getBootstrap().getLoader().getDescription().getAuthors().toString(); + return this.plugin + .getBootstrap() + .getLoader() + .getDescription() + .getAuthors() + .toString(); } @Override diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java index 2cf8a1f..9ed97cc 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperCommandSuggestionsListener.java @@ -3,10 +3,12 @@ package dev.xhyrom.lighteco.paper.listeners; import com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; -import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; -import dev.xhyrom.lighteco.paper.util.PaperBrigadier; -import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; + import dev.xhyrom.lighteco.common.command.CommandSource; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; +import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; +import dev.xhyrom.lighteco.paper.util.PaperBrigadier; + import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -23,23 +25,37 @@ public class PaperCommandSuggestionsListener implements Listener { PaperCommandSender sender = new PaperCommandSender(event.getPlayer()); CommandSource source = new CommandSource(this.plugin, sender); if (event.isAsynchronous() || !event.hasFiredAsync()) { - for (CommandNode command : this.plugin.getCommandManager().getDispatcher().getRoot().getChildren()) { + for (CommandNode command : + this.plugin.getCommandManager().getDispatcher().getRoot().getChildren()) { PaperBrigadier.removeChild(event.getCommandNode(), command.getName()); - PaperBrigadier.removeChild(event.getCommandNode(), this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + command.getName()); + PaperBrigadier.removeChild( + event.getCommandNode(), + this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + + command.getName()); if (!command.canUse(source)) continue; addChild(event, source, command, createClone(command)); - addChild(event, source, command, createClone(this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + command.getName(), command)); + addChild( + event, + source, + command, + createClone( + this.plugin.getBootstrap().getLoader().getName().toLowerCase() + ":" + + command.getName(), + command)); } } } @SuppressWarnings({"rawtypes", "unchecked", "deprecation"}) - private void addChild(AsyncPlayerSendCommandsEvent event, CommandSource source, CommandNode command, CommandNode clone) { + private void addChild( + AsyncPlayerSendCommandsEvent event, + CommandSource source, + CommandNode command, + CommandNode clone) { for (CommandNode child : command.getChildren()) { - if (child.canUse(source)) - clone.addChild(child); + if (child.canUse(source)) clone.addChild(child); } event.getCommandNode().addChild((CommandNode) clone); @@ -49,7 +65,14 @@ public class PaperCommandSuggestionsListener implements Listener { return createClone(command.getName(), command); } - private CommandNode createClone(String name, CommandNode command) { - return new LiteralCommandNode<>(name, command.getCommand(), command.getRequirement(), command.getRedirect(), command.getRedirectModifier(), command.isFork()); + private CommandNode createClone( + String name, CommandNode command) { + return new LiteralCommandNode<>( + name, + command.getCommand(), + command.getRequirement(), + command.getRedirect(), + command.getRedirectModifier(), + command.isFork()); } } diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java index a8e511f..5ec1797 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/listeners/PaperConnectionListener.java @@ -1,11 +1,13 @@ package dev.xhyrom.lighteco.paper.listeners; -import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.paper.PaperLightEcoPlugin; + import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; + import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -29,21 +31,24 @@ public class PaperConnectionListener implements Listener { } try { - this.plugin.getStorage().loadUser(event.getUniqueId(), event.getName()).join(); + this.plugin + .getStorage() + .loadUser(event.getUniqueId(), event.getName()) + .join(); } catch (Exception e) { - this.plugin.getBootstrap().getLogger() - .error("Failed to load user data for %s (%s)", e, event.getName(), event.getUniqueId()); + this.plugin + .getBootstrap() + .getLogger() + .error( + "Failed to load user data for %s (%s)", + e, event.getName(), event.getUniqueId()); Component reason = miniMessage.deserialize( - "LightEco Failed to load your data. Contact a staff member for assistance." - ); + "LightEco Failed to load your data. Contact a staff member for assistance."); event.disallow( AsyncPlayerPreLoginEvent.Result.KICK_OTHER, - LegacyComponentSerializer.legacySection().serialize( - reason - ) - ); + LegacyComponentSerializer.legacySection().serialize(reason)); } } @@ -57,9 +62,11 @@ public class PaperConnectionListener implements Listener { return; } - this.plugin.getUserManager().saveUser(user) - .thenAccept(v -> this.plugin.getMessagingService().ifPresent(service -> { - for (Currency currency : this.plugin.getCurrencyManager().getRegisteredCurrencies()) { + this.plugin.getUserManager().saveUser(user).thenAccept(v -> this.plugin + .getMessagingService() + .ifPresent(service -> { + for (Currency currency : + this.plugin.getCurrencyManager().getRegisteredCurrencies()) { service.pushUserUpdate(user, currency); } })); diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java index abbfb30..ab1f3c2 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperCommandManager.java @@ -1,13 +1,14 @@ package dev.xhyrom.lighteco.paper.manager; import com.mojang.brigadier.ParseResults; -import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; + import dev.xhyrom.lighteco.common.command.CommandManager; import dev.xhyrom.lighteco.common.command.CommandSource; import dev.xhyrom.lighteco.common.command.abstraction.Command; -import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import dev.xhyrom.lighteco.paper.chat.PaperCommandSender; import dev.xhyrom.lighteco.paper.util.PaperCommandMapUtil; + import org.bukkit.command.CommandExecutor; import org.bukkit.command.TabCompleter; @@ -37,28 +38,27 @@ public class PaperCommandManager extends CommandManager { }; TabCompleter completer = (sender, bukkitCommand, s, args) -> { - final List suggestions = new ArrayList<>(); - final CommandSource source = new CommandSource(plugin, new PaperCommandSender(sender)); + final List suggestions = new ArrayList<>(); + final CommandSource source = new CommandSource(plugin, new PaperCommandSender(sender)); - final ParseResults parseResults = getDispatcher().parse( - command.getName() + (args.length > 0 ? " " + String.join(" ", args) : ""), - source - ); + final ParseResults parseResults = getDispatcher() + .parse( + command.getName() + + (args.length > 0 ? " " + String.join(" ", args) : ""), + source); - getDispatcher().getCompletionSuggestions( - parseResults - ).join().getList().forEach(suggestion -> suggestions.add(suggestion.getText())); + getDispatcher() + .getCompletionSuggestions(parseResults) + .join() + .getList() + .forEach(suggestion -> suggestions.add(suggestion.getText())); - return suggestions; + return suggestions; }; List aliases = new ArrayList<>(command.getAliases()); aliases.add(command.getName()); - this.commandMapUtil.register( - executor, - completer, - aliases - ); + this.commandMapUtil.register(executor, completer, aliases); } } diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java index b63e8f8..e93280d 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/manager/PaperContextManager.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.paper.manager; import dev.xhyrom.lighteco.api.manager.ContextManager; + import org.bukkit.entity.Player; import org.checkerframework.checker.nullness.qual.NonNull; diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java index ad4a7bc..0feb412 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperBrigadier.java @@ -17,7 +17,7 @@ public class PaperBrigadier { CHILDREN_FIELD = CommandNode.class.getDeclaredField("children"); LITERALS_FIELD = CommandNode.class.getDeclaredField("literals"); ARGUMENTS_FIELD = CommandNode.class.getDeclaredField("arguments"); - CHILDREN_FIELDS = new Field[]{CHILDREN_FIELD, LITERALS_FIELD, ARGUMENTS_FIELD}; + CHILDREN_FIELDS = new Field[] {CHILDREN_FIELD, LITERALS_FIELD, ARGUMENTS_FIELD}; for (Field field : CHILDREN_FIELDS) { field.setAccessible(true); diff --git a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java index 2e3b06c..68a7464 100644 --- a/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java +++ b/paper/src/main/java/dev/xhyrom/lighteco/paper/util/PaperCommandMapUtil.java @@ -1,6 +1,7 @@ package dev.xhyrom.lighteco.paper.util; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + import org.bukkit.command.*; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -14,7 +15,8 @@ public class PaperCommandMapUtil { static { try { - COMMAND_CONSTRUCTOR = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); + COMMAND_CONSTRUCTOR = + PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); COMMAND_CONSTRUCTOR.setAccessible(true); } catch (NoSuchMethodException e) { throw new ExceptionInInitializerError(e); @@ -22,6 +24,7 @@ public class PaperCommandMapUtil { } private final LightEcoPlugin plugin; + public PaperCommandMapUtil(LightEcoPlugin plugin) { this.plugin = plugin; } @@ -33,14 +36,16 @@ public class PaperCommandMapUtil { for (String name : aliases) { if (!name.toLowerCase().equals(name)) { - throw new IllegalArgumentException("Command aliases must be lowercase! (name: " + name + ")"); + throw new IllegalArgumentException( + "Command aliases must be lowercase! (name: " + name + ")"); } try { PluginCommand command = COMMAND_CONSTRUCTOR.newInstance(name, bukkitPlugin); commandMap.register(bukkitPlugin.getName().toLowerCase(), command); - knownCommands.put(bukkitPlugin.getName().toLowerCase() + ":" + name.toLowerCase(), command); + knownCommands.put( + bukkitPlugin.getName().toLowerCase() + ":" + name.toLowerCase(), command); knownCommands.put(name, command); command.setLabel(name); diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java index c1f736d..960da94 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java @@ -1,11 +1,14 @@ package dev.xhyrom.lighteco.sponge; import com.google.inject.Inject; + import dev.xhyrom.lighteco.common.plugin.bootstrap.LightEcoBootstrap; import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import dev.xhyrom.lighteco.common.plugin.logger.PluginLogger; import dev.xhyrom.lighteco.common.plugin.scheduler.SchedulerAdapter; + import net.kyori.adventure.audience.Audience; + import org.apache.logging.log4j.Logger; import org.spongepowered.plugin.builtin.jvm.Plugin; diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java index a80b1b6..e1e55e1 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoPlugin.java @@ -8,7 +8,9 @@ import dev.xhyrom.lighteco.common.manager.currency.StandardCurrencyManager; import dev.xhyrom.lighteco.common.manager.user.StandardUserManager; import dev.xhyrom.lighteco.common.messaging.MessagingFactory; import dev.xhyrom.lighteco.common.plugin.AbstractLightEcoPlugin; + import lombok.Getter; + import org.checkerframework.checker.nullness.qual.NonNull; import org.spongepowered.api.entity.living.player.Player; @@ -18,10 +20,13 @@ public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin { @Getter private StandardUserManager userManager; + @Getter private StandardCurrencyManager currencyManager; + @Getter private CommandManager commandManager; + @Getter private ContextManager contextManager; @@ -31,15 +36,16 @@ public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin { @Override protected void registerListeners() { - //this.bootstrap.getServer().getPluginManager().registerEvents(new BukkitConnectionListener(this), this.bootstrap); + // this.bootstrap.getServer().getPluginManager().registerEvents(new + // BukkitConnectionListener(this), this.bootstrap); } @Override public void setupManagers() { this.userManager = new StandardUserManager(this); this.currencyManager = new StandardCurrencyManager(this); - //this.commandManager = new BukkitCommandManager(this); - //this.contextManager = new BukkitContextManager(); + // this.commandManager = new BukkitCommandManager(this); + // this.contextManager = new BukkitContextManager(); } @Override @@ -49,18 +55,15 @@ public class SpongeLightEcoPlugin extends AbstractLightEcoPlugin { @Override protected void registerApiOnPlatform(LightEco api) { - //this.getBootstrap().getServer().getServicesManager().register(LightEco.class, api, this.getBootstrap(), ServicePriority.Normal); + // this.getBootstrap().getServer().getServicesManager().register(LightEco.class, api, + // this.getBootstrap(), ServicePriority.Normal); } @Override - protected void registerPlatformHooks() { - - } + protected void registerPlatformHooks() {} @Override - protected void removePlatformHooks() { - - } + protected void removePlatformHooks() {} @Override public Platform.@NonNull Type getPlatformType() { diff --git a/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java index e501613..9730636 100644 --- a/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java +++ b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestCurrency2.java @@ -23,7 +23,7 @@ public class TestCurrency2 implements Currency { @Override public int fractionalDigits() { - return 2; + return 2; } @Override diff --git a/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java index 2b28756..05e8c57 100644 --- a/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java +++ b/test/paper/src/main/java/dev/xhyrom/lighteco/test/paper/TestPlugin.java @@ -3,6 +3,7 @@ package dev.xhyrom.lighteco.test.paper; import dev.xhyrom.lighteco.api.LightEco; import dev.xhyrom.lighteco.api.LightEcoProvider; import dev.xhyrom.lighteco.api.manager.CurrencyManager; + import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; @@ -19,7 +20,9 @@ public class TestPlugin extends JavaPlugin implements Listener { getLogger().info("TestCurrency registered!"); - currencyManager.getRegisteredCurrencies().forEach(currency -> getLogger().info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + currency.fractionalDigits() + ", " + currency.isPayable() + ")")); + currencyManager.getRegisteredCurrencies().forEach(currency -> getLogger() + .info("Currency: " + currency.getIdentifier() + " (" + currency.getType() + ", " + + currency.fractionalDigits() + ", " + currency.isPayable() + ")")); provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test")); provider.getCommandManager().registerCurrencyCommand(currencyManager.getCurrency("test2"));