diff --git a/gradle.properties b/gradle.properties index 3aaae1730..c227761ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ fabric_version = 0.91.0+1.20.1 # Mappings # https://lambdaurora.dev/tools/import_quilt.html -qm_version = 1 +qm_version = 23 # https://parchmentmc.org/docs/getting-started parchment_version = none diff --git a/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/PortingLibConfig.java b/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/PortingLibConfig.java index f554f234d..a0c087471 100644 --- a/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/PortingLibConfig.java +++ b/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/PortingLibConfig.java @@ -4,10 +4,10 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; -import java.util.stream.Collectors; -import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking; +import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; + +import net.minecraft.server.level.ServerPlayer; import org.slf4j.Logger; @@ -16,23 +16,32 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; -import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.world.level.storage.LevelResource; public class PortingLibConfig implements ModInitializer { + public static final String ID = "porting_lib_config"; private static final Logger LOGGER = LogUtils.getLogger(); private static final LevelResource SERVERCONFIG = new LevelResource("serverconfig"); - private Path getServerConfigPath(final MinecraftServer server) { - final Path serverConfig = server.getWorldPath(SERVERCONFIG); + private Path getServerConfigPath(MinecraftServer server) { + Path serverConfig = server.getWorldPath(SERVERCONFIG); getOrCreateDirectory(serverConfig, "serverconfig"); return serverConfig; } - public static final ResourceLocation CONFIG_SYNC = new ResourceLocation("porting_lib_config", "config_sync"); + /** + * The ID for the config sync packet. + */ + public static final ResourceLocation CONFIG_SYNC = new ResourceLocation(ID, "config_sync"); + /** + * An event phase for {@link ServerPlayConnectionEvents#JOIN} that comes after config sync. + * If you need to send packets to players on join that depend on config values already being synced, + * register using this phase. + */ + public static final ResourceLocation AFTER_CONFIG_SYNC = new ResourceLocation(ID, "after_config_sync"); @Override public void onInitialize() { @@ -43,22 +52,25 @@ public void onInitialize() { ConfigTracker.INSTANCE.unloadConfigs(ConfigType.SERVER, getServerConfigPath(server)); }); - ServerLoginConnectionEvents.QUERY_START.register((handler, server, sender, synchronizer) -> { - synchronizer.waitFor(server.submit(() -> { - final Map configData = ConfigTracker.INSTANCE.configSets().get(ConfigType.SERVER).stream().collect(Collectors.toMap(ModConfig::getFileName, mc -> { - try { - return Files.readAllBytes(mc.getFullPath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - })); - configData.forEach((key, value) -> { + ServerPlayConnectionEvents.JOIN.addPhaseOrdering(CONFIG_SYNC, AFTER_CONFIG_SYNC); + ServerPlayConnectionEvents.JOIN.register(CONFIG_SYNC, (handler, sender, server) -> { + ServerPlayer player = handler.player; + if (server.isSingleplayerOwner(player.getGameProfile())) + return; // don't sync to self + + ConfigTracker.INSTANCE.configSets().get(ConfigType.SERVER).forEach(config -> { + try { + String name = config.getFileName(); + byte[] data = Files.readAllBytes(config.getFullPath()); + FriendlyByteBuf buf = PacketByteBufs.create(); - buf.writeUtf(key); - buf.writeByteArray(value); + buf.writeUtf(name); + buf.writeByteArray(data); sender.sendPacket(CONFIG_SYNC, buf); - }); - })); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); }); } diff --git a/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/client/PortingLibConfigClient.java b/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/client/PortingLibConfigClient.java index 4d78c7bd0..149bcbf19 100644 --- a/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/client/PortingLibConfigClient.java +++ b/modules/config/src/main/java/io/github/fabricators_of_create/porting_lib/config/client/PortingLibConfigClient.java @@ -1,24 +1,28 @@ package io.github.fabricators_of_create.porting_lib.config.client; -import java.util.Optional; -import java.util.concurrent.CompletableFuture; - import io.github.fabricators_of_create.porting_lib.config.ConfigTracker; +import io.github.fabricators_of_create.porting_lib.config.ModConfig; import io.github.fabricators_of_create.porting_lib.config.PortingLibConfig; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking; -import net.minecraft.client.Minecraft; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class PortingLibConfigClient implements ClientModInitializer { + private static final Logger logger = LoggerFactory.getLogger(PortingLibConfigClient.class); + @Override public void onInitializeClient() { - ClientLoginNetworking.registerGlobalReceiver(PortingLibConfig.CONFIG_SYNC, (client, handler, buf, listenerAdder) -> { - if (!Minecraft.getInstance().isLocalServer()) { - String fileName = buf.readUtf(); - byte[] bytes = buf.readByteArray(); - Optional.ofNullable(ConfigTracker.INSTANCE.fileMap().get(fileName)).ifPresent(mc-> mc.acceptSyncedConfig(bytes)); + ClientPlayNetworking.registerGlobalReceiver(PortingLibConfig.CONFIG_SYNC, (client, handler, buf, sender) -> { + String name = buf.readUtf(); + byte[] data = buf.readByteArray(); + ModConfig config = ConfigTracker.INSTANCE.fileMap().get(name); + if (config != null) { + config.acceptSyncedConfig(data); + } else { + logger.warn("Received config data for unknown file {}", name); } - return CompletableFuture.completedFuture(null); }); } }