diff --git a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java index b4dafc89..67abf110 100644 --- a/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java +++ b/api/src/main/java/net/draycia/carbon/api/users/CarbonPlayer.java @@ -340,6 +340,9 @@ public interface CarbonPlayer extends Audience, Identified { * A list of all the channels the player has left * using the leave command. * + *

The returned collection is immutable, use + * {@link #joinChannel(ChatChannel)} and {@link #leaveChannel(ChatChannel)} to mutate.

+ * * @return a list of the channels. * @since 2.1.0 */ diff --git a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java index a4c99c23..cbc34405 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java +++ b/common/src/main/java/net/draycia/carbon/common/users/CarbonPlayerCommon.java @@ -21,10 +21,12 @@ package net.draycia.carbon.common.users; import com.google.inject.Inject; import java.time.Duration; -import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.Set; import java.util.UUID; import java.util.stream.Stream; import net.draycia.carbon.api.CarbonChat; @@ -52,28 +54,27 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing private transient long transientLoadedSince = -1; protected final PersistentUserProperty muted; - protected boolean deafened = false; - - protected @Nullable Key selectedChannel = null; + protected final PersistentUserProperty deafened; + protected final PersistentUserProperty selectedChannel; // All players have these protected transient @MonotonicNonNull String username = null; protected @MonotonicNonNull UUID uuid; // Display information - protected @Nullable Component displayName = null; + protected final PersistentUserProperty displayName; // Whispers protected transient @Nullable UUID lastWhisperTarget = null; protected transient @Nullable UUID whisperReplyTarget = null; // Administrative - protected boolean spying = false; + protected final PersistentUserProperty spying; // Punishments - protected List ignoredPlayers = new ArrayList<>(); + protected final PersistentUserProperty> ignoredPlayers; - protected List leftChannels = new ArrayList<>(); + protected final PersistentUserProperty> leftChannels; public CarbonPlayerCommon( final boolean muted, @@ -87,14 +88,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing final boolean spying ) { this.muted = PersistentUserProperty.of(muted); - this.deafened = deafened; - this.selectedChannel = selectedChannel; + this.deafened = PersistentUserProperty.of(deafened); + this.selectedChannel = PersistentUserProperty.of(selectedChannel); this.username = username; this.uuid = uuid; - this.displayName = displayName; + this.displayName = PersistentUserProperty.of(displayName); this.lastWhisperTarget = lastWhisperTarget; this.whisperReplyTarget = whisperReplyTarget; - this.spying = spying; + this.spying = PersistentUserProperty.of(spying); + this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet()); + this.leftChannels = PersistentUserProperty.of(Collections.emptySet()); } public CarbonPlayerCommon( @@ -102,16 +105,36 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing final UUID uuid ) { this.muted = PersistentUserProperty.of(false); + this.deafened = PersistentUserProperty.of(false); + this.selectedChannel = PersistentUserProperty.empty(); + this.displayName = PersistentUserProperty.empty(); + this.spying = PersistentUserProperty.of(false); + this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet()); + this.leftChannels = PersistentUserProperty.of(Collections.emptySet()); this.username = username; this.uuid = uuid; } public CarbonPlayerCommon() { this.muted = PersistentUserProperty.of(false); + this.deafened = PersistentUserProperty.of(false); + this.selectedChannel = PersistentUserProperty.empty(); + this.displayName = PersistentUserProperty.empty(); + this.spying = PersistentUserProperty.of(false); + this.ignoredPlayers = PersistentUserProperty.of(Collections.emptySet()); + this.leftChannels = PersistentUserProperty.of(Collections.emptySet()); } public boolean needsSave() { - return Stream.of(this.muted).anyMatch(PersistentUserProperty::changed); + return Stream.of( + this.muted, + this.deafened, + this.selectedChannel, + this.displayName, + this.spying, + this.ignoredPlayers, + this.leftChannels + ).anyMatch(PersistentUserProperty::changed); } @Override @@ -130,16 +153,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public @Nullable Component displayName() { - if (this.displayName != null) { - return this.displayName; - } - - return null; + return this.displayName.orNull(); } @Override public void displayName(final @Nullable Component displayName) { - this.displayName = displayName; + this.displayName.set(displayName); } @Override @@ -159,7 +178,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public boolean muted() { - return this.muted.require(); + return this.muted.get(); } @Override @@ -167,13 +186,13 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing this.muted.set(muted); } - public List ignoredPlayers() { - return this.ignoredPlayers; + public Set ignoredPlayers() { + return this.ignoredPlayers.get(); } @Override public boolean ignoring(final UUID player) { - return this.ignoredPlayers.contains(player); + return this.ignoredPlayers.get().contains(player); } @Override @@ -181,15 +200,23 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing return this.ignoring(player.uuid()); } + public void ignoring(final UUID player, final boolean nowIgnoring, final boolean internal) { + final Set newIgnored = new HashSet<>(this.ignoredPlayers.get()); + if (nowIgnoring) { + newIgnored.add(player); + } else { + newIgnored.remove(player); + } + if (internal) { + this.ignoredPlayers.setInternal(Collections.unmodifiableSet(newIgnored)); + } else { + this.ignoredPlayers.set(Collections.unmodifiableSet(newIgnored)); + } + } + @Override public void ignoring(final UUID player, final boolean nowIgnoring) { - if (nowIgnoring) { - if (!this.ignoredPlayers.contains(player)) { - this.ignoredPlayers.add(player); - } - } else { - this.ignoredPlayers.remove(player); - } + this.ignoring(player, nowIgnoring, false); } @Override @@ -199,22 +226,22 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public boolean deafened() { - return this.deafened; + return this.deafened.get(); } @Override public void deafened(final boolean deafened) { - this.deafened = deafened; + this.deafened.set(deafened); } @Override public boolean spying() { - return this.spying; + return this.spying.get(); } @Override public void spying(final boolean spying) { - this.spying = spying; + this.spying.set(spying); } @Override @@ -264,17 +291,37 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public List leftChannels() { - return this.leftChannels; + return List.copyOf(this.leftChannels.get()); + } + + public void joinChannel(final Key key, final boolean internal) { + final Set newKeys = new HashSet<>(this.leftChannels.get()); + newKeys.remove(key); + if (internal) { + this.leftChannels.setInternal(Collections.unmodifiableSet(newKeys)); + } else { + this.leftChannels.set(Collections.unmodifiableSet(newKeys)); + } } @Override public void joinChannel(final ChatChannel channel) { - this.leftChannels.remove(channel.key()); + this.joinChannel(channel.key(), false); + } + + public void leaveChannel(final ChatChannel channel, final boolean internal) { + final Set newKeys = new HashSet<>(this.leftChannels.get()); + newKeys.add(channel.key()); + if (internal) { + this.leftChannels.setInternal(Collections.unmodifiableSet(newKeys)); + } else { + this.leftChannels.set(Collections.unmodifiableSet(newKeys)); + } } @Override public void leaveChannel(final ChatChannel channel) { - this.leftChannels.add(channel.key()); + this.leaveChannel(channel, false); } @Override @@ -289,23 +336,23 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public @Nullable ChatChannel selectedChannel() { - if (this.selectedChannel == null) { + if (!this.selectedChannel.hasValue()) { return this.carbonChat.channelRegistry().defaultValue(); } - return this.carbonChat.channelRegistry().get(this.selectedChannel); + return this.carbonChat.channelRegistry().get(this.selectedChannel.get()); } public @Nullable Key selectedChannelKey() { - return this.selectedChannel; + return this.selectedChannel.orNull(); } @Override public void selectedChannel(final @Nullable ChatChannel chatChannel) { if (chatChannel == null) { - this.selectedChannel = null; + this.selectedChannel.set(null); } else { - this.selectedChannel = chatChannel.key(); + this.selectedChannel.set(chatChannel.key()); } } @@ -323,8 +370,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing public String username() { if (this.username == null) { this.username = Objects.requireNonNull( - Objects.requireNonNull(this.profileResolver, "profileResolver") - .resolveName(this.uuid).join(), + this.profileResolver.resolveName(this.uuid).join(), "name" ); } @@ -350,7 +396,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public boolean hasCustomDisplayName() { - return this.displayName != null; + return this.displayName.hasValue(); } @Override diff --git a/common/src/main/java/net/draycia/carbon/common/users/PersistentUserProperty.java b/common/src/main/java/net/draycia/carbon/common/users/PersistentUserProperty.java index 38e731db..d710d0f3 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/PersistentUserProperty.java +++ b/common/src/main/java/net/draycia/carbon/common/users/PersistentUserProperty.java @@ -31,6 +31,7 @@ import java.util.Objects; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; +import org.jetbrains.annotations.ApiStatus; @DefaultQualifier(NonNull.class) public final class PersistentUserProperty { @@ -42,6 +43,16 @@ public final class PersistentUserProperty { this.value = value; } + /** + * Set the value without setting the changed flag. This is a hack. + * + * @param value value + */ + @ApiStatus.Internal + public void setInternal(final @Nullable T value) { + this.value = value; + } + public void set(final @Nullable T value) { if (Objects.equals(value, this.value)) { return; @@ -50,11 +61,15 @@ public final class PersistentUserProperty { this.changed = true; } - public T require() { + public T get() { return Objects.requireNonNull(this.value, "value required but not present"); } - public @Nullable T get() { + public boolean hasValue() { + return this.value != null; + } + + public @Nullable T orNull() { return this.value; } @@ -81,7 +96,7 @@ public final class PersistentUserProperty { @Override public JsonElement serialize(final PersistentUserProperty src, final Type typeOfSrc, final JsonSerializationContext context) { final Type propType = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[0]; - return context.serialize(src.get(), propType); + return context.serialize(src.orNull(), propType); } } diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java index d27cbe19..92d753c3 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/mysql/MySQLUserManager.java @@ -90,7 +90,7 @@ public final class MySQLUserManager extends DatabaseUserManager { handle.createQuery(this.locator.query("select-ignores")) .bind("id", uuid) .mapTo(UUID.class) - .forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer)); + .forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoring(ignoredPlayer, true, true)); handle.createQuery(this.locator.query("select-leftchannels")) .bind("id", uuid) .mapTo(Key.class) @@ -101,7 +101,7 @@ public final class MySQLUserManager extends DatabaseUserManager { if (chatChannel == null) { return; } - carbonPlayerCommon.get().leftChannels().add(channel); + carbonPlayerCommon.get().leaveChannel(chatChannel, true); }); return carbonPlayerCommon.get(); }); diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java index 456e5055..75e3984d 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/postgresql/PostgreSQLUserManager.java @@ -82,7 +82,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager { handle.createQuery(this.locator.query("select-ignores")) .bind("id", uuid) .mapTo(UUID.class) - .forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true)); + .forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true, true)); handle.createQuery(this.locator.query("select-leftchannels")) .bind("id", uuid) @@ -94,7 +94,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager { if (chatChannel == null) { return; } - carbonPlayerCommon.leftChannels().add(channel); + carbonPlayerCommon.leaveChannel(chatChannel, true); }); return carbonPlayerCommon; } catch (final IllegalStateException exception) { diff --git a/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java index 023f1b62..27029fe9 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/json/JSONUserManager.java @@ -96,9 +96,13 @@ public class JSONUserManager extends CachingUserManager { if (player == null) { throw new IllegalStateException("Player file found but was empty."); } - player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat() - .channelRegistry() - .get(channel) == null); + player.leftChannels().forEach(channel -> { + if (CarbonChatProvider.carbonChat() + .channelRegistry() + .get(channel) == null) { + player.joinChannel(channel, true); + } + }); return player; } catch (final IOException exception) {