-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
293 lines (245 loc) · 10.2 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import java.util.*
plugins {
java
idea
id("net.minecraftforge.gradle")
id("org.parchmentmc.librarian.forgegradle")
}
// Mod
val modID: String by project
val modName: String by project
val modAuthors: String by project
val modCredits: String by project
val modIssueTrackerURL: String by project
val modHomeURL: String by project
val modDescription: String by project
val modLicense: String by project
val modVersion: String by project
val modDisplayedAsResourcePack: String by project
var modDisplayedAsResourcePackBoolean: Boolean = modDisplayedAsResourcePack.toBoolean()
val modDisplayTest: String by project
val modLogoFile: String by project
val modLogoBlur: String by project
var modLogoBlurBoolean: Boolean = modLogoBlur.toBoolean()
val modUpdateURL: String by project
// Resources
val resourcesPackFormat: String by project
val resourcesDescription: String by project
// Forge
val forgeVersion: String by project
// Minecraft
val minecraftVersion: String by project
// Dev Workspace
val mappingsType: String by project
val parchmentVersion: String by project
val mappingsVersion: String = when (mappingsType) {
"parchment" -> "${minecraftVersion}-${parchmentVersion}-${minecraftVersion}"
else -> minecraftVersion
}
val useAdvancedClassRedef: String by project
val useAdvancedClassRedefBoolean: Boolean = useAdvancedClassRedef.toBoolean()
val ramAmountForRuns: String by project
// Computed variables not found in the gradle.properties file.
val forgeVersionRange: String = "[" + forgeVersion.substring(0, 2) + ",)"
println("Used Mappings: $mappingsType / $mappingsVersion with mod version $modVersion")
val resourcesGenerated: String = "${buildDir}/generated-resources/resources"
val modsTomlGenerated: File = file("${resourcesGenerated}/META-INF/mods.toml")
val packMetaGenerated: File = file("${resourcesGenerated}/pack.mcmeta")
// Variable validation.
if (!modID.matches("^[a-z][a-z0-9_]{1,63}\$".toRegex())) {
throw InvalidUserDataException("Invalid modID Variable: ($modID) Must match: ^[a-z][a-z0-9_]{1,63}\$")
} else if (modID.isEmpty() or modID.isBlank())
throw InvalidUserDataException("Missing/Empty modID Variable: ($modID)")
if (modLicense.isEmpty() or modLicense.isBlank()) {
throw InvalidUserDataException("Missing/Empty modLicense Variable!")
}
if (modDescription.isEmpty() or modDescription.isBlank()) {
throw InvalidUserDataException("Missing/Empty modDescription Variable!")
}
if (!arrayListOf("MATCH_VERSION", "IGNORE_SERVER_VERSION", "IGNORE_ALL_VERSION", "NONE").contains(modDisplayTest)) {
throw InvalidUserDataException("Invalid modDisplayTest Variable: ($modDisplayTest)")
}
if (forgeVersion.isEmpty() or forgeVersion.isBlank()) {
throw InvalidUserDataException("Missing/Empty forgeVersion Variable!")
}
if (modLogoBlur.isEmpty() or modLogoBlur.isBlank()) {
modLogoBlurBoolean = true
}
if (modDisplayedAsResourcePack.isEmpty() or modDisplayedAsResourcePack.isBlank()) {
modDisplayedAsResourcePackBoolean = true
}
base {
archivesName.set("${modID}-${modVersion}-forge")
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(project.properties["javaVersion"] as String))
}
repositories {
mavenCentral()
maven(url = "https://www.cursemaven.com") // CurseMaven
{
content {
includeGroup("curse.maven")
}
}
flatDir {
dir("libs")
}
}
dependencies {
minecraft("net.minecraftforge", "forge", version = "$minecraftVersion-$forgeVersion")
}
sourceSets {
main {
resources {
srcDir(resourcesGenerated)
}
}
}
tasks {
jar {
manifest {
attributes["Implementation-Version"] = modVersion
}
}
// Inserts the properties into whatever files.
withType<ProcessResources>
{
mkdir("${resourcesGenerated}/META-INF")
modsTomlGenerated.createNewFile()
modsTomlGenerated.writeText(
"# THIS FILE IS GENERATED BY THE BUILDSCRIPT DO NOT EDIT! VALUES CAN BE EDITED IN THE gradle.properties FILE!\n" +
"modLoader = \"javafml\"\n" +
"loaderVersion = \"${forgeVersionRange}\"\n" +
"license = \"${modLicense}\"\n" +
"showAsResourcePack = $modDisplayedAsResourcePackBoolean" +
"\n" +
"[[mods]]\n" +
"modId=\"${modID}\"\n" +
"version=\"${modVersion}\"\n" +
"displayName=\"${modName}\"\n" +
"issueTrackerURL=\"${modIssueTrackerURL}\"\n" +
"displayURL=\"${modHomeURL}\"\n" +
"credits=\"${modCredits}\"\n" +
"authors=\"${modAuthors}\"\n" +
"logoFile=\"${modLogoFile}\"\n" +
"logoBlur=${modLogoBlur}\n" +
"updateJSONURL=\"${modUpdateURL}\"\n" +
"description='''\n" +
"${modDescription}\n" +
"'''\n" +
"displayTest = \"${modDisplayTest}\""
)
// Find and isolate all the dependency variables following the "dep#Object" format.
val reg = "^(dep\\d+).*\$".toRegex()
val uniqueDep: MutableSet<String> = mutableSetOf()
val depList: List<String> = getProperties().keys.filter { it.toLowerCase(Locale.ROOT).contains(reg) }
// Deduplicate the "dep#" properties to get how many dependencies there are.
depList.forEach { dep ->
val subString: MatchResult? = reg.find(dep, 0)
if (subString != null) {
uniqueDep.add(subString.groupValues[1])
}
}
// Validate all the data and then write each dependency.
uniqueDep.forEach { dep ->
val depModID: String = project.properties[dep + "ID"].toString()
val depModMandatory: Boolean = project.properties[dep + "Mandatory"].toString().toBoolean()
var depModOrder: String = project.properties[dep + "Order"].toString()
var depModSide: String = project.properties[dep + "Side"].toString()
if (depModID.isBlank() or depModID.isEmpty())
throw InvalidUserDataException("Missing/Empty depModID Variable for $dep! This is required!")
else if (!depModID.matches("^[a-z][a-z0-9_]{1,63}\$".toRegex())) {
throw InvalidUserDataException("Invalid depModID Variable: ($dep=$depModID) Must match: ^[a-z][a-z0-9_]{1,63}\$")
}
if (depModOrder.isBlank() or depModOrder.isEmpty())
depModOrder = "NONE"
else if (!arrayListOf("BEFORE", "AFTER", "NONE").contains(depModOrder)) {
throw InvalidUserDataException("Invalid depModOrder Variable: ($dep=$depModOrder)")
}
if (depModSide.isBlank() or depModSide.isEmpty())
depModSide = "BOTH"
else if (!arrayListOf("CLIENT", "SERVER", "BOTH").contains(depModSide)) {
throw InvalidUserDataException("Invalid depModOrder Variable: ($dep=$depModSide)")
}
modsTomlGenerated.appendText(
"\n" +
"[[dependencies.${modID}]]\n" +
" modId = \"$depModID\"\n" +
" mandatory = \"$depModMandatory\"\n" +
" versionRange = \"" + project.properties[dep + "VersionRange"] + "\"\n" +
" ordering = \"$depModOrder\"\n" +
" side = \"$depModSide\"" +
"\n")
}
if (modDisplayedAsResourcePackBoolean) {
packMetaGenerated.createNewFile()
packMetaGenerated.writeText("{\n" +
"\"__comment\": \"THIS FILE IS GENERATED BY THE BUILDSCRIPT DO NOT EDIT! VALUES CAN BE EDITED IN THE gradle.properties FILE!\",\n" +
" \"pack\": {\n" +
" \"pack_format\": ${resourcesPackFormat},\n" +
" \"description\": \"${resourcesDescription}\"\n" +
" }\n" +
"}")
}
finalizedBy("fixResourcesForFML")
}
// REQUIRED TO FIX https://youtrack.jetbrains.com/issue/IDEA-196103/IntelliJ-Gradle-ProcessResources-out-directory-classpath-issue
// Can't use suggested option of change the run actions to "Use Gradle" because FML seems to require the out directory.
register<Copy>("fixResourcesForFML")
{
val outFile: File = file(layout.projectDirectory.dir("out"))
if (outFile.exists()) {
dependsOn(processResources, compileJava, "prepareRunClient")
from(layout.projectDirectory.dir("build/resourcesGenerated"))
into(layout.projectDirectory.dir("out/production/resources"))
}
}
register("generateSources", Copy::class) {
from("src/main/java/")
into("${buildDir}/generated-src")
filesMatching("**/${modName}.java") {
expand(mapOf("modID" to modID))
}
}
compileJava {
setSource("${buildDir}/generated-src")
dependsOn("generateSources")
}
}
minecraft {
mappings(mappingsType, mappingsVersion)
runs {
all {
property("forge.logging.console.level", "debug")
property("forge.enabledGameTestNamespaces", modID)
jvmArgs("-ea", "-Xmx$ramAmountForRuns", "-Xms$ramAmountForRuns")
if (useAdvancedClassRedef.toBoolean()) {
jvmArg("-XX:+AllowEnhancedClassRedefinition")
}
ideaModule("${modName}.test")
mods.create(modID) {
source(sourceSets.main.get())
}
}
register("client") {
workingDirectory("${buildDir}/run/client")
}
register("server") {
workingDirectory("run/server")
arg("--nogui")
}
register("gameTestServer") {
workingDirectory("run/gametest")
arg("--nogui")
forceExit(false)
}
}
}
idea {
module {
for (fileName in listOf("run", "out", "logs")) {
excludeDirs.add(file(fileName))
}
}
}