1
0
Fork 0
mirror of https://github.com/xHyroM/lighteco.git synced 2024-11-12 18:38:07 +01:00

refactor: move methods to CommandHelper

This commit is contained in:
Jozef Steinhübl 2024-07-16 17:05:45 +02:00
parent f677ab002f
commit 5b2806fd2a
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
12 changed files with 97 additions and 71 deletions

View file

@ -0,0 +1,55 @@
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;
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;
import java.util.Map;
@UtilityClass
public class CommandHelper {
public static User getUser(CommandContext<CommandSource> 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<String, CurrencyMessageConfig> config = plugin.getConfig().messages.currency;
CurrencyMessageConfig currencyMessageConfig = config.get(currency.getIdentifier());
if (currencyMessageConfig == null) {
return config.get("default");
}
return currencyMessageConfig;
}
}

View file

@ -2,19 +2,13 @@ package dev.xhyrom.lighteco.common.command.abstraction;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import dev.xhyrom.lighteco.common.command.CommandSource;
import dev.xhyrom.lighteco.common.config.message.CurrencyMessageConfig;
import dev.xhyrom.lighteco.common.model.currency.Currency;
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 java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Getter
public abstract class Command {
@ -39,15 +33,4 @@ public abstract class Command {
protected LiteralArgumentBuilder<CommandSource> builder() {
return LiteralArgumentBuilder.literal(name);
}
protected CurrencyMessageConfig getCurrencyMessageConfig(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;
}
}

View file

@ -5,6 +5,7 @@ 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;
@ -16,7 +17,7 @@ import java.util.UUID;
public class OfflineUserArgument implements ArgumentType<String> {
private OfflineUserArgument() {}
public static User getOfflineUser(CommandContext<CommandSource> context, String name) {
public static User getOfflineUser(CommandContext<CommandSource> context, String name) throws LockedUserException {
String userName = context.getArgument(name, String.class);
LightEcoPlugin plugin = context.getSource().plugin();
CommandSender sender = context.getSource().sender();
@ -27,7 +28,7 @@ public class OfflineUserArgument implements ArgumentType<String> {
}
if (sender.getUniqueId() != uniqueId && plugin.getCommandManager().getLocks().contains(uniqueId)) {
return null;
throw new LockedUserException(uniqueId);
}
// Lock the user to prevent race conditions
@ -36,17 +37,6 @@ public class OfflineUserArgument implements ArgumentType<String> {
return plugin.getUserManager().loadUser(uniqueId).join();
}
public static void handleMissing(CommandContext<CommandSource> context, String name) {
String userName = context.getArgument(name, String.class);
LightEcoPlugin plugin = context.getSource().plugin();
CommandSender sender = context.getSource().sender();
sender.sendMessage(MiniMessage.miniMessage().deserialize(
plugin.getConfig().messages.invalidUser,
Placeholder.parsed("username", userName)
));
}
@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return reader.readString();

View file

@ -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");
}
}

View file

@ -2,12 +2,9 @@ package dev.xhyrom.lighteco.common.commands;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
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.argument.type.OfflineUserArgument;
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;
@ -20,6 +17,7 @@ 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;
@ -45,11 +43,9 @@ public class BalanceCommand extends Command {
LightEcoPlugin plugin = context.getSource().plugin();
CommandSender sender = context.getSource().sender();
User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
final User target = getUser(context);
if (target == null)
return SINGLE_SUCCESS;
}
BigDecimal balance = target.getBalance(currency);

View file

@ -15,6 +15,7 @@ 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;

View file

@ -9,7 +9,6 @@ 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.argument.type.OfflineUserArgument;
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;
@ -23,6 +22,7 @@ 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();
@ -42,11 +42,9 @@ public class GiveCommand extends Command {
final LightEcoPlugin plugin = context.getSource().plugin();
final CommandSender sender = context.getSource().sender();
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
final User target = getUser(context);
if (target == null)
return;
}
BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0
? context.getArgument("amount", Double.class)
@ -59,7 +57,7 @@ public class GiveCommand extends Command {
} catch (CannotBeGreaterThan e) {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString())
)
);
@ -69,7 +67,7 @@ public class GiveCommand extends Command {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).set,
getCurrencyMessageConfig(plugin, this.currency).set,
Placeholder.parsed("currency", currency.getIdentifier()),
Placeholder.parsed("target", target.getUsername()),
Placeholder.parsed("amount", amount.toPlainString())

View file

