Skip to content

Commit

Permalink
adjust player party api
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Sep 30, 2023
1 parent 3a3d36f commit f9988d7
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 70 deletions.
11 changes: 5 additions & 6 deletions api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.audience.Audience;
Expand Down Expand Up @@ -248,7 +249,7 @@ record ChannelMessage(Component message, ChatChannel channel) {}
/**
* Adds the player to and removes the player from the ignore list.
*
* @param player the player to be added/removed
* @param player the player to be added/removed
* @param nowIgnoring if the player should be ignored
* @since 2.0.0
*/
Expand All @@ -257,7 +258,7 @@ record ChannelMessage(Component message, ChatChannel channel) {}
/**
* Adds the player to and removes the player from the ignore list.
*
* @param player the player to be added/removed
* @param player the player to be added/removed
* @param nowIgnoring if the player should be ignored
* @since 2.0.0
*/
Expand Down Expand Up @@ -404,10 +405,8 @@ record ChannelMessage(Component message, ChatChannel channel) {}
*/
void leaveChannel(ChatChannel channel);

// TODO use Party type for public API
CompletableFuture<@Nullable Party> party();

@Nullable UUID party();

void party(@Nullable UUID id);
void party(@Nullable Party party);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -60,26 +61,27 @@ public PartyChatChannel() {

@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return player.party() != null
return player.party().join() != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
}

@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return player.party() != null
return player.party().join() != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
}

@Override
public List<Audience> recipients(final CarbonPlayer sender) {
final @Nullable UUID party = sender.party();
final WrappedCarbonPlayer wrapped = (WrappedCarbonPlayer) sender;
final @Nullable UUID party = wrapped.partyId();
if (party == null) {
throw new IllegalStateException();
}
final List<Audience> recipients = super.recipients(sender);
recipients.removeIf(r -> r instanceof CarbonPlayer p && !Objects.equals(p.party(), party));
recipients.removeIf(r -> r instanceof WrappedCarbonPlayer p && !Objects.equals(p.partyId(), party));
return recipients;
}

