1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-21 22:41:06 +01:00

feat: allow global cmds registration for one currency

This commit is contained in:
Jozef Steinhübl 2023-08-27 13:23:10 +02:00
parent 7ebcfedcf6
commit 9305b33f13
15 changed files with 363 additions and 166 deletions

View file

@ -5,4 +5,5 @@ import org.checkerframework.checker.nullness.qual.NonNull;
public interface CommandManager { public interface CommandManager {
void registerCurrencyCommand(@NonNull Currency currency); void registerCurrencyCommand(@NonNull Currency currency);
void registerCurrencyCommand(@NonNull Currency currency, boolean main);
} }

View file

@ -0,0 +1,57 @@
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;
import java.util.List;
@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);
})
};
}
private 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())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.manager.onBalance(sender, currency, result);
});
}
}

View file

@ -0,0 +1,14 @@
package dev.xhyrom.lighteco.bukkit.commands;
import dev.jorel.commandapi.CommandAPICommand;
import java.util.List;
public interface Command {
default CommandAPICommand build() {
return null;
};
default CommandAPICommand[] multipleBuild() {
return null;
};
}

View file

@ -0,0 +1,54 @@
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().getDecimalPlaces() > 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)) return;
this.manager.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.manager.onGive(sender, currency, result, amount);
});
}
}

View file

@ -0,0 +1,53 @@
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().getDecimalPlaces() > 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)) return;
this.manager.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.manager.onPay(sender, currency, result, amount);
});
}
}

View file

@ -0,0 +1,54 @@
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().getDecimalPlaces() > 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)) return;
this.manager.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.manager.onSet(sender, currency, result, amount);
});
}
}

View file

@ -0,0 +1,54 @@
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().getDecimalPlaces() > 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)) return;
this.manager.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.manager.onTake(sender, currency, result, amount);
});
}
}

View file

