mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-22 15:01:05 +01:00
feat: balance, balance other cmds in common & double, integer arg
This commit is contained in:
parent
723fd6abd4
commit
35dc14a261
8 changed files with 184 additions and 9 deletions
|
@ -1,6 +1,9 @@
|
|||
package dev.xhyrom.lighteco.common.command.abstraction;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.argument.Argument;
|
||||
import dev.xhyrom.lighteco.common.command.argument.Arguments;
|
||||
import dev.xhyrom.lighteco.common.model.chat.CommandSender;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import lombok.Getter;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
@ -25,4 +28,6 @@ public abstract class Command {
|
|||
this.permission = permission;
|
||||
this.args = List.of(args);
|
||||
}
|
||||
|
||||
public abstract void execute(LightEcoPlugin plugin, CommandSender sender, Arguments args);
|
||||
}
|
||||
|
|
|
@ -6,15 +6,13 @@ import lombok.Getter;
|
|||
public abstract class Argument<T> {
|
||||
@Getter
|
||||
private final String name;
|
||||
protected final LightEcoPlugin plugin;
|
||||
|
||||
protected Argument(LightEcoPlugin plugin, String name) {
|
||||
this.plugin = plugin;
|
||||
protected Argument(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract Class<T> getPrimitiveType();
|
||||
public abstract ArgumentType getArgumentType();
|
||||
|
||||
public abstract T parse(String input);
|
||||
public abstract T parse(LightEcoPlugin plugin, String input);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument;
|
||||
|
||||
import dev.xhyrom.lighteco.common.model.user.User;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Arguments {
|
||||
private final Map<String, ?> arguments;
|
||||
|
||||
public Arguments(Map<String, ?> arguments) {
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public User offlineUser(String name) {
|
||||
return (User) this.arguments.get(name);
|
||||
}
|
||||
|
||||
public int integer(String name) {
|
||||
return (int) this.arguments.get(name);
|
||||
}
|
||||
|
||||
public double dbl(String name) {
|
||||
return (double) this.arguments.get(name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument.type;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.argument.Argument;
|
||||
import dev.xhyrom.lighteco.common.command.argument.ArgumentType;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
|
||||
public class DoubleArgument extends Argument<Double> {
|
||||
protected DoubleArgument(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Double> getPrimitiveType() {
|
||||
return Double.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getArgumentType() {
|
||||
return ArgumentType.DOUBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double parse(LightEcoPlugin plugin, String input) {
|
||||
return Double.parseDouble(input);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package dev.xhyrom.lighteco.common.command.argument.type;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.argument.Argument;
|
||||
import dev.xhyrom.lighteco.common.command.argument.ArgumentType;
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
|
||||
public class IntegerArgument extends Argument<Integer> {
|
||||
protected IntegerArgument(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Integer> getPrimitiveType() {
|
||||
return Integer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArgumentType getArgumentType() {
|
||||
return ArgumentType.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer parse(LightEcoPlugin plugin, String input) {
|
||||
return Integer.parseInt(input);
|
||||
}
|
||||
}
|
|
@ -8,8 +8,8 @@ import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
|||
import java.util.UUID;
|
||||
|
||||
public class OfflineUserArgument extends Argument<User> {
|
||||
public OfflineUserArgument(LightEcoPlugin plugin, String name) {
|
||||
super(plugin, name);
|
||||
public OfflineUserArgument(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,12 +23,12 @@ public class OfflineUserArgument extends Argument<User> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public User parse(String input) {
|
||||
UUID uniqueId = this.plugin.getBootstrap().lookupUniqueId(input).orElse(null);
|
||||
public User parse(LightEcoPlugin plugin, String input) {
|
||||
UUID uniqueId = plugin.getBootstrap().lookupUniqueId(input).orElse(null);
|
||||
if (uniqueId == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.plugin.getUserManager().loadUser(uniqueId).join();
|
||||
return plugin.getUserManager().loadUser(uniqueId).join();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package dev.xhyrom.lighteco.common.commands;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.abstraction.Command;
|
||||
import dev.xhyrom.lighteco.common.command.argument.Argument;
|
||||
import dev.xhyrom.lighteco.common.command.argument.Arguments;
|
||||
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 org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Map;
|
||||
|
||||
public class BalanceCommand extends Command {
|
||||
private final Currency currency;
|
||||
|
||||
public BalanceCommand(@Nullable String permission, @NonNull Currency currency) {
|
||||
this(permission, currency, new Argument<?>[0]);
|
||||
}
|
||||
|
||||
public BalanceCommand(@Nullable String permission, @NonNull Currency currency, @NonNull Argument<?>... args) {
|
||||
super("balance", permission + ".balance", args);
|
||||
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(LightEcoPlugin plugin, CommandSender sender, Arguments args) {
|
||||
User user = plugin.getUserManager().getIfLoaded(sender.getUniqueId());
|
||||
BigDecimal balance = user.getBalance(currency);
|
||||
|
||||
sender.sendMessage(
|
||||
MiniMessage.miniMessage().deserialize(
|
||||
getConfig(plugin, currency).balance,
|
||||
Placeholder.parsed("currency", currency.getIdentifier()),
|
||||
Placeholder.parsed("balance", balance.toPlainString())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected CurrencyMessageConfig getConfig(LightEcoPlugin plugin, Currency currency) {
|
||||
Map<String, CurrencyMessageConfig> config = plugin.getConfig().messages.currency;
|
||||
CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier());
|
||||
|
||||
if (currencyMessageConfig == null) {
|
||||
return config.get("default");
|
||||
}
|
||||
|
||||
return currencyMessageConfig;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package dev.xhyrom.lighteco.common.commands;
|
||||
|
||||
import dev.xhyrom.lighteco.common.command.argument.Arguments;
|
||||
import dev.xhyrom.lighteco.common.command.argument.type.OfflineUserArgument;
|
||||
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 org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class BalanceOtherCommand extends BalanceCommand {
|
||||
private final Currency currency;
|
||||
|
||||
public BalanceOtherCommand(@Nullable String permission, @NonNull Currency currency) {
|
||||
super(permission + ".balance.others", currency, new OfflineUserArgument("target"));
|
||||
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(LightEcoPlugin plugin, CommandSender sender, Arguments args) {
|
||||
User target = args.offlineUser("target");
|
||||
BigDecimal balance = target.getBalance(currency);
|
||||
|
||||
sender.sendMessage(
|
||||
MiniMessage.miniMessage().deserialize(
|
||||
getConfig(plugin, currency).balanceOthers,
|
||||
Placeholder.parsed("currency", currency.getIdentifier()),
|
||||
Placeholder.parsed("target", target.getUsername()),
|
||||
Placeholder.parsed("balance", balance.toPlainString())
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue