mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Save players on fabric
This commit is contained in:
6 files changed
+69
-31
No files matched your search
@@ -26,6 +26,7 @@ import com.google.inject.TypeLiteral;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
@@ -116,7 +117,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
final long saveDelay = 5 * 60 * 20;
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this,
|
||||
() -> PlayerUtils.savePlayers(this.carbonServerBukkit, this.userManager), saveDelay, saveDelay);
|
||||
() -> PlayerUtils.saveLoggedInPlayers(this.carbonServerBukkit, this.userManager), saveDelay, saveDelay);
|
||||
|
||||
// Load channels
|
||||
((CarbonChannelRegistry) this.channelRegistry()).loadChannels();
|
||||
@@ -124,7 +125,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
PlayerUtils.savePlayers(this.carbonServerBukkit, this.userManager);
|
||||
PlayerUtils.saveLoggedInPlayers(this.carbonServerBukkit, this.userManager).forEach(CompletableFuture::join);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,10 @@
|
||||
*/
|
||||
package net.draycia.carbon.common.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
@@ -30,24 +33,36 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
public final class PlayerUtils {
|
||||
|
||||
private PlayerUtils() {
|
||||
|
||||
}
|
||||
|
||||
public static void savePlayers(final CarbonServer carbonServer, final UserManager<CarbonPlayerCommon> userManager) {
|
||||
for (final var player : carbonServer.players()) {
|
||||
final var saveResult = userManager.savePlayer(((WrappedCarbonPlayer) player).carbonPlayerCommon());
|
||||
public static List<CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>>> saveLoggedInPlayers(
|
||||
final CarbonServer carbonServer,
|
||||
final UserManager<CarbonPlayerCommon> userManager
|
||||
) {
|
||||
return carbonServer.players().stream()
|
||||
.map(player -> savePlayer(carbonServer, userManager, (WrappedCarbonPlayer) player))
|
||||
.toList();
|
||||
}
|
||||
|
||||
saveResult.thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
carbonServer.console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
private static CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(
|
||||
final CarbonServer carbonServer,
|
||||
final UserManager<CarbonPlayerCommon> userManager,
|
||||
final WrappedCarbonPlayer player
|
||||
) {
|
||||
final var saveResult = userManager.savePlayer(player.carbonPlayerCommon());
|
||||
|
||||
saveResult.exceptionally(exception -> {
|
||||
exception.getCause().printStackTrace();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
saveResult.thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
carbonServer.console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
|
||||
saveResult.exceptionally(exception -> {
|
||||
exception.getCause().printStackTrace();
|
||||
return null;
|
||||
});
|
||||
|
||||
return saveResult;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
@@ -36,10 +37,12 @@ import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.draycia.carbon.common.util.ListenerUtils;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import net.draycia.carbon.fabric.callback.ChatCallback;
|
||||
import net.draycia.carbon.fabric.listeners.FabricChatListener;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.ModContainer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -82,10 +85,9 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
|
||||
this.userManager = this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<UserManager<CarbonPlayerCommon>>() {}));
|
||||
|
||||
// Platform Listeners
|
||||
ChatCallback.setup();
|
||||
ChatCallback.INSTANCE.registerListener(new FabricChatListener(this, this.channelRegistry));
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> this.minecraftServer = server);
|
||||
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.minecraftServer = null);
|
||||
this.registerChatListener();
|
||||
this.registerServerLifecycleListeners();
|
||||
this.registerTickListeners();
|
||||
|
||||
// Listeners
|
||||
ListenerUtils.registerCommonListeners(this.injector);
|
||||
@@ -93,12 +95,29 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
|
||||
// Commands
|
||||
CloudUtils.registerCommands(this.injector);
|
||||
|
||||
// TODO: save player data, find scheduler or use java's
|
||||
|
||||
// Load channels
|
||||
((CarbonChannelRegistry) this.channelRegistry()).loadChannels();
|
||||
}
|
||||
|
||||
// TODO: save player data on shutdown
|
||||
private void registerChatListener() {
|
||||
ChatCallback.setup();
|
||||
ChatCallback.INSTANCE.registerListener(new FabricChatListener(this, this.channelRegistry));
|
||||
}
|
||||
|
||||
private void registerServerLifecycleListeners() {
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> this.minecraftServer = server);
|
||||
ServerLifecycleEvents.SERVER_STOPPING.register($ -> PlayerUtils.saveLoggedInPlayers(this.carbonServerFabric, this.userManager).forEach(CompletableFuture::join));
|
||||
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.minecraftServer = null);
|
||||
}
|
||||
|
||||
private void registerTickListeners() {
|
||||
final long saveDelay = 5 * 60 * 20; // 5 minutes
|
||||
|
||||
ServerTickEvents.END_SERVER_TICK.register(server -> {
|
||||
if (server.getTickCount() != 0 && server.getTickCount() % saveDelay == 0) {
|
||||
PlayerUtils.saveLoggedInPlayers(this.carbonServerFabric, this.userManager);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -101,7 +101,7 @@ public enum ChatCallback {
|
||||
return;
|
||||
}
|
||||
|
||||
final Chat.MessageFormatter formatter = c.formatter == null ? defaultFormatter() : c.formatter;
|
||||
final MessageFormatter formatter = c.formatter == null ? defaultFormatter() : c.formatter;
|
||||
|
||||
final Audience console = fabricServerAudiences.console();
|
||||
final @Nullable Component msgForConsole = formatter.format(sender, chat, console);
|
||||
@@ -122,8 +122,8 @@ public enum ChatCallback {
|
||||
ServerLifecycleEvents.SERVER_STOPPED.register(server -> INSTANCE.shutdownExecutor());
|
||||
}
|
||||
|
||||
private static Chat.MessageFormatter defaultFormatter() {
|
||||
return new Chat.MessageFormatter() {
|
||||
private static MessageFormatter defaultFormatter() {
|
||||
return new MessageFormatter() {
|
||||
private @MonotonicNonNull Component rendered;
|
||||
|
||||
@Override
|
||||
@@ -157,11 +157,12 @@ public enum ChatCallback {
|
||||
|
||||
List<ServerPlayer> recipients();
|
||||
|
||||
interface MessageFormatter {
|
||||
}
|
||||
|
||||
@Nullable Component format(ServerPlayer sender, String message, Audience viewer);
|
||||
@FunctionalInterface
|
||||
public interface MessageFormatter {
|
||||
|
||||
}
|
||||
@Nullable Component format(ServerPlayer sender, String message, Audience viewer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ public final class PlayerStatusMessageEvents {
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MessageEventListener {
|
||||
void onMessage(final MessageEvent event);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.google.inject.Injector;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
@@ -121,13 +122,13 @@ public final class CarbonChatSponge implements CarbonChat {
|
||||
Sponge.asyncScheduler().submit(Task.builder()
|
||||
.interval(5, TimeUnit.MINUTES)
|
||||
.plugin(this.pluginContainer)
|
||||
.execute(() -> PlayerUtils.savePlayers(this.carbonServerSponge, this.userManager))
|
||||
.execute(() -> PlayerUtils.saveLoggedInPlayers(this.carbonServerSponge, this.userManager))
|
||||
.build());
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onDisable(final StoppingEngineEvent<Server> event) {
|
||||
PlayerUtils.savePlayers(this.carbonServerSponge, this.userManager);
|
||||
PlayerUtils.saveLoggedInPlayers(this.carbonServerSponge, this.userManager).forEach(CompletableFuture::join);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user