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] 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;