This commit is contained in:
Jason Penilla committed 2023-03-19 18:24:03 -07:00
1 parent e803fa4203
commit 57af49418e
14 files changed
+320 -412

No files matched your search

@@ -0,0 +1,98 @@
/*
* 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 java.util.UUID;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
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 ImposterSaveOnChange implements SaveOnChange {
private static final SaveOnChange IMPOSTER = new ImposterSaveOnChange();
@Override
public int saveDisplayName(UUID id, @Nullable Component displayName) {
return -1;
}
@Override
public int saveMuted(UUID id, boolean muted) {
return -1;
}
@Override
public int saveDeafened(UUID id, boolean deafened) {
return -1;
}
@Override
public int saveSpying(UUID id, boolean spying) {
return -1;
}
@Override
public int saveSelectedChannel(UUID id, @Nullable Key selectedChannel) {
return -1;
}
@Override
public int saveLastWhisperTarget(UUID id, @Nullable UUID lastWhisperTarget) {
return -1;
}
@Override
public int saveWhisperReplyTarget(UUID id, @Nullable UUID whisperReplyTarget) {
return -1;
}
@Override
public int addIgnore(UUID id, UUID ignoredPlayer) {
return -1;
}
@Override
public int removeIgnore(UUID id, UUID ignoredPlayer) {
return -1;
}
@Override
public int addLeftChannel(UUID id, Key channel) {
return -1;
}
@Override
public int removeLeftChannel(UUID id, Key channel) {
return -1;
}
public static SaveOnChange impersonateIfNeeded(final UserManager<?> userManager) {
if (userManager instanceof SaveOnChange saveOnChange) {
return saveOnChange;
} else {
return IMPOSTER;
}
}
}
@@ -0,0 +1,118 @@
/*
* 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 java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public abstract class PlatformUserManager<C extends WrappedCarbonPlayer> implements UserManagerInternal<C>, SaveOnChange {
private final UserManagerInternal<CarbonPlayerCommon> wrapped;
private final SaveOnChange saveOnChange;
public PlatformUserManager(final UserManagerInternal<CarbonPlayerCommon> wrapped) {
this.wrapped = wrapped;
this.saveOnChange = ImposterSaveOnChange.impersonateIfNeeded(wrapped);
}
@Override
public CompletableFuture<C> user(UUID uuid) {
return this.wrapped.user(uuid).thenApply(this::wrap);
}
protected abstract C wrap(CarbonPlayerCommon common);
@Override
public int saveDisplayName(final UUID id, final @Nullable Component component) {
return this.saveOnChange.saveDisplayName(id, component);
}
@Override
public int saveMuted(final UUID id, final boolean muted) {
return this.saveOnChange.saveMuted(id, muted);
}
@Override
public int saveDeafened(final UUID id, final boolean deafened) {
return this.saveOnChange.saveDeafened(id, deafened);
}
@Override
public int saveSpying(final UUID id, final boolean spying) {
return this.saveOnChange.saveSpying(id, spying);
}
@Override
public int saveSelectedChannel(final UUID id, final @Nullable Key selectedChannel) {
return this.saveOnChange.saveSelectedChannel(id, selectedChannel);
}
@Override
public int saveLastWhisperTarget(final UUID id, final @Nullable UUID lastWhisperTarget) {
return this.saveOnChange.saveLastWhisperTarget(id, lastWhisperTarget);
}
@Override
public int saveWhisperReplyTarget(final UUID id, final @Nullable UUID whisperReplyTarget) {
return this.saveOnChange.saveWhisperReplyTarget(id, whisperReplyTarget);
}
@Override
public int addIgnore(final UUID id, final UUID ignoredPlayer) {
return this.saveOnChange.addIgnore(id, ignoredPlayer);
}
@Override
public int removeIgnore(final UUID id, final UUID ignoredPlayer) {
return this.saveOnChange.removeIgnore(id, ignoredPlayer);
}
@Override
public int addLeftChannel(final UUID id, final Key channel) {
return this.saveOnChange.addLeftChannel(id, channel);
}
@Override
public int removeLeftChannel(final UUID id, final Key channel) {
return this.saveOnChange.removeLeftChannel(id, channel);
}
@Override
public void shutdown() {
this.wrapped.shutdown();
}
@Override
public CompletableFuture<Void> save(C player) {
return this.wrapped.save(player.carbonPlayerCommon());
}
@Override
public CompletableFuture<Void> loggedOut(final UUID uuid) {
return this.wrapped.loggedOut(uuid);
}
}
@@ -1,3 +1,22 @@
/*
* 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 java.util.UUID;
@@ -1,12 +1,28 @@
/*
* 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 java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -19,11 +35,4 @@ public interface UserManagerInternal<C extends CarbonPlayer> extends UserManager
CompletableFuture<Void> loggedOut(UUID uuid);
@Override
default CompletableFuture<ComponentPlayerResult<C>> savePlayer(final C player) {
return this.save(player)
.thenApply($ -> new ComponentPlayerResult<>(player, Component.empty()))
.exceptionally(thr -> new ComponentPlayerResult<>(null, ComponentMessageThrowable.getOrConvertMessage(thr)));
}
}
@@ -28,7 +28,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.ConcurrentUtil;
@@ -40,8 +39,6 @@ import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.PreparedBatch;
import org.jdbi.v3.core.statement.Update;
import static net.kyori.adventure.text.Component.empty;
@DefaultQualifier(NonNull.class)
public abstract class DatabaseUserManager implements UserManagerInternal<CarbonPlayerCommon> {
@@ -60,8 +57,8 @@ public abstract class DatabaseUserManager implements UserManagerInternal<CarbonP
}
@Override
final public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(final CarbonPlayerCommon player) {
return CompletableFuture.supplyAsync(() -> this.jdbi.withHandle(handle -> {
final public CompletableFuture<Void> save(final CarbonPlayerCommon player) {
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
this.bindPlayerArguments(handle.createUpdate(this.locator.query("save-player")), player)
.execute();
@@ -84,7 +81,7 @@ public abstract class DatabaseUserManager implements UserManagerInternal<CarbonP
batch.execute();
}
// TODO: save ignoredplayers
return new ComponentPlayerResult<>(player, empty());
return null;
}), this.executor);
}
@@ -25,12 +25,10 @@ import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.common.config.DatabaseSettings;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.SaveOnChange;
@@ -50,9 +48,6 @@ import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.Update;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
// TODO: Dispatch updates using messaging system when users are modified
@DefaultQualifier(NonNull.class)
public final class MySQLUserManager extends DatabaseUserManager implements SaveOnChange {
@@ -104,9 +99,9 @@ public final class MySQLUserManager extends DatabaseUserManager implements SaveO
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) {
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
final var playerResult = this.userCache.computeIfAbsent(uuid, key -> this.jdbi.withHandle(handle -> {
return this.userCache.computeIfAbsent(uuid, key -> this.jdbi.withHandle(handle -> {
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
.bind("id", uuid)
.mapTo(CarbonPlayerCommon.class)
@@ -140,14 +135,7 @@ public final class MySQLUserManager extends DatabaseUserManager implements SaveO
});
return carbonPlayerCommon.get();
}));
return new ComponentPlayerResult<>(playerResult, empty());
}, this.executor).completeOnTimeout(new ComponentPlayerResult<>(null, text("Timed out loading data of UUID [" + uuid + " ]")), 30, TimeUnit.SECONDS);
}
@Override
public CompletableFuture<CarbonPlayerCommon> user(UUID uuid) {
throw new UnsupportedOperationException();
}, this.executor);
}
@Override
@@ -164,15 +152,6 @@ public final class MySQLUserManager extends DatabaseUserManager implements SaveO
.bind("spying", player.spying());
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.savePlayer(player).thenApply(result -> {
this.userCache.remove(player.uuid());
return result;
});
}
@Override
public int saveDisplayName(final UUID id, final @Nullable Component displayName) {
return this.jdbi.withExtension(MySQLSaveOnChange.class, changeSaver -> changeSaver.saveDisplayName(id, displayName));
@@ -228,11 +207,6 @@ public final class MySQLUserManager extends DatabaseUserManager implements SaveO
return this.jdbi.withExtension(MySQLSaveOnChange.class, changeSaver -> changeSaver.removeLeftChannel(id, channel));
}
@Override
public CompletableFuture<Void> save(CarbonPlayerCommon player) {
throw new UnsupportedOperationException();
}
@Override
public CompletableFuture<Void> loggedOut(UUID uuid) {
throw new UnsupportedOperationException();
@@ -24,12 +24,10 @@ import com.zaxxer.hikari.HikariDataSource;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.common.config.DatabaseSettings;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.SaveOnChange;
@@ -51,9 +49,6 @@ import org.jdbi.v3.core.statement.Update;
import org.jdbi.v3.postgres.PostgresPlugin;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
// TODO: Dispatch updates using messaging system when users are modified
@DefaultQualifier(NonNull.class)
public final class PostgreSQLUserManager extends DatabaseUserManager implements SaveOnChange {
@@ -98,12 +93,12 @@ public final class PostgreSQLUserManager extends DatabaseUserManager implements
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) {
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
final @Nullable CarbonPlayerCommon cachedPlayer = this.userCache.get(uuid);
if (cachedPlayer != null) {
return new ComponentPlayerResult<>(cachedPlayer, empty());
return cachedPlayer;
}
return this.jdbi.withHandle(handle -> {
@@ -130,7 +125,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager implements
}
carbonPlayerCommon.leftChannels().add(channel);
});
return new ComponentPlayerResult<>(carbonPlayerCommon, empty());
return carbonPlayerCommon;
} catch (final IllegalStateException exception) {
// Player doesn't exist in the DB, create them!
final String name = Objects.requireNonNull(
@@ -141,15 +136,10 @@ public final class PostgreSQLUserManager extends DatabaseUserManager implements
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
.execute();
return new ComponentPlayerResult<>(player, text(""));
return player;
}
});
}, this.executor).completeOnTimeout(new ComponentPlayerResult<>(null, text("Timed out loading data of UUID [" + uuid + " ]")), 30, TimeUnit.SECONDS);
}
@Override
public CompletableFuture<CarbonPlayerCommon> user(UUID uuid) {
throw new UnsupportedOperationException();
}, this.executor);
}
@Override
@@ -166,15 +156,6 @@ public final class PostgreSQLUserManager extends DatabaseUserManager implements
.bind("spying", player.spying());
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.savePlayer(player).thenApply(result -> {
this.userCache.remove(player.uuid());
return result;
});
}
@Override
public int saveDisplayName(final UUID id, final @Nullable Component displayName) {
return this.jdbi.withExtension(PostgreSQLSaveOnChange.class, changeSaver -> changeSaver.saveDisplayName(id, displayName));
@@ -230,11 +211,6 @@ public final class PostgreSQLUserManager extends DatabaseUserManager implements
return this.jdbi.withExtension(PostgreSQLSaveOnChange.class, changeSaver -> changeSaver.removeLeftChannel(id, channel));
}
@Override
public CompletableFuture<Void> save(CarbonPlayerCommon player) {
throw new UnsupportedOperationException();
}
@Override
public CompletableFuture<Void> loggedOut(UUID uuid) {
throw new UnsupportedOperationException();
@@ -37,7 +37,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
import net.draycia.carbon.common.serialisation.gson.UUIDSerializerGson;
@@ -45,14 +44,11 @@ import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import static net.kyori.adventure.text.Component.empty;
@DefaultQualifier(NonNull.class)
public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon> {
@@ -87,13 +83,6 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
this.executor = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "JSONUserManager"));
}
@Override
public synchronized CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) {
return this.user(uuid)
.thenApply(user -> new ComponentPlayerResult<>(user, empty()))
.exceptionally(thr -> new ComponentPlayerResult<>(null, ComponentMessageThrowable.getOrConvertMessage(thr)));
}
@Override
public synchronized CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
return this.cache.computeIfAbsent(uuid, $ -> {
@@ -168,11 +157,6 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
}, this.executor);
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.loggedOut(player.uuid()).thenApply($ -> new ComponentPlayerResult<>(player, null));
}
@Override
public void shutdown() {
for (final UUID id : List.copyOf(this.cache.keySet())) {
@@ -1,3 +1,22 @@
/*
* 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.util;
import com.google.common.util.concurrent.ThreadFactoryBuilder;