Use PersistentUserProperty for all serialized values

This commit is contained in:
Jason Penilla committed 2023-03-25 11:36:12 -07:00
1 parent e1e9c4f62e
commit 6fda12425e
6 files changed
+123 -55

No files matched your search

@@ -340,6 +340,9 @@ public interface CarbonPlayer extends Audience, Identified {
* A list of all the channels the player has left * A list of all the channels the player has left
* using the leave command. * using the leave command.
* *
* <p>The returned collection is immutable, use
* {@link #joinChannel(ChatChannel)} and {@link #leaveChannel(ChatChannel)} to mutate.</p>
*
* @return a list of the channels. * @return a list of the channels.
* @since 2.1.0 * @since 2.1.0
*/ */
@@ -21,10 +21,12 @@ package net.draycia.carbon.common.users;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
@@ -52,28 +54,27 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
private transient long transientLoadedSince = -1; private transient long transientLoadedSince = -1;
protected final PersistentUserProperty<Boolean> muted; protected final PersistentUserProperty<Boolean> muted;
protected boolean deafened = false; protected final PersistentUserProperty<Boolean> deafened;
protected final PersistentUserProperty<Key> selectedChannel;
protected @Nullable Key selectedChannel = null;
// All players have these // All players have these
protected transient @MonotonicNonNull String username = null; protected transient @MonotonicNonNull String username = null;
protected @MonotonicNonNull UUID uuid; protected @MonotonicNonNull UUID uuid;
// Display information // Display information
protected @Nullable Component displayName = null; protected final PersistentUserProperty<Component> displayName;
// Whispers // Whispers
protected transient @Nullable UUID lastWhisperTarget = null; protected transient @Nullable UUID lastWhisperTarget = null;
protected transient @Nullable UUID whisperReplyTarget = null; protected transient @Nullable UUID whisperReplyTarget = null;
// Administrative // Administrative
protected boolean spying = false; protected final PersistentUserProperty<Boolean> spying;
// Punishments // Punishments
protected List<UUID> ignoredPlayers = new ArrayList<>(); protected final PersistentUserProperty<Set<UUID>> ignoredPlayers;
protected List<Key> leftChannels = new ArrayList<>(); protected final PersistentUserProperty<Set<Key>> leftChannels;
public CarbonPlayerCommon( public CarbonPlayerCommon(
final boolean muted, final boolean muted,
@@ -87,14 +88,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
final boolean spying final boolean spying
) { ) {
this.muted = PersistentUserProperty.of(muted); this.muted = PersistentUserProperty.of(muted);
this.deafened = deafened; this.deafened = PersistentUserProperty.of(deafened);
this.selectedChannel = selectedChannel; this.selectedChannel = PersistentUserProperty.of(selectedChannel);
this.username = username; this.username = username;
this.uuid = uuid; this.uuid = uuid;
this.displayName = displayName; this.displayName = PersistentUserProperty.of(displayName);
this.lastWhisperTarget = lastWhisperTarget; this.lastWhisperTarget = lastWhisperTarget;
this.whisperReplyTarget = whisperReplyTarget; 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( public CarbonPlayerCommon(
@@ -102,16 +105,36 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
final UUID uuid final UUID uuid
) { ) {
this.muted = PersistentUserProperty.of(false); 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.username = username;
this.uuid = uuid; this.uuid = uuid;
} }
public CarbonPlayerCommon() { public CarbonPlayerCommon() {
this.muted = PersistentUserProperty.of(false); 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() { 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 @Override
@@ -130,16 +153,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public @Nullable Component displayName() { public @Nullable Component displayName() {
if (this.displayName != null) { return this.displayName.orNull();
return this.displayName;
}
return null;
} }
@Override @Override
public void displayName(final @Nullable Component displayName) { public void displayName(final @Nullable Component displayName) {
this.displayName = displayName; this.displayName.set(displayName);
} }
@Override @Override
@@ -159,7 +178,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public boolean muted() { public boolean muted() {
return this.muted.require(); return this.muted.get();
} }
@Override @Override
@@ -167,13 +186,13 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
this.muted.set(muted); this.muted.set(muted);
} }
public List<UUID> ignoredPlayers() { public Set<UUID> ignoredPlayers() {
return this.ignoredPlayers; return this.ignoredPlayers.get();
} }
@Override @Override
public boolean ignoring(final UUID player) { public boolean ignoring(final UUID player) {
return this.ignoredPlayers.contains(player); return this.ignoredPlayers.get().contains(player);
} }
@Override @Override
@@ -181,15 +200,23 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
return this.ignoring(player.uuid()); return this.ignoring(player.uuid());
} }
public void ignoring(final UUID player, final boolean nowIgnoring, final boolean internal) {
final Set<UUID> 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 @Override
public void ignoring(final UUID player, final boolean nowIgnoring) { public void ignoring(final UUID player, final boolean nowIgnoring) {
if (nowIgnoring) { this.ignoring(player, nowIgnoring, false);
if (!this.ignoredPlayers.contains(player)) {
this.ignoredPlayers.add(player);
}
} else {
this.ignoredPlayers.remove(player);
}
} }
@Override @Override
@@ -199,22 +226,22 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public boolean deafened() { public boolean deafened() {
return this.deafened; return this.deafened.get();
} }
@Override @Override
public void deafened(final boolean deafened) { public void deafened(final boolean deafened) {
this.deafened = deafened; this.deafened.set(deafened);
} }
@Override @Override
public boolean spying() { public boolean spying() {
return this.spying; return this.spying.get();
} }
@Override @Override
public void spying(final boolean spying) { public void spying(final boolean spying) {
this.spying = spying; this.spying.set(spying);
} }
@Override @Override
@@ -264,17 +291,37 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public List<Key> leftChannels() { public List<Key> leftChannels() {
return this.leftChannels; return List.copyOf(this.leftChannels.get());
}
public void joinChannel(final Key key, final boolean internal) {
final Set<Key> 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 @Override
public void joinChannel(final ChatChannel channel) { 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<Key> 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 @Override
public void leaveChannel(final ChatChannel channel) { public void leaveChannel(final ChatChannel channel) {
this.leftChannels.add(channel.key()); this.leaveChannel(channel, false);
} }
@Override @Override
@@ -289,23 +336,23 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public @Nullable ChatChannel selectedChannel() { public @Nullable ChatChannel selectedChannel() {
if (this.selectedChannel == null) { if (!this.selectedChannel.hasValue()) {
return this.carbonChat.channelRegistry().defaultValue(); return this.carbonChat.channelRegistry().defaultValue();
} }
return this.carbonChat.channelRegistry().get(this.selectedChannel); return this.carbonChat.channelRegistry().get(this.selectedChannel.get());
} }
public @Nullable Key selectedChannelKey() { public @Nullable Key selectedChannelKey() {
return this.selectedChannel; return this.selectedChannel.orNull();
} }
@Override @Override
public void selectedChannel(final @Nullable ChatChannel chatChannel) { public void selectedChannel(final @Nullable ChatChannel chatChannel) {
if (chatChannel == null) { if (chatChannel == null) {
this.selectedChannel = null; this.selectedChannel.set(null);
} else { } else {
this.selectedChannel = chatChannel.key(); this.selectedChannel.set(chatChannel.key());
} }
} }
@@ -323,8 +370,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
public String username() { public String username() {
if (this.username == null) { if (this.username == null) {
this.username = Objects.requireNonNull( this.username = Objects.requireNonNull(
Objects.requireNonNull(this.profileResolver, "profileResolver") this.profileResolver.resolveName(this.uuid).join(),
.resolveName(this.uuid).join(),
"name" "name"
); );
} }
@@ -350,7 +396,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override @Override
public boolean hasCustomDisplayName() { public boolean hasCustomDisplayName() {
return this.displayName != null; return this.displayName.hasValue();
} }
@Override @Override
@@ -31,6 +31,7 @@ import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier; import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.ApiStatus;
@DefaultQualifier(NonNull.class) @DefaultQualifier(NonNull.class)
public final class PersistentUserProperty<T> { public final class PersistentUserProperty<T> {
@@ -42,6 +43,16 @@ public final class PersistentUserProperty<T> {
this.value = value; 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) { public void set(final @Nullable T value) {
if (Objects.equals(value, this.value)) { if (Objects.equals(value, this.value)) {
return; return;
@@ -50,11 +61,15 @@ public final class PersistentUserProperty<T> {
this.changed = true; this.changed = true;
} }
public T require() { public T get() {
return Objects.requireNonNull(this.value, "value required but not present"); 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; return this.value;
} }
@@ -81,7 +96,7 @@ public final class PersistentUserProperty<T> {
@Override @Override
public JsonElement serialize(final PersistentUserProperty<?> src, final Type typeOfSrc, final JsonSerializationContext context) { public JsonElement serialize(final PersistentUserProperty<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
final Type propType = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[0]; final Type propType = ((ParameterizedType) typeOfSrc).getActualTypeArguments()[0];
return context.serialize(src.get(), propType); return context.serialize(src.orNull(), propType);
} }
} }
@@ -90,7 +90,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
handle.createQuery(this.locator.query("select-ignores")) handle.createQuery(this.locator.query("select-ignores"))
.bind("id", uuid) .bind("id", uuid)
.mapTo(UUID.class) .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")) handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid) .bind("id", uuid)
.mapTo(Key.class) .mapTo(Key.class)
@@ -101,7 +101,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
if (chatChannel == null) { if (chatChannel == null) {
return; return;
} }
carbonPlayerCommon.get().leftChannels().add(channel); carbonPlayerCommon.get().leaveChannel(chatChannel, true);
}); });
return carbonPlayerCommon.get(); return carbonPlayerCommon.get();
}); });
@@ -82,7 +82,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
handle.createQuery(this.locator.query("select-ignores")) handle.createQuery(this.locator.query("select-ignores"))
.bind("id", uuid) .bind("id", uuid)
.mapTo(UUID.class) .mapTo(UUID.class)
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true)); .forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true, true));
handle.createQuery(this.locator.query("select-leftchannels")) handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid) .bind("id", uuid)
@@ -94,7 +94,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
if (chatChannel == null) { if (chatChannel == null) {
return; return;
} }
carbonPlayerCommon.leftChannels().add(channel); carbonPlayerCommon.leaveChannel(chatChannel, true);
}); });
return carbonPlayerCommon; return carbonPlayerCommon;
} catch (final IllegalStateException exception) { } catch (final IllegalStateException exception) {
@@ -96,9 +96,13 @@ public class JSONUserManager extends CachingUserManager {
if (player == null) { if (player == null) {
throw new IllegalStateException("Player file found but was empty."); throw new IllegalStateException("Player file found but was empty.");
} }
player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat() player.leftChannels().forEach(channel -> {
.channelRegistry() if (CarbonChatProvider.carbonChat()
.get(channel) == null); .channelRegistry()
.get(channel) == null) {
player.joinChannel(channel, true);
}
});
return player; return player;
} catch (final IOException exception) { } catch (final IOException exception) {