Skip to content

Commit

Permalink
adjust party command messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 1, 2023
1 parent 0da3919 commit fb28746
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,45 +179,24 @@ private void invitePlayer(final CommandContext<Commander> 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<Commander> ctx) {
final @Nullable CarbonPlayer sender = ctx.getOrDefault("sender", null);
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final @Nullable Cache<UUID, UUID> cache = this.partyInvites.invitesFor(player.uuid());
final @Nullable Pair<UUID, UUID> 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<UUID, UUID> map = Map.copyOf(cache.asMap());
if (map.size() == 1) {
final Map.Entry<UUID, UUID> 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();
if (old != null) {
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<Commander> ctx) {
Expand Down Expand Up @@ -250,4 +229,38 @@ private void disbandParty(final CommandContext<Commander> ctx) {
this.messages.disbandedParty(player, old.name());
}

private @Nullable Invite findInvite(final CarbonPlayer player, final @Nullable CarbonPlayer sender) {
final @Nullable Cache<UUID, UUID> cache = this.partyInvites.invitesFor(player.uuid());
final @Nullable Map<UUID, UUID> 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<UUID, UUID> 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) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ command.party.current_party=<green>You are in party<white>:</white></green> <par
command.party.must_leave_current_first=<red>You must leave your current party first.
command.party.name_too_long=<red>Party name is too long.
command.party.received_invite=<green>You were invited to the party '</green><party_name><green>' by </green><sender_display_name><green>.
command.party.sent_invite=<green>Sent party invite to </green><recipient_display_name><green>.
command.party.must_specify_invite=<red>You must specify whose party invite to accept.
command.party.no_pending_invites=<red>You do not have any pending party invites.
command.party.no_invite_from=<red>You do not have a pending invite from </red><sender_display_name><red>.
command.party.joined_party=<green>Successfully joined party '</green><party_name><green>'!
command.party.left_party=<green>Successfully left party '</green><party_name><green>'.
command.party.disbanded=<green>Successfully disbanded party '</green><party_name><green>'.
Expand Down

0 comments on commit fb28746

Please sign in to comment.