From fb28746e7da2678d3264e40793c3b7239b4890a8 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 1 Oct 2023 07:36:54 -0700 Subject: [PATCH] adjust party command messages --- .../command/commands/PartyCommands.java | 69 +++++++++++-------- .../common/messages/CarbonMessages.java | 6 ++ .../locale/messages-en_US.properties | 2 + 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java b/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java index cc9de8a09..3af0fcc68 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/PartyCommands.java @@ -23,10 +23,10 @@ import cloud.commandframework.arguments.standard.StringArgument; import cloud.commandframework.context.CommandContext; import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys; -import cloud.commandframework.types.tuples.Pair; import com.github.benmanes.caffeine.cache.Cache; import com.google.inject.Inject; import java.util.Map; +import java.util.Optional; import java.util.UUID; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.Party; @@ -179,35 +179,14 @@ private void invitePlayer(final CommandContext ctx) { } this.partyInvites.sendInvite(player.uuid(), recipient.uuid(), party.id()); this.messages.receivedPartyInvite(recipient, player.displayName(), party.name()); + this.messages.sentPartyInvite(player, recipient.displayName(), party.name()); } private void acceptInvite(final CommandContext ctx) { final @Nullable CarbonPlayer sender = ctx.getOrDefault("sender", null); final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer(); - final @Nullable Cache cache = this.partyInvites.invitesFor(player.uuid()); - final @Nullable Pair inv; - if (cache == null) { - inv = null; - } else if (sender != null) { - final @Nullable UUID i = cache.getIfPresent(sender.uuid()); - inv = i == null ? null : Pair.of(sender.uuid(), i); - } else { - final Map map = Map.copyOf(cache.asMap()); - if (map.size() == 1) { - final Map.Entry e = map.entrySet().iterator().next(); - inv = Pair.of(e.getKey(), e.getValue()); - } else { - this.messages.mustSpecifyPartyInvite(player); - return; - } - } - if (inv == null) { - this.messages.noPendingPartyInvites(player); - return; - } - final @Nullable Party party = this.userManager.party(inv.getSecond()).join(); - if (party == null) { - this.messages.noPendingPartyInvites(player); + final @Nullable Invite invite = this.findInvite(player, sender); + if (invite == null) { return; } final @Nullable Party old = player.party().join(); @@ -215,9 +194,9 @@ private void acceptInvite(final CommandContext ctx) { this.messages.mustLeavePartyFirst(player); return; } - this.partyInvites.invalidateInvite(inv.getFirst(), player.uuid()); - party.addMember(player.uuid()); - this.messages.joinedParty(player, party.name()); + this.partyInvites.invalidateInvite(invite.sender(), player.uuid()); + invite.party().addMember(player.uuid()); + this.messages.joinedParty(player, invite.party().name()); } private void leaveParty(final CommandContext ctx) { @@ -250,4 +229,38 @@ private void disbandParty(final CommandContext ctx) { this.messages.disbandedParty(player, old.name()); } + private @Nullable Invite findInvite(final CarbonPlayer player, final @Nullable CarbonPlayer sender) { + final @Nullable Cache cache = this.partyInvites.invitesFor(player.uuid()); + final @Nullable Map map = cache != null ? Map.copyOf(cache.asMap()) : null; + + if (map == null || map.isEmpty()) { + this.messages.noPendingPartyInvites(player); + return null; + } else if (sender != null) { + final @Nullable Party p = Optional.ofNullable(map.get(sender.uuid())) + .map(id -> this.userManager.party(id).join()) + .orElse(null); + if (p == null) { + this.messages.noPartyInviteFrom(player, sender.displayName()); + return null; + } + return new Invite(sender.uuid(), p); + } + + if (map.size() == 1) { + final Map.Entry e = map.entrySet().iterator().next(); + final @Nullable Party p = this.userManager.party(e.getValue()).join(); + if (p == null) { + this.messages.noPendingPartyInvites(player); + return null; + } + return new Invite(e.getKey(), p); + } + + this.messages.mustSpecifyPartyInvite(player); + return null; + } + + private record Invite(UUID sender, Party party) {} + } diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 92793fdf3..22ce8d751 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -445,12 +445,18 @@ void errorCommandCommandExecution( @Message("command.party.received_invite") void receivedPartyInvite(Audience audience, Component senderDisplayName, Component partyName); + @Message("command.party.sent_invite") + void sentPartyInvite(Audience audience, Component recipientDisplayName, Component partyName); + @Message("command.party.must_specify_invite") void mustSpecifyPartyInvite(Audience audience); @Message("command.party.no_pending_invites") void noPendingPartyInvites(Audience audience); + @Message("command.party.no_invite_from") + void noPartyInviteFrom(Audience audience, Component senderDisplayName); + @Message("command.party.joined_party") void joinedParty(Audience audience, Component partyName); diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index 4165423e3..9d746f2ce 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -67,8 +67,10 @@ command.party.current_party=You are in party: You must leave your current party first. command.party.name_too_long=Party name is too long. command.party.received_invite=You were invited to the party '' by . +command.party.sent_invite=Sent party invite to . command.party.must_specify_invite=You must specify whose party invite to accept. command.party.no_pending_invites=You do not have any pending party invites. +command.party.no_invite_from=You do not have a pending invite from . command.party.joined_party=Successfully joined party ''! command.party.left_party=Successfully left party ''. command.party.disbanded=Successfully disbanded party ''.