diff --git a/common/src/main/java/com/railwayteam/railways/Railways.java b/common/src/main/java/com/railwayteam/railways/Railways.java index a89a4d2f5..805caedb7 100644 --- a/common/src/main/java/com/railwayteam/railways/Railways.java +++ b/common/src/main/java/com/railwayteam/railways/Railways.java @@ -32,6 +32,7 @@ import com.railwayteam.railways.multiloader.Loader; import com.railwayteam.railways.registry.CRCommands; import com.railwayteam.railways.registry.CRPackets; +import com.railwayteam.railways.util.RailwaysUpdateCheck; import com.railwayteam.railways.util.Utils; import com.simibubi.create.Create; import com.simibubi.create.foundation.data.CreateRegistrate; @@ -91,6 +92,7 @@ private static void migrateConfig(Path path, Function converter) public static void init() { LOGGER.info("{} v{} initializing! Commit hash: {} Create version: {} on platform: {}", NAME, RailwaysBuildInfo.VERSION, RailwaysBuildInfo.GIT_COMMIT, Create.VERSION, Loader.getFormatted()); + RailwaysUpdateCheck.execute(); Path configDir = Utils.configDir(); Path clientConfigDir = configDir.resolve(MODID + "-client.toml"); diff --git a/common/src/main/java/com/railwayteam/railways/compat/Mods.java b/common/src/main/java/com/railwayteam/railways/compat/Mods.java index 04dbae35a..8a7172817 100644 --- a/common/src/main/java/com/railwayteam/railways/compat/Mods.java +++ b/common/src/main/java/com/railwayteam/railways/compat/Mods.java @@ -98,6 +98,10 @@ public void assertForDataGen() { assert (!requiredForDataGen || isLoaded); } + public static boolean isModLoaded(String id) { + return isModLoaded(id, null); + } + @ExpectPlatform public static boolean isModLoaded(String id, @Nullable String fabricId) { throw new AssertionError(); diff --git a/common/src/main/java/com/railwayteam/railways/multiloader/Loader.java b/common/src/main/java/com/railwayteam/railways/multiloader/Loader.java index 04e5de3c0..36271beed 100644 --- a/common/src/main/java/com/railwayteam/railways/multiloader/Loader.java +++ b/common/src/main/java/com/railwayteam/railways/multiloader/Loader.java @@ -18,6 +18,7 @@ package com.railwayteam.railways.multiloader; +import com.railwayteam.railways.compat.Mods; import com.railwayteam.railways.util.TextUtils; import dev.architectury.injectables.annotations.ExpectPlatform; import org.jetbrains.annotations.ApiStatus.Internal; @@ -26,7 +27,7 @@ import java.util.function.Supplier; public enum Loader { - FORGE, FABRIC; + FORGE, NEOFORGE, FABRIC, QUILT; public static final Loader CURRENT = getCurrent(); @@ -40,7 +41,15 @@ public void runIfCurrent(Supplier run) { } public static String getFormatted() { - return TextUtils.titleCaseConversion(Loader.CURRENT.name().toLowerCase(Locale.ROOT)); + return TextUtils.titleCaseConversion(getActual().name().toLowerCase(Locale.ROOT)); + } + + // Returns the actual loader, ex: quilt on quilt instead of fabric for quilt + public static Loader getActual() { + //noinspection ConstantValue + if (FABRIC.isCurrent() && Mods.isModLoaded("quilt_loader")) + return QUILT; + return CURRENT; } @Internal diff --git a/common/src/main/java/com/railwayteam/railways/util/RailwaysUpdateCheck.java b/common/src/main/java/com/railwayteam/railways/util/RailwaysUpdateCheck.java new file mode 100644 index 000000000..f4513fb63 --- /dev/null +++ b/common/src/main/java/com/railwayteam/railways/util/RailwaysUpdateCheck.java @@ -0,0 +1,61 @@ +/* + * Steam 'n' Rails + * Copyright (c) 2024 The Railways Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.railwayteam.railways.util; + +import com.railwayteam.railways.Railways; +import com.railwayteam.railways.RailwaysBuildInfo; +import com.railwayteam.railways.multiloader.Loader; +import net.minecraft.SharedConstants; +import net.minecraft.Util; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; + +public class RailwaysUpdateCheck { + public static void execute() { + // TODO switch to nonCriticalIoPool() in 1.21.1 + Util.ioPool().submit(() -> { + String uri = String.format( + "https://update.api.ithundxr.dev/update-check?mod_id=%s&mod_version=%s&mc_version=%s&loader=%s", + Railways.MODID, + RailwaysBuildInfo.VERSION, + SharedConstants.getCurrentVersion().getName(), + Loader.getActual() + ); + + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create(uri)) + .build(); + + try { + HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10L)) + .followRedirects(HttpClient.Redirect.ALWAYS) + .build() + .send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + } catch (IOException | InterruptedException ignored) {} + }); + } +} \ No newline at end of file diff --git a/forge/src/main/java/com/railwayteam/railways/forge/RailwaysImpl.java b/forge/src/main/java/com/railwayteam/railways/forge/RailwaysImpl.java index 9eb610891..891544d05 100644 --- a/forge/src/main/java/com/railwayteam/railways/forge/RailwaysImpl.java +++ b/forge/src/main/java/com/railwayteam/railways/forge/RailwaysImpl.java @@ -26,18 +26,14 @@ import com.railwayteam.railways.registry.forge.CRParticleTypesParticleEntryImpl; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands.CommandSelection; -import net.minecraftforge.common.util.MavenVersionStringHelper; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.forgespi.language.IModInfo; import java.util.HashSet; -import java.util.List; import java.util.Set; import java.util.function.BiConsumer;