bump fabric to 1.19.4 and change server instance handling

This commit is contained in:
Jason Penilla committed 2023-03-25 12:54:02 -07:00
1 parent 64a9bd410b
commit 442f5e07e0
17 files changed
+139 -133

No files matched your search

@@ -36,13 +36,11 @@ import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.users.ProfileCache;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.fabric.callback.ChatCallback;
import net.draycia.carbon.fabric.command.DeleteMessageCommand;
import net.draycia.carbon.fabric.listeners.FabricChatListener;
import net.draycia.carbon.fabric.listeners.FabricChatPreviewListener;
import net.draycia.carbon.fabric.listeners.FabricPlayerJoinListener;
import net.draycia.carbon.fabric.listeners.FabricPlayerLeaveListener;
import net.draycia.carbon.fabric.listeners.FabricJoinQuitListener;
import net.draycia.carbon.fabric.users.CarbonPlayerFabric;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.message.v1.ServerMessageDecoratorEvent;
@@ -55,20 +53,14 @@ import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.ChatType;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
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 java.util.Objects.requireNonNull;
@DefaultQualifier(NonNull.class)
@Singleton
public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabric> {
private @Nullable MinecraftServer minecraftServer;
public static ResourceKey<ChatType> CHAT_TYPE = ResourceKey.create(Registries.CHAT_TYPE, new ResourceLocation("carbon", "chat"));
@Inject
@@ -79,7 +71,7 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
final @PeriodicTasks ScheduledExecutorService periodicTasks,
final ProfileCache profileCache,
final ProfileResolver profileResolver,
final UserManagerInternal<CarbonPlayerFabric> userManager,
final FabricUserManager userManager,
final ExecutionCoordinatorHolder commandExecutor,
final CarbonServer carbonServer,
final CarbonMessages carbonMessages,
@@ -124,20 +116,13 @@ public final class CarbonChatFabric extends CarbonChatInternal<CarbonPlayerFabri
}
private void registerServerLifecycleListeners() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> this.minecraftServer = server);
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
this.shutdown();
this.minecraftServer = null;
});
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.shutdown());
}
private void registerPlayerStatusListeners() {
ServerPlayConnectionEvents.DISCONNECT.register(this.injector().getInstance(FabricPlayerLeaveListener.class));
ServerPlayConnectionEvents.JOIN.register(this.injector().getInstance(FabricPlayerJoinListener.class));
}
public MinecraftServer minecraftServer() {
return requireNonNull(this.minecraftServer, "Attempted to get the MinecraftServer instance when one is not active.");
final FabricJoinQuitListener listener = this.injector().getInstance(FabricJoinQuitListener.class);
ServerPlayConnectionEvents.DISCONNECT.register(listener);
ServerPlayConnectionEvents.JOIN.register(listener);
}
public boolean luckPermsLoaded() {
@@ -106,8 +106,8 @@ public final class CarbonChatFabricModule extends AbstractModule {
}
@Provides
public PlayerSuggestions playerSuggestions(final Provider<CarbonChatFabric> carbon) {
return (ctx, input) -> carbon.get().minecraftServer().getPlayerList().getPlayers().stream()
public PlayerSuggestions playerSuggestions(final MinecraftServerHolder serverHolder) {
return (ctx, input) -> serverHolder.requireServer().getPlayerList().getPlayers().stream()
.map(player -> player.getGameProfile().getName())
.toList();
}
@@ -38,28 +38,28 @@ import org.jetbrains.annotations.NotNull;
@DefaultQualifier(NonNull.class)
public final class CarbonServerFabric implements CarbonServer, ForwardingAudience.Single {
private final CarbonChatFabric carbonChatFabric;
private final MinecraftServerHolder serverHolder;
private final FabricUserManager userManager;
@Inject
private CarbonServerFabric(final CarbonChatFabric carbonChatFabric, final FabricUserManager userManager) {
this.carbonChatFabric = carbonChatFabric;
private CarbonServerFabric(final MinecraftServerHolder serverHolder, final FabricUserManager userManager) {
this.serverHolder = serverHolder;
this.userManager = userManager;
}
@Override
public @NotNull Audience audience() {
return FabricServerAudiences.of(this.carbonChatFabric.minecraftServer()).all();
return FabricServerAudiences.of(this.serverHolder.requireServer()).all();
}
@Override
public Audience console() {
return this.carbonChatFabric.minecraftServer().createCommandSourceStack();
return this.serverHolder.requireServer().createCommandSourceStack();
}
@Override
public List<? extends CarbonPlayer> players() {
return this.carbonChatFabric.minecraftServer().getPlayerList().getPlayers().stream()
return this.serverHolder.requireServer().getPlayerList().getPlayers().stream()
.map(serverPlayer -> this.userManager.user(serverPlayer.getUUID()).getNow(null))
.filter(Objects::nonNull)
.toList();
@@ -20,6 +20,7 @@
package net.draycia.carbon.fabric;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import net.draycia.carbon.common.users.Backing;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
@@ -33,17 +34,23 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@Singleton
public final class FabricUserManager extends PlatformUserManager<CarbonPlayerFabric> {
private final CarbonChatFabric carbonChatFabric;
private final MinecraftServerHolder serverHolder;
private final Provider<CarbonChatFabric> carbonChat;
@Inject
private FabricUserManager(final @Backing UserManagerInternal<CarbonPlayerCommon> proxiedUserManager, final CarbonChatFabric carbonChatFabric) {
super(proxiedUserManager);
this.carbonChatFabric = carbonChatFabric;
private FabricUserManager(
final @Backing UserManagerInternal<CarbonPlayerCommon> backingManager,
final MinecraftServerHolder serverHolder,
final Provider<CarbonChatFabric> carbonChat
) {
super(backingManager);
this.serverHolder = serverHolder;
this.carbonChat = carbonChat;
}
@Override
protected CarbonPlayerFabric wrap(final CarbonPlayerCommon common) {
return new CarbonPlayerFabric(common, this.carbonChatFabric);
return new CarbonPlayerFabric(common, this.serverHolder, this.carbonChat);
}
@Override
@@ -0,0 +1,45 @@
/*
* 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.fabric;
import com.google.inject.Inject;
import java.util.Objects;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.server.MinecraftServer;
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 MinecraftServerHolder {
private @Nullable MinecraftServer server;
@Inject
private MinecraftServerHolder() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> this.server = server);
ServerLifecycleEvents.SERVER_STOPPED.register(server -> this.server = null);
}
public MinecraftServer requireServer() {
return Objects.requireNonNull(this.server, "server requested when not active");
}
}
@@ -26,6 +26,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType;
@@ -33,7 +34,6 @@ import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.thread.NamedThreadFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -54,8 +54,8 @@ public enum ChatCallback {
private @Nullable ExecutorService chatExecutor;
private final List<Consumer<Chat>> listeners = new CopyOnWriteArrayList<>();
private void initExecutor() {
this.chatExecutor = Executors.newSingleThreadExecutor(new NamedThreadFactory("CarbonChat-Chat-Thread"));
private void initExecutor(final Logger logger) {
this.chatExecutor = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "Chat Handling"));
}
private void shutdownExecutor() {
@@ -114,7 +114,7 @@ public enum ChatCallback {
}
public static void setup() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> INSTANCE.initExecutor());
ServerLifecycleEvents.SERVER_STARTING.register(server -> INSTANCE.initExecutor(LOGGER));
ServerLifecycleEvents.SERVER_STOPPED.register(server -> INSTANCE.shutdownExecutor());
}
@@ -23,25 +23,36 @@ import com.google.inject.Inject;
import java.util.List;
import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.users.ProfileCache;
import net.draycia.carbon.common.users.UserManagerInternal;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.protocol.game.ClientboundCustomChatCompletionsPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
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;
@DefaultQualifier(NonNull.class)
public class FabricPlayerJoinListener implements ServerPlayConnectionEvents.Join {
public class FabricJoinQuitListener implements ServerPlayConnectionEvents.Join, ServerPlayConnectionEvents.Disconnect {
private final ProfileCache profileCache;
private final Logger logger;
private final ConfigFactory configFactory;
private final UserManagerInternal<?> userManager;
@Inject
public FabricPlayerJoinListener(final ConfigFactory configFactory, final ProfileCache profileCache) {
public FabricJoinQuitListener(
final Logger logger,
final ConfigFactory configFactory,
final ProfileCache profileCache,
final UserManagerInternal<?> userManager
) {
this.logger = logger;
this.configFactory = configFactory;
this.profileCache = profileCache;
this.userManager = userManager;
}
@Override
@@ -54,8 +65,15 @@ public class FabricPlayerJoinListener implements ServerPlayConnectionEvents.Join
return;
}
sender.sendPacket(new ClientboundCustomChatCompletionsPacket(ClientboundCustomChatCompletionsPacket.Action.SET,
suggestions));
sender.sendPacket(new ClientboundCustomChatCompletionsPacket(ClientboundCustomChatCompletionsPacket.Action.SET, suggestions));
}
@Override
public void onPlayDisconnect(final ServerGamePacketListenerImpl handler, final MinecraftServer server) {
this.userManager.loggedOut(handler.getPlayer().getGameProfile().getId()).exceptionally(thr -> {
this.logger.warn("Exception saving data for player " + handler.getPlayer().getGameProfile().getName() + " with uuid " + handler.getPlayer().getGameProfile().getId());
return null;
});
}
}
@@ -1,54 +0,0 @@
/*
* 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.fabric.listeners;
import com.google.inject.Inject;
import net.draycia.carbon.fabric.FabricUserManager;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public class FabricPlayerLeaveListener implements ServerPlayConnectionEvents.Disconnect {
private final FabricUserManager userManager;
private final Logger logger;
@Inject
public FabricPlayerLeaveListener(
final Logger logger,
final FabricUserManager userManager
) {
this.logger = logger;
this.userManager = userManager;
}
@Override
public void onPlayDisconnect(final ServerGamePacketListenerImpl handler, final MinecraftServer server) {
this.userManager.loggedOut(handler.getPlayer().getGameProfile().getId()).exceptionally(thr -> {
this.logger.warn("Exception saving data for player " + handler.getPlayer().getGameProfile().getName() + " with uuid " + handler.getPlayer().getGameProfile().getId());
return null;
});
}
}
@@ -19,6 +19,7 @@
*/
package net.draycia.carbon.fabric.users;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -30,6 +31,7 @@ import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
import net.draycia.carbon.fabric.CarbonChatFabric;
import net.draycia.carbon.fabric.MinecraftServerHolder;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
@@ -46,10 +48,12 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public class CarbonPlayerFabric extends WrappedCarbonPlayer implements ForwardingAudience.Single {
private final CarbonPlayerCommon carbonPlayerCommon;
private final CarbonChatFabric carbonChatFabric;
private final MinecraftServerHolder serverHolder;
private final Provider<CarbonChatFabric> carbonChatFabric;
public CarbonPlayerFabric(final CarbonPlayerCommon carbonPlayerCommon, final CarbonChatFabric carbonChatFabric) {
public CarbonPlayerFabric(final CarbonPlayerCommon carbonPlayerCommon, final MinecraftServerHolder serverHolder, final Provider<CarbonChatFabric> carbonChatFabric) {
this.carbonPlayerCommon = carbonPlayerCommon;
this.serverHolder = serverHolder;
this.carbonChatFabric = carbonChatFabric;
}
@@ -60,7 +64,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
public Optional<ServerPlayer> player() {
return Optional.ofNullable(
this.carbonChatFabric.minecraftServer().getPlayerList()
this.serverHolder.requireServer().getPlayerList()
.getPlayer(this.carbonPlayerCommon.uuid())
);
}
@@ -109,7 +113,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
}
final @Nullable ServerPlayer player = this.player().orElse(null);
final @Nullable ServerPlayer otherPlayer = this.carbonChatFabric.minecraftServer()
final @Nullable ServerPlayer otherPlayer = this.serverHolder.requireServer()
.getPlayerList().getPlayer(other.uuid());
if (player == null || otherPlayer == null) {
@@ -130,7 +134,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
}
final Optional<ServerPlayer> player = this.player();
final @Nullable ServerPlayer otherPlayer = this.carbonChatFabric.minecraftServer()
final @Nullable ServerPlayer otherPlayer = this.serverHolder.requireServer()
.getPlayerList().getPlayer(other.uuid());
if (player.isEmpty() || otherPlayer == null) {
@@ -184,7 +188,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
@Override
public String primaryGroup() {
if (!this.carbonChatFabric.luckPermsLoaded()) {
if (!this.carbonChatFabric.get().luckPermsLoaded()) {
return "default";
}
@@ -193,7 +197,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
@Override
public List<String> groups() {
if (!this.carbonChatFabric.luckPermsLoaded()) {
if (!this.carbonChatFabric.get().luckPermsLoaded()) {
return List.of("default");
}
@@ -25,7 +25,7 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.common.users.MojangProfileResolver;
import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.fabric.CarbonChatFabric;
import net.draycia.carbon.fabric.MinecraftServerHolder;
import net.minecraft.server.level.ServerPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -35,18 +35,18 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public final class FabricProfileResolver implements ProfileResolver {
private final CarbonChatFabric carbonChatFabric;
private final MinecraftServerHolder serverHolder;
private final ProfileResolver mojang;
@Inject
private FabricProfileResolver(final CarbonChatFabric carbonChatFabric, final MojangProfileResolver mojang) {
this.carbonChatFabric = carbonChatFabric;
private FabricProfileResolver(final MinecraftServerHolder serverHolder, final MojangProfileResolver mojang) {
this.serverHolder = serverHolder;
this.mojang = mojang;
}
@Override
public CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) {
final @Nullable ServerPlayer online = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayerByName(username);
final @Nullable ServerPlayer online = this.serverHolder.requireServer().getPlayerList().getPlayerByName(username);
if (online != null) {
return CompletableFuture.completedFuture(online.getUUID());
}
@@ -56,7 +56,7 @@ public final class FabricProfileResolver implements ProfileResolver {
@Override
public CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) {
final @Nullable ServerPlayer online = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayer(uuid);
final @Nullable ServerPlayer online = this.serverHolder.requireServer().getPlayerList().getPlayer(uuid);
if (online != null) {
return CompletableFuture.completedFuture(online.getGameProfile().getName());
}