From 490fa847b9eb3b5246f554183231e406f6dc7594 Mon Sep 17 00:00:00 2001 From: "Josiah (Gaming32) Glosson" Date: Sun, 5 Nov 2023 06:14:47 -0600 Subject: [PATCH] Port to NeoForge 20.2 --- build.gradle.kts | 2 + settings.gradle.kts | 1 + .../io/github/gaming32/worldhost/Loader.java | 6 +- .../github/gaming32/worldhost/WorldHost.java | 29 +++++-- .../gui/screen/OnlineFriendsScreen.java | 6 +- .../worldhost/mixin/MixinTitleScreen.java | 4 +- src/main/resources/META-INF/mods.toml | 20 ++++- version.gradle.kts | 76 ++++++++++++------- 8 files changed, 102 insertions(+), 42 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 905c696..beb8fea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,6 +10,7 @@ repositories { preprocess { val fabric12002 = createNode("1.20.2-fabric", 1_20_02, "yarn") + val neoforge12002 = createNode("1.20.2-neoforge", 1_20_02, "yarn") val fabric12001 = createNode("1.20.1-fabric", 1_20_01, "yarn") val forge12001 = createNode("1.20.1-forge", 1_20_01, "srg") val fabric11904 = createNode("1.19.4-fabric", 1_19_04, "yarn") @@ -24,6 +25,7 @@ preprocess { val forge11605 = createNode("1.16.5-forge", 1_16_05, "srg") val fabric11601 = createNode("1.16.1-fabric", 1_16_01, "yarn") + fabric12002.link(neoforge12002) fabric12002.link(fabric12001) fabric12001.link(forge12001) forge12001.link(forge11904) diff --git a/settings.gradle.kts b/settings.gradle.kts index 197ae71..9ef05f1 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -35,6 +35,7 @@ listOf( "1.19.4-fabric", "1.20.1-forge", "1.20.1-fabric", + "1.20.2-neoforge", "1.20.2-fabric", ).forEach { version -> include(":$version") diff --git a/src/main/java/io/github/gaming32/worldhost/Loader.java b/src/main/java/io/github/gaming32/worldhost/Loader.java index 5e038da..164cd23 100644 --- a/src/main/java/io/github/gaming32/worldhost/Loader.java +++ b/src/main/java/io/github/gaming32/worldhost/Loader.java @@ -3,7 +3,7 @@ import java.util.Locale; public enum Loader { - FABRIC, FORGE; + FABRIC, FORGE, NEOFORGE; private final String lowercase = name().toLowerCase(Locale.ROOT); @@ -11,4 +11,8 @@ public enum Loader { public String toString() { return lowercase; } + + public boolean isForgeLike() { + return this == FORGE || this == NEOFORGE; + } } diff --git a/src/main/java/io/github/gaming32/worldhost/WorldHost.java b/src/main/java/io/github/gaming32/worldhost/WorldHost.java index f358ef6..4036d6e 100644 --- a/src/main/java/io/github/gaming32/worldhost/WorldHost.java +++ b/src/main/java/io/github/gaming32/worldhost/WorldHost.java @@ -90,14 +90,25 @@ //$$ import net.minecraft.client.gui.screens.Screen; //$$ import net.minecraft.server.packs.PackType; //$$ import net.minecraft.server.packs.PackResources; +//#if FORGE //$$ import net.minecraftforge.api.distmarker.Dist; //$$ 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.event.lifecycle.FMLClientSetupEvent; +//#else +//$$ import net.neoforged.api.distmarker.Dist; +//$$ import net.neoforged.bus.api.SubscribeEvent; +//$$ import net.neoforged.fml.ModList; +//$$ import net.neoforged.fml.ModLoadingContext; +//$$ import net.neoforged.fml.common.Mod; +//$$ import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +//#endif //$$ import java.util.function.BiFunction; -//#if MC >= 1.19.2 +//#if NEOFORGE +//$$ import net.neoforged.neoforge.client.ConfigScreenHandler; +//#elseif MC >= 1.19.2 //$$ import net.minecraftforge.client.ConfigScreenHandler; //#elseif MC >= 1.18.2 //$$ import net.minecraftforge.client.ConfigGuiHandler; @@ -106,7 +117,9 @@ //#else //$$ import net.minecraftforge.fml.ExtensionPoint; //#endif -//#if MC > 1.17.1 +//#if NEOFORGE +//$$ import net.neoforged.neoforge.resource.ResourcePackLoader; +//#elseif MC > 1.17.1 //$$ import net.minecraftforge.resource.ResourcePackLoader; //#elseif MC > 1.16.5 //$$ import net.minecraftforge.fmllegacy.packs.ResourcePackLoader; @@ -115,7 +128,7 @@ //#endif //#endif -//#if FORGE +//#if FORGELIKE //$$ @Mod(WorldHost.MOD_ID) //#endif public class WorldHost @@ -124,7 +137,7 @@ public class WorldHost //#endif { public static final String MOD_ID = - //#if FORGE + //#if FORGELIKE //$$ "world_host"; //#else "world-host"; @@ -138,7 +151,9 @@ public class WorldHost //#endif public static final Loader MOD_LOADER = - //#if FORGE + //#if NEOFORGE + //$$ Loader.NEOFORGE; + //#elseif FORGE //$$ Loader.FORGE; //#else Loader.FABRIC; @@ -573,7 +588,7 @@ public static ServerStatus createEmptyServerStatus() { //#if MC >= 1.19.4 return new ServerStatus( Components.EMPTY, Optional.empty(), Optional.empty(), Optional.empty(), false - //#if FORGE + //#if FORGELIKE //$$ , Optional.empty() //#endif ); @@ -784,7 +799,7 @@ public static int getMMCLines(boolean isPause) { } //#endif - //#if FORGE + //#if FORGELIKE //$$ @Mod.EventBusSubscriber(modid = MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) //$$ public static class ClientModEvents { //$$ @SubscribeEvent diff --git a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java index 1535044..2341067 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java @@ -289,7 +289,7 @@ public class OnlineFriendsListEntry extends ObjectSelectionList.Entry= 1.19.4 - private byte @Nullable [] iconData; + private byte[] iconData; //#else //$$ @Nullable //$$ private String iconData; @@ -350,7 +350,7 @@ public void render( } //#if MC >= 1.19.4 - final byte @Nullable [] icon = serverInfo.getIconBytes(); + final byte[] icon = serverInfo.getIconBytes(); if (!Arrays.equals(icon, iconData)) { //#else //$$ final String icon = serverInfo.getIconB64(); @@ -512,7 +512,7 @@ private String getName() { private boolean uploadServerIcon( //#if MC >= 1.19.4 - byte @Nullable [] newIconData + byte[] newIconData //#else //$$ @Nullable String newIconData //#endif diff --git a/src/main/java/io/github/gaming32/worldhost/mixin/MixinTitleScreen.java b/src/main/java/io/github/gaming32/worldhost/mixin/MixinTitleScreen.java index be61649..c0f4400 100644 --- a/src/main/java/io/github/gaming32/worldhost/mixin/MixinTitleScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/mixin/MixinTitleScreen.java @@ -20,7 +20,9 @@ //$$ import net.fabricmc.loader.api.FabricLoader; //#endif //#else -//#if MC > 1.17.1 +//#if NEOFORGE +//$$ import net.neoforged.neoforge.internal.BrandingControl; +//#elseif MC > 1.17.1 //$$ import net.minecraftforge.internal.BrandingControl; //#elseif MC > 1.16.5 //$$ import net.minecraftforge.fmllegacy.BrandingControl; diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index c3068b6..019f73e 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -1,5 +1,9 @@ modLoader = "javafml" -loaderVersion = "[32,)" +##if FORGE +#?? loaderVersion = "[32,)" +##else +loaderVersion = "[1,)" +##endif issueTrackerURL = "https://github.com/Gaming32/world-host/issues" license = "MIT" @@ -12,10 +16,20 @@ logoFile = "icon.png" authors = "Gaming32" description = "Host your singleplayer worlds without having to run a server or setup port forwarding! In the future, we also plan to support Bedrock Edition clients through the use of Geyser." +##if NEOFORGE +#?? [[mixins]] +#?? config = "world-host.mixins.json" +##endif + [[dependencies.world_host]] -modId = "forge" +##if FORGE +#?? modId = "forge" +#?? versionRange = "[32,)" +##else +modId = "neoforge" +versionRange = "[20,)" +##endif mandatory = true -versionRange = "[32,)" ordering = "NONE" side = "BOTH" diff --git a/version.gradle.kts b/version.gradle.kts index 8d0f0bc..75211b9 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -1,4 +1,5 @@ import com.modrinth.minotaur.ModrinthExtension +import com.replaymod.gradle.preprocess.PreprocessTask import groovy.lang.GroovyObjectSupport import net.raphimc.javadowngrader.gradle.task.DowngradeSourceSetTask import xyz.wagyourtail.unimined.api.minecraft.MinecraftConfig @@ -27,6 +28,11 @@ val modVersion = project.properties["mod.version"] as String val mcVersionString by extra(name.substringBefore("-")) val loaderName by extra(name.substringAfter("-")) +val isFabric = loaderName == "fabric" +val isForge = loaderName == "forge" +val isNeoForge = loaderName == "neoforge" +val isForgeLike = isForge || isNeoForge + base.archivesName.set(rootProject.name) // major.minor.?patch @@ -46,7 +52,7 @@ repositories { lateinit var minecraft: MinecraftConfig unimined.minecraft { version(mcVersionString) - if (mcVersion != 1_20_01 || loaderName != "forge") { + if (mcVersion != 1_20_01 || !isForge) { side("client") } @@ -77,12 +83,11 @@ unimined.minecraft { } } - if (loaderName == "fabric") { - fabric { + when { + isFabric -> fabric { loader("0.14.22") } - } else { - minecraftForge { + isForge -> minecraftForge { loader(when(mcVersion) { 1_20_01 -> "47.1.3" 1_19_04 -> "45.1.0" @@ -90,10 +95,17 @@ unimined.minecraft { 1_18_02 -> "40.2.0" 1_17_01 -> "37.1.1" 1_16_05 -> "36.2.34" - else -> throw IllegalArgumentException("unknown forge version for $mcVersionString") + else -> throw IllegalStateException("Unknown Forge version for $mcVersionString") }) mixinConfig("world-host.mixins.json") } + isNeoForge -> neoForged { + loader(when (mcVersion) { + 1_20_02 -> "35-beta" + else -> throw IllegalStateException("Unknown NeoForge version for $mcVersionString") + }) + } + else -> throw IllegalStateException() } minecraft = this @@ -123,7 +135,7 @@ val tinyMappings: File = file("${projectDir}/build/tmp/tinyMappings.tiny").also export.exportFunc((minecraft.mappings as MappingsProvider).mappingTree) } mappingsConfig.setGroovyProperty("tinyMappings", tinyMappings.toPath()) -if (loaderName == "forge") { +if (isForge) { val tinyMappingsWithSrg: File = file("${projectDir}/build/tmp/tinyMappingsWithSrg.tiny").also { file -> val export = ExportMappingsTaskImpl.ExportImpl(minecraft.mappings as MappingsProvider).apply { location = file @@ -185,7 +197,7 @@ minecraft.apply { dependencies { fun bundle(dependency: Any) { - if (loaderName == "fabric") { + if (isFabric) { "include"(dependency) } else { "shade"(dependency) @@ -198,7 +210,7 @@ dependencies { } bundleImplementation("org.quiltmc.qup:json:0.2.0") - if (loaderName == "forge") { + if (isForgeLike) { "minecraftLibraries"("org.quiltmc.qup:json:0.2.0") } @@ -208,7 +220,7 @@ dependencies { // implementation("com.github.LlamaLad7.MixinExtras:mixinextras-common:0.2.0-beta.6") // } - if (loaderName == "fabric") { + if (isFabric) { when (mcVersion) { 1_20_02 -> "8.0.0-beta.2" // TODO: Update out of beta 1_20_01 -> "7.0.1" @@ -226,9 +238,11 @@ dependencies { } } - modRuntimeOnly("me.djtheredstoner:DevAuth-${if (loaderName == "fabric") "fabric" else "forge-latest"}:1.1.2") + if (!isNeoForge) { // TODO: Remove check when DevAuth for NeoForge is released (which depends on Arch) + modRuntimeOnly("me.djtheredstoner:DevAuth-${if (isFabric) "fabric" else "forge-latest"}:1.1.2") + } - if (loaderName == "fabric") { + if (isFabric) { when (mcVersion) { 1_20_02 -> "0.89.2+1.20.2" 1_20_01 -> "0.89.0+1.20.1" @@ -245,11 +259,11 @@ dependencies { } } - if (loaderName == "fabric" && mcVersion >= 1_18_02) { + if (isFabric && mcVersion >= 1_18_02) { modCompileOnly("dev.isxander:main-menu-credits:1.1.2") } - if (loaderName == "fabric") { + if (isFabric) { when { mcVersion >= 1_20_02 -> "2.9.0" mcVersion >= 1_20_01 -> "2.8.7-SNAPSHOT" @@ -271,18 +285,20 @@ java { } preprocess { + fun Boolean.toInt() = if (this) 1 else 0 + vars.putAll(mapOf( - "FORGE" to 0, - "FABRIC" to 0, - )) - vars.putAll(mapOf( - loaderName.uppercase() to 1, - "MC" to mcVersion + "FABRIC" to isFabric.toInt(), + "FORGE" to isForge.toInt(), + "NEOFORGE" to isNeoForge.toInt(), + "FORGELIKE" to isForgeLike.toInt(), + "MC" to mcVersion, )) patternAnnotation.set("io.github.gaming32.worldhost.versions.Pattern") keywords.value(keywords.get()) - keywords.put(".json", keywords.get().getValue(".json").copy(eval = "//??")) + keywords.put(".json", PreprocessTask.DEFAULT_KEYWORDS.copy(eval = "//??")) + keywords.put(".toml", PreprocessTask.CFG_KEYWORDS.copy(eval = "#??")) } //println("Parallel: ${gradle.startParameter.isParallelProjectExecutionEnabled}") @@ -298,7 +314,13 @@ modrinth { } projectId.set("world-host") versionNumber.set(version.toString()) - versionName.set("[${if (loaderName == "fabric") "Fabric/Quilt" else "Forge"} $mcVersionString] World Host $modVersion") + val loadersText = when { + isFabric -> "Fabric/Quilt" + isForge -> "Forge" + isNeoForge -> "NeoForge" + else -> throw IllegalStateException() + } + versionName.set("[$loadersText $mcVersionString] World Host $modVersion") uploadFile.set(tasks.named("remapJar")) additionalFiles.add(tasks.named("sourcesJar")) gameVersions.add(mcVersionString) @@ -308,11 +330,11 @@ modrinth { gameVersions.add("1.20") } loaders.add(loaderName) - if (loaderName == "fabric") { + if (isFabric) { loaders.add("quilt") } dependencies { - if (loaderName == "fabric") { + if (isFabric) { optional.project(if (isStaging) "fred-3" else "modmenu") } } @@ -367,7 +389,7 @@ tasks.processResources { )) } - if (loaderName == "fabric") { + if (isFabric) { exclude("pack.mcmeta", "META-INF/mods.toml") } else { exclude("fabric.mod.json") @@ -375,7 +397,7 @@ tasks.processResources { doLast { val resources = "${layout.buildDirectory.get()}/resources/main" - if (loaderName == "forge") { + if (isForgeLike) { copy { from(file("$resources/assets/world-host/icon.png")) into(resources) @@ -390,7 +412,7 @@ tasks.processResources { tasks.withType { shade.files.forEach { from(project.zipTree(it)) } manifest { - if (loaderName == "forge") { + if (isForge) { attributes["MixinConfigs"] = "world-host.mixins.json" } }