Expand All @@ -90,8 +92,7 @@ public List<Audience> recipients(final CarbonPlayer sender) {
final Component message,
final Component originalMessage
) {
final @Nullable UUID pid = sender.party();
final @Nullable Party party = pid == null ? null : this.users.party(pid).join();
final @Nullable Party party = sender.party().join();
return this.carbonMessages().chatFormat(
SourcedAudience.of(sender, recipient),
sender.uuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public Key key() {

private void info(final CommandContext<Commander> ctx) {
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final @Nullable UUID pid = player.party();
final @Nullable Party party = pid != null ? this.userManager.party(pid).join() : null;
final @Nullable Party party = player.party().join();
if (party == null) {
player.sendMessage(Component.text("You are not in a party.", NamedTextColor.RED));
} else {
Expand All @@ -130,8 +129,7 @@ private void info(final CommandContext<Commander> ctx) {

private void createParty(final CommandContext<Commander> ctx) {
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final @Nullable UUID oldId = player.party();
final @Nullable Party oldParty = oldId != null ? this.userManager.party(oldId).join() : null;
final @Nullable Party oldParty = player.party().join();
if (oldParty != null) {
player.sendMessage(Component.text("You must leave your current party first.", NamedTextColor.RED));
return;
Expand All @@ -148,12 +146,7 @@ private void createParty(final CommandContext<Commander> ctx) {
private void invitePlayer(final CommandContext<Commander> ctx) {
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final CarbonPlayer recipient = ctx.get("player");
final @Nullable UUID partyId = player.party();
if (partyId == null) {
mustBeInParty(player);
return;
}
final @Nullable Party party = this.userManager.party(partyId).join();
final @Nullable Party party = player.party().join();
if (party == null) {
mustBeInParty(player);
return;
Expand Down Expand Up @@ -191,13 +184,10 @@ private void acceptInvite(final CommandContext<Commander> ctx) {
player.sendMessage(Component.text("You do not have a pending party invite.", NamedTextColor.RED));
return;
}
final @Nullable UUID oldPartyId = player.party();
if (oldPartyId != null) {
final @Nullable Party old = this.userManager.party(oldPartyId).join();
if (old != null) {
player.sendMessage(Component.text("You must leave your current party first.", NamedTextColor.RED));
return;
}
final @Nullable Party old = player.party().join();
if (old != null) {
player.sendMessage(Component.text("You must leave your current party first.", NamedTextColor.RED));
return;
}
this.partyInvites.invalidateInvite(inv.getFirst(), player.uuid());
party.addMember(player.uuid());
Expand All @@ -206,43 +196,32 @@ private void acceptInvite(final CommandContext<Commander> ctx) {

private void leaveParty(final CommandContext<Commander> ctx) {
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final @Nullable UUID partyId = player.party();
if (partyId != null) {
final @Nullable Party old = this.userManager.party(partyId).join();
if (old == null) {
mustBeInParty(player);
return;
}
if (old.members().size() == 1) {
this.disbandParty(ctx);
return;
}
old.removeMember(player.uuid());
} else {
final @Nullable Party old = player.party().join();
if (old == null) {
mustBeInParty(player);
return;
}
if (old.members().size() == 1) {
this.disbandParty(ctx);
return;
}
old.removeMember(player.uuid());
player.sendMessage(Component.text("u left party"));
}

private void disbandParty(final CommandContext<Commander> ctx) {
final CarbonPlayer player = ((PlayerCommander) ctx.getSender()).carbonPlayer();
final @Nullable UUID partyId = player.party();
if (partyId != null) {
final @Nullable Party old = this.userManager.party(partyId).join();
if (old == null) {
mustBeInParty(player);
return;
}
if (old.members().size() != 1) {
player.sendMessage(Component.text("Cannot disband, you are not the last member.", NamedTextColor.RED));
return;
}
old.disband();
player.sendMessage(Component.text("u disbanded party"));
} else {
final @Nullable Party old = player.party().join();
if (old == null) {
mustBeInParty(player);
return;
}
if (old.members().size() != 1) {
player.sendMessage(Component.text("Cannot disband, you are not the last member.", NamedTextColor.RED));
return;
}
old.disband();
player.sendMessage(Component.text("u disbanded party"));
}

private static void mustBeInParty(final Audience player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.config.ConfigManager;
Expand All @@ -56,6 +58,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private transient @MonotonicNonNull @Inject PlatformScheduler scheduler;
private transient @MonotonicNonNull @Inject ConfigManager config;
private transient @MonotonicNonNull @Inject CarbonMessageRenderer messageRenderer;
private transient @MonotonicNonNull @Inject UserManagerInternal<?> users;
private volatile transient long transientLoadedSince = -1;

protected final PersistentUserProperty<Boolean> muted;
Expand Down Expand Up @@ -483,14 +486,21 @@ public void saved() {
this.properties().forEach(PersistentUserProperty::saved);
}

@Override
public @Nullable UUID party() {
public @Nullable UUID partyId() {
return this.party.orNull();
}

@Override
public void party(final @Nullable UUID id) {
this.party.set(id);
public CompletableFuture<@Nullable Party> party() {
final @Nullable UUID id = this.party.orNull();
if (id == null) {
return CompletableFuture.completedFuture(null);
}
return this.users.party(id);
}

@Override
public void party(final @Nullable Party party) {
this.party.set(party == null ? null : party.id());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.api.util.InventorySlot;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
Expand Down Expand Up @@ -247,12 +249,12 @@ public void leaveChannel(final ChatChannel channel) {
}

@Override
public @Nullable UUID party() {
return null;
public CompletableFuture<@Nullable Party> party() {
return CompletableFuture.completedFuture(null);
}

@Override
public void party(final @Nullable UUID id) {
public void party(final @Nullable Party party) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void addMember(final UUID id) {
};
this.userManager.saveParty(this).whenComplete(exceptionHandler);
this.userManager.user(id).thenCompose(user -> {
final @Nullable UUID oldPartyId = user.party();
user.party(this.id);
final WrappedCarbonPlayer wrapped = (WrappedCarbonPlayer) user;
final @Nullable UUID oldPartyId = wrapped.partyId();
user.party(this);
if (oldPartyId != null) {
return this.userManager.party(oldPartyId).thenAccept(old -> {
if (old != null) {
Expand All @@ -109,7 +110,8 @@ public void removeMember(final UUID id) {
};
this.userManager.saveParty(this).whenComplete(exceptionHandler);
this.userManager.user(id).thenAccept(user -> {
if (Objects.equals(user.party(), this.id)) {
final WrappedCarbonPlayer wrapped = (WrappedCarbonPlayer) user;
if (Objects.equals(wrapped.partyId(), this.id)) {
user.party(null);
}
}).whenComplete(exceptionHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.config.PrimaryConfig;
import net.draycia.carbon.common.messages.SourcedAudience;
Expand Down Expand Up @@ -428,14 +430,18 @@ public int hashCode() {
return this.carbonPlayerCommon.hashCode();
}

public @Nullable UUID partyId() {
return this.carbonPlayerCommon.partyId();
}

@Override
public @Nullable UUID party() {
public CompletableFuture<@Nullable Party> party() {
return this.carbonPlayerCommon.party();
}

@Override
public void party(final @Nullable UUID id) {
this.carbonPlayerCommon.party(id);
public void party(final @Nullable Party party) {
this.carbonPlayerCommon.party(party);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private Update bindPlayerArguments(final Update update, final CarbonPlayerCommon
.bind("whisperreplytarget", player.whisperReplyTarget())
.bind("spying", player.spying())
.bind("ignoringdms", player.ignoringDirectMessages())
.bind("party", player.party());
.bind("party", player.partyId());
}

public static final class Factory {
Expand Down

0 comments on commit f9988d7

Please sign in to comment.