Skip to content

Commit 9fe7691

Browse files
committed
Update Forge MDK templates
Up to a8dce04c1be62f27e788ffe0dd753be18147d75f
1 parent 23fab59 commit 9fe7691

File tree

6 files changed

+146
-65
lines changed

6 files changed

+146
-65
lines changed

src/main/kotlin/platform/forge/creator/asset-steps.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ class ForgeProjectFilesStep(parent: NewProjectWizardStep) : AbstractLongRunningA
118118
"src/main/resources/META-INF/mods.toml" to MinecraftTemplates.MODS_TOML_TEMPLATE,
119119
)
120120

121+
val configTemplate = when {
122+
mcVersion >= MinecraftVersions.MC1_20 -> MinecraftTemplates.FG3_1_20_CONFIG_TEMPLATE
123+
else -> null
124+
}
125+
126+
if (configTemplate != null) {
127+
assets.addTemplates(
128+
project,
129+
"src/main/java/${mainPackageName.replace('.', '/')}/Config.java" to configTemplate,
130+
)
131+
}
132+
121133
assets.addLicense(project)
122134
}
123135
}

src/main/kotlin/util/MinecraftTemplates.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class MinecraftTemplates : FileTemplateGroupDescriptorFactory {
8787
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_19_MAIN_CLASS_TEMPLATE))
8888
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_19_3_MAIN_CLASS_TEMPLATE))
8989
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_20_MAIN_CLASS_TEMPLATE))
90+
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_1_20_CONFIG_TEMPLATE))
9091
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_BUILD_GRADLE_TEMPLATE))
9192
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_GRADLE_PROPERTIES_TEMPLATE))
9293
forgeGroup.addTemplate(FileTemplateDescriptor(FG3_SETTINGS_GRADLE_TEMPLATE))
@@ -204,6 +205,7 @@ class MinecraftTemplates : FileTemplateGroupDescriptorFactory {
204205
const val FG3_1_19_MAIN_CLASS_TEMPLATE = "Forge (1.19+) Main Class.java"
205206
const val FG3_1_19_3_MAIN_CLASS_TEMPLATE = "Forge (1.19.3+) Main Class.java"
206207
const val FG3_1_20_MAIN_CLASS_TEMPLATE = "Forge (1.20+) Main Class.java"
208+
const val FG3_1_20_CONFIG_TEMPLATE = "Forge (1.20+) Config.java"
207209
const val FG3_BUILD_GRADLE_TEMPLATE = "Forge (1.13+) build.gradle"
208210
const val FG3_GRADLE_PROPERTIES_TEMPLATE = "Forge (1.13+) gradle.properties"
209211
const val FG3_SETTINGS_GRADLE_TEMPLATE = "Forge (1.13+) settings.gradle"

src/main/resources/fileTemplates/j2ee/forge/Forge (1.13+) build.gradle.ft

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ minecraft {
4848
// Simply re-run your setup task after changing the mappings to update your workspace.
4949
mappings channel: mapping_channel, version: mapping_version
5050

51-
// When true, this property will have all Eclipse run configurations run the "prepareX" task for the given run configuration before launching the game.
52-
// In most cases, it is not necessary to enable.
51+
// 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.
5352
// enableEclipsePrepareRuns = true
54-
55-
// When true, this property will have all IntelliJ IDEA run configurations run the "prepareX" task for the given run configuration before launching the game.
56-
// In most cases, it is not necessary to enable.
5753
// enableIdeaPrepareRuns = true
5854

5955
// This property allows configuring Gradle's ProcessResources task(s) to run on IDE output locations before launching the game.
@@ -77,7 +73,8 @@ minecraft {
7773
// Default run configurations.
7874
// These can be tweaked, removed, or duplicated as needed.
7975
runs {
80-
client {
76+
// applies to all the run configs below
77+
configureEach {
8178
workingDirectory project.file('run')
8279

8380
// Recommended logging data for a userdev environment
@@ -93,73 +90,43 @@ minecraft {
9390
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
9491
property 'forge.logging.console.level', 'debug'
9592

96-
#if (${GAME_TEST_FRAMEWORK})
97-
// Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
98-
property 'forge.enabledGameTestNamespaces', mod_id
99-
#end
100-
10193
mods {
10294
"${mod_id}" {
10395
source sourceSets.main
10496
}
10597
}
10698
}
10799

108-
server {
109-
workingDirectory project.file('run')
110-
111-
property 'forge.logging.markers', 'REGISTRIES'
112-
113-
property 'forge.logging.console.level', 'debug'
114-
115-
#if (${GAME_TEST_FRAMEWORK})
100+
#if (${GAME_TEST_FRAMEWORK})
101+
client {
102+
// Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
116103
property 'forge.enabledGameTestNamespaces', mod_id
117-
#end
104+
}
105+
#end
118106

119-
mods {
120-
"${mod_id}" {
121-
source sourceSets.main
122-
}
123-
}
107+
server {
108+
#if (${GAME_TEST_FRAMEWORK})
109+
property 'forge.enabledGameTestNamespaces', mod_id
110+
#end
111+
args '--nogui'
124112
}
125113
#if (${GAME_TEST_FRAMEWORK})
126114

127115
// This run config launches GameTestServer and runs all registered gametests, then exits.
128116
// By default, the server will crash when no gametests are provided.
129117
// The gametest system is also enabled by default for other run configs under the /test command.
130118
gameTestServer {
131-
workingDirectory project.file('run')
132-
133-
property 'forge.logging.markers', 'REGISTRIES'
134-
135-
property 'forge.logging.console.level', 'debug'
136-
137119
property 'forge.enabledGameTestNamespaces', mod_id
138-
139-
mods {
140-
"${mod_id}" {
141-
source sourceSets.main
142-
}
143-
}
144120
}
145121
#end
146122
#if (${HAS_DATA})
147123

148124
data {
149-
workingDirectory project.file('run')
150-
151-
property 'forge.logging.markers', 'REGISTRIES'
152-
153-
property 'forge.logging.console.level', 'debug'
125+
// example of overriding the workingDirectory set in configureEach above
126+
workingDirectory project.file('run-data')
154127

155128
// Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
156129
args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
157-
158-
mods {
159-
"${mod_id}" {
160-
source sourceSets.main
161-
}
162-
}
163130
}
164131
#end
165132
}
@@ -219,25 +186,23 @@ dependencies {
219186
// A missing property will result in an error. Properties are expanded using ${} Groovy notation.
220187
// When "copyIdeResources" is enabled, this will also run before the game launches in IDE environments.
221188
// See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html
222-
def resourceTargets = ['META-INF/mods.toml', 'pack.mcmeta']
223-
def replaceProperties = [
224-
minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range,
225-
forge_version: forge_version, forge_version_range: forge_version_range,
226-
loader_version_range: loader_version_range,
227-
mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
228-
mod_authors: mod_authors, mod_description: mod_description
229-
]
230-
processResources {
189+
tasks.named('processResources', ProcessResources).configure {
190+
var replaceProperties = [
191+
minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range,
192+
forge_version: forge_version, forge_version_range: forge_version_range,
193+
loader_version_range: loader_version_range,
194+
mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
195+
mod_authors: mod_authors, mod_description: mod_description,
196+
]
197+
231198
inputs.properties replaceProperties
232-
replaceProperties.put 'project', project
233199

234-
filesMatching(resourceTargets) {
235-
expand replaceProperties
236-
}
237-
}
200+
filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
201+
expand replaceProperties + [project: project]
202+
}}
238203

239204
// Example for how to get properties into the manifest for reading at runtime.
240-
jar {
205+
tasks.named('jar', Jar).configure {
241206
manifest {
242207
attributes([
243208
"Specification-Title": mod_id,
@@ -249,9 +214,10 @@ jar {
249214
"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
250215
])
251216
}
252-
}
253217

254-
jar.finalizedBy('reobfJar')
218+
// This is the preferred method to reobfuscate your jar file
219+
finalizedBy 'reobfJar'
220+
}
255221

256222
tasks.withType(JavaCompile).configureEach {
257223
options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ${PACKAGE_NAME};
2+
3+
import net.minecraft.resources.ResourceLocation;
4+
import net.minecraft.world.item.Item;
5+
import net.minecraftforge.common.ForgeConfigSpec;
6+
import net.minecraftforge.eventbus.api.SubscribeEvent;
7+
import net.minecraftforge.fml.common.Mod;
8+
import net.minecraftforge.fml.event.config.ModConfigEvent;
9+
import net.minecraftforge.registries.ForgeRegistries;
10+
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Set;
14+
import java.util.stream.Collectors;
15+
16+
// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
17+
// Demonstrates how to use Forge's config APIs
18+
@Mod.EventBusSubscriber(modid = ${CLASS_NAME}.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
19+
public class Config
20+
{
21+
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
22+
23+
private static final ForgeConfigSpec.BooleanValue LOG_DIRT_BLOCK = BUILDER
24+
.comment("Whether to log the dirt block on common setup")
25+
.define("logDirtBlock", true);
26+
27+
private static final ForgeConfigSpec.IntValue MAGIC_NUMBER = BUILDER
28+
.comment("A magic number")
29+
.defineInRange("magicNumber", 42, 0, Integer.MAX_VALUE);
30+
31+
public static final ForgeConfigSpec.ConfigValue<String> MAGIC_NUMBER_INTRODUCTION = BUILDER
32+
.comment("What you want the introduction message to be for the magic number")
33+
.define("magicNumberIntroduction", "The magic number is... ");
34+
35+
// a list of strings that are treated as resource locations for items
36+
private static final ForgeConfigSpec.ConfigValue<List<? extends String>> ITEM_STRINGS = BUILDER
37+
.comment("A list of items to log on common setup.")
38+
.defineListAllowEmpty(Collections.singletonList("items"), () -> List.of("minecraft:iron_ingot"), Config::validateItemName);
39+
40+
static final ForgeConfigSpec SPEC = BUILDER.build();
41+
42+
public static boolean logDirtBlock;
43+
public static int magicNumber;
44+
public static String magicNumberIntroduction;
45+
public static Set<Item> items;
46+
47+
private static boolean validateItemName(final Object obj)
48+
{
49+
return obj instanceof final String itemName && ForgeRegistries.ITEMS.containsKey(new ResourceLocation(itemName));
50+
}
51+
52+
@SubscribeEvent
53+
static void onLoad(final ModConfigEvent event)
54+
{
55+
logDirtBlock = LOG_DIRT_BLOCK.get();
56+
magicNumber = MAGIC_NUMBER.get();
57+
magicNumberIntroduction = MAGIC_NUMBER_INTRODUCTION.get();
58+
59+
// convert the list of strings into a set of items
60+
items = ITEM_STRINGS.get().stream()
61+
.map(itemName -> ForgeRegistries.ITEMS.getValue(new ResourceLocation(itemName)))
62+
.collect(Collectors.toSet());
63+
}
64+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
Minecraft Development for IntelliJ
3+
4+
https://mcdev.io/
5+
6+
Copyright (C) 2023 minecraft-dev
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU Lesser General Public License as published
10+
by the Free Software Foundation, version 3.0 only.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public License
18+
along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
-->
20+
21+
<html>
22+
<body>
23+
<p>This is a built-in file template used to create a new config class for Forge projects 1.20 and above</p>
24+
</body>
25+
</html>

src/main/resources/fileTemplates/j2ee/forge/Forge (1.20+) Main Class.java.ft

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
1818
import net.minecraftforge.event.server.ServerStartingEvent;
1919
import net.minecraftforge.eventbus.api.IEventBus;
2020
import net.minecraftforge.eventbus.api.SubscribeEvent;
21+
import net.minecraftforge.fml.ModLoadingContext;
2122
import net.minecraftforge.fml.common.Mod;
23+
import net.minecraftforge.fml.config.ModConfig;
2224
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
2325
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
2426
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@@ -77,12 +79,22 @@ public class ${CLASS_NAME} {
7779

7880
// Register the item to a creative tab
7981
modEventBus.addListener(this::addCreative);
82+
83+
// Register our mod's ForgeConfigSpec so that Forge can create and load the config file for us
84+
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, Config.SPEC);
8085
}
8186

8287
private void commonSetup(final FMLCommonSetupEvent event) {
8388
// Some common setup code
8489
LOGGER.info("HELLO FROM COMMON SETUP");
8590
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
91+
92+
if (Config.logDirtBlock)
93+
LOGGER.info("DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
94+
95+
LOGGER.info(Config.magicNumberIntroduction + Config.magicNumber);
96+
97+
Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString()));
8698
}
8799

88100
// Add the example block item to the building blocks tab

0 commit comments

Comments
 (0)