Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor /ignorelist command #404

Merged
merged 1 commit into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,83 @@
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.Description;
import java.util.List;
import java.util.UUID;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Stream;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.entity.Player;
import vg.civcraft.mc.civchat2.ChatStrings;
import org.jetbrains.annotations.NotNull;
import vg.civcraft.mc.civchat2.CivChat2;
import vg.civcraft.mc.civchat2.database.CivChatDAO;
import vg.civcraft.mc.namelayer.NameAPI;

public class IgnoreList extends BaseCommand {

public final class IgnoreList extends BaseCommand {
@CommandAlias("ignorelist")
@Description("Lists the players and groups you are ignoring")
public void execute(Player player) {
CivChatDAO db = CivChat2.getInstance().getDatabaseManager();
List<UUID> players = db.getIgnoredPlayers(player.getUniqueId());
List<String> groups = db.getIgnoredGroups(player.getUniqueId());
private void execute(
final @NotNull Player sender
) {
final CivChatDAO db = CivChat2.getInstance().getDatabaseManager();

// No players ignored
if (players == null || players.isEmpty()) {
player.sendMessage(ChatStrings.chatNotIgnoringAnyPlayers);
} else {
StringBuilder sb = new StringBuilder();
sb.append("<a>Ignored Players: \n<n>");
for (UUID playerUUID : players) {
String playerName = NameAPI.getCurrentName(playerUUID);
if (playerName != null) {
sb.append(playerName);
sb.append(", ");
}
}
String msg = sb.toString();
if (msg.endsWith(", ")) {
msg = msg.substring(0, msg.length() - 2);
}
player.sendMessage(msg);
}
// Ignored players
sender.sendMessage(
Component.text()
.content("Ignored players: [")
.append(
db.getIgnoredPlayers(sender.getUniqueId())
.stream()
.map(NameAPI::getCurrentName)
.filter(Objects::nonNull)
.flatMap(commaSeparatedClickableNames("/ignore %s"))
.toList()
)
.append(Component.text("]"))
);

// Ignored groups
sender.sendMessage(
Component.text()
.content("Ignored groups: [")
.append(
db.getIgnoredGroups(sender.getUniqueId())
.stream()
.flatMap(commaSeparatedClickableNames("/ignoregroup %s"))
.toList()
)
.append(Component.text("]"))
);
}

// No groups ignored
if (groups == null || groups.isEmpty()) {
player.sendMessage(ChatStrings.chatNotIgnoringAnyGroups);
return;
} else {
StringBuilder sb = new StringBuilder();
sb.append("<a>Ignored Groups: \n<n>");
for (String s : groups) {
sb.append(s);
sb.append(", ");
private static @NotNull Function<String, Stream<Component>> commaSeparatedClickableNames(
final @NotNull String formattedCommand
) {
final var hasPreviousName = new AtomicBoolean(false);
return (name) -> {
final Component clickableName = Component.text()
.content(name)
.color(NamedTextColor.YELLOW)
.decoration(TextDecoration.UNDERLINED, TextDecoration.State.TRUE)
.clickEvent(ClickEvent.clickEvent(
ClickEvent.Action.COPY_TO_CLIPBOARD,
formattedCommand.formatted(name)
))
.hoverEvent(HoverEvent.showText(Component.text("Click to copy un-ignore command")))
.build();
if (hasPreviousName.get()) {
return Stream.of(
Component.text(", "),
clickableName
);
}
String msg = sb.toString();
if (msg.endsWith(", ")) {
msg = msg.substring(0, msg.length() - 2);
else {
hasPreviousName.set(true);
return Stream.of(clickableName);
}
player.sendMessage(msg);
}
};
}
}
Loading