@ -1,26 +1,16 @@
package dev.xhyrom.lighteco.bukkit.manager; package dev.xhyrom.lighteco.bukkit.manager;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand; import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.DoubleArgument; import dev.xhyrom.lighteco.bukkit.commands.*;
import dev.jorel.commandapi.arguments.IntegerArgument;
import dev.jorel.commandapi.arguments.OfflinePlayerArgument;
import dev.jorel.commandapi.executors.CommandArguments;
import dev.xhyrom.lighteco.bukkit.chat.BukkitCommandSender;
import dev.xhyrom.lighteco.common.manager.command.AbstractCommandManager; import dev.xhyrom.lighteco.common.manager.command.AbstractCommandManager;
import dev.xhyrom.lighteco.common.model.currency.Currency; import dev.xhyrom.lighteco.common.model.currency.Currency;
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.math.BigDecimal;
import java.util.List;
public class BukkitCommandManager extends AbstractCommandManager { public class BukkitCommandManager extends AbstractCommandManager {
private final BukkitAudiences audienceFactory; public final BukkitAudiences audienceFactory;
public BukkitCommandManager(LightEcoPlugin plugin) { public BukkitCommandManager(LightEcoPlugin plugin) {
super(plugin); super(plugin);
@ -28,166 +18,42 @@ public class BukkitCommandManager extends AbstractCommandManager {
this.audienceFactory = BukkitAudiences.create((JavaPlugin) this.plugin.getBootstrap().getLoader()); this.audienceFactory = BukkitAudiences.create((JavaPlugin) this.plugin.getBootstrap().getLoader());
} }
private List<CommandAPICommand> getBalanceCommands(String name, String permissionBase, Currency currency) {
return List.of(
new CommandAPICommand(name)
.withPermission(permissionBase + "balance.others")
.withArguments(new OfflinePlayerArgument("target"))
.executes((sender, args) -> {
this.handleBalance(sender, args, currency);
}),
new CommandAPICommand(name)
.withPermission(permissionBase + "balance")
.executesPlayer((sender, args) -> {
this.handleBalance(sender, args, currency);
})
);
}
@Override @Override
public void registerCurrencyCommand(@NonNull Currency currency) { public void registerCurrencyCommand(@NonNull Currency currency) {
String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command."; String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command.";
// Balance // Balance
getBalanceCommands(currency.getIdentifier(), permissionBase, currency) for (CommandAPICommand cmd : new BalanceCommand(
.forEach(CommandAPICommand::register); this,
currency.getIdentifier(),
List<CommandAPICommand> balanceCommands = getBalanceCommands("balance", permissionBase, currency); currency,
permissionBase
).multipleBuild()) {
cmd.register();
}
new CommandAPICommand(currency.getIdentifier()) new CommandAPICommand(currency.getIdentifier())
.withSubcommand(new CommandAPICommand("set") .withSubcommand(new SetCommand(this, currency, permissionBase).build())
.withPermission(permissionBase + "set") .withSubcommand(new GiveCommand(this, currency, permissionBase).build())
.withArguments( .withSubcommand(new TakeCommand(this, currency, permissionBase).build())
new OfflinePlayerArgument("target"), .withSubcommand(new PayCommand(this, currency, permissionBase).build())
currency.getProxy().getDecimalPlaces() > 0 .withSubcommands(new BalanceCommand(this, "balance", currency, permissionBase).multipleBuild())
? new DoubleArgument("amount", 0)
: new IntegerArgument("amount", 0)
)
.executes((sender, args) -> {
this.handleSet(sender, args, currency);
})
)
.withSubcommand(new CommandAPICommand("give")
.withPermission(permissionBase + "give")
.withArguments(
new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0
? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1)
)
.executes((sender, args) -> {
this.handleGive(sender, args, currency);
})
)
.withSubcommand(new CommandAPICommand("take")
.withPermission(permissionBase + "take")
.withArguments(
new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0
? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1)
)
.executes((sender, args) -> {
this.handleTake(sender, args, currency);
})
)
.withSubcommand(new CommandAPICommand("pay")
.withPermission(permissionBase + "pay")
.withArguments(
new OfflinePlayerArgument("target"),
currency.getProxy().getDecimalPlaces() > 0
? new DoubleArgument("amount", 1)
: new IntegerArgument("amount", 1)
)
.executesPlayer((sender, args) -> {
this.handlePay(sender, args, currency);
})
)
.withSubcommand(balanceCommands.get(0))
.withSubcommands(balanceCommands.get(1))
.register(); .register();
} }
private void handleBalance(CommandSender originalSender, CommandArguments args, Currency currency) { @Override
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory); public void registerCurrencyCommand(@NonNull Currency currency, boolean main) {
OfflinePlayer target = (OfflinePlayer) args.get("target"); if (!main) {
registerCurrencyCommand(currency);
if (target == null) {
this.onBalance(sender, currency);
return; return;
} }
this.plugin.getUserManager().loadUser(target.getUniqueId()) String permissionBase = "lighteco.currency." + currency.getIdentifier() + ".command.";
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onBalance(sender, currency, result); new SetCommand(this, currency, permissionBase).build().register();
}); new GiveCommand(this, currency, permissionBase).build().register();
} new TakeCommand(this, currency, permissionBase).build().register();
new PayCommand(this, currency, permissionBase).build().register();
private void handleSet(CommandSender originalSender, CommandArguments args, Currency currency) { new BalanceCommand(this, "balance", currency, permissionBase).build().register();
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
if (!this.canUse(sender)) return;
this.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onSet(sender, currency, result, amount);
});
}
private void handleGive(CommandSender originalSender, CommandArguments args, Currency currency) {
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
if (!this.canUse(sender)) return;
this.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onGive(sender, currency, result, amount);
});
}
private void handleTake(CommandSender originalSender, CommandArguments args, Currency currency) {
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
if (!this.canUse(sender)) return;
this.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onTake(sender, currency, result, amount);
});
}
private void handlePay(CommandSender originalSender, CommandArguments args, Currency currency) {
BukkitCommandSender sender = new BukkitCommandSender(originalSender, this.audienceFactory);
OfflinePlayer target = (OfflinePlayer) args.get("target");
BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(args.getRaw("amount")));
if (!this.canUse(sender)) return;
this.plugin.getUserManager().loadUser(target.getUniqueId())
.thenAcceptAsync(result -> {
String name = target.getName() != null ? target.getName() : args.getRaw("target");
result.setUsername(name);
this.onPay(sender, currency, result, amount);
});
} }
} }

