Skip to content

Commit

Permalink
feat: tab complete for collection name
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyAreaBean committed Apr 22, 2024
1 parent c461292 commit c8406a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import org.inventivetalent.update.spiget.SpigetUpdate;
import org.inventivetalent.update.spiget.UpdateCallback;
import org.inventivetalent.update.spiget.comparator.VersionComparator;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import revxrsal.commands.autocomplete.SuggestionProvider;
import revxrsal.commands.bukkit.BukkitCommandHandler;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.ExecutableCommand;

import java.io.File;
import java.net.URI;
Expand All @@ -33,6 +37,8 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Slf4j(topic = "SimpleJoinMessage")
public class SimpleJoinMessage extends JavaPlugin {
Expand Down Expand Up @@ -138,6 +144,8 @@ public void loadSync() {

logger().warn("The collection [%s] does not exist, skipping sync process.".formatted(collection));
});

Bukkit.getScheduler().runTaskLater(this, () -> database.updateCollectionNames(), 1L);
}

private void loadMisc() {
Expand All @@ -160,6 +168,12 @@ private void registerCommands() {
commandHandler = BukkitCommandHandler.create(this);
commandHandler.enableAdventure(adventure);
commandHandler.setMessagePrefix(LEGACY_SERIALIZER.serialize(Constants.PREFIX));
commandHandler.getAutoCompleter().registerSuggestion("sjmData", new SuggestionProvider() {
@Override
public @NotNull Collection<String> getSuggestions(@NotNull List<String> list, @NotNull CommandActor commandActor, @NotNull ExecutableCommand executableCommand) throws Throwable {
return database.getCollections();
}
});
commandHandler.setHelpWriter((command, actor) -> {
StringBuilder sb = new StringBuilder();
sb.append("&8• &e/");
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import revxrsal.commands.annotation.AutoComplete;
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.Default;
import revxrsal.commands.annotation.DefaultFor;
Expand Down Expand Up @@ -154,12 +155,14 @@ public void collectionCreate(BukkitCommandActor actor, @Named("collection") Stri
return;
}

SimpleJoinMessage.getInstance().getDatabase().updateCollectionNames();
actor.reply(Constants.PREFIX.append(text("Collection %s has been successfully created.".formatted(collectionName), NamedTextColor.GREEN)));
});
}

@Subcommand({"collection remove"})
@Description("Remove a collection")
@AutoComplete("@sjmData")
public void collectionRemove(BukkitCommandActor actor, @Named("collection") String collectionName) {
actor.reply(Constants.PREFIX.append(text("Removing collection %s...".formatted(collectionName), NamedTextColor.YELLOW)));

Expand All @@ -173,12 +176,14 @@ public void collectionRemove(BukkitCommandActor actor, @Named("collection") Stri
return;
}

SimpleJoinMessage.getInstance().getDatabase().updateCollectionNames();
actor.reply(Constants.PREFIX.append(text("Collection %s has been successfully removed.".formatted(collectionName), NamedTextColor.GREEN)));
});
}

@Subcommand({"collection update"})
@Description("Update a collection with current join message")
@AutoComplete("@sjmData")
public void collectionUpdate(BukkitCommandActor actor, @Named("collection") String collectionName) {
actor.reply(Constants.PREFIX.append(text("Updating collection %s...".formatted(collectionName), NamedTextColor.YELLOW)));

Expand All @@ -196,19 +201,9 @@ public void collectionUpdate(BukkitCommandActor actor, @Named("collection") Stri
});
}

@Subcommand({"collection list"})
@Description("List all the available collection")
public void collectionList(BukkitCommandActor actor) {
SimpleJoinMessage.getInstance().getDatabase().getData().streamAllValuesAsync()
.thenAsync(data -> {
List<String> results = data.map(SJMData::getCollectionName).sorted().toList();
actor.reply(Constants.PREFIX.append(text("Available collection%s:".formatted(results.size() > 1 ? "s" : ""), NamedTextColor.GREEN)));
actor.reply(Constants.PREFIX.append(text(String.join(", ", results), NamedTextColor.YELLOW)));
});
}

@Subcommand({"collection show"})
@Description("Show join message from the collection target")
@AutoComplete("@sjmData")
public void collectionShow(BukkitCommandActor actor, @Named("collection") String collectionName) {
Player player = actor.requirePlayer();

Expand Down Expand Up @@ -237,8 +232,20 @@ public void collectionShow(BukkitCommandActor actor, @Named("collection") String
}));
}

@Subcommand({"collection list"})
@Description("List all the available collection")
public void collectionList(BukkitCommandActor actor) {
SimpleJoinMessage.getInstance().getDatabase().getData().streamAllValuesAsync()
.thenAsync(data -> {
List<String> results = data.map(SJMData::getCollectionName).sorted().toList();
actor.reply(Constants.PREFIX.append(text("Available collection%s:".formatted(results.size() > 1 ? "s" : ""), NamedTextColor.GREEN)));
actor.reply(Constants.PREFIX.append(text(String.join(", ", results), NamedTextColor.YELLOW)));
});
}

@Subcommand({"sync set"})
@Description("Set collection target for sync on startup")
@AutoComplete("@sjmData")
public void syncSet(BukkitCommandActor actor, @Named("collection") String collectionName) {

SJMSync sync = SimpleJoinMessage.getInstance().getSyncConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

Expand All @@ -30,6 +32,7 @@
public class DatabaseManager {

private final Repository<String, SJMData> data;
private final List<String> collections = new ArrayList<>();

public DatabaseManager() {
loadDatabases();
Expand Down Expand Up @@ -147,5 +150,11 @@ public Optional<SJMData> getContent(String collectionName) {
.findOne(NQuery.predicate("$.collection_name == " + collectionName));
}

public void updateCollectionNames() {
collections.clear();
data.streamAllValuesAsync().thenAsync(stream -> {
stream.map(SJMData::getCollectionName).sorted().forEachOrdered(collections::add);
});
}

}

0 comments on commit c8406a7

Please sign in to comment.