mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-29 11:59:17 +00:00
refactor: format
This commit is contained in:
@@ -89,7 +89,12 @@ public class BotList {
|
||||
}
|
||||
|
||||
public ServerBot loadNewBot(String realName) {
|
||||
return this.loadNewBot(realName, this.dataStorage);
|
||||
try {
|
||||
return this.loadNewBot(realName, this.dataStorage);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Failed to load bot {}", realName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ServerBot loadNewBot(String realName, IPlayerDataStorage playerIO) {
|
||||
|
||||
@@ -41,7 +41,7 @@ public class Configs {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <O, I, E extends AbstractBotConfig<O, I, E>> @NotNull E register(AbstractBotConfig<O, I, E> instance) {
|
||||
private static <Value, Type, E extends AbstractBotConfig<Value, Type, E>> @NotNull E register(AbstractBotConfig<Value, Type, E> instance) {
|
||||
configs.put(instance.getClass(), instance);
|
||||
return (E) instance;
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.event.HoverEvent.showText;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.AQUA;
|
||||
|
||||
public abstract class AbstractBotConfig<O, I, E extends AbstractBotConfig<O, I, E>> {
|
||||
public abstract class AbstractBotConfig<Value, Type, E extends AbstractBotConfig<Value, Type, E>> {
|
||||
private final String name;
|
||||
private final WrappedArgument<I> argument;
|
||||
private final WrappedArgument<Type> argument;
|
||||
private final Supplier<E> creator;
|
||||
|
||||
protected ServerBot bot;
|
||||
|
||||
public AbstractBotConfig(String name, ArgumentType<I> type, Supplier<E> creator) {
|
||||
public AbstractBotConfig(String name, ArgumentType<Type> type, Supplier<E> creator) {
|
||||
this.name = name;
|
||||
this.argument = new WrappedArgument<>(name, type);
|
||||
if (shouldApplySuggestions()) {
|
||||
@@ -39,7 +39,7 @@ public abstract class AbstractBotConfig<O, I, E extends AbstractBotConfig<O, I,
|
||||
public void applySuggestions(final CommandContext context, final SuggestionsBuilder builder) throws CommandSyntaxException {
|
||||
}
|
||||
|
||||
public AbstractBotConfig<O, I, E> setBot(ServerBot bot) {
|
||||
public AbstractBotConfig<Value, Type, E> setBot(ServerBot bot) {
|
||||
this.bot = bot;
|
||||
return this;
|
||||
}
|
||||
@@ -48,11 +48,11 @@ public abstract class AbstractBotConfig<O, I, E extends AbstractBotConfig<O, I,
|
||||
return creator.get();
|
||||
}
|
||||
|
||||
public abstract O getValue();
|
||||
public abstract Value getValue();
|
||||
|
||||
public abstract void setValue(O value) throws CommandSyntaxException;
|
||||
public abstract void setValue(Value value) throws CommandSyntaxException;
|
||||
|
||||
public abstract O loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException;
|
||||
public abstract Value loadFromCommand(@NotNull CommandContext context) throws CommandSyntaxException;
|
||||
|
||||
public List<Pair<String, String>> getExtraData() {
|
||||
return List.of();
|
||||
@@ -75,7 +75,7 @@ public abstract class AbstractBotConfig<O, I, E extends AbstractBotConfig<O, I,
|
||||
return result;
|
||||
}
|
||||
|
||||
public WrappedArgument<I> getArgument() {
|
||||
public WrappedArgument<Type> getArgument() {
|
||||
return argument;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public class CommandUtils {
|
||||
candidates.add(Candidate.of(item, damerauLevenshteinDistance(lastLower, itemLower)));
|
||||
}
|
||||
}
|
||||
candidates.sort(Comparator.comparingInt(c -> c.score));
|
||||
candidates.sort(Comparator.comparingInt(Candidate::score));
|
||||
|
||||
List<String> results = new ArrayList<>(candidates.size());
|
||||
for (Candidate candidate : candidates) {
|
||||
|
||||
@@ -44,17 +44,14 @@ public class WrappedArgument<T> {
|
||||
public RequiredArgumentBuilder<CommandSourceStack, T> compile() {
|
||||
RequiredArgumentBuilder<CommandSourceStack, T> builder = Commands.argument(name, type);
|
||||
if (asyncSuggestionProvider != null) {
|
||||
builder.suggests(
|
||||
(context, b) ->
|
||||
asyncSuggestionProvider.getSuggestions(new CommandContext(context), b)
|
||||
builder.suggests((context, b) ->
|
||||
asyncSuggestionProvider.getSuggestions(new CommandContext(context), b)
|
||||
);
|
||||
} else if (suggestionApplier != null) {
|
||||
builder.suggests(
|
||||
(context, b) -> {
|
||||
suggestionApplier.applySuggestions(new CommandContext(context), b);
|
||||
return CompletableFuture.completedFuture(b.build());
|
||||
}
|
||||
);
|
||||
builder.suggests((context, b) -> {
|
||||
suggestionApplier.applySuggestions(new CommandContext(context), b);
|
||||
return CompletableFuture.completedFuture(b.build());
|
||||
});
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -6,11 +6,8 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.leavesmc.leaves.bot.BotList;
|
||||
import org.leavesmc.leaves.bot.ServerBot;
|
||||
import org.leavesmc.leaves.entity.bot.Bot;
|
||||
import org.leavesmc.leaves.entity.bot.CraftBot;
|
||||
import org.leavesmc.leaves.command.CommandContext;
|
||||
import org.leavesmc.leaves.command.CustomArgumentType;
|
||||
|
||||
@@ -25,25 +22,25 @@ public class BotArgument implements CustomArgumentType<ServerBot, String> {
|
||||
|
||||
@Override
|
||||
public ServerBot transform(String value) throws CommandSyntaxException {
|
||||
CraftBot craftBot = (CraftBot) Bukkit.getBotManager().getBot(value);
|
||||
if (craftBot == null) {
|
||||
ServerBot bot = BotList.INSTANCE.getBotByName(value);
|
||||
if (bot == null) {
|
||||
throw new CommandSyntaxException(
|
||||
CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument(),
|
||||
Component.literal("Bot with name '" + value + "' does not exist")
|
||||
);
|
||||
}
|
||||
return craftBot.getHandle();
|
||||
return bot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Suggestions> getSuggestions(CommandContext context, SuggestionsBuilder builder) throws CommandSyntaxException {
|
||||
Collection<Bot> bots = Bukkit.getBotManager().getBots();
|
||||
Collection<ServerBot> bots = BotList.INSTANCE.bots;
|
||||
if (bots.isEmpty()) {
|
||||
return builder
|
||||
.suggest("<NO BOT EXISTS>", net.minecraft.network.chat.Component.literal("There are no bots in the server, create one first."))
|
||||
.buildFuture();
|
||||
}
|
||||
bots.stream().map(Player::getName).forEach(builder::suggest);
|
||||
bots.stream().map(ServerBot::getScoreboardName).forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ActionCommand extends BotSubcommand {
|
||||
|
||||
public static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
|
||||
protected BotArgument() {
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
children(
|
||||
StartCommand::new,
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ConfigCommand extends BotSubcommand {
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
|
||||
protected BotArgument() {
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
Configs.getConfigs().stream()
|
||||
.map(this::configNodeCreator)
|
||||
@@ -77,10 +77,10 @@ public class ConfigCommand extends BotSubcommand {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ConfigNode<O> extends LiteralNode {
|
||||
private final AbstractBotConfig<O, ?, ?> config;
|
||||
private static class ConfigNode<Value> extends LiteralNode {
|
||||
private final AbstractBotConfig<Value, ?, ?> config;
|
||||
|
||||
private ConfigNode(@NotNull AbstractBotConfig<O, ?, ?> config) {
|
||||
private ConfigNode(@NotNull AbstractBotConfig<Value, ?, ?> config) {
|
||||
super(config.getName());
|
||||
this.config = config;
|
||||
}
|
||||
@@ -100,7 +100,7 @@ public class ConfigCommand extends BotSubcommand {
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) throws CommandSyntaxException {
|
||||
ServerBot bot = BotArgument.getBot(context);
|
||||
AbstractBotConfig<O, ?, ?> botConfig = bot.getConfig(config);
|
||||
AbstractBotConfig<Value, ?, ?> botConfig = bot.getConfig(config);
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Bot", GRAY),
|
||||
asAdventure(bot.getDisplayName()).append(text("'s", GRAY)),
|
||||
@@ -114,7 +114,7 @@ public class ConfigCommand extends BotSubcommand {
|
||||
|
||||
private boolean executeSet(CommandContext context) throws CommandSyntaxException {
|
||||
ServerBot bot = BotArgument.getBot(context);
|
||||
AbstractBotConfig<O, ?, ?> botConfig = bot.getConfig(config);
|
||||
AbstractBotConfig<Value, ?, ?> botConfig = bot.getConfig(config);
|
||||
try {
|
||||
botConfig.setValue(botConfig.loadFromCommand(context));
|
||||
} catch (ClassCastException e) {
|
||||
|
||||
@@ -100,7 +100,7 @@ public class CreateCommand extends BotSubcommand {
|
||||
|
||||
private static class NameArgument extends ArgumentNode<String> {
|
||||
|
||||
public NameArgument() {
|
||||
private NameArgument() {
|
||||
super("name", StringArgumentType.word());
|
||||
children(SkinNameArgument::new);
|
||||
}
|
||||
@@ -113,7 +113,7 @@ public class CreateCommand extends BotSubcommand {
|
||||
|
||||
private static class SkinNameArgument extends ArgumentNode<String> {
|
||||
|
||||
public SkinNameArgument() {
|
||||
private SkinNameArgument() {
|
||||
super("skin_name", StringArgumentType.word());
|
||||
children(WorldArgument::new);
|
||||
}
|
||||
@@ -122,12 +122,11 @@ public class CreateCommand extends BotSubcommand {
|
||||
protected boolean execute(CommandContext context) throws CommandSyntaxException {
|
||||
return handleCreateCommand(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class WorldArgument extends ArgumentNode<ResourceLocation> {
|
||||
|
||||
public WorldArgument() {
|
||||
private WorldArgument() {
|
||||
super("world", DimensionArgument.dimension());
|
||||
children(LocationArgument::new);
|
||||
}
|
||||
@@ -140,7 +139,7 @@ public class CreateCommand extends BotSubcommand {
|
||||
|
||||
private static class LocationArgument extends ArgumentNode<Coordinates> {
|
||||
|
||||
public LocationArgument() {
|
||||
private LocationArgument() {
|
||||
super("location", Vec3Argument.vec3(true));
|
||||
}
|
||||
|
||||
|
||||
@@ -56,13 +56,13 @@ public class ListCommand extends BotSubcommand {
|
||||
}
|
||||
|
||||
protected static @Nullable Component getBotListMessage(@NotNull World world) {
|
||||
BotList botList = BotList.INSTANCE;
|
||||
List<ServerBot> botsInLevel = botList.bots.stream()
|
||||
List<ServerBot> botsInLevel = BotList.INSTANCE.bots.stream()
|
||||
.filter((bot) -> bot.getBukkitEntity().getWorld().equals(world))
|
||||
.toList();
|
||||
if (botsInLevel.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Component botsMsg = botsInLevel.stream()
|
||||
.map(Player::getDisplayName)
|
||||
.map(PaperAdventure::asAdventure)
|
||||
@@ -78,7 +78,7 @@ public class ListCommand extends BotSubcommand {
|
||||
|
||||
private static class WorldArgument extends ArgumentNode<ResourceLocation> {
|
||||
|
||||
protected WorldArgument() {
|
||||
private WorldArgument() {
|
||||
super("world", DimensionArgument.dimension());
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public class LoadCommand extends BotSubcommand {
|
||||
|
||||
private static class BotNameArgument extends ArgumentNode<String> {
|
||||
|
||||
public BotNameArgument() {
|
||||
private BotNameArgument() {
|
||||
super("bot_name", StringArgumentType.word());
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ public class LoadCommand extends BotSubcommand {
|
||||
text("Successfully loaded bot", NamedTextColor.GRAY),
|
||||
asAdventure(bot.getDisplayName())
|
||||
));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class RemoveCommand extends BotSubcommand {
|
||||
|
||||
private static class BotArgument extends CustomArgumentNode<ServerBot, String> {
|
||||
|
||||
public BotArgument() {
|
||||
private BotArgument() {
|
||||
super("bot", new org.leavesmc.leaves.command.bot.BotArgument());
|
||||
children(RemoveTimeArgument::new);
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public class StartCommand extends LiteralNode {
|
||||
private class ActionLiteralNode extends LiteralNode {
|
||||
private final AbstractBotAction<?> action;
|
||||
|
||||
public ActionLiteralNode(@NotNull AbstractBotAction<?> action) {
|
||||
private ActionLiteralNode(@NotNull AbstractBotAction<?> action) {
|
||||
super(action.getName());
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class StopCommand extends LiteralNode {
|
||||
|
||||
private static class StopIndexArgument extends ArgumentNode<Integer> {
|
||||
|
||||
protected StopIndexArgument() {
|
||||
private StopIndexArgument() {
|
||||
super("index", IntegerArgumentType.integer(0));
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class StopCommand extends LiteralNode {
|
||||
|
||||
private static class StopAll extends LiteralNode {
|
||||
|
||||
public StopAll() {
|
||||
private StopAll() {
|
||||
super("all");
|
||||
}
|
||||
|
||||
|
||||
@@ -29,9 +29,9 @@ public class ConfigCommand extends LeavesSubcommand {
|
||||
|
||||
private static class PathArgument extends ArgumentNode<String> {
|
||||
|
||||
public PathArgument() {
|
||||
private PathArgument() {
|
||||
super("path", StringArgumentType.string());
|
||||
children(ValueArgument::new);
|
||||
children(ConfigCommand.ValueArgument::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,63 +76,64 @@ public class ConfigCommand extends LeavesSubcommand {
|
||||
return verifiedConfig;
|
||||
}
|
||||
|
||||
private static class ValueArgument extends ArgumentNode<String> {
|
||||
}
|
||||
|
||||
public ValueArgument() {
|
||||
super("value", StringArgumentType.greedyString());
|
||||
private static class ValueArgument extends ArgumentNode<String> {
|
||||
|
||||
private ValueArgument() {
|
||||
super("value", StringArgumentType.greedyString());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Suggestions> getSuggestions(@NotNull CommandContext context, @NotNull SuggestionsBuilder builder) {
|
||||
String path = context.getArgument(PathArgument.class);
|
||||
VerifiedConfig verifiedConfig = GlobalConfigManager.getVerifiedConfig(path);
|
||||
if (verifiedConfig == null) {
|
||||
return builder
|
||||
.suggest("<ERROR CONFIG>", net.minecraft.network.chat.Component.literal("This config path does not exist."))
|
||||
.buildFuture();
|
||||
}
|
||||
verifiedConfig.validator().valueSuggest().forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompletableFuture<Suggestions> getSuggestions(@NotNull CommandContext context, @NotNull SuggestionsBuilder builder) {
|
||||
String path = context.getArgument(PathArgument.class);
|
||||
VerifiedConfig verifiedConfig = GlobalConfigManager.getVerifiedConfig(path);
|
||||
if (verifiedConfig == null) {
|
||||
return builder
|
||||
.suggest("<ERROR CONFIG>", net.minecraft.network.chat.Component.literal("This config path does not exist."))
|
||||
.buildFuture();
|
||||
}
|
||||
verifiedConfig.validator().valueSuggest().forEach(builder::suggest);
|
||||
return builder.buildFuture();
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
VerifiedConfig verifiedConfig = PathArgument.getVerifiedConfig(context);
|
||||
String path = context.getArgument(PathArgument.class);
|
||||
String value = context.getArgument(ValueArgument.class);
|
||||
if (verifiedConfig == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean execute(@NotNull CommandContext context) {
|
||||
VerifiedConfig verifiedConfig = getVerifiedConfig(context);
|
||||
String path = context.getArgument(PathArgument.class);
|
||||
String value = context.getArgument(ValueArgument.class);
|
||||
if (verifiedConfig == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
verifiedConfig.set(value);
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Config", GRAY),
|
||||
text(path, AQUA),
|
||||
text("changed to", GRAY),
|
||||
text(verifiedConfig.getString(), AQUA)
|
||||
));
|
||||
Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.filter(player -> player.hasPermission("leaves.command.config.notify") && player != context.getSender())
|
||||
.forEach(
|
||||
player -> player.sendMessage(join(spaces(),
|
||||
text(context.getSender().getName() + ":", GRAY),
|
||||
text("Config", GRAY),
|
||||
text(path, AQUA),
|
||||
text("changed to", GRAY),
|
||||
text(verifiedConfig.getString(), AQUA)
|
||||
))
|
||||
);
|
||||
return true;
|
||||
} catch (IllegalArgumentException exception) {
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Config", GRAY),
|
||||
text(path, RED),
|
||||
text("modify error by", GRAY),
|
||||
text(exception.getMessage(), RED)
|
||||
));
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
verifiedConfig.set(value);
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Config", GRAY),
|
||||
text(path, AQUA),
|
||||
text("changed to", GRAY),
|
||||
text(verifiedConfig.getString(), AQUA)
|
||||
));
|
||||
Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.filter(player -> player.hasPermission("leaves.command.config.notify") && player != context.getSender())
|
||||
.forEach(
|
||||
player -> player.sendMessage(join(spaces(),
|
||||
text(context.getSender().getName() + ":", GRAY),
|
||||
text("Config", GRAY),
|
||||
text(path, AQUA),
|
||||
text("changed to", GRAY),
|
||||
text(verifiedConfig.getString(), AQUA)
|
||||
))
|
||||
);
|
||||
return true;
|
||||
} catch (IllegalArgumentException exception) {
|
||||
context.getSender().sendMessage(join(spaces(),
|
||||
text("Config", GRAY),
|
||||
text(path, RED),
|
||||
text("modify error by", GRAY),
|
||||
text(exception.getMessage(), RED)
|
||||
));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user