User: rednesto Date: 27 Jun 23 17:24 Revision: 9fe7691120b368c2102060fea09efcb60a021337 Summary: Update Forge MDK templates Up to a8dce04c1be62f27e788ffe0dd753be18147d75f TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=8555&personal=false Index: src/main/kotlin/platform/forge/creator/asset-steps.kt =================================================================== --- src/main/kotlin/platform/forge/creator/asset-steps.kt (revision 23fab598dfca9587166f1ac09b2d8e7fca7c231d) +++ src/main/kotlin/platform/forge/creator/asset-steps.kt (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -118,6 +118,18 @@ "src/main/resources/META-INF/mods.toml" to MinecraftTemplates.MODS_TOML_TEMPLATE, ) + val configTemplate = when { + mcVersion >= MinecraftVersions.MC1_20 -> MinecraftTemplates.FG3_1_20_CONFIG_TEMPLATE + else -> null + } + + if (configTemplate != null) { + assets.addTemplates( + project, + "src/main/java/${mainPackageName.replace('.', '/')}/Config.java" to configTemplate, + ) + } + assets.addLicense(project) } } Index: src/main/kotlin/util/MinecraftTemplates.kt =================================================================== --- src/main/kotlin/util/MinecraftTemplates.kt (revision 23fab598dfca9587166f1ac09b2d8e7fca7c231d) +++ src/main/kotlin/util/MinecraftTemplates.kt (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -87,6 +87,7 @@ forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_19_MAIN_CLASS_TEMPLATE)) forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_19_3_MAIN_CLASS_TEMPLATE)) forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_20_MAIN_CLASS_TEMPLATE)) + forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_20_CONFIG_TEMPLATE)) forgeGroup.addTemplate(FileTemplateDescriptor(FG3_BUILD_GRADLE_TEMPLATE)) forgeGroup.addTemplate(FileTemplateDescriptor(FG3_GRADLE_PROPERTIES_TEMPLATE)) forgeGroup.addTemplate(FileTemplateDescriptor(FG3_SETTINGS_GRADLE_TEMPLATE)) @@ -204,6 +205,7 @@ const val FG3_1_19_MAIN_CLASS_TEMPLATE = "Forge (1.19+) Main Class.java" const val FG3_1_19_3_MAIN_CLASS_TEMPLATE = "Forge (1.19.3+) Main Class.java" const val FG3_1_20_MAIN_CLASS_TEMPLATE = "Forge (1.20+) Main Class.java" + const val FG3_1_20_CONFIG_TEMPLATE = "Forge (1.20+) Config.java" const val FG3_BUILD_GRADLE_TEMPLATE = "Forge (1.13+) build.gradle" const val FG3_GRADLE_PROPERTIES_TEMPLATE = "Forge (1.13+) gradle.properties" const val FG3_SETTINGS_GRADLE_TEMPLATE = "Forge (1.13+) settings.gradle" Index: src/main/resources/fileTemplates/j2ee/forge/Forge (1.13+) build.gradle.ft =================================================================== --- src/main/resources/fileTemplates/j2ee/forge/Forge (1.13+) build.gradle.ft (revision 23fab598dfca9587166f1ac09b2d8e7fca7c231d) +++ src/main/resources/fileTemplates/j2ee/forge/Forge (1.13+) build.gradle.ft (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -48,12 +48,8 @@ // Simply re-run your setup task after changing the mappings to update your workspace. mappings channel: mapping_channel, version: mapping_version - // When true, this property will have all Eclipse run configurations run the "prepareX" task for the given run configuration before launching the game. - // In most cases, it is not necessary to enable. + // When true, this property will have all Eclipse/IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game. // enableEclipsePrepareRuns = true - - // When true, this property will have all IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game. - // In most cases, it is not necessary to enable. // enableIdeaPrepareRuns = true // This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game. @@ -77,7 +73,8 @@ // Default run configurations. // These can be tweaked, removed, or duplicated as needed. runs { - client { + // applies to all the run configs below + configureEach { workingDirectory project.file('run') // Recommended logging data for a userdev environment @@ -93,11 +90,6 @@ // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels property 'forge.logging.console.level', 'debug' - #if (${GAME_TEST_FRAMEWORK}) - // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. - property 'forge.enabledGameTestNamespaces', mod_id - #end - mods { "${mod_id}" { source sourceSets.main @@ -105,62 +97,37 @@ } } - server { - workingDirectory project.file('run') - - property 'forge.logging.markers', 'REGISTRIES' - - property 'forge.logging.console.level', 'debug' - - #if (${GAME_TEST_FRAMEWORK}) +#if (${GAME_TEST_FRAMEWORK}) + client { + // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id + } - #end +#end - mods { - "${mod_id}" { - source sourceSets.main + server { +#if (${GAME_TEST_FRAMEWORK}) + property 'forge.enabledGameTestNamespaces', mod_id +#end + args '--nogui' - } + } - } - } #if (${GAME_TEST_FRAMEWORK}) // This run config launches GameTestServer and runs all registered gametests, then exits. // By default, the server will crash when no gametests are provided. // The gametest system is also enabled by default for other run configs under the /test command. gameTestServer { - workingDirectory project.file('run') - - property 'forge.logging.markers', 'REGISTRIES' - - property 'forge.logging.console.level', 'debug' - property 'forge.enabledGameTestNamespaces', mod_id - - mods { - "${mod_id}" { - source sourceSets.main - } + } - } - } #end #if (${HAS_DATA}) data { - workingDirectory project.file('run') + // example of overriding the workingDirectory set in configureEach above + workingDirectory project.file('run-data') - property 'forge.logging.markers', 'REGISTRIES' - - property 'forge.logging.console.level', 'debug' - // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') - - mods { - "${mod_id}" { - source sourceSets.main - } + } - } - } #end } } @@ -219,25 +186,23 @@ // A missing property will result in an error. Properties are expanded using ${} Groovy notation. // When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments. // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html -def resourceTargets = ['META-INF/mods.toml', 'pack.mcmeta'] -def replaceProperties = [ +tasks.named('processResources', ProcessResources).configure { + var replaceProperties = [ - minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range, - forge_version: forge_version, forge_version_range: forge_version_range, - loader_version_range: loader_version_range, - mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version, + minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range, + forge_version: forge_version, forge_version_range: forge_version_range, + loader_version_range: loader_version_range, + mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version, - mod_authors: mod_authors, mod_description: mod_description + mod_authors: mod_authors, mod_description: mod_description, -] + ] -processResources { + inputs.properties replaceProperties - replaceProperties.put 'project', project - filesMatching(resourceTargets) { - expand replaceProperties - } -} + filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { + expand replaceProperties + [project: project] + }} // Example for how to get properties into the manifest for reading at runtime. -jar { +tasks.named('jar', Jar).configure { manifest { attributes([ "Specification-Title": mod_id, @@ -249,10 +214,11 @@ "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ]) } + + // This is the preferred method to reobfuscate your jar file + finalizedBy 'reobfJar' } -jar.finalizedBy('reobfJar') - tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation } Index: src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.ft =================================================================== --- src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.ft (revision 9fe7691120b368c2102060fea09efcb60a021337) +++ src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.ft (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -0,0 +1,64 @@ +package ${PACKAGE_NAME}; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.config.ModConfigEvent; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +// An example config class. This is not required, but it's a good idea to have one to keep your config organized. +// Demonstrates how to use Forge's config APIs +@Mod.EventBusSubscriber(modid = ${CLASS_NAME}.MODID, bus = Mod.EventBusSubscriber.Bus.MOD) +public class Config +{ + private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder(); + + private static final ForgeConfigSpec.BooleanValue LOG_DIRT_BLOCK = BUILDER + .comment("Whether to log the dirt block on common setup") + .define("logDirtBlock", true); + + private static final ForgeConfigSpec.IntValue MAGIC_NUMBER = BUILDER + .comment("A magic number") + .defineInRange("magicNumber", 42, 0, Integer.MAX_VALUE); + + public static final ForgeConfigSpec.ConfigValue MAGIC_NUMBER_INTRODUCTION = BUILDER + .comment("What you want the introduction message to be for the magic number") + .define("magicNumberIntroduction", "The magic number is... "); + + // a list of strings that are treated as resource locations for items + private static final ForgeConfigSpec.ConfigValue> ITEM_STRINGS = BUILDER + .comment("A list of items to log on common setup.") + .defineListAllowEmpty(Collections.singletonList("items"), () -> List.of("minecraft:iron_ingot"), Config::validateItemName); + + static final ForgeConfigSpec SPEC = BUILDER.build(); + + public static boolean logDirtBlock; + public static int magicNumber; + public static String magicNumberIntroduction; + public static Set items; + + private static boolean validateItemName(final Object obj) + { + return obj instanceof final String itemName && ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName)); + } + + @SubscribeEvent + static void onLoad(final ModConfigEvent event) + { + logDirtBlock = LOG_DIRT_BLOCK.get(); + magicNumber = MAGIC_NUMBER.get(); + magicNumberIntroduction = MAGIC_NUMBER_INTRODUCTION.get(); + + // convert the list of strings into a set of items + items = ITEM_STRINGS.get().stream() + .map(itemName -> ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName))) + .collect(Collectors.toSet()); + } +} Index: src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.html =================================================================== --- src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.html (revision 9fe7691120b368c2102060fea09efcb60a021337) +++ src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Config.java.html (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -0,0 +1,25 @@ + + + + +

This is a built-in file template used to create a new config class for Forge projects 1.20 and above

+ + Index: src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Main Class.java.ft =================================================================== --- src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Main Class.java.ft (revision 23fab598dfca9587166f1ac09b2d8e7fca7c231d) +++ src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Main Class.java.ft (revision 9fe7691120b368c2102060fea09efcb60a021337) @@ -18,7 +18,9 @@ import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -77,12 +79,22 @@ // Register the item to a creative tab modEventBus.addListener(this::addCreative); + + // Register our mod's ForgeConfigSpec so that Forge can create and load the config file for us + ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(final FMLCommonSetupEvent event) { // Some common setup code LOGGER.info("HELLO FROM COMMON SETUP"); LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT)); + + if (Config.logDirtBlock) + LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT)); + + LOGGER.info(Config.magicNumberIntroduction + Config.magicNumber); + + Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString())); } // Add the example block item to the building blocks tab