mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
Add PersistentUserProperty
This commit is contained in:
4 files changed
+105
-13
No files matched your search
@@ -26,6 +26,7 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Stream;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
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 @MonotonicNonNull @Inject ProfileResolver profileResolver;
|
||||||
private transient long transientLoadedSince = -1;
|
private transient long transientLoadedSince = -1;
|
||||||
|
|
||||||
protected boolean muted = false;
|
protected final PersistentUserProperty<Boolean> muted;
|
||||||
protected boolean deafened = false;
|
protected boolean deafened = false;
|
||||||
|
|
||||||
protected @Nullable Key selectedChannel = null;
|
protected @Nullable Key selectedChannel = null;
|
||||||
@@ -85,7 +86,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
final @Nullable UUID whisperReplyTarget,
|
final @Nullable UUID whisperReplyTarget,
|
||||||
final boolean spying
|
final boolean spying
|
||||||
) {
|
) {
|
||||||
this.muted = muted;
|
this.muted = PersistentUserProperty.of(muted);
|
||||||
this.deafened = deafened;
|
this.deafened = deafened;
|
||||||
this.selectedChannel = selectedChannel;
|
this.selectedChannel = selectedChannel;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
@@ -100,12 +101,17 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
final String username,
|
final String username,
|
||||||
final UUID uuid
|
final UUID uuid
|
||||||
) {
|
) {
|
||||||
|
this.muted = PersistentUserProperty.of(false);
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CarbonPlayerCommon() {
|
public CarbonPlayerCommon() {
|
||||||
|
this.muted = PersistentUserProperty.of(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean needsSave() {
|
||||||
|
return Stream.of(this.muted).anyMatch(PersistentUserProperty::changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -153,12 +159,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean muted() {
|
public boolean muted() {
|
||||||
return this.muted;
|
return this.muted.require();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void muted(final boolean muted) {
|
public void muted(final boolean muted) {
|
||||||
this.muted = muted;
|
this.muted.set(muted);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UUID> ignoredPlayers() {
|
public List<UUID> ignoredPlayers() {
|
||||||
|
|||||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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<T> {
|
||||||
|
|
||||||
|
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 <T> PersistentUserProperty<T> of(final @Nullable T value) {
|
||||||
|
return new PersistentUserProperty<>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PersistentUserProperty<T> empty() {
|
||||||
|
return new PersistentUserProperty<>(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class Serializer implements JsonSerializer<PersistentUserProperty<?>>, JsonDeserializer<PersistentUserProperty<?>> {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -61,8 +61,8 @@ public abstract class DatabaseUserManager extends CachingUserManager {
|
|||||||
@Override
|
@Override
|
||||||
public final CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
public final CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
||||||
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
|
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
|
||||||
if (!this.modifiedPlayerObject(player)) {
|
if (!player.needsSave()) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("save-player")), player)
|
this.bindPlayerArguments(handle.createUpdate(this.locator.query("save-player")), player)
|
||||||
@@ -91,13 +91,6 @@ public abstract class DatabaseUserManager extends CachingUserManager {
|
|||||||
}), this.executor);
|
}), 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);
|
abstract protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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.serialisation.gson.UUIDSerializerGson;
|
||||||
import net.draycia.carbon.common.users.CachingUserManager;
|
import net.draycia.carbon.common.users.CachingUserManager;
|
||||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
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.users.ProfileResolver;
|
||||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
@@ -76,6 +77,7 @@ public class JSONUserManager extends CachingUserManager {
|
|||||||
.apply(new GsonBuilder())
|
.apply(new GsonBuilder())
|
||||||
.registerTypeAdapter(ChatChannel.class, channelSerializer)
|
.registerTypeAdapter(ChatChannel.class, channelSerializer)
|
||||||
.registerTypeAdapter(UUID.class, uuidSerializer)
|
.registerTypeAdapter(UUID.class, uuidSerializer)
|
||||||
|
.registerTypeAdapter(PersistentUserProperty.class, new PersistentUserProperty.Serializer())
|
||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
@@ -125,6 +127,8 @@ public class JSONUserManager extends CachingUserManager {
|
|||||||
// quick and dirty check to not create files for/save default data
|
// 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()))) {
|
} else if (json.equals(this.emptyUserJson.replace(EMPTY_USER_UUID, player.uuid().toString()))) {
|
||||||
return;
|
return;
|
||||||
|
} else if (!player.needsSave()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Files.exists(userFile)) {
|
if (!Files.exists(userFile)) {
|
||||||
|
|||||||
Reference in new issue
Block a user