From ac3fa4dbd2c514e0de7d92d35830b6a25b57399b Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Fri, 6 Dec 2024 02:03:57 -0500 Subject: [PATCH] Add the list subcommand to the party command --- .../xyz/nucleoid/parties/PartyCommand.java | 58 +++++++++++++++++++ .../xyz/nucleoid/parties/PartyManager.java | 5 ++ .../java/xyz/nucleoid/parties/PartyTexts.java | 38 ++++++++++++ .../data/game_parties/lang/en_us.json | 6 ++ 4 files changed, 107 insertions(+) diff --git a/src/main/java/xyz/nucleoid/parties/PartyCommand.java b/src/main/java/xyz/nucleoid/parties/PartyCommand.java index 5f7b04a..861c59a 100644 --- a/src/main/java/xyz/nucleoid/parties/PartyCommand.java +++ b/src/main/java/xyz/nucleoid/parties/PartyCommand.java @@ -7,10 +7,15 @@ import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.GameProfileArgumentType; import net.minecraft.command.argument.UuidArgumentType; +import net.minecraft.screen.ScreenTexts; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; import net.minecraft.util.Formatting; import xyz.nucleoid.plasmid.api.util.PlayerRef; +import java.util.ArrayList; +import java.util.Comparator; + import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; @@ -19,6 +24,10 @@ public final class PartyCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register( literal("party") + .then(literal("list") + .requires(source -> source.hasPermissionLevel(2)) + .executes(PartyCommand::listParties) + ) .then(literal("invite") .then(argument("player", EntityArgumentType.player()) .executes(PartyCommand::invitePlayer) @@ -45,6 +54,55 @@ public static void register(CommandDispatcher dispatcher) { } // @formatter:on + private static int listParties(CommandContext ctx) throws CommandSyntaxException { + var source = ctx.getSource(); + var server = source.getServer(); + + var partyManager = PartyManager.get(server); + var parties = new ArrayList<>(partyManager.getAllParties()); + + if (parties.isEmpty()) { + source.sendError(PartyTexts.noParties()); + return 0; + } + + parties.sort(Comparator.comparing(Party::getUuid)); + + source.sendFeedback(() -> { + var text = Text.empty(); + boolean first = true; + + for (var party : parties) { + if (first) { + first = false; + } else { + text.append(ScreenTexts.LINE_BREAK); + } + + text.append(PartyTexts.listEntry(party.getUuid())); + + var members = new ArrayList<>(party.getMembers()); + members.sort(Comparator.comparing(PlayerRef::id)); + + for (var member : members) { + text.append(ScreenTexts.LINE_BREAK); + + if (party.isOwner(member)) { + text.append(PartyTexts.listMemberEntryType(member, server, PartyTexts.listMemberTypeOwner().formatted(Formatting.LIGHT_PURPLE))); + } else if (party.contains(member)) { + text.append(PartyTexts.listMemberEntry(member, server)); + } else { + text.append(PartyTexts.listMemberEntryType(member, server, PartyTexts.listMemberTypePending().formatted(Formatting.GRAY))); + } + } + } + + return text; + }, false); + + return parties.size(); + } + private static int invitePlayer(CommandContext ctx) throws CommandSyntaxException { var source = ctx.getSource(); var owner = source.getPlayer(); diff --git a/src/main/java/xyz/nucleoid/parties/PartyManager.java b/src/main/java/xyz/nucleoid/parties/PartyManager.java index dd9abd9..e07a1d1 100644 --- a/src/main/java/xyz/nucleoid/parties/PartyManager.java +++ b/src/main/java/xyz/nucleoid/parties/PartyManager.java @@ -12,6 +12,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.UUID; import java.util.List; @@ -222,4 +223,8 @@ public Collection getPartyMembers(ServerPlayerEntity player) return Collections.singleton(player); } } + + public Collection getAllParties() { + return new HashSet<>(this.playerToParty.values()); + } } diff --git a/src/main/java/xyz/nucleoid/parties/PartyTexts.java b/src/main/java/xyz/nucleoid/parties/PartyTexts.java index f6539b4..792a3bd 100644 --- a/src/main/java/xyz/nucleoid/parties/PartyTexts.java +++ b/src/main/java/xyz/nucleoid/parties/PartyTexts.java @@ -2,10 +2,14 @@ import java.util.UUID; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import net.minecraft.text.Texts; +import net.minecraft.util.Formatting; import xyz.nucleoid.plasmid.api.game.GameTexts; +import xyz.nucleoid.plasmid.api.util.PlayerRef; public final class PartyTexts { public static MutableText displayError(PartyError error, ServerPlayerEntity player) { @@ -71,4 +75,38 @@ public static MutableText inviteNotificationLink(ServerPlayerEntity owner, UUID public static MutableText leftGame(ServerPlayerEntity player) { return Text.translatable("text.game_parties.party.left_game", player.getDisplayName()); } + + public static MutableText noParties() { + return Text.translatable("text.game_parties.party.list.none"); + } + + public static MutableText listEntry(UUID uuid) { + return Text.translatable("text.game_parties.party.list.entry", Texts.bracketedCopyable(uuid.toString())); + } + + public static MutableText listMemberEntry(PlayerRef member, MinecraftServer server) { + return Text.translatable("text.game_parties.party.list.member.entry", name(member, server)); + } + + public static MutableText listMemberEntryType(PlayerRef member, MinecraftServer server, Text type) { + return Text.translatable("text.game_parties.party.list.member.entry.type", name(member, server), type); + } + + public static MutableText listMemberTypeOwner() { + return Text.translatable("text.game_parties.party.list.member.type.owner"); + } + + public static MutableText listMemberTypePending() { + return Text.translatable("text.game_parties.party.list.member.type.pending"); + } + + private static Text name(PlayerRef ref, MinecraftServer server) { + var player = ref.getEntity(server); + if (player == null) { + Text id = Text.literal(ref.id().toString()); + return Texts.bracketed(id).formatted(Formatting.GRAY); + } + + return player.getDisplayName(); + } } diff --git a/src/main/resources/data/game_parties/lang/en_us.json b/src/main/resources/data/game_parties/lang/en_us.json index a9cdd5b..4d7269b 100644 --- a/src/main/resources/data/game_parties/lang/en_us.json +++ b/src/main/resources/data/game_parties/lang/en_us.json @@ -14,6 +14,12 @@ "text.game_parties.party.kicked.receiver": "You have been kicked from the party", "text.game_parties.party.kicked.sender": "%s has been kicked from the party", "text.game_parties.party.leave.success": "%s has left the party!", + "text.game_parties.party.list.entry": " - Party %s", + "text.game_parties.party.list.member.entry": " - %s", + "text.game_parties.party.list.member.entry.type": " - %s (%s)", + "text.game_parties.party.list.member.type.owner": "owner", + "text.game_parties.party.list.member.type.pending": "pending", + "text.game_parties.party.list.none": "There are no parties!", "text.game_parties.party.transferred.receiver": "%s's party has been transferred to you", "text.game_parties.party.transferred.sender": "Your party has been transferred to %s", "text.game_parties.party.left_game": "%s left the game and has been removed from the party!"