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 8e1069f6..a4c99c23 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 @@ -26,6 +26,7 @@ import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.UUID; +import java.util.stream.Stream; import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; @@ -50,7 +51,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing private transient @MonotonicNonNull @Inject ProfileResolver profileResolver; private transient long transientLoadedSince = -1; - protected boolean muted = false; + protected final PersistentUserProperty muted; protected boolean deafened = false; protected @Nullable Key selectedChannel = null; @@ -85,7 +86,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing final @Nullable UUID whisperReplyTarget, final boolean spying ) { - this.muted = muted; + this.muted = PersistentUserProperty.of(muted); this.deafened = deafened; this.selectedChannel = selectedChannel; this.username = username; @@ -100,12 +101,17 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing final String username, final UUID uuid ) { + this.muted = PersistentUserProperty.of(false); this.username = username; this.uuid = uuid; } public CarbonPlayerCommon() { + this.muted = PersistentUserProperty.of(false); + } + public boolean needsSave() { + return Stream.of(this.muted).anyMatch(PersistentUserProperty::changed); } @Override @@ -153,12 +159,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing @Override public boolean muted() { - return this.muted; + return this.muted.require(); } @Override public void muted(final boolean muted) { - this.muted = muted; + this.muted.set(muted); } public List ignoredPlayers() { 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 new file mode 100644 index 00000000..38e731db --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/users/PersistentUserProperty.java @@ -0,0 +1,89 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.users; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Objects; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.framework.qual.DefaultQualifier; + +@DefaultQualifier(NonNull.class) +public final class PersistentUserProperty { + + private @Nullable T value; + private boolean changed = false; + + public PersistentUserProperty(final @Nullable T value) { + this.value = value; + } + + public void set(final @Nullable T value) { + if (Objects.equals(value, this.value)) { + return; + } + this.value = value; + this.changed = true; + } + + public T require() { + return Objects.requireNonNull(this.value, "value required but not present"); + } + + public @Nullable T get() { + return this.value; + } + + public boolean changed() { + return this.changed; + } + + public static PersistentUserProperty of(final @Nullable T value) { + return new PersistentUserProperty<>(value); + } + + public static PersistentUserProperty empty() { + return new PersistentUserProperty<>(null); + } + + public static final class Serializer implements JsonSerializer>, JsonDeserializer> { + + @Override + public PersistentUserProperty deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { + final Type propType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0]; + return new PersistentUserProperty<>(context.deserialize(json, propType)); + } + + @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); + } + + } + +} diff --git a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java index 2b30c0f3..18bc1c18 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java +++ b/common/src/main/java/net/draycia/carbon/common/users/db/DatabaseUserManager.java @@ -61,8 +61,8 @@ public abstract class DatabaseUserManager extends CachingUserManager { @Override public final CompletableFuture save(final CarbonPlayerCommon player) { return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> { - if (!this.modifiedPlayerObject(player)) { - return CompletableFuture.completedFuture(null); + if (!player.needsSave()) { + return null; } this.bindPlayerArguments(handle.createUpdate(this.locator.query("save-player")), player) @@ -91,13 +91,6 @@ public abstract class DatabaseUserManager extends CachingUserManager { }), this.executor); } - private boolean modifiedPlayerObject(final CarbonPlayerCommon player) { - // TODO: Find a better way to do this? - return player.muted() || player.deafened() || player.selectedChannel() != null || - player.hasCustomDisplayName() || player.spying() || !player.ignoredPlayers().isEmpty() || - !player.leftChannels().isEmpty(); - } - abstract protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player); } 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 9018ba16..023f1b62 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 @@ -37,6 +37,7 @@ import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson; import net.draycia.carbon.common.serialisation.gson.UUIDSerializerGson; import net.draycia.carbon.common.users.CachingUserManager; import net.draycia.carbon.common.users.CarbonPlayerCommon; +import net.draycia.carbon.common.users.PersistentUserProperty; import net.draycia.carbon.common.users.ProfileResolver; import net.draycia.carbon.common.util.ConcurrentUtil; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -76,6 +77,7 @@ public class JSONUserManager extends CachingUserManager { .apply(new GsonBuilder()) .registerTypeAdapter(ChatChannel.class, channelSerializer) .registerTypeAdapter(UUID.class, uuidSerializer) + .registerTypeAdapter(PersistentUserProperty.class, new PersistentUserProperty.Serializer()) .setPrettyPrinting() .create(); @@ -125,6 +127,8 @@ public class JSONUserManager extends CachingUserManager { // quick and dirty check to not create files for/save default data } else if (json.equals(this.emptyUserJson.replace(EMPTY_USER_UUID, player.uuid().toString()))) { return; + } else if (!player.needsSave()) { + return; } if (!Files.exists(userFile)) {