Skip to content

Commit 54b3bb7

Browse files
committed
added neoforge & classgraph
1 parent 875c062 commit 54b3bb7

File tree

22 files changed

+379
-59
lines changed

22 files changed

+379
-59
lines changed

build.gradle.kts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -17,8 +17,6 @@
1717

1818
import org.gradle.internal.jvm.*
1919
import net.fabricmc.loom.api.LoomGradleExtensionAPI
20-
import org.apache.tools.ant.taskdefs.condition.Os
21-
import java.io.FileNotFoundException
2220
import java.util.*
2321

2422
val modId: String by project
@@ -41,7 +39,7 @@ plugins {
4139
id("org.jetbrains.dokka") version "2.0.0"
4240
id("architectury-plugin") version "3.4-SNAPSHOT"
4341
id("dev.architectury.loom") version "1.10-SNAPSHOT" apply false
44-
id("com.github.johnrengelman.shadow") version "8.1.1" apply false
42+
id("com.gradleup.shadow") version "9.0.0-beta13" apply false
4543
id("maven-publish")
4644
}
4745

@@ -56,7 +54,10 @@ subprojects {
5654

5755
dependencies {
5856
"minecraft"("com.mojang:minecraft:$minecraftVersion")
59-
"mappings"("net.fabricmc:yarn:$minecraftVersion+$yarnMappings:v2")
57+
"mappings"(loom.layered {
58+
mappings("net.fabricmc:yarn:$minecraftVersion+$yarnMappings:v2")
59+
mappings("dev.architectury:yarn-mappings-patch-neoforge:1.21+build.4")
60+
})
6061
}
6162

6263
publishing {
@@ -96,7 +97,7 @@ subprojects {
9697
property("lambda.dev", "youtu.be/RYnFIRc0k6E")
9798
property("org.lwjgl.util.Debug", "true")
9899

99-
vmArgs("-XX:+HeapDumpOnOutOfMemoryError", "-XX:+CreateCoredumpOnCrash", "-XX:+UseOSErrorReporting", "-Xrs")
100+
vmArgs("-XX:+HeapDumpOnOutOfMemoryError", "-XX:+CreateCoredumpOnCrash", "-XX:+UseOSErrorReporting")
100101
programArgs("--username", "Steve", "--uuid", "8667ba71b85a4004af54457a9734eed7", "--accessToken", "****", "--userType", "msa")
101102
}
102103
}

common/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val mockkVersion: String by project
2727

2828
base.archivesName = "${base.archivesName.get()}-api"
2929

30-
architectury { common("fabric", "forge") }
30+
architectury { common("fabric", "forge", "neoforge") }
3131

3232
loom {
3333
silentMojangMappingsLicense()
@@ -41,7 +41,7 @@ repositories {
4141
dependencies {
4242
// We depend on fabric loader here to use the fabric @Environment annotations and get the mixin dependencies
4343
// Do NOT use other classes from fabric loader
44-
modImplementation("net.fabricmc:fabric-loader:$fabricLoaderVersion")
44+
modImplementation("net.fabricmc:fabric-loader:$fabricLoaderVersion") { isTransitive = false }
4545

4646
// Add dependencies on the required Kotlin modules.
4747
implementation("org.reflections:reflections:0.10.2")
@@ -54,6 +54,8 @@ dependencies {
5454
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
5555
implementation("io.ktor:ktor-serialization-gson:$ktorVersion")
5656

57+
implementation("io.github.classgraph:classgraph:4.8.179")
58+
5759
// Baritone
5860
modImplementation("com.github.rfresh2:baritone-fabric:$minecraftVersion")
5961

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ import com.lambda.config.serializer.GameProfileSerializer
2626
import com.lambda.config.serializer.ItemStackSerializer
2727
import com.lambda.config.serializer.KeyCodeSerializer
2828
import com.lambda.config.serializer.OptionalSerializer
29+
import com.lambda.core.Loadable
2930
import com.lambda.core.Loader
31+
import com.lambda.threading.awaitMainThread
3032
import com.lambda.threading.recordRenderCall
33+
import com.lambda.threading.runGameScheduled
3134
import com.lambda.util.KeyCode
35+
import com.lambda.util.reflections.createInstance
3236
import com.mojang.authlib.GameProfile
37+
import io.github.classgraph.ClassGraph
38+
import kotlinx.coroutines.Dispatchers
39+
import kotlinx.coroutines.runBlocking
40+
import kotlinx.coroutines.withContext
3341
import net.minecraft.block.Block
3442
import net.minecraft.client.MinecraftClient
3543
import net.minecraft.item.ItemStack
@@ -69,3 +77,29 @@ object Lambda {
6977

7078
fun initialize(block: (Long) -> Unit) = recordRenderCall { Loader.initialize().apply(block) }
7179
}
80+
81+
/**
82+
* Execute a block of code on the main thread
83+
*/
84+
suspend fun <T> executeOnMainThread(block: () -> T): T {
85+
return withContext(Dispatchers.Main) {
86+
println("Executing on ${Thread.currentThread().name}")
87+
block()
88+
}
89+
}
90+
91+
suspend fun main() {
92+
val classes = ClassGraph()
93+
.enableAllInfo()
94+
.scan()
95+
.use { result ->
96+
result.getClassesImplementing(Loadable::class.java)
97+
.map { Class.forName(it.name) }
98+
}
99+
100+
val loadables = classes.mapNotNull {
101+
executeOnMainThread { createInstance<Loadable>(it) }
102+
}
103+
104+
loadables.forEach { println(it.load()) }
105+
}

common/src/main/kotlin/com/lambda/util/reflections/Reflections.kt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,11 @@ package com.lambda.util.reflections
1919

2020
import com.lambda.util.extension.isObject
2121
import com.lambda.util.extension.objectInstance
22-
import org.reflections.Reflections
22+
import io.github.classgraph.ClassGraph
23+
import io.github.classgraph.ResourceList
2324
import org.reflections.util.ConfigurationBuilder
2425
import java.lang.reflect.Modifier
25-
import java.util.*
26-
27-
val cache = mutableMapOf<Int, Reflections>()
28-
29-
/**
30-
* This function retrieves or create a reflection instance to avoid redundant
31-
* reflection calls
32-
*
33-
* Every time you instantiate a [Reflections] class, it scans the entire classloader and caches its result in a store
34-
*/
35-
inline fun reflectionCache(block: ConfigurationBuilder.() -> Unit): Reflections {
36-
val config = ConfigurationBuilder().apply(block)
37-
val cacheKey = Objects.hash(config.classLoaders, config.urls, config.scanners, config.inputsFilter)
38-
39-
return cache.getOrPut(cacheKey) { Reflections(config) }
40-
}
26+
import kotlin.jvm.java
4127

4228
/**
4329
* Retrieves all instances of the specified type `T`.
@@ -47,9 +33,21 @@ inline fun reflectionCache(block: ConfigurationBuilder.() -> Unit): Reflections
4733
*
4834
* @return A list of instances of type `T`
4935
*/
50-
inline fun <reified T : Any> getInstances(block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }) =
51-
reflectionCache(block).getSubTypesOf(T::class.java)
52-
.mapNotNull { createInstance<T>(it) }
36+
inline fun <reified T : Any> getInstances(block: ClassGraph.() -> Unit = { enableClassInfo(); acceptPackages("com.lambda") }): List<T> =
37+
ClassGraph().apply(block)
38+
.scan()
39+
.use { result ->
40+
val clazz = T::class.java
41+
42+
return when {
43+
clazz.isInterface -> result.getClassesImplementing(T::class.java)
44+
45+
clazz.isObject || Modifier.isAbstract(clazz.modifiers) ->
46+
result.getSubclasses(T::class.java)
47+
48+
else -> throw IllegalAccessException("class ${clazz.name} is neither an interface or abstract class")
49+
}.mapNotNull { createInstance<T>(Class.forName(it.name)) }
50+
}
5351

5452
/**
5553
* Retrieves all resource paths that match the given pattern.
@@ -62,8 +60,10 @@ inline fun <reified T : Any> getInstances(block: ConfigurationBuilder.() -> Unit
6260
*
6361
* @return A set of resource paths that match the specified pattern.
6462
*/
65-
inline fun getResources(pattern: String, block: ConfigurationBuilder.() -> Unit = { forPackage("com.lambda") }) =
66-
reflectionCache(block).getResources(pattern)
63+
inline fun getResources(pattern: String, block: ClassGraph.() -> Unit = { enableAllInfo(); acceptPackages("com.lambda") }): ResourceList =
64+
ClassGraph().apply(block)
65+
.scan()
66+
.use { it.getResourcesMatchingWildcard(pattern) }
6767

6868
inline fun <reified T : Any> createInstance(clazz: Class<*>): T? {
6969
return when {

fabric/build.gradle.kts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -23,13 +23,14 @@ val kotlinFabricVersion: String by project
2323
val reflectionsVersion: String by project
2424
val pngEncoderVersion: String by project
2525
val discordIPCVersion: String by project
26+
val classGraphVersion: String by project
2627
val kotlinVersion: String by project
2728
val ktorVersion: String by project
2829

2930
base.archivesName = "${base.archivesName.get()}-fabric"
3031

3132
plugins {
32-
id("com.github.johnrengelman.shadow") version "8.1.1"
33+
id("com.gradleup.shadow") version "9.0.0-beta13"
3334
}
3435

3536
architectury {
@@ -45,6 +46,7 @@ loom {
4546
val common: Configuration by configurations.creating {
4647
configurations.compileClasspath.get().extendsFrom(this)
4748
configurations.runtimeClasspath.get().extendsFrom(this)
49+
configurations["developmentFabric"].extendsFrom(this)
4850
isCanBeConsumed = false
4951
}
5052

@@ -79,6 +81,7 @@ dependencies {
7981

8082
// Add dependencies on the required Kotlin modules.
8183
includeLib("org.reflections:reflections:$reflectionsVersion")
84+
includeLib("io.github.classgraph:classgraph:${classGraphVersion}")
8285
includeLib("com.github.Edouard127:KDiscordIPC:$discordIPCVersion")
8386
includeLib("com.pngencoder:pngencoder:$pngEncoderVersion")
8487

@@ -95,7 +98,7 @@ dependencies {
9598

9699
// Common (Do not touch)
97100
common(project(":common", configuration = "namedElements")) { isTransitive = false }
98-
shadowBundle(project(":common", configuration = "transformProductionFabric")) { isTransitive = false }
101+
shadowBundle(project(":common", configuration = "transformProductionFabric"))
99102

100103
// Finish the configuration
101104
setupConfigurations()

fabric/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2024 Lambda
2+
# Copyright 2025 Lambda
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by

fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by

fabric/src/main/kotlin/com/lambda/fabric/LoaderInfoImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by

forge/build.gradle.kts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -24,12 +24,13 @@ val kotlinForgeVersion: String by project
2424
val reflectionsVersion: String by project
2525
val pngEncoderVersion: String by project
2626
val discordIPCVersion: String by project
27+
val classGraphVersion: String by project
2728
val ktorVersion: String by project
2829

2930
base.archivesName = "${base.archivesName.get()}-forge"
3031

3132
plugins {
32-
id("com.github.johnrengelman.shadow") version "8.1.1"
33+
id("com.gradleup.shadow") version "9.0.0-beta13"
3334
}
3435

3536
architectury {
@@ -53,6 +54,7 @@ repositories {
5354
val common: Configuration by configurations.creating {
5455
configurations.compileClasspath.get().extendsFrom(this)
5556
configurations.runtimeClasspath.get().extendsFrom(this)
57+
configurations["developmentForge"].extendsFrom(this)
5658
isCanBeConsumed = false
5759
}
5860

@@ -66,12 +68,10 @@ fun DependencyHandlerScope.setupConfigurations() {
6668
includeLib.dependencies.forEach {
6769
implementation(it)
6870
include(it)
69-
// shadowBundle(it)
7071
}
7172

7273
includeMod.dependencies.forEach {
7374
implementation(it)
74-
// include(it)
7575
}
7676

7777
shadowLib.dependencies.forEach {
@@ -89,6 +89,7 @@ dependencies {
8989

9090
// Add dependencies on the required Kotlin modules.
9191
includeLib("org.reflections:reflections:$reflectionsVersion")
92+
includeLib("io.github.classgraph:classgraph:${classGraphVersion}")
9293
includeLib("com.github.Edouard127:KDiscordIPC:$discordIPCVersion")
9394
includeLib("com.pngencoder:pngencoder:$pngEncoderVersion")
9495

@@ -108,7 +109,7 @@ dependencies {
108109

109110
// Common (Do not touch)
110111
common(project(":common", configuration = "namedElements")) { isTransitive = false }
111-
shadowBundle(project(path = ":common", configuration = "transformProductionForge")) { isTransitive = false }
112+
shadowBundle(project(path = ":common", configuration = "transformProductionForge"))
112113

113114
// Finish the configuration
114115
setupConfigurations()
@@ -141,6 +142,8 @@ tasks {
141142
remapJar {
142143
dependsOn(processResources, shadowJar)
143144

145+
atAccessWideners.add("src/main/resources/$modId.accesswidener")
146+
144147
archiveVersion = "$modVersion+$minecraftVersion"
145148
inputFile = shadowJar.get().archiveFile
146149
}

forge/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2024 Lambda
2+
# Copyright 2025 Lambda
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by

0 commit comments

Comments
 (0)