Skip to content

Commit

Permalink
Add the list subcommand to the party command
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Patbox committed Dec 8, 2024
1 parent 9618a78 commit ac3fa4d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/xyz/nucleoid/parties/PartyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +24,10 @@ public final class PartyCommand {
public static void register(CommandDispatcher<ServerCommandSource> 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)
Expand All @@ -45,6 +54,55 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
}
// @formatter:on

private static int listParties(CommandContext<ServerCommandSource> 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<ServerCommandSource> ctx) throws CommandSyntaxException {
var source = ctx.getSource();
var owner = source.getPlayer();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/nucleoid/parties/PartyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.UUID;
import java.util.List;

Expand Down Expand Up @@ -222,4 +223,8 @@ public Collection<ServerPlayerEntity> getPartyMembers(ServerPlayerEntity player)
return Collections.singleton(player);
}
}

public Collection<Party> getAllParties() {
return new HashSet<>(this.playerToParty.values());
}
}
38 changes: 38 additions & 0 deletions src/main/java/xyz/nucleoid/parties/PartyTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
6 changes: 6 additions & 0 deletions src/main/resources/data/game_parties/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down

0 comments on commit ac3fa4d

Please sign in to comment.