View file

@ -12,8 +12,17 @@ public class ApiCommandManager extends ApiAbstractManager<dev.xhyrom.lighteco.co
@Override @Override
public void registerCurrencyCommand(@NonNull Currency currency) { 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.handler.registerCurrencyCommand(internal); this.handler.registerCurrencyCommand(internal);
} }
}
@Override
public void registerCurrencyCommand(@NonNull Currency currency, boolean main) {
dev.xhyrom.lighteco.common.model.currency.Currency internal = this.plugin.getCurrencyManager()
.getIfLoaded(currency.getIdentifier());
this.handler.registerCurrencyCommand(internal, main);
}
}

View file

@ -1,5 +1,6 @@
package dev.xhyrom.lighteco.common.config; package dev.xhyrom.lighteco.common.config;
import dev.xhyrom.lighteco.common.config.storage.StorageConfig;
import eu.okaeri.configs.OkaeriConfig; import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment; import eu.okaeri.configs.annotation.Comment;
import eu.okaeri.configs.annotation.Header; import eu.okaeri.configs.annotation.Header;
@ -12,7 +13,6 @@ public class Config extends OkaeriConfig {
@Comment("Used for local currencies.") @Comment("Used for local currencies.")
public String server = "none"; public String server = "none";
@Comment("The storage provider to use.") @Comment("Storage settings.")
@Comment("Supported providers: memory") public StorageConfig storage = new StorageConfig();
public String storage = "memory";
} }

View file

@ -0,0 +1,18 @@
package dev.xhyrom.lighteco.common.config.storage;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;
import eu.okaeri.configs.annotation.Variable;
public class StorageConfig extends OkaeriConfig {
@Comment("Storage provider.")
@Comment("Available providers: memory")
public String provider = "memory";
@Comment("Data storage settings.")
public StorageDataConfig data = new StorageDataConfig();
@Variable("table-prefix")
@Comment("Table prefix.")
public String tablePrefix = "lighteco_";
}

View file

@ -0,0 +1,16 @@
package dev.xhyrom.lighteco.common.config.storage;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;
public class StorageDataConfig extends OkaeriConfig {
@Comment("Define the address and port of the database.")
public String address = "localhost";
@Comment("The name of the database to use.")
public String database = "lighteco";
@Comment("Credentials for connecting to the database.")
public String username = "root";
public String password = "password";
}

View file

@ -13,7 +13,7 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public abstract class AbstractCommandManager implements CommandManager { public abstract class AbstractCommandManager implements CommandManager {
protected final LightEcoPlugin plugin; public final LightEcoPlugin plugin;
private final MiniMessage miniMessage = MiniMessage.miniMessage(); private final MiniMessage miniMessage = MiniMessage.miniMessage();
private final ArrayList<UUID> mustWait = new ArrayList<>(); private final ArrayList<UUID> mustWait = new ArrayList<>();

View file

@ -9,6 +9,7 @@ import java.math.BigDecimal;
public interface CommandManager { public interface CommandManager {
void registerCurrencyCommand(@NonNull Currency currency); void registerCurrencyCommand(@NonNull Currency currency);
void registerCurrencyCommand(@NonNull Currency currency, boolean main);
boolean canUse(CommandSender sender); boolean canUse(CommandSender sender);

View file

@ -13,7 +13,7 @@ public class StorageFactory {
public Storage get() { public Storage get() {
// todo: use config // todo: use config
String provider = this.plugin.getConfig().get("storage", String.class); String provider = this.plugin.getConfig().storage.provider;
return new Storage(this.plugin, createProvider(provider)); return new Storage(this.plugin, createProvider(provider));
} }