Save and invalidate player on quit on all platforms

This commit is contained in:
Draycia authored and Josua Parks committed 2021-11-24 21:26:29 -08:00
1 parent 15c994bdbd
commit 20e384522e
9 files changed
+306 -36

No files matched your search

@@ -49,7 +49,7 @@ public class BukkitPlayerJoinListener implements Listener {
final CarbonChat carbonChat,
final PrimaryConfig primaryConfig,
final UserManager<CarbonPlayerCommon> userManager
) {
) {
this.carbonChat = carbonChat;
this.primaryConfig = primaryConfig;
this.userManager = userManager;
@@ -71,30 +71,8 @@ public class BukkitPlayerJoinListener implements Listener {
}
}
@EventHandler(priority = EventPriority.LOWEST)
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();
if (result.player() == null) {
return;
}
final CarbonPlayer player = result.player();
// Don't show quit messages when muted
if (!player.muteEntries().isEmpty()) {
event.quitMessage(null);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void saveOnQuit(final PlayerQuitEvent event) {
public void onQuit(final PlayerQuitEvent event) {
final ComponentPlayerResult<CarbonPlayer> result =
this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
@@ -104,6 +82,12 @@ public class BukkitPlayerJoinListener implements Listener {
final CarbonPlayer player = result.player();
if (this.primaryConfig.hideMutedJoinLeaveQuit()) {
if (!player.muteEntries().isEmpty()) {
event.quitMessage(null);
}
}
PlayerUtils.saveAndInvalidatePlayer(this.carbonChat.server(), this.userManager, (CarbonPlayerBukkit) player);
}
@@ -150,7 +150,7 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(CarbonPlayerCommon player) {
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.savePlayer(player).thenApply(result -> {
this.userCache.remove(player.uuid());
@@ -39,7 +39,11 @@ 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.callback.PlayerStatusMessageEvents;
import net.draycia.carbon.fabric.listeners.FabricChatListener;
import net.draycia.carbon.fabric.listeners.FabricPlayerDeathListener;
import net.draycia.carbon.fabric.listeners.FabricPlayerJoinListener;
import net.draycia.carbon.fabric.listeners.FabricPlayerLeaveListener;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
@@ -87,6 +91,7 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
// Platform Listeners
this.registerChatListener();
this.registerServerLifecycleListeners();
this.registerPlayerStatusListeners();
this.registerTickListeners();
// Listeners
@@ -110,6 +115,12 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.minecraftServer = null);
}
private void registerPlayerStatusListeners() {
PlayerStatusMessageEvents.JOIN_MESSAGE.register(this.injector.getInstance(FabricPlayerJoinListener.class));
PlayerStatusMessageEvents.QUIT_MESSAGE.register(this.injector.getInstance(FabricPlayerLeaveListener.class));
PlayerStatusMessageEvents.DEATH_MESSAGE.register(this.injector.getInstance(FabricPlayerDeathListener.class));
}
private void registerTickListeners() {
final long saveDelay = 5 * 60 * 20; // 5 minutes
@@ -0,0 +1,66 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.fabric.listeners;
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.common.config.PrimaryConfig;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class FabricPlayerDeathListener implements PlayerStatusMessageEvents.MessageEventListener {
private final CarbonChat carbonChat;
private final PrimaryConfig primaryConfig;
@Inject
public FabricPlayerDeathListener(
final CarbonChat carbonChat,
final PrimaryConfig primaryConfig
) {
this.carbonChat = carbonChat;
this.primaryConfig = primaryConfig;
}
@Override
public void onMessage(final PlayerStatusMessageEvents.MessageEvent 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.player().getUUID()).join();
if (result.player() == null) {
return;
}
final CarbonPlayer player = result.player();
if (!player.muteEntries().isEmpty()) {
event.disableMessage();
}
}
}
@@ -0,0 +1,63 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.fabric.listeners;
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.common.config.PrimaryConfig;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class FabricPlayerJoinListener implements PlayerStatusMessageEvents.MessageEventListener {
private final CarbonChat carbonChat;
private final PrimaryConfig primaryConfig;
@Inject
public FabricPlayerJoinListener(
final CarbonChat carbonChat,
final PrimaryConfig primaryConfig
) {
this.carbonChat = carbonChat;
this.primaryConfig = primaryConfig;
}
@Override
public void onMessage(final PlayerStatusMessageEvents.MessageEvent event) {
final ComponentPlayerResult<CarbonPlayer> result =
this.carbonChat.server().player(event.player().getUUID()).join();
final @Nullable CarbonPlayer player = result.player();
if (player == null) {
return;
}
// Don't show join messages when muted
if (this.primaryConfig.hideMutedJoinLeaveQuit() && !player.muteEntries().isEmpty()) {
event.disableMessage();
}
}
}
@@ -0,0 +1,73 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.fabric.listeners;
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.common.config.PrimaryConfig;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.PlayerUtils;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import net.draycia.carbon.fabric.users.CarbonPlayerFabric;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class FabricPlayerLeaveListener implements PlayerStatusMessageEvents.MessageEventListener {
private final CarbonChat carbonChat;
private final PrimaryConfig primaryConfig;
private final UserManager<CarbonPlayerCommon> userManager;
@Inject
public FabricPlayerLeaveListener(
final CarbonChat carbonChat,
final PrimaryConfig primaryConfig,
final UserManager<CarbonPlayerCommon> userManager
) {
this.carbonChat = carbonChat;
this.primaryConfig = primaryConfig;
this.userManager = userManager;
}
@Override
public void onMessage(final PlayerStatusMessageEvents.MessageEvent event) {
final ComponentPlayerResult<CarbonPlayer> result =
this.carbonChat.server().player(event.player().getUUID()).join();
if (result.player() == null) {
return;
}
final CarbonPlayer player = result.player();
if (this.primaryConfig.hideMutedJoinLeaveQuit()) {
if (!player.muteEntries().isEmpty()) {
event.disableMessage();
}
}
PlayerUtils.saveAndInvalidatePlayer(this.carbonChat.server(), this.userManager, (CarbonPlayerFabric) player);
}
}
@@ -23,7 +23,12 @@ 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.common.config.PrimaryConfig;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.PlayerUtils;
import net.draycia.carbon.sponge.users.CarbonPlayerSponge;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -35,14 +40,17 @@ public class SpongePlayerJoinListener {
private final CarbonChat carbonChat;
private final PrimaryConfig primaryConfig;
private final UserManager<CarbonPlayerCommon> userManager;
@Inject
public SpongePlayerJoinListener(
final CarbonChat carbonChat,
final PrimaryConfig primaryConfig
final PrimaryConfig primaryConfig,
final UserManager<CarbonPlayerCommon> userManager
) {
this.carbonChat = carbonChat;
this.primaryConfig = primaryConfig;
this.userManager = userManager;
}
@Listener
@@ -62,12 +70,8 @@ public class SpongePlayerJoinListener {
@Listener
public void onPlayerQuit(final ServerSideConnectionEvent.Disconnect 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.player().uniqueId()).join();
final ComponentPlayerResult<CarbonPlayer> result =
this.carbonChat.server().player(event.player().uniqueId()).join();
if (result.player() == null) {
return;
@@ -75,10 +79,13 @@ public class SpongePlayerJoinListener {
final CarbonPlayer player = result.player();
// Don't show quit messages when muted
if (!player.muteEntries().isEmpty()) {
event.setMessage(null); // TODO: null? empty? it's not MessageCancellable, which is odd
if (this.primaryConfig.hideMutedJoinLeaveQuit()) {
if (!player.muteEntries().isEmpty()) {
event.setAudience(Audience.empty());
}
}
PlayerUtils.saveAndInvalidatePlayer(this.carbonChat.server(), this.userManager, (CarbonPlayerSponge) player);
}
}
@@ -41,6 +41,7 @@ import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.common.util.ListenerUtils;
import net.draycia.carbon.velocity.listeners.VelocityChatListener;
import net.draycia.carbon.velocity.listeners.VelocityPlayerJoinListener;
import net.kyori.adventure.text.Component;
import net.kyori.moonshine.message.IMessageRenderer;
import org.apache.logging.log4j.LogManager;
@@ -61,7 +62,8 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public class CarbonChatVelocity implements CarbonChat {
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
VelocityChatListener.class
VelocityChatListener.class,
VelocityPlayerJoinListener.class
);
private final Path dataDirectory;
@@ -0,0 +1,64 @@
/*
* CarbonChat
*
* Copyright (c) 2021 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.velocity.listeners;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
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.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.util.PlayerUtils;
import net.draycia.carbon.velocity.users.CarbonPlayerVelocity;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class VelocityPlayerJoinListener {
private final CarbonChat carbonChat;
private final UserManager<CarbonPlayerCommon> userManager;
@Inject
public VelocityPlayerJoinListener(
final CarbonChat carbonChat,
final UserManager<CarbonPlayerCommon> userManager
) {
this.carbonChat = carbonChat;
this.userManager = userManager;
}
@Subscribe
public void onPlayerLeave(final DisconnectEvent 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, (CarbonPlayerVelocity) player);
}
}