mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Propogate user data changes cross-server (#235)
This commit is contained in:
1 parent
1397cd4c4f
commit
366b679f7a
18 files changed
+300
-63
No files matched your search
@@ -50,7 +50,11 @@ public interface UserManager<C extends CarbonPlayer> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and returns the CarbonPlayer with the corresponding uuid.
|
||||
* Gets the {@link CarbonPlayer} for the provided player {@link UUID}, whether they are online or not.
|
||||
*
|
||||
* <p>Note that the returned user object/future is <i>not</i> guaranteed to be the same for subsequent calls.</p>
|
||||
*
|
||||
* <p>Because of this, the return value should <i>not</i> be cached, it should be queried each time it is needed. The implementation handles caching as is appropriate.</p>
|
||||
*
|
||||
* @param uuid the player's id
|
||||
* @return the player
|
||||
|
||||
@@ -44,6 +44,7 @@ import net.draycia.carbon.common.messages.placeholders.ComponentPlaceholderResol
|
||||
import net.draycia.carbon.common.messages.placeholders.KeyPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.Backing;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
@@ -124,6 +125,7 @@ public final class CarbonCommonModule extends AbstractModule {
|
||||
@Override
|
||||
protected void configure() {
|
||||
this.install(new FactoryModuleBuilder().build(ArgumentFactory.class));
|
||||
this.install(new FactoryModuleBuilder().build(PacketFactory.class));
|
||||
this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class);
|
||||
}
|
||||
|
||||
|
||||
+11
-8
@@ -20,8 +20,9 @@
|
||||
package net.draycia.carbon.common.messaging;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.SaveCompletedPacket;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.Tag;
|
||||
@@ -35,18 +36,20 @@ import org.jetbrains.annotations.NotNull;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonChatPacketHandler extends AbstractMessagingHandler {
|
||||
|
||||
final CarbonChannelRegistry channelRegistry;
|
||||
private final UserManagerInternal<?> userManager;
|
||||
|
||||
CarbonChatPacketHandler(
|
||||
final MessagingManager messagingManager,
|
||||
final CarbonChannelRegistry channelRegistry
|
||||
) {
|
||||
super(messagingManager.packetService());
|
||||
this.channelRegistry = channelRegistry;
|
||||
CarbonChatPacketHandler(final MessagingManager messagingManager, final UserManagerInternal<?> userManager) {
|
||||
super(messagingManager.requirePacketService());
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean handlePacket(final @NotNull Packet packet) {
|
||||
if (packet instanceof SaveCompletedPacket statePacket) {
|
||||
this.userManager.saveCompleteMessageReceived(statePacket.playerId());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(packet instanceof ChatMessagePacket messagePacket)) {
|
||||
this.logger.info("Messaging packet received - Not a ChatMessagePacket.");
|
||||
return false;
|
||||
|
||||
@@ -23,17 +23,20 @@ import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.function.Consumer;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.events.CarbonShutdownEvent;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.config.MessagingSettings;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.draycia.carbon.common.messaging.packets.SaveCompletedPacket;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||
import net.draycia.carbon.common.util.ExceptionLoggingScheduledThreadPoolExecutor;
|
||||
import ninja.egg82.messenger.MessagingService;
|
||||
@@ -52,24 +55,40 @@ import ninja.egg82.messenger.packets.server.PacketVersionPacket;
|
||||
import ninja.egg82.messenger.packets.server.PacketVersionRequestPacket;
|
||||
import ninja.egg82.messenger.packets.server.ShutdownPacket;
|
||||
import ninja.egg82.messenger.services.PacketService;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class MessagingManager {
|
||||
|
||||
private static final byte protocolVersion = 0;
|
||||
|
||||
private final ScheduledExecutorService executorService;
|
||||
private final PacketService packetService;
|
||||
private MessagingService messagingService;
|
||||
private final CarbonChat carbonChat;
|
||||
private final @MonotonicNonNull ScheduledExecutorService executorService;
|
||||
private final @MonotonicNonNull PacketService packetService;
|
||||
private @MonotonicNonNull MessagingService messagingService;
|
||||
|
||||
@Inject
|
||||
public MessagingManager(
|
||||
final CarbonChannelRegistry channelRegistry,
|
||||
final ConfigFactory configFactory,
|
||||
final CarbonChat carbonChat
|
||||
final CarbonChat carbonChat,
|
||||
final Logger logger,
|
||||
final UserManagerInternal<?> userManager
|
||||
) {
|
||||
if (!configFactory.primaryConfig().messagingSettings().enabled()) {
|
||||
logger.info("Messaging services disabled in config. Cross-server will not work without this!");
|
||||
this.messagingService = EMPTY_MESSAGING_SERVICE;
|
||||
this.packetService = null;
|
||||
this.carbonChat = carbonChat;
|
||||
this.executorService = null;
|
||||
return;
|
||||
}
|
||||
|
||||
PacketManager.register(MultiPacket.class, MultiPacket::new);
|
||||
PacketManager.register(KeepAlivePacket.class, KeepAlivePacket::new);
|
||||
PacketManager.register(InitializationPacket.class, InitializationPacket::new);
|
||||
@@ -78,6 +97,7 @@ public class MessagingManager {
|
||||
PacketManager.register(ShutdownPacket.class, ShutdownPacket::new);
|
||||
//PacketManager.register(HeartbeatPacket.class, HeartbeatPacket::new);
|
||||
PacketManager.register(ChatMessagePacket.class, ChatMessagePacket::new);
|
||||
PacketManager.register(SaveCompletedPacket.class, SaveCompletedPacket::new);
|
||||
|
||||
this.packetService = new PacketService(4, false, protocolVersion);
|
||||
this.executorService = new ExceptionLoggingScheduledThreadPoolExecutor(10,
|
||||
@@ -86,7 +106,7 @@ public class MessagingManager {
|
||||
|
||||
final MessagingHandlerImpl handlerImpl = new MessagingHandlerImpl(this.packetService);
|
||||
handlerImpl.addHandler(new CarbonServerHandler(carbonChat.serverId(), this.packetService, handlerImpl));
|
||||
handlerImpl.addHandler(new CarbonChatPacketHandler(this, channelRegistry));
|
||||
handlerImpl.addHandler(new CarbonChatPacketHandler(this, userManager));
|
||||
|
||||
try {
|
||||
this.initMessagingService(this.packetService, handlerImpl, new File("/"),
|
||||
@@ -120,18 +140,28 @@ public class MessagingManager {
|
||||
});
|
||||
}
|
||||
|
||||
public PacketService packetService() {
|
||||
public @Nullable PacketService packetService() {
|
||||
return this.packetService;
|
||||
}
|
||||
|
||||
private void onShutdown() {
|
||||
try {
|
||||
this.executorService.awaitTermination(15, TimeUnit.SECONDS);
|
||||
} catch (final InterruptedException ignored) {
|
||||
public PacketService requirePacketService() {
|
||||
return Objects.requireNonNull(this.packetService, "packetService");
|
||||
}
|
||||
|
||||
public void withPacketService(final Consumer<PacketService> consumer) {
|
||||
if (this.packetService != null) {
|
||||
consumer.accept(this.packetService);
|
||||
}
|
||||
}
|
||||
|
||||
private void onShutdown() {
|
||||
if (this.executorService != null) {
|
||||
ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.SECONDS, 10);
|
||||
}
|
||||
if (this.packetService != null) {
|
||||
this.packetService.flushQueue();
|
||||
this.packetService.shutdown();
|
||||
}
|
||||
this.packetService.flushQueue();
|
||||
this.packetService.shutdown();
|
||||
this.messagingService.close();
|
||||
}
|
||||
|
||||
@@ -144,12 +174,6 @@ public class MessagingManager {
|
||||
final String name = "engine1";
|
||||
final String channelName = "carbon-data";
|
||||
|
||||
if (!messagingSettings.enabled()) {
|
||||
this.carbonChat.logger().info("Messaging services disabled in config. Cross-server will not work without this!");
|
||||
this.messagingService = EMPTY_MESSAGING_SERVICE;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (messagingSettings.brokerType()) {
|
||||
case RABBITMQ -> {
|
||||
this.carbonChat.logger().info("Initializing RabbitMQ Messaging services...");
|
||||
@@ -189,7 +213,8 @@ public class MessagingManager {
|
||||
|
||||
this.messagingService = builder.build();
|
||||
}
|
||||
case NONE -> throw new IllegalStateException("MessagingManager initialized with no messaging broker selected!");
|
||||
case NONE ->
|
||||
throw new IllegalStateException("MessagingManager initialized with no messaging broker selected!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.messaging.packets;
|
||||
|
||||
import java.util.UUID;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface PacketFactory {
|
||||
|
||||
SaveCompletedPacket saveCompletedPacket(UUID playerId);
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.messaging.packets;
|
||||
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
import com.google.inject.assistedinject.AssistedInject;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class SaveCompletedPacket extends CarbonPacket {
|
||||
|
||||
private @MonotonicNonNull UUID player;
|
||||
|
||||
@AssistedInject
|
||||
public SaveCompletedPacket(final CarbonChat carbonChat, final @Assisted UUID player) {
|
||||
super(carbonChat.serverId());
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public SaveCompletedPacket(final UUID sender, final ByteBuf data) {
|
||||
super(sender);
|
||||
this.read(data);
|
||||
}
|
||||
|
||||
public UUID playerId() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(final ByteBuf buffer) {
|
||||
this.player = this.readUUID(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final ByteBuf buffer) {
|
||||
this.writeUUID(this.player, buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
package net.draycia.carbon.common.users;
|
||||
|
||||
import com.google.inject.MembersInjector;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,6 +31,9 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.db.DatabaseUserManager;
|
||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -43,6 +47,8 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
protected final ExecutorService executor;
|
||||
protected final ProfileResolver profileResolver;
|
||||
private final MembersInjector<CarbonPlayerCommon> playerInjector;
|
||||
private final Provider<MessagingManager> messagingManager;
|
||||
private final PacketFactory packetFactory;
|
||||
private final ReentrantLock cacheLock;
|
||||
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
|
||||
|
||||
@@ -50,18 +56,46 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
final Logger logger,
|
||||
final ExecutorService executorService,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
this.logger = logger;
|
||||
this.executor = executorService;
|
||||
this.profileResolver = profileResolver;
|
||||
this.playerInjector = playerInjector;
|
||||
this.messagingManager = messagingManager;
|
||||
this.packetFactory = packetFactory;
|
||||
this.cacheLock = new ReentrantLock();
|
||||
this.cache = new HashMap<>();
|
||||
}
|
||||
|
||||
protected abstract CarbonPlayerCommon loadOrCreate(UUID uuid);
|
||||
|
||||
@Override
|
||||
public void saveCompleteMessageReceived(final UUID playerId) {
|
||||
this.cacheLock.lock();
|
||||
try {
|
||||
this.cache.remove(playerId);
|
||||
} finally {
|
||||
this.cacheLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> saveIfNeeded(final CarbonPlayerCommon player) {
|
||||
if (!player.needsSave()) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
return this.save(player).whenComplete(($, $$) -> {
|
||||
player.saved();
|
||||
this.messagingManager.get().withPacketService(packetService -> {
|
||||
packetService.queuePacket(this.packetFactory.saveCompletedPacket(player.uuid()));
|
||||
packetService.flushQueue();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
||||
this.cacheLock.lock();
|
||||
@@ -70,6 +104,9 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
||||
final CarbonPlayerCommon player = this.loadOrCreate(uuid);
|
||||
this.playerInjector.injectMembers(player);
|
||||
if (this instanceof DatabaseUserManager) {
|
||||
player.registerPropertyUpdateListener(() -> this.save(player));
|
||||
}
|
||||
return player;
|
||||
}, this.executor);
|
||||
this.attachPostLoad(uuid, future);
|
||||
@@ -107,7 +144,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
if (remove != null && remove.isDone()) { // don't need to save if it never finished loading
|
||||
final @Nullable CarbonPlayerCommon join = remove.join();
|
||||
if (join != null) {
|
||||
return this.save(join);
|
||||
return this.saveIfNeeded(join);
|
||||
}
|
||||
}
|
||||
return CompletableFuture.completedFuture(null);
|
||||
@@ -126,7 +163,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
continue;
|
||||
}
|
||||
this.cache.remove(entry.getKey());
|
||||
this.save(getNow).exceptionally(thr -> {
|
||||
this.saveIfNeeded(getNow).exceptionally(thr -> {
|
||||
this.logger.warn("Exception saving data for player {} with UUID {}", getNow.username(), getNow.uuid(), thr);
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -126,6 +126,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
public boolean needsSave() {
|
||||
return this.properties().anyMatch(PersistentUserProperty::changed);
|
||||
}
|
||||
|
||||
private Stream<PersistentUserProperty<?>> properties() {
|
||||
return Stream.of(
|
||||
this.muted,
|
||||
this.deafened,
|
||||
@@ -134,7 +138,11 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
this.spying,
|
||||
this.ignoredPlayers,
|
||||
this.leftChannels
|
||||
).anyMatch(PersistentUserProperty::changed);
|
||||
);
|
||||
}
|
||||
|
||||
public void registerPropertyUpdateListener(final Runnable task) {
|
||||
this.properties().forEach(prop -> prop.registerUpdateListener(task));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -413,4 +421,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
return this.uuid.hashCode();
|
||||
}
|
||||
|
||||
public void saved() {
|
||||
this.properties().forEach(PersistentUserProperty::saved);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,9 @@ import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -37,7 +39,8 @@ import org.jetbrains.annotations.ApiStatus;
|
||||
public final class PersistentUserProperty<T> {
|
||||
|
||||
private @Nullable T value;
|
||||
private boolean changed = false;
|
||||
private volatile boolean changed = false;
|
||||
private final List<Runnable> updateListeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
public PersistentUserProperty(final @Nullable T value) {
|
||||
this.value = value;
|
||||
@@ -59,6 +62,17 @@ public final class PersistentUserProperty<T> {
|
||||
}
|
||||
this.value = value;
|
||||
this.changed = true;
|
||||
for (final Runnable updateListener : this.updateListeners) {
|
||||
updateListener.run();
|
||||
}
|
||||
}
|
||||
|
||||
public void saved() {
|
||||
this.changed = false;
|
||||
}
|
||||
|
||||
public void registerUpdateListener(final Runnable runnable) {
|
||||
this.updateListeners.add(runnable);
|
||||
}
|
||||
|
||||
public T get() {
|
||||
|
||||
@@ -51,6 +51,16 @@ public abstract class PlatformUserManager<C extends WrappedCarbonPlayer> impleme
|
||||
this.backingManager.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveCompleteMessageReceived(final UUID playerId) {
|
||||
this.backingManager.saveCompleteMessageReceived(playerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> saveIfNeeded(final C player) {
|
||||
return this.backingManager.saveIfNeeded(player.carbonPlayerCommon());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> save(final C player) {
|
||||
return this.backingManager.save(player.carbonPlayerCommon());
|
||||
|
||||
@@ -31,10 +31,14 @@ public interface UserManagerInternal<C extends CarbonPlayer> extends UserManager
|
||||
|
||||
void shutdown();
|
||||
|
||||
CompletableFuture<Void> saveIfNeeded(C player);
|
||||
|
||||
CompletableFuture<Void> save(C player);
|
||||
|
||||
CompletableFuture<Void> loggedOut(UUID uuid);
|
||||
|
||||
void saveCompleteMessageReceived(UUID playerId);
|
||||
|
||||
void cleanup();
|
||||
|
||||
}
|
||||
@@ -20,9 +20,12 @@
|
||||
package net.draycia.carbon.common.users.db;
|
||||
|
||||
import com.google.inject.MembersInjector;
|
||||
import com.google.inject.Provider;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.CachingUserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
@@ -46,13 +49,17 @@ public abstract class DatabaseUserManager extends CachingUserManager {
|
||||
final QueriesLocator locator,
|
||||
final Logger logger,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
super(
|
||||
logger,
|
||||
Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "DatabaseUserManager")),
|
||||
profileResolver,
|
||||
playerInjector
|
||||
playerInjector,
|
||||
messagingManager,
|
||||
packetFactory
|
||||
);
|
||||
this.jdbi = jdbi;
|
||||
this.locator = locator;
|
||||
@@ -60,9 +67,6 @@ public abstract class DatabaseUserManager extends CachingUserManager {
|
||||
|
||||
@Override
|
||||
public final CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
||||
if (!player.needsSave()) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
|
||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("save-player")), player)
|
||||
.execute();
|
||||
|
||||
+17
-4
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.users.db.mysql;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.MembersInjector;
|
||||
import com.google.inject.Provider;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.util.Objects;
|
||||
@@ -32,6 +33,8 @@ import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.config.DatabaseSettings;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.users.db.ComponentArgumentFactory;
|
||||
@@ -59,14 +62,18 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
||||
final Jdbi jdbi,
|
||||
final Logger logger,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
super(
|
||||
jdbi,
|
||||
new QueriesLocator(DBType.MYSQL),
|
||||
logger,
|
||||
profileResolver,
|
||||
playerInjector
|
||||
playerInjector,
|
||||
messagingManager,
|
||||
packetFactory
|
||||
);
|
||||
}
|
||||
|
||||
@@ -128,18 +135,24 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
||||
private final Logger logger;
|
||||
private final ProfileResolver profileResolver;
|
||||
private final MembersInjector<CarbonPlayerCommon> playerInjector;
|
||||
private final Provider<MessagingManager> messagingManager;
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
@Inject
|
||||
private Factory(
|
||||
final ConfigFactory configFactory,
|
||||
final Logger logger,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
this.databaseSettings = configFactory.primaryConfig().databaseSettings();
|
||||
this.logger = logger;
|
||||
this.profileResolver = profileResolver;
|
||||
this.playerInjector = playerInjector;
|
||||
this.messagingManager = messagingManager;
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public MySQLUserManager create() {
|
||||
@@ -181,7 +194,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
||||
.registerRowMapper(new MySQLPlayerRowMapper())
|
||||
.installPlugin(new SqlObjectPlugin());
|
||||
|
||||
return new MySQLUserManager(jdbi, this.logger, this.profileResolver, this.playerInjector);
|
||||
return new MySQLUserManager(jdbi, this.logger, this.profileResolver, this.playerInjector, this.messagingManager, this.packetFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+17
-4
@@ -21,6 +21,7 @@ package net.draycia.carbon.common.users.db.postgresql;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.MembersInjector;
|
||||
import com.google.inject.Provider;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.util.Objects;
|
||||
@@ -31,6 +32,8 @@ import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.config.DatabaseSettings;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.users.db.ComponentArgumentFactory;
|
||||
@@ -61,14 +64,18 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||
final Jdbi jdbi,
|
||||
final Logger logger,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
super(
|
||||
jdbi,
|
||||
new QueriesLocator(DBType.POSTGRESQL),
|
||||
logger,
|
||||
profileResolver,
|
||||
playerInjector
|
||||
playerInjector,
|
||||
messagingManager,
|
||||
packetFactory
|
||||
);
|
||||
}
|
||||
|
||||
@@ -134,18 +141,24 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||
private final Logger logger;
|
||||
private final ProfileResolver profileResolver;
|
||||
private final MembersInjector<CarbonPlayerCommon> playerInjector;
|
||||
private final Provider<MessagingManager> messagingManager;
|
||||
private final PacketFactory packetFactory;
|
||||
|
||||
@Inject
|
||||
private Factory(
|
||||
final ConfigFactory configFactory,
|
||||
final Logger logger,
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) {
|
||||
this.databaseSettings = configFactory.primaryConfig().databaseSettings();
|
||||
this.logger = logger;
|
||||
this.profileResolver = profileResolver;
|
||||
this.playerInjector = playerInjector;
|
||||
this.messagingManager = messagingManager;
|
||||
this.packetFactory = packetFactory;
|
||||
}
|
||||
|
||||
public PostgreSQLUserManager create() {
|
||||
@@ -183,7 +196,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||
.installPlugin(new SqlObjectPlugin())
|
||||
.installPlugin(new PostgresPlugin());
|
||||
|
||||
return new PostgreSQLUserManager(jdbi, this.logger, this.profileResolver, this.playerInjector);
|
||||
return new PostgreSQLUserManager(jdbi, this.logger, this.profileResolver, this.playerInjector, this.messagingManager, this.packetFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.MembersInjector;
|
||||
import com.google.inject.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.file.Files;
|
||||
@@ -33,6 +34,8 @@ import java.util.concurrent.Executors;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.common.DataDirectory;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
|
||||
import net.draycia.carbon.common.serialisation.gson.UUIDSerializerGson;
|
||||
import net.draycia.carbon.common.users.CachingUserManager;
|
||||
@@ -59,13 +62,17 @@ public class JSONUserManager extends CachingUserManager {
|
||||
final ProfileResolver profileResolver,
|
||||
final MembersInjector<CarbonPlayerCommon> playerInjector,
|
||||
final ChatChannelSerializerGson channelSerializer,
|
||||
final UUIDSerializerGson uuidSerializer
|
||||
final UUIDSerializerGson uuidSerializer,
|
||||
final Provider<MessagingManager> messagingManager,
|
||||
final PacketFactory packetFactory
|
||||
) throws IOException {
|
||||
super(
|
||||
logger,
|
||||
Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "JSONUserManager")),
|
||||
profileResolver,
|
||||
playerInjector
|
||||
playerInjector,
|
||||
messagingManager,
|
||||
packetFactory
|
||||
);
|
||||
this.userDirectory = dataDirectory.resolve("users");
|
||||
|
||||
@@ -117,9 +124,6 @@ public class JSONUserManager extends CachingUserManager {
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
||||
if (!player.needsSave()) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
final Path userFile = this.userFile(player.uuid());
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ public final class PlayerUtils {
|
||||
final C player,
|
||||
final Logger logger
|
||||
) {
|
||||
final var saveResult = userManager.save(player);
|
||||
final var saveResult = userManager.saveIfNeeded(player);
|
||||
saveResult.exceptionally(thr -> {
|
||||
logger.warn("Exception saving data for player {} with UUID {}", player.username(), player.uuid(), thr);
|
||||
return null;
|
||||
|
||||
@@ -20,11 +20,9 @@
|
||||
package net.draycia.carbon.paper.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import net.draycia.carbon.common.users.ProfileCache;
|
||||
import net.draycia.carbon.paper.PaperUserManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -58,13 +56,7 @@ public class PaperPlayerJoinListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onJoin(final PlayerJoinEvent event) {
|
||||
this.userManager.user(event.getPlayer().getUniqueId()).thenAccept(result -> {
|
||||
Optional.ofNullable(result.displayName()).ifPresent(displayName -> {
|
||||
final Player player = event.getPlayer();
|
||||
player.displayName(displayName);
|
||||
player.playerListName(displayName);
|
||||
});
|
||||
}).exceptionally(thr -> {
|
||||
this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(thr -> {
|
||||
this.logger.warn("Exception handling player join", thr);
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -22,6 +22,7 @@ package net.draycia.carbon.paper.users;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
@@ -50,6 +51,14 @@ public final class CarbonPlayerPaper extends WrappedCarbonPlayer implements Forw
|
||||
|
||||
public CarbonPlayerPaper(final CarbonPlayerCommon carbonPlayerCommon) {
|
||||
this.carbonPlayerCommon = carbonPlayerCommon;
|
||||
this.player().ifPresent(player -> Optional.ofNullable(carbonPlayerCommon.displayName()).ifPresent(displayName -> {
|
||||
if (!Objects.equals(player.displayName(), displayName)) {
|
||||
player.displayName(displayName);
|
||||
}
|
||||
if (!Objects.equals(player.playerListName(), displayName)) {
|
||||
player.playerListName(displayName);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private Optional<Player> player() {
|
||||
|
||||
Reference in new issue
Block a user