mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Save and invalidate on quit (bukkit)
This commit is contained in:
1 parent
c36fed6f43
commit
15c994bdbd
4 files changed
+72
-8
No files matched your search
@@ -50,4 +50,13 @@ public interface UserManager<C extends CarbonPlayer> {
|
||||
*/
|
||||
CompletableFuture<ComponentPlayerResult<C>> savePlayer(final C player);
|
||||
|
||||
/**
|
||||
* Saves the {@link CarbonPlayer}, returns the result, and invalidates the player entry.
|
||||
*
|
||||
* @param player the player to save
|
||||
* @return the result
|
||||
* @since 2.0.0
|
||||
*/
|
||||
CompletableFuture<ComponentPlayerResult<C>> saveAndInvalidatePlayer(final C player);
|
||||
|
||||
}
|
||||
+29
-6
@@ -23,7 +23,11 @@ import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -38,19 +42,23 @@ public class BukkitPlayerJoinListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
private final UserManager<CarbonPlayerCommon> userManager;
|
||||
|
||||
@Inject
|
||||
public BukkitPlayerJoinListener(
|
||||
final CarbonChat carbonChat,
|
||||
final PrimaryConfig primaryConfig
|
||||
) {
|
||||
final PrimaryConfig primaryConfig,
|
||||
final UserManager<CarbonPlayerCommon> userManager
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.primaryConfig = primaryConfig;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerLogin(final PlayerJoinEvent event) {
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
public void handleJoinMessages(final PlayerJoinEvent event) {
|
||||
final ComponentPlayerResult<CarbonPlayer> result =
|
||||
this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final @Nullable CarbonPlayer player = result.player();
|
||||
|
||||
if (player == null) {
|
||||
@@ -64,13 +72,14 @@ public class BukkitPlayerJoinListener implements Listener {
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerQuit(final PlayerQuitEvent event) {
|
||||
public void handleQuitMessages(final PlayerQuitEvent event) {
|
||||
// Early exit in case "hide muted join / leave messages when muted" is disabled
|
||||
if (!this.primaryConfig.hideMutedJoinLeaveQuit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final ComponentPlayerResult<CarbonPlayer> result =
|
||||
this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
@@ -84,4 +93,18 @@ public class BukkitPlayerJoinListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void saveOnQuit(final PlayerQuitEvent event) {
|
||||
final ComponentPlayerResult<CarbonPlayer> result =
|
||||
this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
PlayerUtils.saveAndInvalidatePlayer(this.carbonChat.server(), this.userManager, (CarbonPlayerBukkit) player);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -149,4 +149,13 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(CarbonPlayerCommon player) {
|
||||
return this.savePlayer(player).thenApply(result -> {
|
||||
this.userCache.remove(player.uuid());
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,12 +44,35 @@ public final class PlayerUtils {
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(
|
||||
public static CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(
|
||||
final CarbonServer carbonServer,
|
||||
final UserManager<CarbonPlayerCommon> userManager,
|
||||
final WrappedCarbonPlayer player
|
||||
) {
|
||||
final var saveResult = userManager.savePlayer(player.carbonPlayerCommon());
|
||||
final var saveResult =
|
||||
userManager.savePlayer(player.carbonPlayerCommon());
|
||||
|
||||
saveResult.thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
carbonServer.console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
|
||||
saveResult.exceptionally(exception -> {
|
||||
exception.getCause().printStackTrace();
|
||||
return null;
|
||||
});
|
||||
|
||||
return saveResult;
|
||||
}
|
||||
|
||||
public static CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(
|
||||
final CarbonServer carbonServer,
|
||||
final UserManager<CarbonPlayerCommon> userManager,
|
||||
final WrappedCarbonPlayer player
|
||||
) {
|
||||
final var saveResult =
|
||||
userManager.saveAndInvalidatePlayer(player.carbonPlayerCommon());
|
||||
|
||||
saveResult.thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
|
||||
Reference in new issue
Block a user