@ -9,7 +9,6 @@ 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.argument.type.OfflineUserArgument;
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;
@ -23,6 +22,7 @@ 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();
@ -42,11 +42,9 @@ public class PayCommand extends Command {
final LightEcoPlugin plugin = context.getSource().plugin();
final CommandSender sender = context.getSource().sender();
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
final User target = getUser(context);
if (target == null)
return;
}
BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0
? context.getArgument("amount", Double.class)
@ -61,7 +59,7 @@ public class PayCommand extends Command {
if (user.getBalance(this.currency).compareTo(amount) < 0) {
sender.sendMessage(
miniMessage.deserialize(this.getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney)
miniMessage.deserialize(getCurrencyMessageConfig(plugin, this.currency).notEnoughMoney)
);
return;
@ -80,7 +78,7 @@ public class PayCommand extends Command {
} catch (CannotBeGreaterThan e) {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString())
)
);
@ -89,12 +87,12 @@ public class PayCommand extends Command {
}
String template = tax.compareTo(BigDecimal.ZERO) > 0
? this.getCurrencyMessageConfig(plugin, this.currency).payWithTax
: this.getCurrencyMessageConfig(plugin, this.currency).pay;
? getCurrencyMessageConfig(plugin, this.currency).payWithTax
: getCurrencyMessageConfig(plugin, this.currency).pay;
String templateReceived = tax.compareTo(BigDecimal.ZERO) > 0
? this.getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax
: this.getCurrencyMessageConfig(plugin, this.currency).payReceived;
? getCurrencyMessageConfig(plugin, this.currency).payReceivedWithTax
: getCurrencyMessageConfig(plugin, this.currency).payReceived;
sender.sendMessage(
miniMessage.deserialize(

View file

@ -9,7 +9,6 @@ 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.argument.type.OfflineUserArgument;
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;
@ -23,6 +22,7 @@ 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();
@ -42,11 +42,9 @@ public class SetCommand extends Command {
final LightEcoPlugin plugin = context.getSource().plugin();
final CommandSender sender = context.getSource().sender();
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
final User target = getUser(context);
if (target == null)
return;
}
BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0
? context.getArgument("amount", Double.class)
@ -59,7 +57,7 @@ public class SetCommand extends Command {
} catch (CannotBeGreaterThan e) {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString())
)
);
@ -69,7 +67,7 @@ public class SetCommand extends Command {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).set,
getCurrencyMessageConfig(plugin, this.currency).set,
Placeholder.parsed("currency", currency.getIdentifier()),
Placeholder.parsed("target", target.getUsername()),
Placeholder.parsed("amount", amount.toPlainString())

View file

@ -9,7 +9,6 @@ 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.argument.type.OfflineUserArgument;
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;
@ -23,6 +22,7 @@ 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();
@ -42,11 +42,9 @@ public class TakeCommand extends Command {
final LightEcoPlugin plugin = context.getSource().plugin();
final CommandSender sender = context.getSource().sender();
final User target = OfflineUserArgument.getOfflineUser(context, "target");
if (target == null || target.getUsername() == null) {
OfflineUserArgument.handleMissing(context, "target");
final User target = getUser(context);
if (target == null)
return;
}
BigDecimal amount = BigDecimal.valueOf(currency.fractionalDigits() > 0
? context.getArgument("amount", Double.class)
@ -59,7 +57,7 @@ public class TakeCommand extends Command {
} catch (CannotBeGreaterThan e) {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
getCurrencyMessageConfig(plugin, this.currency).cannotBeGreaterThan,
Placeholder.parsed("max", plugin.getConfig().maximumBalance.toPlainString())
)
);
@ -69,7 +67,7 @@ public class TakeCommand extends Command {
sender.sendMessage(
miniMessage.deserialize(
this.getCurrencyMessageConfig(plugin, this.currency).set,
getCurrencyMessageConfig(plugin, this.currency).set,
Placeholder.parsed("currency", currency.getIdentifier()),
Placeholder.parsed("target", target.getUsername()),
Placeholder.parsed("amount", amount.toPlainString())

View file

@ -9,5 +9,5 @@ public class MessageConfig extends OkaeriConfig {
public Map<String, CurrencyMessageConfig> currency = Collections.singletonMap("default", new CurrencyMessageConfig());
public String wait = "<red>Please wait a moment before using this command again.";
public String invalidUser = "<red>User <username> not found.";
public String userNotFound = "<red>User <username> not found.";
}

View file

@ -1,7 +1,7 @@
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